Search in sources :

Example 6 with NetworkConfigurationException

use of org.hyperledger.fabric.sdk.exception.NetworkConfigurationException in project fabric-sdk-java by hyperledger.

the class NetworkConfig method createAllOrderers.

/*
    **
     * Returns a peer from the specified organization and having the desired role.
     * <p>
     * Note that if more than one peer matches the supplied attributes, it is arbitrary which peer will be returned.
     *
     * @param orgName The name of the organization (or null to use the client organization)
     * @param role The desired role (or null for any role)
     * @return A matching peer (or null if a suitable peer was not found)
     *
    Peer findPeerWithRole(String orgName, PeerRole role) {

        JsonObject org = orgName == null ? clientOrganization : getOrganization(orgName);
        if (org == null) {
            // The organization is not defined, so no suitable peer exists
            return null;
        }

        // Examine the peers associated with this organization
        JsonArray peerNames = getJsonValueAsArray(org.get("peers"));
        if (peerNames != null) {
            for (JsonValue val: peerNames) {
                String peerName = getJsonValueAsString(val);
                if (peerName != null) {
                    Node peer = peers.get(peerName);
                    if (peer != null) {
                        // TODO: Currently we are ignoring the role - because roles are channel-based and hence we need to know the channel before we can get the roles!
                        try {
                            return Peer.createNewInstance(peer.getName(), peer.getUrl(), peer.getProperties());
                        } catch (InvalidArgumentException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }

        return null;
    }
*/
// Creates Node instances representing all the orderers defined in the config file
private void createAllOrderers() throws NetworkConfigurationException {
    // Sanity check
    if (orderers != null) {
        throw new NetworkConfigurationException("INTERNAL ERROR: orderers has already been initialized!");
    }
    orderers = new HashMap<>();
    // orderers is a JSON object containing a nested object for each orderers
    JsonObject jsonOrderers = getJsonObject(jsonConfig, "orderers");
    if (jsonOrderers != null) {
        for (Entry<String, JsonValue> entry : jsonOrderers.entrySet()) {
            String ordererName = entry.getKey();
            JsonObject jsonOrderer = getJsonValueAsObject(entry.getValue());
            if (jsonOrderer == null) {
                throw new NetworkConfigurationException(format("Error loading config. Invalid orderer entry: %s", ordererName));
            }
            Node orderer = createNode(ordererName, jsonOrderer, "url");
            orderers.put(ordererName, orderer);
        }
    }
}
Also used : JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) NetworkConfigurationException(org.hyperledger.fabric.sdk.exception.NetworkConfigurationException)

Example 7 with NetworkConfigurationException

use of org.hyperledger.fabric.sdk.exception.NetworkConfigurationException in project fabric-sdk-java by hyperledger.

the class NetworkConfig method getTLSCerts.

private void getTLSCerts(String nodeName, JsonObject jsonOrderer, Properties props) throws NetworkConfigurationException {
    JsonObject jsonTlsCaCerts = getJsonObject(jsonOrderer, "tlsCACerts");
    if (jsonTlsCaCerts != null) {
        String pemFilename = getJsonValueAsString(jsonTlsCaCerts.get("path"));
        String pemBytes = getJsonValueAsString(jsonTlsCaCerts.get("pem"));
        if (pemFilename != null && pemBytes != null) {
            throw new NetworkConfigurationException(format("Endpoint %s should not specify both tlsCACerts path and pem", nodeName));
        }
        if (pemFilename != null) {
            // Determine full pathname and ensure the file exists
            File pemFile = new File(pemFilename);
            String fullPathname = pemFile.getAbsolutePath();
            props.put("pemFile", fullPathname);
        }
        if (pemBytes != null) {
            props.put("pemBytes", pemBytes.getBytes());
        }
    }
}
Also used : JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) NetworkConfigurationException(org.hyperledger.fabric.sdk.exception.NetworkConfigurationException) File(java.io.File)

Example 8 with NetworkConfigurationException

use of org.hyperledger.fabric.sdk.exception.NetworkConfigurationException in project fabric-sdk-java by hyperledger.

the class NetworkConfig method loadChannel.

// public Set<CertificateAuthority> getPeerCertificateAuthorites(String peerName) {
// return null;
// }
/**
 * Returns a channel configured using the details in the Network Configuration file
 *
 * @param client      The associated client
 * @param channelName The name of the channel
 * @return A configured Channel instance
 */
