Search in sources :

Example 1 with NameRecord

use of edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord in project GNS by MobilityFirst.

the class ActiveCodeHandler method handleActiveCode.

/**
   * This interface is used for the class out of activecode package to trigger active code.
   * It requires the parameters for running active code such as guid, field, and value.
   * It runs the requests and returns the processed result to the caller.
   *
   *
   * @param header header is needed for depth query
   * @param guid
   * @param field
   * @param action the actions in {@code ActiveCode}
   * @param value
   * @param db db is needed for fetching active code to run
   * @return the processed result as an JSONObject, the original value is returned if there is an error with code execution
   * @throws InternalRequestException
   */
public static JSONObject handleActiveCode(InternalRequestHeader header, String guid, String field, String action, JSONObject value, BasicRecordMap db) throws InternalRequestException {
    if (Config.getGlobalBoolean(GNSConfig.GNSC.DISABLE_ACTIVE_CODE)) {
        return value;
    }
    if (header != null) {
        // this is a depth query, and we do not call the code again, as it will form a infinite loop if not.
        if (guid.equals(header.getOriginatingGUID()) && header.getTTL() < InternalRequestHeader.DEFAULT_TTL) {
            return value;
        }
    } else {
        // without a header, the code can misbehave without any regulation, therefore we return the value immediately if no header presents
        return value;
    }
    long t = System.nanoTime();
    ActiveCodeHandler.getLogger().log(DEBUG_LEVEL, "OOOOOOOOOOOOO handles:[guid:{0},field:{1},action:{2},value:{3},header:{4}]", new Object[] { guid, field, action, value, header });
    /**
     * Only execute active code for user field
     * FIXME:
     * <p>
     * Read can be a single-field read or multi-field read.
     * If it's a single-field read, then the field can not be a internal field.
     * If it's a multi-field read, then there may be some field is internal.
     * <p>
     * Write has no field value, but if there should not be an internal
     * field in the JSONObject value.
     */
    if ((action.equals(ActiveCode.READ_ACTION) && field != null && InternalField.isInternalField(field)) || (action.equals(ActiveCode.WRITE_ACTION) && ((value != null && containInternalField(value)) || (field != null && InternalField.isInternalField(field))))) {
        ActiveCodeHandler.getLogger().log(DEBUG_LEVEL, "OOOOOOOOOOOOO no need to handle:[guid:{0},field:{1},action:{2},value:{3},header:{4}]", new Object[] { guid, field, action, value, header });
        return value;
    }
    JSONObject newResult = value;
    if (field == null || !InternalField.isInternalField(field)) {
        //FIXME: Seems like this field lookup all could be replaced by something 
        // like NSFieldAccess.lookupJSONFieldLocalNoAuth
        NameRecord activeCodeNameRecord = null;
        try {
            activeCodeNameRecord = NameRecord.getNameRecordMultiUserFields(db, guid, ColumnFieldType.USER_JSON, ActiveCode.getCodeField(action));
        } catch (RecordNotFoundException | FailedDBOperationException | IllegalArgumentException e) {
            e.printStackTrace();
            return value;
        }
        ValuesMap codeMap = null;
        try {
            codeMap = activeCodeNameRecord.getValuesMap();
        } catch (FieldNotFoundException e) {
            e.printStackTrace();
            return value;
        }
        if (codeMap != null && value != null) {
            String code;
            try {
                code = codeMap.getString(ActiveCode.getCodeField(action));
            } catch (JSONException | IllegalArgumentException e) {
                return value;
            }
            // Prepare values for query
            String accessorGuid = header == null ? guid : header.getOriginatingGUID();
            if (header.getSourceAddress() != null) {
                try {
                    value.put(SOURCE_IP_FIELD, header.getSourceAddress());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            // Run code
            newResult = runCode(header, code, guid, accessorGuid, action, value, header.getTTL());
            // Strip the appended fields
            if (newResult.has(SOURCE_IP_FIELD)) {
                newResult.remove(SOURCE_IP_FIELD);
            }
        } else if (codeMap == null) {
            ActiveCodeHandler.getLogger().log(DEBUG_LEVEL, "OOOOOOOOOOOOO no code to run:[guid:{0},field:{1},action:{2},value:{3},header:{4}]", new Object[] { guid, field, action, value, header });
        }
    }
    ActiveCodeHandler.getLogger().log(DEBUG_LEVEL, "OOOOOOOOOOOOO The result after executing active code is {0}", new Object[] { newResult });
    DelayProfiler.updateDelayNano("activeTotal", t);
    return newResult;
}
Also used : ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException) JSONException(org.json.JSONException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject)

Example 2 with NameRecord

use of edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord in project GNS by MobilityFirst.

the class NSFieldAccess method lookupFieldLocalNoAuth.

private static ValuesMap lookupFieldLocalNoAuth(String guid, String field, ColumnFieldType returnFormat, BasicRecordMap database) throws FailedDBOperationException {
    NameRecord nameRecord = null;
    ClientSupportConfig.getLogger().log(Level.FINE, "XXXXXXXXXXXXXXXXXXXXX LOOKUP_FIELD_LOCALLY: {0} : {1}", new Object[] { guid, field });
    // Try to look up the value in the database
    try {
        // Check for the case where we're returning all the fields the entire record.
        if (GNSProtocol.ENTIRE_RECORD.toString().equals(field)) {
            ClientSupportConfig.getLogger().log(Level.FINE, "ALL FIELDS: Format={0}", new Object[] { returnFormat });
            // need everything so just grab all the fields
            nameRecord = NameRecord.getNameRecord(database, guid);
        // Otherwise if field is specified we're just looking up that single field.
        } else if (field != null) {
            ClientSupportConfig.getLogger().log(Level.FINE, "Field={0} Format={1}", new Object[] { field, returnFormat });
            long t = System.nanoTime();
            // otherwise grab the field the user wanted
            nameRecord = NameRecord.getNameRecordMultiUserFields(database, guid, returnFormat, field);
            if (Util.oneIn(100)) {
                DelayProfiler.updateDelayNano("getNameRecordMultiUserFields", t);
            }
        }
        if (nameRecord != null) {
            ClientSupportConfig.getLogger().log(Level.FINE, "VALUES MAP={0}", new Object[] { nameRecord.getValuesMap().toString() });
            return nameRecord.getValuesMap();
        }
    } catch (RecordNotFoundException e) {
        ClientSupportConfig.getLogger().log(Level.FINE, "Record not found for name: {0} Key = {1}", new Object[] { guid, field });
    } catch (FieldNotFoundException e) {
        ClientSupportConfig.getLogger().log(Level.FINE, "Field not found {0} : {1}", new Object[] { guid, e });
    }
    return null;
}
Also used : RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException) JSONObject(org.json.JSONObject)

