2424import java .util .ArrayList ;
2525import java .util .Collection ;
2626import java .util .Collections ;
27- import java .util .EnumSet ;
2827import java .util .HashMap ;
2928import java .util .HashSet ;
3029import java .util .LinkedList ;
5453import org .hyperledger .fabric .sdk .Peer .PeerRole ;
5554import org .hyperledger .fabric .sdk .exception .InvalidArgumentException ;
5655import org .hyperledger .fabric .sdk .exception .NetworkConfigurationException ;
56+ import org .hyperledger .fabric .sdk .helper .Utils ;
5757import org .hyperledger .fabric .sdk .identity .X509Enrollment ;
5858import org .hyperledger .fabric .sdk .security .CryptoSuite ;
5959import org .yaml .snakeyaml .Yaml ;
@@ -166,8 +166,6 @@ public Properties getOrdererProperties(String name) throws InvalidArgumentExcept
166166
167167 }
168168
169-
170-
171169 /**
172170 * Set a specific peer's properties.
173171 *
@@ -381,6 +379,33 @@ public Collection<OrgInfo> getOrganizationInfos() {
381379 return Collections .unmodifiableCollection (organizations .values ());
382380 }
383381
382+ /**
383+ * Find organizations for a peer.
384+ *
385+ * @param peerName name of peer
386+ * @return returns map of orgName to {@link OrgInfo} that the peer belongs to.
387+ * @throws InvalidArgumentException
388+ */
389+ public Map <String , OrgInfo > getPeerOrgInfos (final String peerName ) throws InvalidArgumentException {
390+ if (Utils .isNullOrEmpty (peerName )) {
391+ throw new InvalidArgumentException ("peerName can not be null or empty." );
392+ }
393+
394+ if (organizations == null || organizations .isEmpty ()) {
395+ return new HashMap <>();
396+ }
397+
398+ Map <String , OrgInfo > ret = new HashMap <>(16 );
399+ organizations .forEach ((name , orgInfo ) -> {
400+
401+ if (orgInfo .getPeerNames ().contains (peerName )) {
402+ ret .put (name , orgInfo );
403+ }
404+ });
405+
406+ return ret ;
407+ }
408+
384409 /**
385410 * Returns the admin user associated with the client organization
386411 *
@@ -417,6 +442,17 @@ public UserInfo getPeerAdmin(String orgName) throws NetworkConfigurationExceptio
417442 * @return A configured Channel instance
418443 */
419444 Channel loadChannel (HFClient client , String channelName ) throws NetworkConfigurationException {
445+ return loadChannel (client , channelName , networkConfigAddPeerHandlerDefault , networkConfigAddOrdererHandlerDefault );
446+ }
447+
448+ /**
449+ * Returns a channel configured using the details in the Network Configuration file
450+ *
451+ * @param client The associated client
452+ * @param channelName The name of the channel
453+ * @return A configured Channel instance
454+ */
455+ Channel loadChannel (HFClient client , String channelName , NetworkConfigAddPeerHandler networkConfigAddPeerHandler , NetworkConfigAddOrdererHandler networkConfigAddOrdererHandler ) throws NetworkConfigurationException {
420456
421457 if (logger .isTraceEnabled ()) {
422458 logger .trace (format ("NetworkConfig.loadChannel: %s" , channelName ));
@@ -435,7 +471,7 @@ Channel loadChannel(HFClient client, String channelName) throws NetworkConfigura
435471 // Note that by rights this should never happen as HFClient.loadChannelFromConfig should have already checked for this!
436472 throw new NetworkConfigurationException (format ("Channel %s is already configured in the client!" , channelName ));
437473 }
438- channel = reconstructChannel (client , channelName , jsonChannel );
474+ channel = reconstructChannel (client , channelName , jsonChannel , networkConfigAddPeerHandler , networkConfigAddOrdererHandler );
439475 } else {
440476
441477 final Set <String > channelNames = getChannelNames ();
@@ -502,10 +538,8 @@ private void createAllPeers() throws NetworkConfigurationException {
502538 throw new NetworkConfigurationException ("INTERNAL ERROR: peers has already been initialized!" );
503539 }
504540
505-
506541 peers = new HashMap <>();
507542
508-
509543 // peers is a JSON object containing a nested object for each peer
510544 JsonObject jsonPeers = getJsonObject (jsonConfig , "peers" );
511545
@@ -584,7 +618,7 @@ private void createAllOrganizations(Map<String, JsonObject> foundCertificateAuth
584618 }
585619
586620 // Reconstructs an existing channel
587- private Channel reconstructChannel (HFClient client , String channelName , JsonObject jsonChannel ) throws NetworkConfigurationException {
621+ private Channel reconstructChannel (HFClient client , String channelName , JsonObject jsonChannel , NetworkConfigAddPeerHandler networkConfigAddPeerHandler , NetworkConfigAddOrdererHandler networkConfigAddOrdererHandler ) throws NetworkConfigurationException {
588622
589623 Channel channel = null ;
590624
@@ -593,19 +627,26 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
593627
594628 // orderers is an array of orderer name strings
595629 JsonArray ordererNames = getJsonValueAsArray (jsonChannel .get ("orderers" ));
596- boolean foundOrderer = false ;
597630
598631 //out("Orderer names: " + (ordererNames == null ? "null" : ordererNames.toString()));
599632 if (ordererNames != null ) {
600633 for (JsonValue jsonVal : ordererNames ) {
601634
602635 String ordererName = getJsonValueAsString (jsonVal );
603- Orderer orderer = getOrderer (client , ordererName );
604- if (orderer == null ) {
636+
637+ // Orderer orderer = getOrderer(client, ordererName);
638+ Node node = orderers .get (ordererName );
639+ if (null == node ) {
605640 throw new NetworkConfigurationException (format ("Error constructing channel %s. Orderer %s not defined in configuration" , channelName , ordererName ));
606641 }
607- channel .addOrderer (orderer );
608- foundOrderer = true ;
642+
643+ logger .debug (format ("Channel %s, adding orderer %s, url: %s" , channel .getName (), ordererName , node .url ));
644+ Properties nodeProps = node .properties ;
645+ if (null != nodeProps ) {
646+ nodeProps = (Properties ) nodeProps .clone ();
647+ }
648+
649+ networkConfigAddOrdererHandler .addOrderer (this , client , channel , ordererName , node .url , nodeProps , node .jsonObject );
609650 }
610651 }
611652
@@ -628,8 +669,8 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
628669 throw new NetworkConfigurationException (format ("Error constructing channel %s. Invalid peer entry: %s" , channelName , peerName ));
629670 }
630671
631- Peer peer = getPeer ( client , peerName );
632- if (peer == null ) {
672+ Node node = peers . get ( peerName );
673+ if (node == null ) {
633674 throw new NetworkConfigurationException (format ("Error constructing channel %s. Peer %s not defined in configuration" , channelName , peerName ));
634675 }
635676
@@ -640,13 +681,15 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
640681 setPeerRole (channelName , peerOptions , jsonPeer , peerRole );
641682 }
642683
643- foundPeer = true ;
684+ logger . debug ( format ( "Channel %s, adding peer %s, url: %s" , channel . getName (), peerName , node . url )) ;
644685
645- if (peerOptions .peerRoles == null ) { // means no roles were found but there is an event hub so define all roles but eventing.
646- peerOptions .setPeerRoles (EnumSet .of (PeerRole .ENDORSING_PEER , PeerRole .CHAINCODE_QUERY , PeerRole .LEDGER_QUERY ));
686+ Properties nodeProps = node .properties ;
687+ if (null != nodeProps ) {
688+ nodeProps = (Properties ) nodeProps .clone ();
647689 }
690+ networkConfigAddPeerHandler .addPeer (this , client , channel , peerName , node .url , nodeProps , peerOptions , node .jsonObject );
648691
649- channel . addPeer ( peer , peerOptions ) ;
692+ foundPeer = true ;
650693
651694 }
652695
@@ -664,6 +707,62 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
664707 return channel ;
665708 }
666709
710+ /**
711+ * Interface defining handler for adding peers.
712+ */
713+
714+ public interface NetworkConfigAddPeerHandler {
715+
716+ /**
717+ * @param networkConfig The network configuration.
718+ * @param client The client to be used to create the peer.
719+ * @param channel The channel the peer is to be added.
720+ * @param peerName The peer's name.
721+ * @param peerURL The peers's url
722+ * @param peerProperties properties that were found in the networkconfig
723+ * @param peerOptions options when adding peer to the channel.
724+ * @param jsonPeer json peer was created
725+ * @throws NetworkConfigurationException
726+ */
727+ void addPeer (NetworkConfig networkConfig , HFClient client , Channel channel , String peerName , String peerURL , Properties peerProperties , PeerOptions peerOptions , JsonObject jsonPeer ) throws NetworkConfigurationException ;
728+ }
729+
730+ private static final NetworkConfigAddPeerHandler networkConfigAddPeerHandlerDefault = (networkConfig , client , channel , peerName , peerURL , peerProperties , peerOptions , jsonPeer ) -> {
731+ try {
732+ Peer peer = client .newPeer (peerName , peerURL , peerProperties );
733+ channel .addPeer (peer , peerOptions );
734+ } catch (Exception e ) {
735+ throw new NetworkConfigurationException (format ("Error on creating channel %s peer %s" , channel .getName (), peerName ), e );
736+ }
737+ };
738+
739+ /**
740+ * Interface defining handler for adding orderers.
741+ */
742+ public interface NetworkConfigAddOrdererHandler {
743+
744+ /**
745+ * @param networkConfig The network configuration.
746+ * @param client The client to be used to create the orderer.
747+ * @param channel The channel the orderer is to be added.
748+ * @param ordererName The orderer's name.
749+ * @param ordererURL The orderers's url
750+ * @param ordererProperties properties that were found in the networkconfig
751+ * @param jsonOrderer json orderer was created
752+ * @throws NetworkConfigurationException
753+ */
754+ void addOrderer (NetworkConfig networkConfig , HFClient client , Channel channel , String ordererName , String ordererURL , Properties ordererProperties , JsonObject jsonOrderer ) throws NetworkConfigurationException ;
755+ }
756+
757+ private static final NetworkConfigAddOrdererHandler networkConfigAddOrdererHandlerDefault = (networkConfig , client , channel , ordererName , ordererURL , ordererProperties , jsonOrderer ) -> {
758+ try {
759+ Orderer orderer = client .newOrderer (ordererName , ordererURL , ordererProperties );
760+ channel .addOrderer (orderer );
761+ } catch (Exception e ) {
762+ throw new NetworkConfigurationException (format ("Error on creating channel %s orderer %s" , channel .getName (), ordererName ), e );
763+ }
764+ };
765+
667766 private static void setPeerRole (String channelName , PeerOptions peerOptions , JsonObject jsonPeer , PeerRole role ) throws NetworkConfigurationException {
668767 String propName = roleNameRemap (role );
669768 JsonValue val = jsonPeer .get (propName );
@@ -732,7 +831,7 @@ private Node createNode(String nodeName, JsonObject jsonNode, String urlPropName
732831 // Extract the pem details
733832 getTLSCerts (nodeName , jsonNode , props );
734833
735- return new Node (nodeName , url , props );
834+ return new Node (nodeName , url , props , jsonNode );
736835 }
737836
738837 private void getTLSCerts (String nodeName , JsonObject jsonOrderer , Properties props ) {
@@ -1036,12 +1135,14 @@ private class Node {
10361135
10371136 private final String name ;
10381137 private final String url ;
1138+ public JsonObject jsonObject ;
10391139 private Properties properties ;
10401140
1041- Node (String name , String url , Properties properties ) {
1141+ private Node (String name , String url , Properties properties , JsonObject jsonObject ) {
10421142 this .url = url ;
10431143 this .name = name ;
10441144 this .properties = properties ;
1145+ this .jsonObject = jsonObject ;
10451146 }
10461147
10471148 private String getName () {
@@ -1179,11 +1280,11 @@ public String getMspId() {
11791280 }
11801281
11811282 public List <String > getPeerNames () {
1182- return peerNames ;
1283+ return new LinkedList <>( peerNames ) ;
11831284 }
11841285
11851286 public List <CAInfo > getCertificateAuthorities () {
1186- return certificateAuthorities ;
1287+ return new LinkedList <>( certificateAuthorities ) ;
11871288 }
11881289
11891290 /**
0 commit comments