Search in sources :

Example 1 with Type

use of org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeSpec.Type in project fabric-sdk-java by hyperledger.

the class InstallProposalBuilder method createNetModeTransaction.

private void createNetModeTransaction() throws IOException {
    logger.debug("createNetModeTransaction");
    if (null == chaincodeSource && chaincodeInputStream == null) {
        throw new IllegalArgumentException("Missing chaincodeSource or chaincodeInputStream in InstallRequest");
    }
    if (null != chaincodeSource && chaincodeInputStream != null) {
        throw new IllegalArgumentException("Both chaincodeSource and chaincodeInputStream in InstallRequest were set. Specify one or the other");
    }
    final Type ccType;
    File projectSourceDir = null;
    String targetPathPrefix = null;
    String dplang;
    File metainf = null;
    if (null != chaincodeMetaInfLocation) {
        if (!chaincodeMetaInfLocation.exists()) {
            throw new IllegalArgumentException(format("Directory to find chaincode META-INF %s does not exist", chaincodeMetaInfLocation.getAbsolutePath()));
        }
        if (!chaincodeMetaInfLocation.isDirectory()) {
            throw new IllegalArgumentException(format("Directory to find chaincode META-INF %s is not a directory", chaincodeMetaInfLocation.getAbsolutePath()));
        }
        metainf = new File(chaincodeMetaInfLocation, "META-INF");
        logger.trace("META-INF directory is " + metainf.getAbsolutePath());
        if (!metainf.exists()) {
            throw new IllegalArgumentException(format("The META-INF directory does not exist in %s", chaincodeMetaInfLocation.getAbsolutePath()));
        }
        if (!metainf.isDirectory()) {
            throw new IllegalArgumentException(format("The META-INF in %s is not a directory.", chaincodeMetaInfLocation.getAbsolutePath()));
        }
        File[] files = metainf.listFiles();
        if (files == null) {
            throw new IllegalArgumentException("null for listFiles on: " + chaincodeMetaInfLocation.getAbsolutePath());
        }
        if (files.length < 1) {
            throw new IllegalArgumentException(format("The META-INF directory %s is empty.", metainf.getAbsolutePath()));
        }
        logger.trace(format("chaincode META-INF found %s", metainf.getAbsolutePath()));
    }
    switch(chaincodeLanguage) {
        case GO_LANG:
            // Verify that chaincodePath is being passed
            if (Utils.isNullOrEmpty(chaincodePath)) {
                throw new IllegalArgumentException("Missing chaincodePath in InstallRequest");
            }
            dplang = "Go";
            ccType = Type.GOLANG;
            if (null != chaincodeSource) {
                projectSourceDir = Paths.get(chaincodeSource.toString(), "src", chaincodePath).toFile();
                targetPathPrefix = Paths.get("src", chaincodePath).toString();
            }
            break;
        case JAVA:
            // Verify that chaincodePath is null
            if (!Utils.isNullOrEmpty(chaincodePath)) {
                throw new IllegalArgumentException("chaincodePath must be null for Java chaincode");
            }
            dplang = "Java";
            ccType = Type.JAVA;
            if (null != chaincodeSource) {
                targetPathPrefix = "src";
                projectSourceDir = Paths.get(chaincodeSource.toString()).toFile();
            }
            break;
        case NODE:
            // Verify that chaincodePath is null
            if (!Utils.isNullOrEmpty(chaincodePath)) {
                throw new IllegalArgumentException("chaincodePath must be null for Node chaincode");
            }
            dplang = "Node";
            ccType = Type.NODE;
            if (null != chaincodeSource) {
                projectSourceDir = Paths.get(chaincodeSource.toString()).toFile();
                // Paths.get("src", chaincodePath).toString();
                targetPathPrefix = "src";
            }
            break;
        default:
            throw new IllegalArgumentException("Unexpected chaincode language: " + chaincodeLanguage);
    }
    ccType(ccType);
    final byte[] data;
    String chaincodeID = chaincodeName + "::" + chaincodePath + "::" + chaincodeVersion;
    if (chaincodeSource != null) {
        if (!projectSourceDir.exists()) {
            final String message = "The project source directory does not exist: " + projectSourceDir.getAbsolutePath();
            logger.error(message);
            throw new IllegalArgumentException(message);
        }
        if (!projectSourceDir.isDirectory()) {
            final String message = "The project source directory is not a directory: " + projectSourceDir.getAbsolutePath();
            logger.error(message);
            throw new IllegalArgumentException(message);
        }
        logger.info(format("Installing '%s' language %s chaincode from directory: '%s' with source location: '%s'. chaincodePath:'%s'", chaincodeID, dplang, projectSourceDir.getAbsolutePath(), targetPathPrefix, chaincodePath));
        // generate chaincode source tar
        data = Utils.generateTarGz(projectSourceDir, targetPathPrefix, metainf);
        if (null != diagnosticFileDumper) {
            logger.trace(format("Installing '%s' language %s chaincode from directory: '%s' with source location: '%s'. chaincodePath:'%s' tar file dump %s", chaincodeID, dplang, projectSourceDir.getAbsolutePath(), targetPathPrefix, chaincodePath, diagnosticFileDumper.createDiagnosticTarFile(data)));
        }
    } else {
        logger.info(format("Installing '%s'  %s chaincode chaincodePath:'%s' from input stream", chaincodeID, dplang, chaincodePath));
        data = IOUtils.toByteArray(chaincodeInputStream);
        if (null != diagnosticFileDumper) {
            logger.trace(format("Installing '%s' language %s chaincode from input stream tar file dump %s", chaincodeID, dplang, diagnosticFileDumper.createDiagnosticTarFile(data)));
        }
    }
    final ChaincodeDeploymentSpec depspec = createDeploymentSpec(ccType, this.chaincodeName, this.chaincodePath, this.chaincodeVersion, null, data);
    // set args
    final List<ByteString> argList = new ArrayList<>();
    argList.add(ByteString.copyFrom(action, StandardCharsets.UTF_8));
    argList.add(depspec.toByteString());
    args(argList);
}
Also used : Type(org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeSpec.Type) ChaincodeDeploymentSpec(org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeDeploymentSpec) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) File(java.io.File)

Aggregations

ByteString (com.google.protobuf.ByteString)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 ChaincodeDeploymentSpec (org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeDeploymentSpec)1 Type (org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeSpec.Type)1