Search in sources :

Example 1 with ServerRuntimeException

use of edu.umass.cs.gnscommon.exceptions.server.ServerRuntimeException in project GNS by MobilityFirst.

the class AccountAccess method addMultipleGuids.

/**
   * Add multiple guids to an account.
   *
   * @param header
   * @param commandPacket
   *
   * @param names
   * - the list of names
   * @param publicKeys
   * - the list of public keys associated with the names
   * @param accountInfo
   * @param accountGuidInfo
   * @param handler
   * @return a command response
   */
public static CommandResponse addMultipleGuids(InternalRequestHeader header, CommandPacket commandPacket, List<String> names, List<String> publicKeys, AccountInfo accountInfo, GuidInfo accountGuidInfo, ClientRequestHandlerInterface handler) {
    try {
        long startTime = System.currentTimeMillis();
        //Set<String> guids = new HashSet<>();
        Map<String, JSONObject> hrnMap = new HashMap<>();
        Map<String, JSONObject> guidInfoMap = new HashMap<>();
        for (int i = 0; i < names.size(); i++) {
            String name = names.get(i);
            String publicKey = publicKeys.get(i);
            String guid = SharedGuidUtils.createGuidStringFromBase64PublicKey(publicKey);
            accountInfo.addGuid(guid);
            //guids.add(guid);
            // HRN records
            JSONObject jsonHRN = new JSONObject();
            jsonHRN.put(HRN_GUID, guid);
            hrnMap.put(name, jsonHRN);
            // guid info record
            GuidInfo guidInfo = new GuidInfo(name, guid, publicKey);
            JSONObject jsonGuid = new JSONObject();
            jsonGuid.put(GUID_INFO, guidInfo.toJSONObject());
            jsonGuid.put(PRIMARY_GUID, accountInfo.getGuid());
            // set up ACL to look like this
            // "_GNS_ACL": {
            // "READ_WHITELIST": {"+ALL+": {"MD": [<publickey>, "+ALL+"]}},
            // "WRITE_WHITELIST": {"+ALL+": {"MD": [<publickey>]}}
            JSONObject acl = createACL(GNSProtocol.ENTIRE_RECORD.toString(), Arrays.asList(GNSProtocol.EVERYONE.toString(), accountGuidInfo.getPublicKey()), GNSProtocol.ENTIRE_RECORD.toString(), Arrays.asList(accountGuidInfo.getPublicKey()));
            // prefix is the same for all acls so just pick one to use here
            jsonGuid.put(MetaDataTypeName.READ_WHITELIST.getPrefix(), acl);
            guidInfoMap.put(guid, jsonGuid);
        }
        DelayProfiler.updateDelay("addMultipleGuidsSetup", startTime);
        accountInfo.noteUpdate();
        // First we create the HRN records as a batch
        ResponseCode returnCode;
        // First try to create the HRNS to insure that that name does not already exist
        Map<String, String> nameStates = new HashMap<>();
        for (String key : hrnMap.keySet()) {
            nameStates.put(key, hrnMap.get(key).toString());
        }
        if (!(returnCode = handler.getInternalClient().createOrExists(new CreateServiceName(nameStates))).isExceptionOrError()) {
            // now we update the account info
            if (updateAccountInfoNoAuthentication(header, commandPacket, accountInfo, handler, true).isOKResult()) {
                HashMap<String, String> guidInfoNameStates = new HashMap<>();
                for (String key : guidInfoMap.keySet()) {
                    guidInfoNameStates.put(key, guidInfoMap.get(key).toString());
                }
                handler.getInternalClient().createOrExists(new CreateServiceName(guidInfoNameStates));
                GNSConfig.getLogger().info(DelayProfiler.getStats());
                return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
            }
        }
        return new CommandResponse(returnCode, GNSProtocol.BAD_RESPONSE.toString() + " " + returnCode.getProtocolCode() + " " + names);
    } catch (JSONException e) {
        return new CommandResponse(ResponseCode.JSON_PARSE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.JSON_PARSE_ERROR.toString() + " " + e.getMessage());
    } catch (ServerRuntimeException | ClientException e) {
        return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.getMessage());
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) HashMap(java.util.HashMap) JSONException(org.json.JSONException) RandomString(edu.umass.cs.gnscommon.utils.RandomString) CreateServiceName(edu.umass.cs.reconfiguration.reconfigurationpackets.CreateServiceName) JSONObject(org.json.JSONObject) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) ServerRuntimeException(edu.umass.cs.gnscommon.exceptions.server.ServerRuntimeException)

