Skip to content

Commit

Permalink
Service and impl for fetching existing Collection Config details
Browse files Browse the repository at this point in the history
Signed-off-by: “Nithin <nithin.pankaj@walmartlabs.com>
  • Loading branch information
“Nithin committed Jan 15, 2024
1 parent e397077 commit 8374e30
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ public ResponseEntity<String> getCurrentPackageId(
return new ResponseEntity<>(operationResponse, HttpStatus.OK);
}

@GetMapping(value = "/collection-config")
@MetricsTrackedEndpoint(
name = "Get Collection Config",
method = "GET",
uri = "/chaincode/collection-config")
public ResponseEntity<String> getCollectionConfig(
@RequestParam("network_name") @Validated String networkName,
@RequestParam("chaincode_name") @Validated String chaincodeName,
@RequestParam("chaincode_version") @Validated String chaincodeVersion) {

String operationResponse =
chaincodeOperationsService.getCollectionConfig(
networkName, chaincodeName, chaincodeVersion);

operationResponse = HtmlUtils.htmlEscape(operationResponse);

return new ResponseEntity<>(operationResponse, HttpStatus.OK);
}

@PostMapping(value = "/approved-organisations")
@MetricsTrackedEndpoint(
name = "Fetch Approved Organizations",
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/hlf/java/rest/client/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public enum ErrorCode {

AUTH_INVALID_API_KEY(9000, "Invalid api key"),

NO_COLLECTION_CONFIG_FOUND(9001, "No Collection Config found"),

NOT_DEFINED(
9999,
"The exception is not a BaseException OR error code is not yet defined by the developer");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ String performChaincodeOperation(
*/
String getCurrentPackageId(String networkName, String chaincodeName, String chaincodeVersion);

/**
* Gets the current PDC associated with the specified Chaincode
*
* @param networkName the network name
* @param chaincodeName the chaincode name
* @param chaincodeVersion the chaincode version
* @return the current package id
*/
String getCollectionConfig(String networkName, String chaincodeName, String chaincodeVersion);

Set<String> getApprovedOrganizations(
String networkName,
ChaincodeOperations chaincodeOperationsModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.apache.commons.lang3.StringUtils.isEmpty;

import hlf.java.rest.client.exception.ErrorCode;
import hlf.java.rest.client.exception.NotFoundException;
import hlf.java.rest.client.exception.ServiceException;
import hlf.java.rest.client.model.ChaincodeOperations;
import hlf.java.rest.client.model.ChaincodeOperationsType;
Expand All @@ -16,6 +17,7 @@
import java.io.InputStream;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -226,6 +228,55 @@ public String getCurrentPackageId(
}
}

@Override
public String getCollectionConfig(
String networkName, String chaincodeName, String chaincodeVersion) {

try {

Network network = gateway.getNetwork(networkName);
Channel channel = network.getChannel();

Collection<Peer> peers = channel.getPeers();

final QueryLifecycleQueryChaincodeDefinitionRequest
queryLifecycleQueryChaincodeDefinitionRequest =
hfClientWrapper.getHfClient().newQueryLifecycleQueryChaincodeDefinitionRequest();
queryLifecycleQueryChaincodeDefinitionRequest.setChaincodeName(chaincodeName);

Collection<LifecycleQueryChaincodeDefinitionProposalResponse>
queryChaincodeDefinitionProposalResponses =
channel.lifecycleQueryChaincodeDefinition(
queryLifecycleQueryChaincodeDefinitionRequest, peers);

String collectionConfigAsString;

for (LifecycleQueryChaincodeDefinitionProposalResponse response :
queryChaincodeDefinitionProposalResponses) {

if (response.getStatus().equals(ProposalResponse.Status.SUCCESS)
&& response.getVersion().equals(chaincodeVersion)) {

if (Objects.nonNull(response.getChaincodeCollectionConfiguration())) {
collectionConfigAsString =
new String(response.getChaincodeCollectionConfiguration().getAsBytes());
return collectionConfigAsString;
}
}
}

throw new NotFoundException(
ErrorCode.NO_COLLECTION_CONFIG_FOUND,
"Couldn't find any associated Collection config for the Chaincode");

} catch (ProposalException
| InvalidArgumentException
| ChaincodeCollectionConfigurationException e) {
throw new ServiceException(
ErrorCode.HYPERLEDGER_FABRIC_CHAINCODE_OPERATIONS_REQUEST_REJECTION, e.getMessage(), e);
}
}

@Override
public Set<String> getApprovedOrganizations(
String networkName,
Expand Down

0 comments on commit 8374e30

Please sign in to comment.