Skip to content

Commit

Permalink
Enhancement:removed private variable from service
Browse files Browse the repository at this point in the history
Signed-off-by: n0s09by <[email protected]>
  • Loading branch information
nidhi-singh02 committed Jan 10, 2024
1 parent ee49783 commit d6775ed
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ public ResponseEntity<ClientResponseModel> getDeserializedJson(
*
* @param channelName - the name of the channel for which you wish to add the anchor peer nodes
* to.
* @param anchorPeerParamsDTO - contains the details for the organization peers you wish to be
* @param channelUpdateParamsDTO - contains the details for the organization peers you wish to be
* added to the channel as anchor peer.
* @return ResponseEntity<ClientResponseModel> - contains the result of the operation.
*/
@PostMapping(value = "/channel/{channelName}/add_anchor_peer")
public ResponseEntity<ClientResponseModel> addAnchorPeersToChannel(
@PathVariable @Validated String channelName,
@RequestBody @Validated ChannelUpdateParamsDTO anchorPeerParamsDTO) {
return networkStatus.addAnchorPeersToChannel(channelName, anchorPeerParamsDTO);
@RequestBody @Validated ChannelUpdateParamsDTO channelUpdateParamsDTO) {
return networkStatus.addAnchorPeersToChannel(channelName, channelUpdateParamsDTO);
}
}
10 changes: 0 additions & 10 deletions src/main/java/hlf/java/rest/client/model/AnchorPeerParamsDTO.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@

