use of org.hyperledger.fabric.protos.common.MspPrincipal.MSPRole in project fabric-sdk-java by hyperledger.
the class ChaincodeEndorsementPolicy method parseIdentities.
private static IndexedHashMap<String, MSPPrincipal> parseIdentities(Map<?, ?> identities) throws ChaincodeEndorsementPolicyParseException {
// Only Role types are excepted at this time.
IndexedHashMap<String, MSPPrincipal> ret = new IndexedHashMap<>();
for (Map.Entry<?, ?> kp : identities.entrySet()) {
Object key = kp.getKey();
Object val = kp.getValue();
if (!(key instanceof String)) {
throw new ChaincodeEndorsementPolicyParseException(format("In identities key expected String got %s ", key == null ? "null" : key.getClass().getName()));
}
if (null != ret.get(key)) {
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s is listed more than once ", key));
}
if (!(val instanceof Map)) {
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s value expected Map got %s ", key, val == null ? "null" : val.getClass().getName()));
}
Object role = ((Map<?, ?>) val).get("role");
if (!(role instanceof Map)) {
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s value expected Map for role got %s ", key, role == null ? "null" : role.getClass().getName()));
}
final Map<?, ?> roleMap = (Map<?, ?>) role;
Object nameObj = (roleMap).get("name");
if (!(nameObj instanceof String)) {
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s name expected String in role got %s ", key, nameObj == null ? "null" : nameObj.getClass().getName()));
}
String name = (String) nameObj;
name = name.trim();
Object mspId = roleMap.get("mspId");
if (!(mspId instanceof String)) {
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s mspId expected String in role got %s ", key, mspId == null ? "null" : mspId.getClass().getName()));
}
if (StringUtil.isNullOrEmpty((String) mspId)) {
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s mspId must not be null or empty String in role ", key));
}
MSPRole.MSPRoleType mspRoleType;
switch(name) {
case "member":
mspRoleType = MSPRole.MSPRoleType.MEMBER;
break;
case "admin":
mspRoleType = MSPRole.MSPRoleType.ADMIN;
break;
case "client":
mspRoleType = MSPRole.MSPRoleType.CLIENT;
break;
case "peer":
mspRoleType = MSPRole.MSPRoleType.PEER;
break;
default:
throw new ChaincodeEndorsementPolicyParseException(format("In identities with key %s name expected member, admin, client, or peer in role got %s ", key, name));
}
MSPRole mspRole = MSPRole.newBuilder().setRole(mspRoleType).setMspIdentifier((String) mspId).build();
MSPPrincipal principal = MSPPrincipal.newBuilder().setPrincipalClassification(MSPPrincipal.Classification.ROLE).setPrincipal(mspRole.toByteString()).build();
ret.put((String) key, principal);
}
if (ret.size() == 0) {
throw new ChaincodeEndorsementPolicyParseException("No identities were found in the policy specification");
}
return ret;
}
Aggregations