Channel loadChannel(HFClient client, String channelName) throws NetworkConfigurationException {
    if (logger.isTraceEnabled()) {
        logger.trace(format("NetworkConfig.loadChannel: %s", channelName));
    }
    Channel channel = null;
    JsonObject channels = getJsonObject(jsonConfig, "channels");
    if (channels != null) {
        JsonObject jsonChannel = getJsonObject(channels, channelName);
        if (jsonChannel != null) {
            channel = client.getChannel(channelName);
            if (channel != null) {
                // Note that by rights this should never happen as HFClient.loadChannelFromConfig should have already checked for this!
                throw new NetworkConfigurationException(format("Channel %s is already configured in the client!", channelName));
            }
            channel = reconstructChannel(client, channelName, jsonChannel);
        }
    }
    return channel;
}
Also used : JsonObject(javax.json.JsonObject) NetworkConfigurationException(org.hyperledger.fabric.sdk.exception.NetworkConfigurationException)

Example 9 with NetworkConfigurationException

use of org.hyperledger.fabric.sdk.exception.NetworkConfigurationException in project fabric-sdk-java by hyperledger.

the class NetworkConfig method createAllPeers.

// Creates Node instances representing all the peers (and associated event hubs) defined in the config file
private void createAllPeers() throws NetworkConfigurationException {
    // Sanity checks
    if (peers != null) {
        throw new NetworkConfigurationException("INTERNAL ERROR: peers has already been initialized!");
    }
    if (eventHubs != null) {
        throw new NetworkConfigurationException("INTERNAL ERROR: eventHubs has already been initialized!");
    }
    peers = new HashMap<>();
    eventHubs = new HashMap<>();
    // peers is a JSON object containing a nested object for each peer
    JsonObject jsonPeers = getJsonObject(jsonConfig, "peers");
    // out("Peers: " + (jsonPeers == null ? "null" : jsonPeers.toString()));
    if (jsonPeers != null) {
        for (Entry<String, JsonValue> entry : jsonPeers.entrySet()) {
            String peerName = entry.getKey();
            JsonObject jsonPeer = getJsonValueAsObject(entry.getValue());
            if (jsonPeer == null) {
                throw new NetworkConfigurationException(format("Error loading config. Invalid peer entry: %s", peerName));
            }
            Node peer = createNode(peerName, jsonPeer, "url");
            peers.put(peerName, peer);
            // Also create an event hub with the same name as the peer
            Node eventHub = createNode(peerName, jsonPeer, "eventUrl");
            eventHubs.put(peerName, eventHub);
        }
    }
}
Also used : JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) NetworkConfigurationException(org.hyperledger.fabric.sdk.exception.NetworkConfigurationException)

Example 10 with NetworkConfigurationException

use of org.hyperledger.fabric.sdk.exception.NetworkConfigurationException in project fabric-sdk-java by hyperledger.

the class NetworkConfig method createAllCertificateAuthorities.

// Creates JsonObjects representing all the CertificateAuthorities defined in the config file
private void createAllCertificateAuthorities() throws NetworkConfigurationException {
    // Sanity check
    if (certificateAuthorities != null) {
        throw new NetworkConfigurationException("INTERNAL ERROR: certificateAuthorities has already been initialized!");
    }
    certificateAuthorities = new HashMap<>();
    // certificateAuthorities is a JSON object containing a nested object for each CA
    JsonObject jsonCertificateAuthorities = getJsonObject(jsonConfig, "certificateAuthorities");
    if (jsonCertificateAuthorities != null) {
        for (Entry<String, JsonValue> entry : jsonCertificateAuthorities.entrySet()) {
            String name = entry.getKey();
            JsonObject jsonCA = getJsonValueAsObject(entry.getValue());
            if (jsonCA == null) {
                throw new NetworkConfigurationException(format("Error loading config. Invalid CA entry: %s", name));
            }
            CAInfo ca = createCA(name, jsonCA);
            certificateAuthorities.put(name, ca);
        }
    }
}
Also used : JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) NetworkConfigurationException(org.hyperledger.fabric.sdk.exception.NetworkConfigurationException)

Aggregations

NetworkConfigurationException (org.hyperledger.fabric.sdk.exception.NetworkConfigurationException)10 JsonString (javax.json.JsonString)9 JsonObject (javax.json.JsonObject)8 JsonValue (javax.json.JsonValue)7 File (java.io.File)2 IOException (java.io.IOException)2 JsonArray (javax.json.JsonArray)2 FileInputStream (java.io.FileInputStream)1 PrivateKey (java.security.PrivateKey)1 PeerOptions (org.hyperledger.fabric.sdk.Channel.PeerOptions)1 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)1