@@ -180,6 +180,8 @@ public class Channel implements Serializable {
180180 private final Collection <Peer > peers = Collections .synchronizedSet (new HashSet <>());
181181 private final Map <Peer , PeerOptions > peerOptionsMap = Collections .synchronizedMap (new HashMap <>());
182182 private transient Map <String , Peer > peerEndpointMap = Collections .synchronizedMap (new HashMap <>());
183+ private Map <String , Collection <Peer >> peerMSPIDMap = new HashMap <>();
184+ private Map <String , Collection <Orderer >> ordererMSPIDMap = new HashMap <>();
183185 private final Map <PeerRole , Set <Peer >> peerRoleSetMap = Collections .synchronizedMap (new HashMap <>());
184186 private transient String chaincodeEventUpgradeListenerHandle ;
185187 private transient String transactionListenerProcessorHandle ;
@@ -642,6 +644,7 @@ public Channel addPeer(Peer peer, PeerOptions peerOptions) throws InvalidArgumen
642644 peers .add (peer );
643645 peerOptionsMap .put (peer , peerOptions .clone ());
644646 peerEndpointMap .put (peer .getEndpoint (), peer );
647+ addPeerMSPIDMap (peer );
645648
646649 if (peerOptions .getPeerRoles ().contains (PeerRole .SERVICE_DISCOVERY )) {
647650
@@ -652,9 +655,7 @@ public Channel addPeer(Peer peer, PeerOptions peerOptions) throws InvalidArgumen
652655 TLSCertificateKeyPair tlsCertificateKeyPair = tlsCertificateBuilder .clientCert ();
653656 peer .setTLSCertificateKeyPair (tlsCertificateKeyPair );
654657 }
655-
656658 discoveryEndpoints .add (peer .getEndpoint ());
657-
658659 }
659660
660661 for (Map .Entry <PeerRole , Set <Peer >> peerRole : peerRoleSetMap .entrySet ()) {
@@ -675,6 +676,79 @@ public Channel addPeer(Peer peer, PeerOptions peerOptions) throws InvalidArgumen
675676 return this ;
676677 }
677678
679+ private void addPeerMSPIDMap (final Peer peer ) {
680+ Properties properties = peer .getProperties ();
681+
682+ if (null != properties ) {
683+ final String mspid = properties .getProperty (Peer .PEER_ORGANIZATION_MSPID_PROPERTY );
684+ if (!isNullOrEmpty (mspid )) {
685+ logger .debug (format ("Channel %s mapping peer %s to mspid %s" , name , peer , mspid ));
686+ synchronized (peerMSPIDMap ) {
687+ peerMSPIDMap .computeIfAbsent (mspid , k -> new HashSet <Peer >()).add (peer );
688+ }
689+ }
690+ }
691+ }
692+
693+ private void removePeerMSPIDMap (final Peer peer ) {
694+ Properties properties = peer .getProperties ();
695+
696+ if (null != properties ) {
697+ final String mspid = properties .getProperty (Peer .PEER_ORGANIZATION_MSPID_PROPERTY );
698+ if (!isNullOrEmpty (mspid )) {
699+ logger .debug (format ("Channel %s removing mapping peer %s to mspid %s" , name , peer , mspid ));
700+ synchronized (peerMSPIDMap ) {
701+ final Collection <Peer > peers = peerMSPIDMap .get (mspid );
702+ if (peers != null ) {
703+ peers .remove (peer );
704+ if (peers .isEmpty ()) {
705+ peerMSPIDMap .remove (mspid );
706+ }
707+
708+ }
709+ }
710+ }
711+ }
712+ }
713+
714+ /**
715+ * Get peers that belong to an organization from the organization's MSPID
716+ * These values may not be available till after the channel is initialized.
717+ *
718+ * @param mspid The organizaiions MSPID
719+ * @return A collection of Peers that belong to the organization with that mspid.
720+ * @throws InvalidArgumentException
721+ */
722+
723+ public Collection <Peer > getPeersForOrganization (String mspid ) throws InvalidArgumentException {
724+
725+ if (isNullOrEmpty (mspid )) {
726+ throw new InvalidArgumentException ("The mspid parameter may not be null or empty string." );
727+ }
728+ synchronized (peerMSPIDMap ) {
729+
730+ final Collection <Peer > peers = peerMSPIDMap .get (mspid );
731+ if (peers == null ) {
732+ return Collections .emptySet ();
733+ } else {
734+ return new LinkedList <>(peers ); // return a copy.
735+ }
736+ }
737+ }
738+
739+ /**
740+ * Collection of strings which are the MSPIDs of all the peer organization added.
741+ * These values may not be available till after the channel is initialized.
742+ *
743+ * @return The collection of mspids
744+ */
745+
746+ public Collection <String > getPeersOrganizationMSPIDs () {
747+ synchronized (peerMSPIDMap ) {
748+ return new LinkedList <>(peerMSPIDMap .keySet ());
749+ }
750+ }
751+
678752 /**
679753 * Join the peer to the channel. The peer is added with all roles see {@link PeerOptions}
680754 *
@@ -808,6 +882,7 @@ public Channel joinPeer(Orderer orderer, Peer peer, PeerOptions peerOptions) thr
808882 } catch (Exception e ) {
809883 logger .error (format ("%s removing peer %s due to exception %s" , toString (), peer , e .getMessage ()));
810884 peers .remove (peer );
885+ removePeerMSPIDMap (peer );
811886 logger .error (e );
812887 throw new ProposalException (e .getMessage (), e );
813888 }
@@ -901,6 +976,7 @@ private void removePeerInternal(Peer peer) {
901976 peers .remove (peer );
902977 peerOptionsMap .remove (peer );
903978 peerEndpointMap .remove (peer .getEndpoint ());
979+ removePeerMSPIDMap (peer );
904980
905981 for (Set <Peer > peerRoleSet : peerRoleSetMap .values ()) {
906982 peerRoleSet .remove (peer );
@@ -931,6 +1007,16 @@ public Channel addOrderer(Orderer orderer) throws InvalidArgumentException {
9311007 orderer .setChannel (this );
9321008 ordererEndpointMap .put (orderer .getEndpoint (), orderer );
9331009 orderers .add (orderer );
1010+ final Properties properties = orderer .getProperties ();
1011+ if (properties != null ) {
1012+ final String mspid = properties .getProperty (Orderer .ORDERER_ORGANIZATION_MSPID_PROPERTY );
1013+ if (!isNullOrEmpty (mspid )) {
1014+ synchronized (ordererMSPIDMap ) {
1015+ ordererMSPIDMap .computeIfAbsent (mspid , k -> new HashSet <>()).add (orderer );
1016+ }
1017+ }
1018+ }
1019+
9341020 return this ;
9351021 }
9361022
@@ -949,9 +1035,60 @@ public void removeOrderer(Orderer orderer) throws InvalidArgumentException {
9491035 ordererEndpointMap .remove (orderer .getEndpoint ());
9501036 orderers .remove (orderer );
9511037 orderer .shutdown (true );
1038+ final Properties properties = orderer .getProperties ();
1039+ if (properties != null ) {
1040+ final String mspid = properties .getProperty (Orderer .ORDERER_ORGANIZATION_MSPID_PROPERTY );
1041+ if (!isNullOrEmpty (mspid )) {
1042+ synchronized (ordererMSPIDMap ) {
1043+ final Collection <Orderer > orderers = ordererMSPIDMap .get (mspid );
1044+ orderers .remove (orderer );
1045+ if (orderers .isEmpty ()) {
1046+ ordererMSPIDMap .remove (mspid );
1047+ }
1048+ }
1049+ }
1050+ }
9521051
9531052 }
9541053
1054+ /**
1055+ * Get orderers that belong to an organization from the organization's MSPID
1056+ * These values may not be available till after the channel is initialized.
1057+ *
1058+ * @param mspid The organizaiions MSPID
1059+ * @return A collection of Orderers that belong to the organization with that mspid.
1060+ * @throws InvalidArgumentException
1061+ */
1062+
1063+ public Collection <Orderer > getOrderersForOrganization (String mspid ) throws InvalidArgumentException {
1064+
1065+ if (isNullOrEmpty (mspid )) {
1066+ throw new InvalidArgumentException ("The mspid parameter may not be null or empty string." );
1067+ }
1068+ synchronized (ordererMSPIDMap ) {
1069+
1070+ final Collection <Orderer > orderers = ordererMSPIDMap .get (mspid );
1071+ if (orderers == null ) {
1072+ return Collections .emptySet ();
1073+ } else {
1074+ return new LinkedList <>(orderers ); // return a copy.
1075+ }
1076+ }
1077+ }
1078+
1079+ /**
1080+ * Collection of strings which are the MSPIDs of all the orderer organization added.
1081+ * These values may not be available till after the channel is initialized.
1082+ *
1083+ * @return The collection of mspids
1084+ */
1085+
1086+ public Collection <String > getOrderersOrganizationMSPIDs () {
1087+ synchronized (ordererMSPIDMap ) {
1088+ return new LinkedList <>(ordererMSPIDMap .keySet ());
1089+ }
1090+ }
1091+
9551092 public PeerOptions getPeersOptions (Peer peer ) {
9561093 PeerOptions ret = peerOptionsMap .get (peer );
9571094 if (ret != null ) {
@@ -1211,19 +1348,20 @@ public Map<String, Orderer> getEndpointMap() {
12111348 });
12121349
12131350 for (SDEndorser sdEndorser : sdNetwork .getEndorsers ()) {
1351+ final String sdEndorserMspid = sdEndorser .getMspid ();
12141352 Peer peer = peerEndpointMap .get (sdEndorser .getEndpoint ());
12151353 if (null == peer ) {
12161354 if (shutdown ) {
12171355 return ;
12181356 }
12191357
1220- logger .debug (format ("Channel %s doing channel update found new peer mspid: %s, endpoint: %s" , name , sdEndorser . getMspid () , sdEndorser .getEndpoint ()));
1358+ logger .debug (format ("Channel %s doing channel update found new peer mspid: %s, endpoint: %s" , name , sdEndorserMspid , sdEndorser .getEndpoint ()));
12211359
12221360 sdPeerAddition .addPeer (new SDPeerAdditionInfo () {
12231361
12241362 @ Override
12251363 public String getMspId () {
1226- return sdEndorser . getMspid () ;
1364+ return sdEndorserMspid ;
12271365 }
12281366
12291367 @ Override
@@ -1261,6 +1399,23 @@ public Map<String, Peer> getEndpointMap() {
12611399 }
12621400
12631401 });
1402+ } else if (discoveryEndpoints .contains (sdEndorser .getEndpoint ())) {
1403+
1404+ //hackfest here.... if the user didn't supply msspid retro fit for disovery peers
1405+ if (peer .getProperties () == null || isNullOrEmpty (peer .getProperties ().getProperty (Peer .PEER_ORGANIZATION_MSPID_PROPERTY ))) {
1406+
1407+ synchronized (peerMSPIDMap ) {
1408+ peerMSPIDMap .computeIfAbsent (sdEndorserMspid , k -> new HashSet <>()).add (peer );
1409+ }
1410+ Properties properties = peer .getProperties ();
1411+ if (properties == null ) {
1412+ properties = new Properties ();
1413+ }
1414+ properties .put (Peer .PEER_ORGANIZATION_MSPID_PROPERTY , sdEndorserMspid );
1415+ peer .setProperties (properties );
1416+
1417+ }
1418+
12641419 }
12651420
12661421 }
@@ -1475,6 +1630,8 @@ public Orderer addOrderer(SDOrdererAdditionInfo sdOrdererAdditionInfo) throws In
14751630 properties .put ("pemBytes" , pemBytes );
14761631 }
14771632
1633+ properties .put (Orderer .ORDERER_ORGANIZATION_MSPID_PROPERTY , sdOrdererAdditionInfo .getMspId ());
1634+
14781635 Orderer orderer = sdOrdererAdditionInfo .getClient ().newOrderer (endpoint ,
14791636 protocol + "//" + endpoint ,
14801637 properties );
@@ -1516,6 +1673,8 @@ public Peer addPeer(SDPeerAdditionInfo sdPeerAddition) throws InvalidArgumentExc
15161673 properties .put ("clientCertFile" , clientCertFile );
15171674 }
15181675
1676+ properties .put (Peer .PEER_ORGANIZATION_MSPID_PROPERTY , sdPeerAddition .getMspId ());
1677+
15191678 byte [] clientKeyBytes = (byte []) findClientProp (config , "clientKeyBytes" , mspid , endpoint , null );
15201679 String clientKeyFile = (String ) findClientProp (config , "clientKeyFile" , mspid , endpoint , null );
15211680 if (null != clientKeyBytes ) {
@@ -6146,6 +6305,9 @@ public synchronized void shutdown(boolean force) {
61466305 }
61476306 peers .clear (); // make sure.
61486307
6308+ peerMSPIDMap .clear ();
6309+ ordererMSPIDMap .clear ();
6310+
61496311 peerEndpointMap .clear ();
61506312 ordererEndpointMap .clear ();
61516313
0 commit comments