@Data
public class NewOrgParamsDTO extends ChannelUpdateParamsDTO {
private String organizationName;
private MSPDTO mspDTO;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ ResponseEntity<ClientResponseModel> addOrgToChannel(
String channelName, NewOrgParamsDTO organizationDetails);

ResponseEntity<ClientResponseModel> addAnchorPeersToChannel(
String channelName, ChannelUpdateParamsDTO anchorPeerParamsDTO);
String channelName, ChannelUpdateParamsDTO channelUpdateParamsDTO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,12 @@ public ResponseEntity<ClientResponseModel> addOrgToChannel(

@Override
public ResponseEntity<ClientResponseModel> addAnchorPeersToChannel(
String channelName, ChannelUpdateParamsDTO anchorPeerParamsDTO) {
String channelName, ChannelUpdateParamsDTO channelUpdateParamsDTO) {
Network network = gateway.getNetwork(channelName);
if (network != null && user != null) {
try {
Channel selectedChannel = network.getChannel();
ConfigUpdate configUpdate = getChannelConfig(channelName, anchorPeerParamsDTO);
ConfigUpdate configUpdate = getChannelConfig(channelName, channelUpdateParamsDTO);
String channelConfigString = JsonFormat.printer().print(configUpdate);
log.info(channelConfigDeserialization.deserializeValueFields(channelConfigString));
UpdateChannelConfiguration updateChannelConfiguration = new UpdateChannelConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,24 @@
@Service
public class UpdateChannelImpl implements UpdateChannel {

private NewOrgParamsDTO newOrganizationDetails;
private ChannelUpdateParamsDTO channelUpdateParamsDTO;

@Override
public ConfigGroup buildWriteset(ConfigGroup readset, ChannelUpdateParamsDTO organizationDetails)
throws ServiceException {

newOrganizationDetails = (NewOrgParamsDTO) organizationDetails;
String newOrgMspId = newOrganizationDetails.getOrganizationMspId();

NewOrgParamsDTO newOrgParamsDTO = (NewOrgParamsDTO) organizationDetails;
String newOrgMspId = organizationDetails.getOrganizationMspId();
Map<String, ConfigGroup> existingOrganizations =
FabricChannelUtil.getExistingOrgsFromReadset(readset);

ConfigGroup applicationGroup;

if (newOrganizationDetails.getMspDTO() != null) {
if (newOrgParamsDTO.getMspDTO() != null) {
// New org addition scenario
// The "Application" group
applicationGroup =
ConfigGroup.newBuilder()
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
.putAllPolicies(FabricChannelUtil.setApplicationPolicies(readset))
.putGroups(newOrgMspId, setNewOrgGroup(newOrgMspId))
.putGroups(newOrgMspId, setNewOrgGroup(newOrgMspId, newOrgParamsDTO))
// putAllGroups excludes new organization
.putAllGroups(existingOrganizations)
// Application group version
Expand All @@ -62,7 +58,7 @@ public ConfigGroup buildWriteset(ConfigGroup readset, ChannelUpdateParamsDTO org
ConfigGroup.newBuilder()
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
.putAllPolicies(FabricChannelUtil.setApplicationPolicies(readset))
.putGroups(newOrgMspId, setAnchorPeerInGroup(newOrgMspId, readset))
.putGroups(newOrgMspId, setAnchorPeerInGroup(newOrgMspId, readset, newOrgParamsDTO))
// putAllGroups excludes new organization
.putAllGroups(existingOrganizations)
// Application group version
Expand All @@ -82,11 +78,13 @@ public ConfigGroup buildWriteset(ConfigGroup readset, ChannelUpdateParamsDTO org
.build();
}

private ConfigGroup setAnchorPeerInGroup(String orgMspId, ConfigGroup readSet) {
private ConfigGroup setAnchorPeerInGroup(
String orgMspId, ConfigGroup readSet, ChannelUpdateParamsDTO channelUpdateParamsDTO) {
Map<String, ConfigValue> valueMap = new HashMap<>();
if (newOrganizationDetails.getAnchorPeerDTOs() != null) {
if (channelUpdateParamsDTO.getAnchorPeerDTOs() != null) {
valueMap.put(
FabricClientConstants.CHANNEL_CONFIG_GROUP_VALUE_ANCHORPEERS, setNewOrgAnchorPeerValue());
FabricClientConstants.CHANNEL_CONFIG_GROUP_VALUE_ANCHORPEERS,
setNewOrgAnchorPeerValue(channelUpdateParamsDTO));
}
return ConfigGroup.newBuilder()
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
Expand All @@ -99,13 +97,15 @@ private ConfigGroup setAnchorPeerInGroup(String orgMspId, ConfigGroup readSet) {
.build();
}

private ConfigGroup setNewOrgGroup(String newOrgMspId) {
private ConfigGroup setNewOrgGroup(String newOrgMspId, NewOrgParamsDTO newOrgParamsDTO) {
Map<String, ConfigValue> valueMap = new HashMap<>();
valueMap.put(
FabricClientConstants.CHANNEL_CONFIG_GROUP_VALUE_MSP, setNewOrgMspValue(newOrgMspId));
if (newOrganizationDetails.getAnchorPeerDTOs() != null) {
FabricClientConstants.CHANNEL_CONFIG_GROUP_VALUE_MSP,
setNewOrgMspValue(newOrgMspId, newOrgParamsDTO));
if (newOrgParamsDTO.getAnchorPeerDTOs() != null) {
valueMap.put(
FabricClientConstants.CHANNEL_CONFIG_GROUP_VALUE_ANCHORPEERS, setNewOrgAnchorPeerValue());
FabricClientConstants.CHANNEL_CONFIG_GROUP_VALUE_ANCHORPEERS,
setNewOrgAnchorPeerValue(newOrgParamsDTO));
}

return ConfigGroup.newBuilder()
Expand All @@ -116,35 +116,35 @@ private ConfigGroup setNewOrgGroup(String newOrgMspId) {
.build();
}

private ConfigValue setNewOrgMspValue(String newOrgMspId) {
private ConfigValue setNewOrgMspValue(String newOrgMspId, NewOrgParamsDTO newOrgParamsDTO) {
return ConfigValue.newBuilder()
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
.setValue(
setMspConfig(newOrgMspId)
setMspConfig(newOrgMspId, newOrgParamsDTO)
.toByteString()) // ByteString, need to figure out how to build the proper
// structure
.setVersion(0)
.build();
}

private MSPConfig setMspConfig(String newOrgMspId) {
private MSPConfig setMspConfig(String newOrgMspId, NewOrgParamsDTO newOrgParamsDTO) {
return MSPConfig.newBuilder()
.setType(0)
.setConfig(newOrgValue(newOrgMspId).toByteString())
.setConfig(newOrgValue(newOrgMspId, newOrgParamsDTO).toByteString())
.build();
}

private ConfigValue setNewOrgAnchorPeerValue() {
private ConfigValue setNewOrgAnchorPeerValue(ChannelUpdateParamsDTO channelUpdateParamsDTO) {
return ConfigValue.newBuilder()
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
.setValue(setAnchorPeers().toByteString())
.setValue(setAnchorPeers(channelUpdateParamsDTO).toByteString())
.setVersion(0)
.build();
}

private AnchorPeers setAnchorPeers() {
private AnchorPeers setAnchorPeers(ChannelUpdateParamsDTO channelUpdateParamsDTO) {
List<AnchorPeer> anchorPeerList = new ArrayList<>();
for (AnchorPeerDTO anchorPeerDTO : newOrganizationDetails.getAnchorPeerDTOs()) {
for (AnchorPeerDTO anchorPeerDTO : channelUpdateParamsDTO.getAnchorPeerDTOs()) {
anchorPeerList.add(
AnchorPeer.newBuilder()
.setHost(anchorPeerDTO.getHostname())
Expand All @@ -155,7 +155,7 @@ private AnchorPeers setAnchorPeers() {
}

// Error with this section when converting block.pb from PROTO to JSONBtye
private FabricMSPConfig newOrgValue(String newOrgMspId) {
private FabricMSPConfig newOrgValue(String newOrgMspId, NewOrgParamsDTO newOrgParamsDTO) {
// MSP cacerts full certificate (including the ----BEGIN... and ----END...
// tags), NOT base64 as that's done by fabric on commit
List<ByteString> rootCertCollection = new ArrayList<>();
Expand All @@ -165,22 +165,22 @@ private FabricMSPConfig newOrgValue(String newOrgMspId) {
byte[] ordererCert;
byte[] peerCert;

for (String rootCerts : newOrganizationDetails.getMspDTO().getRootCerts()) {
for (String rootCerts : newOrgParamsDTO.getMspDTO().getRootCerts()) {
rootCertCollection.add(ByteString.copyFrom(rootCerts.getBytes()));
}
for (String tlsRootCerts : newOrganizationDetails.getMspDTO().getTlsRootCerts()) {
for (String tlsRootCerts : newOrgParamsDTO.getMspDTO().getTlsRootCerts()) {
tlsRootCertCollection.add(ByteString.copyFrom(tlsRootCerts.getBytes()));
}
adminCert = newOrganizationDetails.getMspDTO().getAdminOUCert().getBytes();
clientCert = newOrganizationDetails.getMspDTO().getClientOUCert().getBytes();
adminCert = newOrgParamsDTO.getMspDTO().getAdminOUCert().getBytes();
clientCert = newOrgParamsDTO.getMspDTO().getClientOUCert().getBytes();

FabricNodeOUs.Builder builder = null;
if (newOrganizationDetails.getMspDTO().getOrdererOUCert() != null) {
ordererCert = newOrganizationDetails.getMspDTO().getOrdererOUCert().getBytes();
if (newOrgParamsDTO.getMspDTO().getOrdererOUCert() != null) {
ordererCert = newOrgParamsDTO.getMspDTO().getOrdererOUCert().getBytes();
builder = getFabricNodeOUs(true, adminCert, clientCert, ordererCert);
}
if (newOrganizationDetails.getMspDTO().getPeerOUCert() != null) {
peerCert = newOrganizationDetails.getMspDTO().getPeerOUCert().getBytes();
if (newOrgParamsDTO.getMspDTO().getPeerOUCert() != null) {
peerCert = newOrgParamsDTO.getMspDTO().getPeerOUCert().getBytes();
builder = getFabricNodeOUs(false, adminCert, clientCert, peerCert);
}

Expand Down
9 changes: 4 additions & 5 deletions src/test/java/hlf/java/rest/client/IT/ChannelIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ public void channelJoinForTwoOrgsTest() {
@Order(6)
public void addOrgToChannelTest() {
NewOrgParamsDTO newOrgParamsDTO = new NewOrgParamsDTO();
newOrgParamsDTO.setOrganizationName(ORG_2_MSP);
try {
String signcert =
FileUtils.readFileToString(
Expand Down Expand Up @@ -343,16 +342,16 @@ public void addOrgToChannelTest() {
@Test
@Order(7)
public void addAnchorPeersToChannelTest() {
ChannelUpdateParamsDTO anchorPeerParamsDTO = new ChannelUpdateParamsDTO();
anchorPeerParamsDTO.setOrganizationMspId(ORG_1_MSP);
ChannelUpdateParamsDTO channelUpdateParamsDTO = new ChannelUpdateParamsDTO();
channelUpdateParamsDTO.setOrganizationMspId(ORG_1_MSP);
List<AnchorPeerDTO> anchorPeerDTOs = new ArrayList<>();
AnchorPeerDTO anchorPeerDTO = new AnchorPeerDTO();
anchorPeerDTO.setHostname("peer0.org1.example.com");
anchorPeerDTO.setPort(7051);
anchorPeerDTOs.add(anchorPeerDTO);
anchorPeerParamsDTO.setAnchorPeerDTOs(anchorPeerDTOs);
channelUpdateParamsDTO.setAnchorPeerDTOs(anchorPeerDTOs);
ResponseEntity<ClientResponseModel> responseModel =
networkStatus.addAnchorPeersToChannel(CHANNEL_NAME, anchorPeerParamsDTO);
networkStatus.addAnchorPeersToChannel(CHANNEL_NAME, channelUpdateParamsDTO);
Assertions.assertEquals(new Integer(200), responseModel.getStatusCodeValue());
}
}

0 comments on commit d6775ed

Please sign in to comment.