use of org.hyperledger.fabric.sdk.exception.NetworkConfigurationException in project fabric-sdk-java by hyperledger.
the class NetworkConfig method reconstructChannel.
// Reconstructs an existing channel
private Channel reconstructChannel(HFClient client, String channelName, JsonObject jsonChannel) throws NetworkConfigurationException {
Channel channel = null;
try {
channel = client.newChannel(channelName);
// orderers is an array of orderer name strings
JsonArray ordererNames = getJsonValueAsArray(jsonChannel.get("orderers"));
boolean foundOrderer = false;
// out("Orderer names: " + (ordererNames == null ? "null" : ordererNames.toString()));
if (ordererNames != null) {
for (JsonValue jsonVal : ordererNames) {
String ordererName = getJsonValueAsString(jsonVal);
Orderer orderer = getOrderer(client, ordererName);
if (orderer == null) {
throw new NetworkConfigurationException(format("Error constructing channel %s. Orderer %s not defined in configuration", channelName, ordererName));
}
channel.addOrderer(orderer);
foundOrderer = true;
}
}
if (!foundOrderer) {
// orderers is a required field
throw new NetworkConfigurationException(format("Error constructing channel %s. At least one orderer must be specified", channelName));
}
// peers is an object containing a nested object for each peer
JsonObject jsonPeers = getJsonObject(jsonChannel, "peers");
boolean foundPeer = false;
// out("Peers: " + (peers == null ? "null" : peers.toString()));
if (jsonPeers != null) {
for (Entry<String, JsonValue> entry : jsonPeers.entrySet()) {
String peerName = entry.getKey();
if (logger.isTraceEnabled()) {
logger.trace(format("NetworkConfig.reconstructChannel: Processing peer %s", peerName));
}
JsonObject jsonPeer = getJsonValueAsObject(entry.getValue());
if (jsonPeer == null) {
throw new NetworkConfigurationException(format("Error constructing channel %s. Invalid peer entry: %s", channelName, peerName));
}
Peer peer = getPeer(client, peerName);
if (peer == null) {
throw new NetworkConfigurationException(format("Error constructing channel %s. Peer %s not defined in configuration", channelName, peerName));
}
// Set the various roles
PeerOptions peerOptions = PeerOptions.createPeerOptions();
setPeerRole(channelName, peerOptions, jsonPeer, PeerRole.ENDORSING_PEER);
setPeerRole(channelName, peerOptions, jsonPeer, PeerRole.CHAINCODE_QUERY);
setPeerRole(channelName, peerOptions, jsonPeer, PeerRole.LEDGER_QUERY);
setPeerRole(channelName, peerOptions, jsonPeer, PeerRole.EVENT_SOURCE);
channel.addPeer(peer, peerOptions);
foundPeer = true;
// Add the event hub associated with this peer
EventHub eventHub = getEventHub(client, peerName);
if (eventHub == null) {
// By rights this should never happen!
throw new NetworkConfigurationException(format("Error constructing channel %s. EventHub for %s not defined in configuration", channelName, peerName));
}
channel.addEventHub(eventHub);
}
}
if (!foundPeer) {
// peers is a required field
throw new NetworkConfigurationException(format("Error constructing channel %s. At least one peer must be specified", channelName));
}
} catch (InvalidArgumentException e) {
throw new IllegalArgumentException(e);
}
return channel;
}
use of org.hyperledger.fabric.sdk.exception.NetworkConfigurationException in project fabric-sdk-java by hyperledger.
the class NetworkConfig method extractPemString.
// Returns the PEM (as a String) from either a path or a pem field
private static String extractPemString(JsonObject json, String fieldName, String msgPrefix) throws NetworkConfigurationException {
String path = null;
String pemString = null;
JsonObject jsonField = getJsonValueAsObject(json.get(fieldName));
if (jsonField != null) {
path = getJsonValueAsString(jsonField.get("path"));
pemString = getJsonValueAsString(jsonField.get("pem"));
}
if (path != null && pemString != null) {
throw new NetworkConfigurationException(format("%s should not specify both %s path and pem", msgPrefix, fieldName));
}
if (path != null) {
// Determine full pathname and ensure the file exists
File pemFile = new File(path);
String fullPathname = pemFile.getAbsolutePath();
if (!pemFile.exists()) {
throw new NetworkConfigurationException(format("%s: %s file %s does not exist", msgPrefix, fieldName, fullPathname));
}
try (FileInputStream stream = new FileInputStream(pemFile)) {
pemString = IOUtils.toString(stream, "UTF-8");
} catch (IOException ioe) {
throw new NetworkConfigurationException(format("Failed to read file: %s", fullPathname), ioe);
}
}
return pemString;
}
use of org.hyperledger.fabric.sdk.exception.NetworkConfigurationException in project fabric-sdk-java by hyperledger.
the class NetworkConfig method setPeerRole.
private static void setPeerRole(String channelName, PeerOptions peerOptions, JsonObject jsonPeer, PeerRole role) throws NetworkConfigurationException {
String propName = role.getPropertyName();
JsonValue val = jsonPeer.get(propName);
if (val != null) {
Boolean isSet = getJsonValueAsBoolean(val);
if (isSet == null) {
// This is an invalid boolean value
throw new NetworkConfigurationException(format("Error constructing channel %s. Role %s has invalid boolean value: %s", channelName, propName, val.toString()));
}
if (isSet) {
peerOptions.addPeerRole(role);
}
}
}
use of org.hyperledger.fabric.sdk.exception.NetworkConfigurationException in project fabric-sdk-java by hyperledger.
the class NetworkConfig method createOrg.
// Creates a new OrgInfo instance from a JSON object
private OrgInfo createOrg(String orgName, JsonObject jsonOrg) throws NetworkConfigurationException {
String msgPrefix = format("Organization %s", orgName);
// TODO: Note the camel-case inconsistency with "mspid" vs "mspId"!
String mspId = getJsonValueAsString(jsonOrg.get("mspid"));
OrgInfo org = new OrgInfo(orgName, mspId);
// Peers
JsonArray jsonPeers = getJsonValueAsArray(jsonOrg.get("peers"));
if (jsonPeers != null) {
for (JsonValue peer : jsonPeers) {
String peerName = getJsonValueAsString(peer);
if (peerName != null) {
org.addPeerName(peerName);
}
}
}
// CAs
JsonArray jsonCertificateAuthorities = getJsonValueAsArray(jsonOrg.get("certificateAuthorities"));
if (jsonCertificateAuthorities != null) {
for (JsonValue jsonCA : jsonCertificateAuthorities) {
String caName = getJsonValueAsString(jsonCA);
if (caName != null) {
// org.addCAName(caName);
CAInfo caInfo = certificateAuthorities.get(caName);
if (caInfo == null) {
throw new NetworkConfigurationException(format("%s: Certificate Authority %s is not defined", msgPrefix, caName));
}
org.addCertificateAuthority(caInfo);
}
}
}
String adminPrivateKeyString = extractPemString(jsonOrg, "adminPrivateKey", msgPrefix);
String signedCert = extractPemString(jsonOrg, "signedCert", msgPrefix);
PrivateKey privateKey = null;
if (adminPrivateKeyString != null) {
try {
privateKey = getPrivateKeyFromString(adminPrivateKeyString);
} catch (IOException ioe) {
throw new NetworkConfigurationException(format("%s: Invalid private key", msgPrefix), ioe);
}
}
if (privateKey != null) {
org.setAdminPrivateKey(privateKey);
}
if (signedCert != null) {
org.setSignedCert(signedCert);
}
return org;
}
use of org.hyperledger.fabric.sdk.exception.NetworkConfigurationException in project fabric-sdk-java by hyperledger.
the class NetworkConfig method createAllOrganizations.
// Creates JsonObjects representing all the Organizations defined in the config file
private void createAllOrganizations() throws NetworkConfigurationException {
// Sanity check
if (organizations != null) {
throw new NetworkConfigurationException("INTERNAL ERROR: organizations has already been initialized!");
}
organizations = new HashMap<>();
// organizations is a JSON object containing a nested object for each Org
JsonObject jsonOrganizations = getJsonObject(jsonConfig, "organizations");
if (jsonOrganizations != null) {
for (Entry<String, JsonValue> entry : jsonOrganizations.entrySet()) {
String orgName = entry.getKey();
JsonObject jsonOrg = getJsonValueAsObject(entry.getValue());
if (jsonOrg == null) {
throw new NetworkConfigurationException(format("Error loading config. Invalid Organization entry: %s", orgName));
}
OrgInfo org = createOrg(orgName, jsonOrg);
organizations.put(orgName, org);
}
}
}
Aggregations