Skip to content

Commit

Permalink
FABJ-345 Network Config add service discovery role
Browse files Browse the repository at this point in the history
Change-Id: I545f8b77c07240b07db5d2ac14a8591741824d26
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed Aug 13, 2018
1 parent ff953b0 commit 3f6cbeb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 40 deletions.
26 changes: 16 additions & 10 deletions src/main/java/org/hyperledger/fabric/sdk/NetworkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -651,11 +651,6 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
}
}

if (!foundOrderer) {
// orderers is a required field
throw new NetworkConfigurationException(format("Error constructing channel %s. At least one orderer must be specified", channelName));
}

// peers is an object containing a nested object for each peer
JsonObject jsonPeers = getJsonObject(jsonChannel, "peers");
boolean foundPeer = false;
Expand All @@ -682,10 +677,10 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje

// Set the various roles
PeerOptions peerOptions = PeerOptions.createPeerOptions();
setPeerRole(channelName, peerOptions, jsonPeer, PeerRole.ENDORSING_PEER);
setPeerRole(channelName, peerOptions, jsonPeer, PeerRole.CHAINCODE_QUERY);
setPeerRole(channelName, peerOptions, jsonPeer, PeerRole.LEDGER_QUERY);
setPeerRole(channelName, peerOptions, jsonPeer, PeerRole.EVENT_SOURCE);

for (PeerRole peerRole : PeerRole.values()) {
setPeerRole(channelName, peerOptions, jsonPeer, peerRole);
}

foundPeer = true;

Expand Down Expand Up @@ -716,7 +711,7 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
}

private static void setPeerRole(String channelName, PeerOptions peerOptions, JsonObject jsonPeer, PeerRole role) throws NetworkConfigurationException {
String propName = role.getPropertyName();
String propName = roleNameRemap(role);
JsonValue val = jsonPeer.get(propName);
if (val != null) {
Boolean isSet = getJsonValueAsBoolean(val);
Expand All @@ -730,6 +725,17 @@ private static void setPeerRole(String channelName, PeerOptions peerOptions, Jso
}
}

private static Map<PeerRole, String> roleNameRemapHash = new HashMap<PeerRole, String>() {
{
put(PeerRole.SERVICE_DISCOVERY, "discover");
}
};

private static String roleNameRemap(PeerRole peerRole) {
String remap = roleNameRemapHash.get(peerRole);
return remap == null ? peerRole.getPropertyName() : remap;
}

// Returns a new Orderer instance for the specified orderer name
private Orderer getOrderer(HFClient client, String ordererName) throws InvalidArgumentException {
Orderer orderer = null;
Expand Down
37 changes: 7 additions & 30 deletions src/test/java/org/hyperledger/fabric/sdk/NetworkConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -185,13 +186,11 @@ public void testGetClientOrg() throws Exception {
Assert.assertEquals(CLIENT_ORG_NAME, config.getClientOrganization().getName());
}

// TODO: At least one orderer must be specified
@Ignore
@Test
public void testNewChannel() throws Exception {

// Should be able to instantiate a new instance of "Channel" with the definition in the network configuration'
JsonObject jsonConfig = getJsonConfig1(1, 0, 0);
JsonObject jsonConfig = getJsonConfig1(1, 0, 1);

NetworkConfig config = NetworkConfig.fromJsonObject(jsonConfig);

Expand All @@ -201,6 +200,7 @@ public void testNewChannel() throws Exception {
Channel channel = client.loadChannelFromConfig(CHANNEL_NAME, config);
assertNotNull(channel);
Assert.assertEquals(CHANNEL_NAME, channel.getName());
Assert.assertEquals(channel.getPeers(EnumSet.of(Peer.PeerRole.SERVICE_DISCOVERY)).size(), 1);
}

@Test
Expand Down Expand Up @@ -245,29 +245,6 @@ public void testGetChannelNoOrderersOrPeers() throws Exception {
//client.getChannel(CHANNEL_NAME);
}

@Test
public void testGetChannelNoOrderers() throws Exception {

thrown.expect(NetworkConfigurationException.class);
thrown.expectMessage("Error constructing");

// Should not be able to instantiate a new instance of "Channel" with no orderers configured
JsonObject jsonConfig = getJsonConfig1(1, 0, 1);

//HFClient client = HFClient.loadFromConfig(jsonConfig);
//TestHFClient.setupClient(client);

//client.getChannel(CHANNEL_NAME);

NetworkConfig config = NetworkConfig.fromJsonObject(jsonConfig);

HFClient client = HFClient.createNewInstance();
TestHFClient.setupClient(client);

client.loadChannelFromConfig(CHANNEL_NAME, config);

}

@Test
public void testGetChannelNoPeers() throws Exception {

Expand Down Expand Up @@ -355,7 +332,6 @@ public void testLoadFromConfigFileYamlNOOverridesButSet() throws Exception {

}


@Test
public void testLoadFromConfigFileYamlOverrides() throws Exception {

Expand Down Expand Up @@ -491,9 +467,9 @@ private static JsonObject getJsonConfig1(int nOrganizations, int nOrderers, int
JsonObject peers = null;
if (nPeers > 0) {
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("peer0.org1.example.com", createJsonChannelPeer("Org1", true, true, true, true));
builder.add("peer0.org1.example.com", createJsonChannelPeer("Org1", true, true, true, true, true));
if (nPeers > 1) {
builder.add("peer0.org2.example.com", createJsonChannelPeer("Org2", true, false, true, false));
builder.add("peer0.org2.example.com", createJsonChannelPeer("Org2", true, false, true, false, false));
}
peers = builder.build();
}
Expand Down Expand Up @@ -597,14 +573,15 @@ private static JsonObject getJsonConfig1(int nOrganizations, int nOrderers, int
return mainConfig.build();
}

private static JsonObject createJsonChannelPeer(String name, Boolean endorsingPeer, Boolean chaincodeQuery, Boolean ledgerQuery, Boolean eventSource) {
private static JsonObject createJsonChannelPeer(String name, Boolean endorsingPeer, Boolean chaincodeQuery, Boolean ledgerQuery, Boolean eventSource, Boolean discover) {

return Json.createObjectBuilder()
.add("name", name)
.add("endorsingPeer", endorsingPeer)
.add("chaincodeQuery", chaincodeQuery)
.add("ledgerQuery", ledgerQuery)
.add("eventSource", eventSource)
.add("discover", discover)
.build();
}

Expand Down

0 comments on commit 3f6cbeb

Please sign in to comment.