Example 3 with NameRecord

use of edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord in project GNS by MobilityFirst.

the class NSFieldAccess method lookupFieldsLocalNoAuth.

/**
   *
   * @param header
   * @param guid
   * @param fields
   * @param returnFormat
   * @param handler
   * @return a values map
   * @throws FailedDBOperationException
   */
public static ValuesMap lookupFieldsLocalNoAuth(InternalRequestHeader header, String guid, List<String> fields, ColumnFieldType returnFormat, ClientRequestHandlerInterface handler) throws FailedDBOperationException {
    // Try to look up the value in the database
    try {
        ClientSupportConfig.getLogger().log(Level.FINE, "Fields={0} Format={1}", new Object[] { fields, returnFormat });
        String[] fieldArray = new String[fields.size()];
        fieldArray = fields.toArray(fieldArray);
        // Grab the fields the user wanted
        NameRecord nameRecord = NameRecord.getNameRecordMultiUserFields(handler.getApp().getDB(), guid, returnFormat, fieldArray);
        if (nameRecord != null) {
            // active code handling
            ValuesMap valuesMap = nameRecord.getValuesMap();
            //if (!Config.getGlobalBoolean(GNSConfig.GNSC.DISABLE_ACTIVE_CODE)) {
            try {
                JSONObject result = ActiveCodeHandler.handleActiveCode(header, guid, null, ActiveCode.READ_ACTION, valuesMap, handler.getApp().getDB());
                valuesMap = result != null ? new ValuesMap(result) : valuesMap;
            } catch (InternalRequestException e) {
                e.printStackTrace();
            }
            //}
            return valuesMap;
        }
    } catch (RecordNotFoundException e) {
        ClientSupportConfig.getLogger().log(Level.FINE, "Record not found for name: {0}", guid);
    } catch (FieldNotFoundException e) {
        ClientSupportConfig.getLogger().log(Level.FINE, "Field not found for {0} : {1}", new Object[] { guid, e });
    }
    return null;
}
Also used : RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException) JSONObject(org.json.JSONObject)

