public class RestUtil {
-
-
+
+
private static final String RELATIONSHIP_KEY = "relationship";
private static final String RELATIONSHIP_LIST_KEY = "relationship-list";
// Parameters for Query AAI Model Data API
private static final Resource GENERIC_VNF = new Resource("generic-vnf");
private static final Resource VF_MODULE = new Resource("vf-module");
private static final Resource L3_NETWORK = new Resource("l3-network");
+ private static final Resource PNF = new Resource("pnf");
public static class Resource {
private String resourceName;
private static final String EMPTY_JSON_STRING = "{}";
- private static final String DEPTH = "?depth=2";
private static Logger logger = LoggerFactory.getLogger(RestUtil.class);
private RestUtil() {
* @return
* @throws DiscoveryException
*/
- public static JSONObject retrieveAAIModelData(RestClient aaiClient, String baseURL, String aaiBasicAuthorization, String aaiServiceInstancePath, String aaiResourceList,
+ public static JSONObject retrieveAAIModelData(RestClient aaiClient, String baseURL, String depth, String aaiBasicAuthorization, String aaiServiceInstancePath, String aaiResourceList,
String transactionId, String serviceInstanceId) throws DiscoveryException {
// Follow two variables for transform purpose
String url = baseURL + generateServiceInstanceURL(aaiServiceInstancePath, serviceInstanceId);
// Response from service instance API call
JSONObject serviceInstancePayload = new JSONObject(
- getResource(aaiClient, url, aaiBasicAuthorization, transactionId));
+ getResource(aaiClient, url, depth, aaiBasicAuthorization, transactionId));
// Handle the case if the service instance is not found in AAI
if (serviceInstancePayload.length() == 0) {
logger.info("Service Instance {} is not found from AAI", serviceInstanceId);
logger.info("The number of the relationship types for service instance id {} is: {}", serviceInstanceId,
relationMap.size());
- JSONObject response = processVNFRelationMap(aaiClient, aaiResourceList, baseURL, aaiBasicAuthorization, transactionId, relationMap, serviceInstancePayload);
-
+ JSONObject response = processVNFRelationMap(aaiClient, aaiResourceList, baseURL, depth, aaiBasicAuthorization, transactionId, relationMap, serviceInstancePayload);
+
if (relationMap.containsKey(L3_NETWORK.getResourceName())) {
- List<String> l3NetworkRelatedLinks = relationMap.get(L3_NETWORK.getResourceName());
- List<JSONObject> l3networkPayload = processResourceList(aaiClient, baseURL, aaiBasicAuthorization, transactionId,
+ List<String> l3NetworkRelatedLinks = relationMap.get(L3_NETWORK.getResourceName());
+ List<JSONObject> l3networkPayload = processResourceList(aaiClient, baseURL, depth, aaiBasicAuthorization, transactionId,
L3_NETWORK.getResourceName(), l3NetworkRelatedLinks);
-
+
response.put(L3_NETWORK.getCollectionName(), l3networkPayload);
}
+ if (relationMap.containsKey(PNF.getResourceName())) {
+ List<String> l3NetworkRelatedLinks = relationMap.get(PNF.getResourceName());
+ List<JSONObject> l3networkPayload = processResourceList(aaiClient, baseURL, depth, aaiBasicAuthorization, transactionId,
+ PNF.getResourceName(), l3NetworkRelatedLinks);
+
+ response.put(PNF.getCollectionName(), l3networkPayload);
+ }
+
+
return response;
}
* @param relationMap
* @throws DiscoveryException
*/
- private static JSONObject processVNFRelationMap(RestClient aaiClient, String aaiResourceList, String baseURL, String aaiBasicAuthorization, String transactionId,
+ private static JSONObject processVNFRelationMap(RestClient aaiClient, String aaiResourceList, String baseURL, String depth, String aaiBasicAuthorization, String transactionId,
Map<String, List<String>> relationMap, JSONObject serviceInstancePayload) throws DiscoveryException {
List<JSONObject> vnfLst = new ArrayList<>(); // List of the VNF JSON along with related resources
List<Resource> resourceTypes = getResourceTypes(aaiResourceList);
if (relationMap.get(GENERIC_VNF.getResourceName()) != null) {
- List<JSONObject> vnfList = processResourceList(aaiClient, baseURL, aaiBasicAuthorization, transactionId,
+ List<JSONObject> vnfList = processResourceList(aaiClient, baseURL, depth, aaiBasicAuthorization, transactionId,
GENERIC_VNF.getResourceName(), relationMap.get(GENERIC_VNF.getResourceName()));
// Logic to Create the Generic VNF JSON and extract further relationships
for (JSONObject vnfPayload : vnfList) {
logger.info("Number of relationships found for generic-vnf '{}', resource type '{}' are: {}", vnfId,
resourceType.getResourceName(), vnfcLinkLst.size());
- List<JSONObject> vnfcList = processResourceList(aaiClient, baseURL, aaiBasicAuthorization,
+ List<JSONObject> vnfcList = processResourceList(aaiClient, baseURL, depth, aaiBasicAuthorization,
transactionId, resourceType.getResourceName(), vnfcLinkLst);
vnfPayload.put(resourceType.getCollectionName(), vnfcList);
}
// Process vf-module looking for l3-network:
- processVfModuleList(aaiClient, baseURL, aaiBasicAuthorization, transactionId, vnfPayload);
+ processVfModuleList(aaiClient, baseURL, depth, aaiBasicAuthorization, transactionId, vnfPayload);
// Add final vnf payload to list
vnfLst.add(vnfPayload);
}
}
- private static void processVfModuleList(RestClient aaiClient, String baseURL, String aaiBasicAuthorization, String transactionId,
+ private static void processVfModuleList(RestClient aaiClient, String baseURL, String depth, String aaiBasicAuthorization, String transactionId,
JSONObject vnfPayload) throws DiscoveryException {
if (!vnfPayload.has(VF_MODULE.getCollectionName())) {
if (!vfmoduleCollection.has(VF_MODULE.getResourceName())) {
return;
}
-
+
JSONArray vfModuleList = vfmoduleCollection.getJSONArray(VF_MODULE.getResourceName());
for (int i = 0; i < vfModuleList.length(); i++) {
logger.error("VF Module not found for vnf-id {}", vnfPayload.opt("vnf-id"));
continue;
}
- processVfModule(aaiClient, baseURL, aaiBasicAuthorization, transactionId, vfModulePayload);
- }
+ processVfModule(aaiClient, baseURL, depth, aaiBasicAuthorization, transactionId, vfModulePayload);
+ }
}
- private static void processVfModule(RestClient aaiClient, String baseURL, String aaiBasicAuthorization,
+ private static void processVfModule(RestClient aaiClient, String baseURL, String depth, String aaiBasicAuthorization,
String transactionId, JSONObject vfModulePayload) throws DiscoveryException {
Map<String, List<String>> relationMap = extractRelationships(vfModulePayload);
logger.info("Number of relationships found for vf-module '{}', resource type '{}' are: {}", vfModuleId, L3_NETWORK.getResourceName(), l3NetworkRelatedLinks.size());
- List<JSONObject> l3NetworkObjects = processResourceList(aaiClient, baseURL, aaiBasicAuthorization,
+ List<JSONObject> l3NetworkObjects = processResourceList(aaiClient, baseURL, depth, aaiBasicAuthorization,
transactionId, L3_NETWORK.getResourceName(), l3NetworkRelatedLinks);
// Add l3-network with related resource payload to the vfModulePayload:
* @return
* @throws DiscoveryException
*/
- private static List<JSONObject> processResourceList(RestClient aaiClient, String aaiBaseURL, String aaiBasicAuthorization, String transactionId,
+ private static List<JSONObject> processResourceList(RestClient aaiClient, String aaiBaseURL, String depth, String aaiBasicAuthorization, String transactionId,
String resourceType, List<String> resourceList) throws DiscoveryException {
List<JSONObject> resourcePayloadList = new ArrayList<>();
for (String resourceLink : resourceList) {
String resourceURL = aaiBaseURL + resourceLink;
- // With latest AAI development, in order to retrieve the both generic VNF + vf_module, we can use
- // one API call but with depth=2
- if (resourceType.equals(GENERIC_VNF.getResourceName())) {
- resourceURL += DEPTH;
- }
// Response from generic VNF API call
JSONObject resourcePayload = new JSONObject(
- getResource(aaiClient, resourceURL, aaiBasicAuthorization, transactionId));
+ getResource(aaiClient, resourceURL, depth, aaiBasicAuthorization, transactionId));
if (resourcePayload.length() == 0) {
logger.info("Resource with url {} is not found from AAI", resourceLink);
} else {
* @return
* @throws DiscoveryException
*/
- private static String getResource(RestClient client, String url, String aaiBasicAuthorization, String transId)
+ private static String getResource(RestClient client, String url, String depth, String aaiBasicAuthorization, String transId)
throws DiscoveryException {
- OperationResult result = client.get(url, buildHeaders(aaiBasicAuthorization, transId), MediaType.valueOf(MediaType.APPLICATION_JSON));
+ OperationResult result = client.get((url + ((null != depth) ? ("?depth=" +depth) : "")), buildHeaders(aaiBasicAuthorization, transId), MediaType.valueOf(MediaType.APPLICATION_JSON));
if (result.wasSuccessful()) {
return result.getResult();
public void testDemoFirewallService() throws Exception {
// setup A&AI responses
addResponse(
- "/aai/v13/nodes/service-instance/c6456519-6acf-4adb-997c-3c363dd4caaf",
+ "/aai/v13/nodes/service-instance/c6456519-6acf-4adb-997c-3c363dd4caaf?depth=2",
"junit/aai-service-instance.json");
addResponse(
"/aai/v13/network/generic-vnfs/generic-vnf/6700c313-fbb7-4cf9-ac70-0293ec56df68?depth=2",
"junit/aai-generic-vnf2.json");
addResponse(
"/aai/v13/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/RegionOne/tenants/tenant"
- + "/b49b830686654191bb1e952a74b014ad/vservers/vserver/b494cd6e-b9f3-45e0-afe7-e1d1a5f5d74a",
+ + "/b49b830686654191bb1e952a74b014ad/vservers/vserver/b494cd6e-b9f3-45e0-afe7-e1d1a5f5d74a?depth=2",
"junit/aai-vserver.json");
addResponse(
- "/aai/v13/network/l3-networks/l3-network/HNP1d77c-1094-41ec-b7f3-94bb30951870",
+ "/aai/v13/network/l3-networks/l3-network/HNP1d77c-1094-41ec-b7f3-94bb30951870?depth=2",
"junit/aai-l3-network1.json");
addResponse(
- "/aai/v13/network/l3-networks/l3-network/HNP1d77c-1094-41ec-b7f3-94bb30951872",
+ "/aai/v13/network/l3-networks/l3-network/HNP1d77c-1094-41ec-b7f3-94bb30951872?depth=2",
"junit/aai-l3-network2.json");
final String serviceInstanceId = "c6456519-6acf-4adb-997c-3c363dd4caaf";
public void testDemoFirewallServiceWithL3Networks() throws Exception {
// setup A&AI responses
addResponse(
- "/aai/v13/nodes/service-instance/c6456519-6acf-4adb-997c-3c363dd4caaf",
+ "/aai/v13/nodes/service-instance/c6456519-6acf-4adb-997c-3c363dd4caaf?depth=2",
"junit/aai-service-instance2.json");
addResponse(
"/aai/v13/network/generic-vnfs/generic-vnf/6700c313-fbb7-4cf9-ac70-0293ec56df68?depth=2",
"junit/aai-generic-vnf2.json");
addResponse(
"/aai/v13/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/RegionOne/tenants/tenant"
- + "/b49b830686654191bb1e952a74b014ad/vservers/vserver/b494cd6e-b9f3-45e0-afe7-e1d1a5f5d74a",
+ + "/b49b830686654191bb1e952a74b014ad/vservers/vserver/b494cd6e-b9f3-45e0-afe7-e1d1a5f5d74a?depth=2",
"junit/aai-vserver.json");
addResponse(
- "/aai/v13/network/l3-networks/l3-network/HNP1d77c-1094-41ec-b7f3-94bb30951870",
+ "/aai/v13/network/l3-networks/l3-network/HNP1d77c-1094-41ec-b7f3-94bb30951870?depth=2",
"junit/aai-l3-network1.json");
addResponse(
- "/aai/v13/network/l3-networks/l3-network/HNP1d77c-1094-41ec-b7f3-94bb30951872",
+ "/aai/v13/network/l3-networks/l3-network/HNP1d77c-1094-41ec-b7f3-94bb30951872?depth=2",
"junit/aai-l3-network2.json");
final String serviceInstanceId = "c6456519-6acf-4adb-997c-3c363dd4caaf";
/** Unknown service-instance-id return HTTP 404. */
@Test
public void testInvalidServiceId() throws Exception {
- aai.stubFor(get("/aai/v13/nodes/service-instance/noSuchServiceId").willReturn(notFound()));
+ aai.stubFor(get("/aai/v13/nodes/service-instance/noSuchServiceId?depth=2").willReturn(notFound()));
Response response =
this.service.getContext(httpRequest, AUTH, "junit", null, "noSuchServiceId");