Skip to content

Commit

Permalink
FAB-9489 Network Config property overrides
Browse files Browse the repository at this point in the history
Change-Id: I230d998c39a6685ffda80fd0029e0d428cdc8190
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed Apr 12, 2018
1 parent 8b976c7 commit 479b498
Show file tree
Hide file tree
Showing 2 changed files with 240 additions and 11 deletions.
151 changes: 150 additions & 1 deletion src/main/java/org/hyperledger/fabric/sdk/NetworkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -74,6 +75,154 @@ public class NetworkConfig {
private Map<String, Node> peers;
private Map<String, Node> eventHubs;

/**
* Names of Peers found
*
* @return Collection of peer names found.
*/
public Collection<String> getPeerNames() {
if (peers == null) {
return Collections.EMPTY_SET;
} else {
return new HashSet<>(peers.keySet());
}
}

/**
* Names of Orderers found
*
* @return Collection of peer names found.
*/
public Collection<String> getOrdererNames() {
if (orderers == null) {
return Collections.EMPTY_SET;
} else {
return new HashSet<>(orderers.keySet());
}
}

/**
* Names of EventHubs found
*
* @return Collection of eventhubs names found.
*/

public Collection<String> getEventHubNames() {
if (eventHubs == null) {
return Collections.EMPTY_SET;
} else {
return new HashSet<>(eventHubs.keySet());
}
}

private Properties getNodeProperties(String type, String name, Map<String, Node> nodes) throws InvalidArgumentException {
if (isNullOrEmpty(name)) {
throw new InvalidArgumentException("Parameter name is null or empty.");
}

Node node = nodes.get(name);
if (node == null) {
throw new InvalidArgumentException(format("%s %s not found.", type, name));
}

if (null == node.properties) {
return new Properties();
} else {

return new Properties(node.properties);
}

}

private void setNodeProperties(String type, String name, Map<String, Node> nodes, Properties properties) throws InvalidArgumentException {
if (isNullOrEmpty(name)) {
throw new InvalidArgumentException("Parameter name is null or empty.");
}
if (properties == null) {
throw new InvalidArgumentException("Parameter properties is null.");
}

Node node = nodes.get(name);
if (node == null) {
throw new InvalidArgumentException(format("%S %s not found.", type, name));
}

Properties ourCopyProps = new Properties();
ourCopyProps.putAll(properties);

node.properties = ourCopyProps;

}

/**
* Get properties for a specific peer.
*
* @param name Name of peer to get the properties for.
* @return The peer's properties.
* @throws InvalidArgumentException
*/
public Properties getPeerProperties(String name) throws InvalidArgumentException {
return getNodeProperties("Peer", name, peers);

}

/**
* Get properties for a specific Orderer.
*
* @param name Name of orderer to get the properties for.
* @return The orderer's properties.
* @throws InvalidArgumentException
*/
public Properties getOrderProperties(String name) throws InvalidArgumentException {
return getNodeProperties("Orderer", name, orderers);

}

/**
* Get properties for a specific eventhub.
*
* @param name Name of eventhub to get the properties for.
* @return The eventhubs's properties.
* @throws InvalidArgumentException
*/
public Properties getEventHubsProperties(String name) throws InvalidArgumentException {
return getNodeProperties("EventHub", name, eventHubs);

}

/**
* Set a specific peer's properties.
*
* @param name The name of the peer's property to set.
* @param properties The properties to set.
* @throws InvalidArgumentException
*/
public void setPeerProperties(String name, Properties properties) throws InvalidArgumentException {
setNodeProperties("Peer", name, peers, properties);
}

/**
* Set a specific orderer's properties.
*
* @param name The name of the orderer's property to set.
* @param properties The properties to set.
* @throws InvalidArgumentException
*/
public void setOrdererProperties(String name, Properties properties) throws InvalidArgumentException {
setNodeProperties("Orderer", name, orderers, properties);
}

/**
* Set a specific eventhub's properties.
*
* @param name The name of the eventhub's property to set.
* @param properties The properties to set.
* @throws InvalidArgumentException
*/
public void setEventHubProperties(String name, Properties properties) throws InvalidArgumentException {
setNodeProperties("EventHub", name, eventHubs, properties);
}

// Organizations, keyed on org name (and not on mspid!)
private Map<String, OrgInfo> organizations;

Expand Down Expand Up @@ -878,7 +1027,7 @@ private class Node {

private final String name;
private final String url;
private final Properties properties;
private Properties properties;

Node(String name, String url, Properties properties) {
this.url = url;
Expand Down
100 changes: 90 additions & 10 deletions src/test/java/org/hyperledger/fabric/sdk/NetworkConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import javax.json.Json;
import javax.json.JsonArray;
Expand All @@ -37,6 +39,9 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class NetworkConfigTest {

Expand Down Expand Up @@ -108,15 +113,15 @@ public void testLoadFromConfigFileYamlBasic() throws Exception {

File f = new File("src/test/fixture/sdkintegration/network_configs/network-config.yaml");
NetworkConfig config = NetworkConfig.fromYamlFile(f);
Assert.assertNotNull(config);
assertNotNull(config);
}

@Test
public void testLoadFromConfigFileJsonBasic() throws Exception {

File f = new File("src/test/fixture/sdkintegration/network_configs/network-config.json");
NetworkConfig config = NetworkConfig.fromJsonFile(f);
Assert.assertNotNull(config);
assertNotNull(config);
}

@Test
Expand All @@ -126,14 +131,14 @@ public void testLoadFromConfigFileYaml() throws Exception {
File f = new File("src/test/fixture/sdkintegration/network_configs/network-config.yaml");
NetworkConfig config = NetworkConfig.fromYamlFile(f);
//HFClient client = HFClient.loadFromConfig(f);
Assert.assertNotNull(config);
assertNotNull(config);

HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
client.setUserContext(TestUtils.getMockUser(USER_NAME, USER_MSP_ID));

Channel channel = client.loadChannelFromConfig("foo", config);
Assert.assertNotNull(channel);
assertNotNull(channel);
}

@Test
Expand All @@ -142,7 +147,7 @@ public void testLoadFromConfigFileJson() throws Exception {
// Should be able to instantiate a new instance of "Client" with a valid path to the JSON configuration
File f = new File("src/test/fixture/sdkintegration/network_configs/network-config.json");
NetworkConfig config = NetworkConfig.fromJsonFile(f);
Assert.assertNotNull(config);
assertNotNull(config);

//HFClient client = HFClient.loadFromConfig(f);
//Assert.assertNotNull(client);
Expand All @@ -152,7 +157,7 @@ public void testLoadFromConfigFileJson() throws Exception {
client.setUserContext(TestUtils.getMockUser(USER_NAME, USER_MSP_ID));

Channel channel = client.loadChannelFromConfig("mychannel", config);
Assert.assertNotNull(channel);
assertNotNull(channel);
}

@Test
Expand Down Expand Up @@ -191,7 +196,7 @@ public void testNewChannel() throws Exception {
TestHFClient.setupClient(client);

Channel channel = client.loadChannelFromConfig(CHANNEL_NAME, config);
Assert.assertNotNull(channel);
assertNotNull(channel);
Assert.assertEquals(CHANNEL_NAME, channel.getName());
}

Expand Down Expand Up @@ -287,6 +292,81 @@ public void testGetChannelNoPeers() throws Exception {

}

@Test
public void testLoadFromConfigFileYamlNOOverrides() throws Exception {

// Should be able to instantiate a new instance of "Client" with a valid path to the YAML configuration
File f = new File("src/test/fixture/sdkintegration/network_configs/network-config.yaml");
NetworkConfig config = NetworkConfig.fromYamlFile(f);

//HFClient client = HFClient.loadFromConfig(f);
assertNotNull(config);

HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
client.setUserContext(TestUtils.getMockUser(USER_NAME, USER_MSP_ID));

Channel channel = client.loadChannelFromConfig("foo", config);
assertNotNull(channel);

assertTrue(!channel.getPeers().isEmpty());

for (Peer peer : channel.getPeers()) {

Properties properties = peer.getProperties();

assertNotNull(properties);
assertNull(properties.get("grpc.NettyChannelBuilderOption.keepAliveTime"));
assertNull(properties.get("grpc.NettyChannelBuilderOption.keepAliveTimeout"));
assertNull(properties.get("grpc.NettyChannelBuilderOption.keepAliveWithoutCalls"));

}

}

@Test
public void testLoadFromConfigFileYamlOverrides() throws Exception {

// Should be able to instantiate a new instance of "Client" with a valid path to the YAML configuration
File f = new File("src/test/fixture/sdkintegration/network_configs/network-config.yaml");
NetworkConfig config = NetworkConfig.fromYamlFile(f);

for (String peerName : config.getPeerNames()) {
Properties peerProperties = config.getPeerProperties(peerName);

//example of setting keepAlive to avoid timeouts on inactive http2 connections.
// Under 5 minutes would require changes to server side to accept faster ping rates.
peerProperties.put("grpc.NettyChannelBuilderOption.keepAliveTime", new Object[] {5L, TimeUnit.MINUTES});
peerProperties.put("grpc.NettyChannelBuilderOption.keepAliveTimeout", new Object[] {8L, TimeUnit.SECONDS});
peerProperties.put("grpc.NettyChannelBuilderOption.keepAliveWithoutCalls", new Object[] {true});
config.setPeerProperties(peerName, peerProperties);
}

//HFClient client = HFClient.loadFromConfig(f);
assertNotNull(config);

HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
client.setUserContext(TestUtils.getMockUser(USER_NAME, USER_MSP_ID));

Channel channel = client.loadChannelFromConfig("foo", config);
assertNotNull(channel);

assertTrue(!channel.getPeers().isEmpty());

for (Peer peer : channel.getPeers()) {

Properties properties = peer.getProperties();

assertNotNull(properties);
assertNotNull(properties.get("grpc.NettyChannelBuilderOption.keepAliveTime"));
assertNotNull(properties.get("grpc.NettyChannelBuilderOption.keepAliveTimeout"));
assertNotNull(properties.get("grpc.NettyChannelBuilderOption.keepAliveWithoutCalls"));

}

}


// TODO: ca-org1 not defined
@Ignore
Expand All @@ -307,18 +387,18 @@ public void testGetChannel() throws Exception {
//TestHFClient.setupClient(client);

//Channel channel = client.getChannel(CHANNEL_NAME);
Assert.assertNotNull(channel);
assertNotNull(channel);
Assert.assertEquals(CHANNEL_NAME, channel.getName());

Collection<Orderer> orderers = channel.getOrderers();
Assert.assertNotNull(orderers);
assertNotNull(orderers);
Assert.assertEquals(1, orderers.size());

Orderer orderer = orderers.iterator().next();
Assert.assertEquals("orderer1.example.com", orderer.getName());

Collection<Peer> peers = channel.getPeers();
Assert.assertNotNull(peers);
assertNotNull(peers);
Assert.assertEquals(1, peers.size());

Peer peer = peers.iterator().next();
Expand Down

0 comments on commit 479b498

Please sign in to comment.