Example 2 with ServerRuntimeException

use of edu.umass.cs.gnscommon.exceptions.server.ServerRuntimeException in project GNS by MobilityFirst.

the class AccountAccess method addGuid.

/**
   * Adds a new GUID associated with an existing account.
   * <p>
   * These records will be created:<br>
   * GUID: "_GNS_PRIMARY_GUID" -- GUID (primary) for secondary guid<br>
   * GUID: "_GNS_GUID_INFO" -- {guid info}<br>
   * HRN: "_GNS_GUID" -- GUID<br>
   *
   * @param header
   * @param commandPacket
   *
   * @param accountInfo
   * - the accountInfo of the account to add the GUID to
   * @param accountGuidInfo
   * @param name
   * = the human readable name to associate with the GUID
   * @param guid
   * - the new GUID
   * @param publicKey
   * - the public key to use with the new account
   * @param handler
   * @return status result
   */
public static CommandResponse addGuid(InternalRequestHeader header, CommandPacket commandPacket, AccountInfo accountInfo, GuidInfo accountGuidInfo, String name, String guid, String publicKey, ClientRequestHandlerInterface handler) {
    /* arun: The commented out code below checking for duplicates is
		 * incorrect. What we need to do is to check for conflicts in HRN-GUID
		 * bindings. If an HRN being created already exists, but the
		 * corresponding GUID does not exist, we should create it. Otherwise,
		 * the caller will interpret the duplicate name exception incorrectly as
		 * a successful creation. 
		 * 
 		 * if ((AccountAccess.lookupGuidAnywhere(name, handler)) != null) {
		 * return new CommandResponse( ResponseCode.DUPLICATE_NAME_EXCEPTION,
		 * GNSProtocol.BAD_RESPONSE.toString() + " " +
		 * GNSProtocol.DUPLICATE_NAME.toString() + " " + name); }
		 * 
		 * if ((AccountAccess.lookupGuidInfoAnywhere(guid, handler)) != null) {
		 * return new CommandResponse( ResponseCode.DUPLICATE_GUID_EXCEPTION,
		 * GNSProtocol.BAD_RESPONSE.toString() + " " +
		 * GNSProtocol.DUPLICATE_GUID.toString() + " " + name); } */
    boolean createdName = false, createdGUID = false;
    try {
        JSONObject jsonHRN = new JSONObject();
        jsonHRN.put(HRN_GUID, guid);
        ResponseCode code;
        code = handler.getInternalClient().createOrExists(new CreateServiceName(name, jsonHRN.toString()));
        /* arun: Return the error if we could not createField the HRN
			 * (alias) record and the error indicates that it is not a duplicate
			 * ID exception because of a limbo create operation from a previous
			 * unsuccessful attempt. */
        String boundGUID = null;
        if (code.equals(ResponseCode.DUPLICATE_ID_EXCEPTION) && !(guid.equals(boundGUID = HRNMatchingGUIDExists(header, handler, code, name, guid)))) {
            return new CommandResponse(ResponseCode.CONFLICTING_GUID_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + ResponseCode.CONFLICTING_GUID_EXCEPTION.getProtocolCode() + " " + name + "(" + guid + ")" + " " + (code.getMessage() != null ? code.getMessage() + " " : "") + "; HRN " + name + " is already bound to GUID " + boundGUID + " != " + guid);
        }
        if (code.isExceptionOrError() && !code.equals(ResponseCode.DUPLICATE_ID_EXCEPTION)) {
            return new CommandResponse(code, GNSProtocol.BAD_RESPONSE.toString() + " " + code.getProtocolCode() + " " + name + "(" + guid + ")" + " " + code.getMessage());
        }
        assert (!code.isExceptionOrError() || guid.equals(boundGUID));
        createdName = true;
        // else name created
        GuidInfo guidInfo = new GuidInfo(name, guid, publicKey);
        JSONObject jsonGuid = new JSONObject();
        jsonGuid.put(GUID_INFO, guidInfo.toJSONObject());
        jsonGuid.put(PRIMARY_GUID, accountInfo.getGuid());
        // set up ACL to look like this
        // "_GNS_ACL": {
        // "READ_WHITELIST": {"+ALL+": {"MD": [<publickey>, "+ALL+"]}},
        // "WRITE_WHITELIST": {"+ALL+": {"MD": [<publickey>]}}
        JSONObject acl = createACL(GNSProtocol.ENTIRE_RECORD.toString(), Arrays.asList(GNSProtocol.EVERYONE.toString(), accountGuidInfo.getPublicKey()), GNSProtocol.ENTIRE_RECORD.toString(), Arrays.asList(accountGuidInfo.getPublicKey()));
        // prefix is the same for all acls so just pick one to use here
        jsonGuid.put(MetaDataTypeName.READ_WHITELIST.getPrefix(), acl);
        // The addGuid needs to be rolled back if the second step fails.
        ResponseCode guidCode;
        guidCode = handler.getInternalClient().createOrExists(new CreateServiceName(guid, jsonGuid.toString()));
        assert (guidCode != null);
        String boundHRN = null;
        if (guidCode.equals(ResponseCode.DUPLICATE_ID_EXCEPTION) && !name.equals(boundHRN = GUIDMatchingHRNExists(header, handler, guidCode, name, // rollback name creation
        guid))) {
            return rollback(handler, ResponseCode.CONFLICTING_GUID_EXCEPTION.setMessage(": Existing GUID " + guid + " is associated with " + boundHRN + " and can not be associated with the HRN " + name), name, guid);
        }
        // redundant to check with GNSClientInternal
        if (guidCode.isExceptionOrError() && !guidCode.equals(ResponseCode.DUPLICATE_ID_EXCEPTION)) {
            return new CommandResponse(guidCode, GNSProtocol.BAD_RESPONSE.toString() + " " + guidCode.getProtocolCode() + " " + guidCode.getMessage());
        }
        // else all good, continue
        assert (!guidCode.isExceptionOrError() || name.equals(boundHRN)) : "code=" + guidCode + "; boundHRN=" + boundHRN + "; name=" + name + "; for GUID=" + guid;
        createdGUID = true;
        // else both name and guid created successfully
        updateAccountInfoNoAuthentication(header, commandPacket, accountInfo.addGuid(guid).noteUpdate(), handler, true);
        return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString() + " " + " [created " + name + " and " + guid + " and updated account info successfully]");
    } catch (ClientException ce) {
        return new CommandResponse(ce.getCode(), GNSProtocol.BAD_RESPONSE.toString() + " " + ce.getCode() + " " + ce.getMessage() + (createdName ? "; created " + name + (createdGUID ? "; created " + guid + "; failed to update account info" : "") : "; created neither " + name + " nor " + guid));
    } catch (JSONException | ServerRuntimeException e) {
        return CommandResponse.toCommandResponse(e);
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) CreateServiceName(edu.umass.cs.reconfiguration.reconfigurationpackets.CreateServiceName) RandomString(edu.umass.cs.gnscommon.utils.RandomString) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) ServerRuntimeException(edu.umass.cs.gnscommon.exceptions.server.ServerRuntimeException)

Aggregations

ResponseCode (edu.umass.cs.gnscommon.ResponseCode)2 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)2 ServerRuntimeException (edu.umass.cs.gnscommon.exceptions.server.ServerRuntimeException)2 RandomString (edu.umass.cs.gnscommon.utils.RandomString)2 CreateServiceName (edu.umass.cs.reconfiguration.reconfigurationpackets.CreateServiceName)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 HashMap (java.util.HashMap)1