struct timespec * parcJSONValue_GetTimespec(const PARCJSONValue *jsonTimespec, struct timespec *timespec) { assertNotNull(jsonTimespec, "Parameter jsonTimeval must be a non-null PARCJSON pointer."); PARCJSON *json = parcJSONValue_GetJSON(jsonTimespec); PARCJSONValue *value = parcJSON_GetValueByName(json, "seconds"); timespec->tv_sec = parcJSONValue_GetInteger(value); value = parcJSON_GetValueByName(json, "nanos"); timespec->tv_nsec = (int) parcJSONValue_GetInteger(value); return timespec; }
struct timeval * parcJSONValue_GetTimeval(const PARCJSONValue *jsonTimeval, struct timeval *timeval) { assertNotNull(jsonTimeval, "Parameter jsonTimeval must be a non-null PARCJSON pointer."); PARCJSON *json = parcJSONValue_GetJSON(jsonTimeval); PARCJSONValue *value = parcJSON_GetValueByName(json, "seconds"); timeval->tv_sec = parcJSONValue_GetInteger(value); value = parcJSON_GetValueByName(json, "micros"); timeval->tv_usec = (int) parcJSONValue_GetInteger(value); return timeval; }
CCNxControl * cpi_CreateResponse(CCNxControl *request, PARCJSON *operation) { PARCJSON *requestJson = ccnxControl_GetJson(request); // use the same key as the request uint64_t seqnum = controlPlaneInterface_GetSequenceNumber(requestJson); PARCJSONValue *value = parcJSON_GetValueByName(requestJson, cpiRequest); assertNotNull(value, "Could not get request or response"); assertTrue(parcJSONValue_IsJSON(value), "cpiRequest should be a JSON object"); PARCJSON *operationJson = parcJSONValue_GetJSON(value); PARCJSONPair *pair = parcJSON_GetPairByIndex(operationJson, 1); const PARCBuffer *opKeyBuf = parcJSONPair_GetName(pair); const char *opKey = parcBuffer_ToString(opKeyBuf); PARCJSON *response = parcJSON_Create(); parcJSON_AddInteger(response, cpiSeqnum, (int) seqnum); parcJSON_AddObject(response, opKey, operation); parcMemory_Deallocate(&opKey); PARCJSON *responseJson = parcJSON_Create(); parcJSON_AddObject(responseJson, cpiResponse, response); parcJSON_Release(&response); CCNxControl *result = ccnxControl_CreateCPIRequest(responseJson); parcJSON_Release(&responseJson); return result; }
static int _athenactl_ListFIB(PARCIdentity *identity, int argc, char **argv) { CCNxName *name = ccnxName_CreateFromURI(CCNxNameAthenaCommand_FIBList); CCNxInterest *interest = ccnxInterest_CreateSimple(name); ccnxName_Release(&name); const char *result = _athenactl_SendInterestControl(identity, interest); if (result) { PARCJSON *jsonContent = parcJSON_ParseString(result); if (jsonContent != NULL) { PARCJSONValue *resultValue = parcJSON_GetValueByName(jsonContent, JSON_KEY_RESULT); PARCJSONArray *fibEntryList = parcJSONValue_GetArray(resultValue); size_t fibEntryListLength = parcJSONArray_GetLength(fibEntryList); printf("Routes (%d):\n", (int) fibEntryListLength); if (fibEntryListLength == 0) { printf(" No Entries\n"); } for (size_t i = 0; i < fibEntryListLength; ++i) { PARCJSONValue *elementValue = parcJSONArray_GetValue(fibEntryList, i); PARCJSON *valueObj = parcJSONValue_GetJSON(elementValue); PARCJSONValue *value = parcJSON_GetValueByName(valueObj, JSON_KEY_NAME); char *prefixString = parcBuffer_ToString(parcJSONValue_GetString(value)); value = parcJSON_GetValueByName(valueObj, JSON_KEY_LINK); char *linkString = parcBuffer_ToString(parcJSONValue_GetString(value)); printf(" %s -> %s\n", prefixString, linkString); parcMemory_Deallocate(&prefixString); parcMemory_Deallocate(&linkString); } parcJSON_Release(&jsonContent); } else { printf("Returned value is not JSON: %s\n", result); } parcMemory_Deallocate(&result); } else { printf("NULL result recieved from route List request\n"); } ccnxMetaMessage_Release(&interest); return 0; }
uint64_t controlPlaneInterface_GetSequenceNumber(const PARCJSON *controlPlaneMessage) { assertNotNull(controlPlaneMessage, "Invalid state, got NULL json from control message"); PARCJSONValue *value = parcJSON_GetValueByName(controlPlaneMessage, cpiRequest); if (value == NULL) { value = parcJSON_GetValueByName(controlPlaneMessage, cpiResponse); } if (value == NULL) { value = parcJSON_GetValueByName(controlPlaneMessage, cpiAck); } assertNotNull(value, "Could not get request or response"); PARCJSON *json = parcJSONValue_GetJSON(value); value = parcJSON_GetValueByName(json, cpiSeqnum); assertNotNull(value, "Could not retrieve key %s from CPI section", cpiSeqnum); return parcJSONValue_GetInteger(value); }
/** * Returns the inner operation JSON from the request. * * INPUT: "{ CPI_REQUEST: { SEQUENCE:number key: { operation } }}" * OUTPUT: "{ key : { operation } }" * * Example return: "{ operation }" * @param <#param1#> * @return The inner json, do not destroy it * * Example: * @code * <#example#> * @endcode */ PARCJSONPair * cpi_ParseRequest(PARCJSON *request) { PARCJSONValue *value = parcJSON_GetValueByName(request, cpiRequest); assertNotNull(value, "Could not find JSON key %s in %s", cpiRequest, parcJSON_ToString(request)); assertTrue(parcJSONValue_IsJSON(value), "cpiRequest is unexpected type"); PARCJSON *requestJson = parcJSONValue_GetJSON(value); PARCJSONPair *result = parcJSON_GetPairByIndex(requestJson, 1); return result; }
PARCJSON * ccnxControlFacade_GetJson(const CCNxTlvDictionary *controlDictionary) { ccnxControlFacade_AssertValid(controlDictionary); PARCJSON *controlJSON = ccnxTlvDictionary_GetJson(controlDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOAD); if (ccnxControlFacade_IsNotification(controlDictionary)) { PARCJSONValue *wrappedJSON = parcJSON_GetValueByName(controlJSON, _NotificationPayload); controlJSON = parcJSONValue_GetJSON(wrappedJSON); } return controlJSON; }
bool ccnxControlFacade_IsNotification(const CCNxTlvDictionary *controlDictionary) { bool result = false; ccnxControlFacade_AssertValid(controlDictionary); PARCJSON *controlJSON = ccnxTlvDictionary_GetJson(controlDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOAD); if (controlJSON != NULL && (parcJSON_GetValueByName(controlJSON, _NotificationIndicator) != NULL)) { result = true; } return result; }
CpiMessageType controlPlaneInterface_GetCPIMessageType(PARCJSON *json) { assertNotNull(json, "Invalid state, got NULL json from control message"); PARCJSONValue *value = parcJSON_GetValueByName(json, cpiResponse); if (value != NULL) { return CPI_RESPONSE; } value = parcJSON_GetValueByName(json, cpiRequest); if (value != NULL) { return CPI_REQUEST; } value = parcJSON_GetValueByName(json, cpiAck); if (value != NULL) { return CPI_ACK; } trapIllegalValue(json, "Expected CpiMessageType, actual %s", parcJSON_ToString(json)); }
/** * If successful, return true and fill in the parameters in the output argument */ bool publicKeySignerPkcs12Store_GetConnectionParams(PARCJSON *connectionJson, struct publickeysigner_params *output) { assertNotNull(connectionJson, "Parameter connectionJson must be non-null"); assertNotNull(output, "Parameter output must be non-null"); PARCJSONValue *value = parcJSON_GetValueByName(connectionJson, publicKeySignerPkcs12Store_GetName()); assertNotNull(value, "JSON key %s missing", publicKeySignerPkcs12Store_GetName()); PARCJSON *keystoreJson = parcJSONValue_GetJSON(value); value = parcJSON_GetValueByName(keystoreJson, param_KEYSTORE_NAME); assertNotNull(value, "JSON key %s missing inside %s", param_KEYSTORE_NAME, publicKeySignerPkcs12Store_GetName()); PARCBuffer *sBuf = parcJSONValue_GetString(value); output->filename = parcBuffer_Overlay(sBuf, 0); value = parcJSON_GetValueByName(keystoreJson, param_KEYSTORE_PASSWD); assertNotNull(value, "JSON key %s missing inside %s", param_KEYSTORE_PASSWD, publicKeySignerPkcs12Store_GetName()); sBuf = parcJSONValue_GetString(value); output->password = parcBuffer_Overlay(sBuf, 0); return true; }
LONGBOW_TEST_CASE(JSON, parcJSON_GetValueByName) { TestData *data = longBowTestCase_GetClipBoardData(testCase); int expected = 31415; const PARCJSONValue *value = parcJSON_GetValueByName(data->json, "integer"); int64_t actual = parcJSONValue_GetInteger(value); PARCBuffer *expectedName = parcBuffer_WrapCString("integer"); assertTrue(expected == actual, "Expected %d, actual %" PRIi64 "", expected, actual); parcBuffer_Release(&expectedName); }
CPIConnectionList * cpiConnectionList_FromJson(PARCJSON *json) { assertNotNull(json, "Parameter must be non-null"); PARCJSONValue *value = parcJSON_GetValueByName(json, cpi_ConnectionList); assertNotNull(value, "JSON key not found %s: %s", cpi_ConnectionList, parcJSON_ToString(json)); PARCJSONArray *tunnelListJson = parcJSONValue_GetArray(value); CPIConnectionList *list = cpiConnectionList_Create(); size_t length = parcJSONArray_GetLength(tunnelListJson); for (size_t i = 0; i < length; i++) { value = parcJSONArray_GetValue(tunnelListJson, i); PARCJSON *tunnelJson = parcJSONValue_GetJSON(value); CPIConnection *tunnel = cpiConnection_CreateFromJson(tunnelJson); cpiConnectionList_Append(list, tunnel); } return list; }
CPIInterfaceSet * cpiInterfaceSet_FromJson(PARCJSON *json) { assertNotNull(json, "Parameter must be non-null"); PARCJSONValue *value = parcJSON_GetValueByName(json, cpi_InterfaceList); assertNotNull(value, "JSON key not found %s: %s", cpi_InterfaceList, parcJSON_ToString(json)); PARCJSONArray *ifaceSetJson = parcJSONValue_GetArray(value); CPIInterfaceSet *set = cpiInterfaceSet_Create(); size_t length = parcJSONArray_GetLength(ifaceSetJson); for (size_t i = 0; i < length; i++) { value = parcJSONArray_GetValue(ifaceSetJson, i); PARCJSON *ifaceJson = parcJSONValue_GetJSON(value); CPIInterface *iface = cpiInterface_FromJson(ifaceJson); cpiInterfaceSet_Add(set, iface); } return set; }
CCNxManifestHashGroup * ccnxManifestHashGroup_CreateFromJson(const PARCJSON *json) { CCNxManifestHashGroup *group = ccnxManifestHashGroup_Create(); PARCJSONValue *ptrListValue = parcJSON_GetValueByName(json, "HashGroup"); PARCJSONArray *ptrList = parcJSONValue_GetArray(ptrListValue); size_t numberOfPointers = parcJSONArray_GetLength(ptrList); for (size_t i = 0; i < numberOfPointers; i++) { PARCJSONValue *pointerValue = parcJSONArray_GetValue(ptrList, i); PARCJSON *typeJson = parcJSONValue_GetJSON(pointerValue); PARCJSONValue *typeValue = parcJSON_GetValueByName(typeJson, "type"); CCNxManifestHashGroupPointerType type; if (parcJSONValue_GetInteger(typeValue) == 0) { type = CCNxManifestHashGroupPointerType_Data; } else { type = CCNxManifestHashGroupPointerType_Manifest; } PARCJSON *digestJson = parcJSONValue_GetJSON(pointerValue); PARCJSONValue *digestValue = parcJSON_GetValueByName(digestJson, "digest"); PARCBuffer *digestHex = parcJSONValue_GetString(digestValue); char *hexString = parcBuffer_ToString(digestHex); PARCBuffer *digest = parcBuffer_Flip(parcBuffer_ParseHexString(hexString)); parcMemory_Deallocate(&hexString); ccnxManifestHashGroup_AppendPointer(group, type, digest); parcBuffer_Release(&digest); } if (parcJSON_GetPairByName(json, "overallDataDigest") != NULL) { PARCJSONValue *overallDataDigestValue = parcJSON_GetValueByName(json, "overallDataDigest"); PARCBuffer *digestHex = parcJSONValue_GetString(overallDataDigestValue); char *hexString = parcBuffer_ToString(digestHex); group->overallDataDigest = parcBuffer_Flip(parcBuffer_ParseHexString(hexString)); parcMemory_Deallocate(&hexString); } if (parcJSON_GetPairByName(json, "locator") != NULL) { PARCJSONValue *locatorValue = parcJSON_GetValueByName(json, "locator"); PARCBuffer *buffer = parcJSONValue_GetString(locatorValue); char *locator = parcBuffer_ToString(buffer); group->locator = ccnxName_CreateFromCString(locator); parcMemory_Deallocate(&locator); } if (parcJSON_GetPairByName(json, "entrySize") != NULL) { PARCJSONValue *childBlockNodeSizeValue = parcJSON_GetValueByName(json, "entrySize"); group->entrySize = parcJSONValue_GetInteger(childBlockNodeSizeValue); } if (parcJSON_GetPairByName(json, "dataSize") != NULL) { PARCJSONValue *totalSizeValue = parcJSON_GetValueByName(json, "dataSize"); group->dataSize = parcJSONValue_GetInteger(totalSizeValue); } if (parcJSON_GetPairByName(json, "blockSize") != NULL) { PARCJSONValue *blockSizeValue = parcJSON_GetValueByName(json, "blockSize"); group->blockSize = parcJSONValue_GetInteger(blockSizeValue); } if (parcJSON_GetPairByName(json, "treeHeight") != NULL) { PARCJSONValue *treeHeightValue = parcJSON_GetValueByName(json, "treeHeight"); group->treeHeight = parcJSONValue_GetInteger(treeHeightValue); } return group; }
static int _athenactl_ListLinks(PARCIdentity *identity, int argc, char **argv) { CCNxName *name = ccnxName_CreateFromURI(CCNxNameAthenaCommand_LinkList); CCNxInterest *interest = ccnxInterest_CreateSimple(name); ccnxName_Release(&name); const char *result = _athenactl_SendInterestControl(identity, interest); printf("Link: Interface list"); if (result) { PARCBuffer *buffer = parcBuffer_WrapCString((char *) result); PARCJSONParser *parser = parcJSONParser_Create(buffer); PARCJSONValue *value = parcJSONValue_Parser(parser); if (value == NULL) { printf("\n\tCould not parse forwarder list response"); } else { PARCJSONArray *array = parcJSONValue_GetArray(value); for (int i = 0; i < parcJSONArray_GetLength(array); i++) { PARCJSONValue *getvalue = parcJSONArray_GetValue(array, i); PARCJSON *json = parcJSONValue_GetJSON(getvalue); PARCJSONValue *pairValue = parcJSON_GetValueByName(json, "linkName"); PARCBuffer *bufferString = parcJSONValue_GetString(pairValue); char *linkName = parcBuffer_ToString(bufferString); pairValue = parcJSON_GetValueByName(json, "index"); int64_t index = parcJSONValue_GetInteger(pairValue); pairValue = parcJSON_GetValueByName(json, "notLocal"); bool notLocal = parcJSONValue_GetBoolean(pairValue); pairValue = parcJSON_GetValueByName(json, "localForced"); bool localForced = parcJSONValue_GetBoolean(pairValue); if (index < 0) { if (notLocal) { printf("\n Link listener%s: %s", localForced ? " (forced remote)" : "", linkName); } else { printf("\n Link listener%s: %s", localForced ? " (forced local)" : "", linkName); } } else { if (notLocal) { printf("\n Link instance [%" PRId64 "] %s: %s", index, localForced ? "(forced remote)" : "(remote)", linkName); } else { printf("\n Link instance [%" PRId64 "] %s: %s", index, localForced ? "(forced local)" : "(local)", linkName); } } parcMemory_Deallocate(&linkName); } parcJSONValue_Release(&value); } parcJSONParser_Release(&parser); parcBuffer_Release(&buffer); parcMemory_Deallocate(&result); } printf("\nDone.\n"); ccnxMetaMessage_Release(&interest); return 0; }
CpiOperation cpi_getCPIOperation2(const PARCJSON *json) { PARCJSONValue *cpi_value = parcJSON_GetValueByName(json, cpiRequest); if (cpi_value == NULL) { cpi_value = parcJSON_GetValueByName(json, cpiResponse); } assertNotNull(cpi_value, "Could not get Request or response"); PARCJSON *cpi_json = parcJSONValue_GetJSON(cpi_value); /* * The JSON is defined as { REQUEST : { SEQUENCE: xxx, <OPERATION>: xxx } } * so we want to get the key of the 2nd item (index 1) of the array of objects * under the request */ PARCJSONPair *item1Pair = parcJSON_GetPairByIndex(cpi_json, 1); PARCBuffer *name = parcJSONPair_GetName(item1Pair); const char *p = parcBuffer_Overlay(name, 0); if (strncasecmp(p, cpiForwarding_AddRouteJsonTag(), strlen(cpiForwarding_AddRouteJsonTag())) == 0) { return CPI_REGISTER_PREFIX; } if (strncasecmp(p, cpiForwarding_RemoveRouteJsonTag(), strlen(cpiForwarding_RemoveRouteJsonTag())) == 0) { return CPI_UNREGISTER_PREFIX; } if (strncasecmp(p, cpiPause, strlen(cpiPause)) == 0) { return CPI_PAUSE; } if (strncasecmp(p, cpiFlush, strlen(cpiFlush)) == 0) { return CPI_FLUSH; } if (strncasecmp(p, cpiCancelFlow_CancelFlowJsonTag(), strlen(cpiCancelFlow_CancelFlowJsonTag())) == 0) { return CPI_CANCEL_FLOW; } if (strncasecmp(p, cpiLinks_InterfaceListJsonTag(), strlen(cpiLinks_InterfaceListJsonTag())) == 0) { return CPI_INTERFACE_LIST; } if (strncasecmp(p, cpiForwarding_RouteListJsonTag(), strlen(cpiForwarding_RouteListJsonTag())) == 0) { return CPI_PREFIX_REGISTRATION_LIST; } if (strncasecmp(p, cpiLinks_CreateTunnelJsonTag(), strlen(cpiLinks_CreateTunnelJsonTag())) == 0) { return CPI_CREATE_TUNNEL; } if (strncasecmp(p, cpiLinks_RemoveTunnelJsonTag(), strlen(cpiLinks_RemoveTunnelJsonTag())) == 0) { return CPI_REMOVE_TUNNEL; } if (strncasecmp(p, cpiLinks_ConnectionListJsonTag(), strlen(cpiLinks_ConnectionListJsonTag())) == 0) { return CPI_CONNECTION_LIST; } if (strncasecmp(p, cpiLinks_AddEtherConnectionJasonTag(), strlen(cpiLinks_AddEtherConnectionJasonTag())) == 0) { return (CPI_ADD_ETHER_CONNECTION); } // Refactor, case 1026 if (strncasecmp(p, "AddConnEther", strlen("AddConnEther")) == 0) { return CPI_ADD_CONNECTION_ETHERNET; } // Refactor, case 1026 if (strncasecmp(p, "RemoveConnEther", strlen("RemoveConnEther")) == 0) { return CPI_REMOVE_CONNECTION_ETHERNET; } // Refactor, case 1026 if (strncasecmp(p, "AddListener", strlen("AddListener")) == 0) { return CPI_ADD_LISTENER; } // Refactor, case 1026 if (strncasecmp(p, "RemoveListener", strlen("RemoveListener")) == 0) { return CPI_REMOVE_LISTENER; } trapIllegalValue(json, "Could not parse: %s\n", parcJSON_ToString(json)); }