Example 4 with NameRecord

use of edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord in project GNS by MobilityFirst.

the class NSFieldAccess method lookupListFieldLocallyNoAuth.

/**
   * Looks up the value of an old-style list field
   * in the guid in the local replica.
   * Returns the value of a field in a GNSProtocol.GUID.toString() as a ResultValue or
   * an empty ResultValue if field cannot be found.
   *
   * @param guid
   * @param field
   * @param database
   * @return ResultValue
   * @throws edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException
   * @throws edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException
   * @throws edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException
   */
public static ResultValue lookupListFieldLocallyNoAuth(String guid, String field, BasicRecordMap database) throws FailedDBOperationException, FieldNotFoundException, RecordNotFoundException {
    NameRecord nameRecord = NameRecord.getNameRecordMultiUserFields(database, guid, ColumnFieldType.LIST_STRING, field);
    ClientSupportConfig.getLogger().log(Level.FINE, "LOOKUPFIELDONTHISSERVER: {0} : {1} -> {2}", new Object[] { guid, field, nameRecord });
    ResultValue result = nameRecord.getUserKeyAsArray(field);
    if (result != null) {
        return result;
    } else {
        return new ResultValue();
    }
}
Also used : NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) ResultValue(edu.umass.cs.gnsserver.utils.ResultValue)

Example 5 with NameRecord

use of edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord in project GNS by MobilityFirst.

the class Admintercessor method collectTaggedGuids.

/**
   * Sends a command to collect all guids that contain the given tag.
   *
   * @param tagName
   * @param handler
   * @return a set of strings
   */
public Set<String> collectTaggedGuids(String tagName, ClientRequestHandlerInterface handler) {
    int id;
    if ((id = sendDumpOutputHelper(tagName, handler)) == -1) {
        return null;
    }
    waitForDumpResponse(id);
    Map<String, TreeSet<NameRecord>> result = dumpResult.get(id);
    dumpResult.remove(id);
    if (result != null) {
        Set<String> allGuids = new HashSet<>();
        for (TreeSet<NameRecord> records : result.values()) {
            try {
                for (NameRecord record : records) {
                    allGuids.add(record.getName());
                }
            } catch (FieldNotFoundException e) {
            }
        }
        return allGuids;
    } else {
        return null;
    }
}
Also used : NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) TreeSet(java.util.TreeSet) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException) HashSet(java.util.HashSet)

Aggregations

NameRecord (edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord)15 JSONObject (org.json.JSONObject)9 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException)8 JSONException (org.json.JSONException)8 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)7 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)6 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)5 RecordExistsException (edu.umass.cs.gnscommon.exceptions.server.RecordExistsException)3 TreeSet (java.util.TreeSet)3 Test (org.junit.Test)3 DumpRequestPacket (edu.umass.cs.gnsserver.gnsapp.packet.admin.DumpRequestPacket)2 HashSet (java.util.HashSet)2 JSONArray (org.json.JSONArray)2 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)1 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)1 AbstractRecordCursor (edu.umass.cs.gnsserver.database.AbstractRecordCursor)1 GuidInfo (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo)1 AdminRequestPacket (edu.umass.cs.gnsserver.gnsapp.packet.admin.AdminRequestPacket)1 SentinalPacket (edu.umass.cs.gnsserver.gnsapp.packet.admin.SentinalPacket)1 GNSRecordMap (edu.umass.cs.gnsserver.gnsapp.recordmap.GNSRecordMap)1