From 5ed1062dcc39cfa7a99b8a5b6882a75f4acffea6 Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Tue, 30 Apr 2019 07:04:39 +0530 Subject: Modify instance resource list creation Modify instance resource list creation to avoid hard locking of model name in UUI request. e.g. VF resource and gruop resource has resource input vf { resource_input : { key1:value2, key2:[vf_prop_list,INDEX,key]|default} Derive the key ("vf_prop_list") from resource input and use this info to map UUI request. UUI req: { ... resourceInput: { vf_prop_list : /// mapped with resource input of resource { .... Change-Id: Ic40079a094b2628bcf6f5758121b7492ee3c1353 Issue-ID: SO-1393 Signed-off-by: subhash kumar singh --- .../bpmn/common/resource/InstanceResourceList.java | 175 ++++++++++----------- 1 file changed, 85 insertions(+), 90 deletions(-) (limited to 'bpmn/MSOCommonBPMN/src/main/java') diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/common/resource/InstanceResourceList.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/common/resource/InstanceResourceList.java index b50ecdad8b..71ea3b565b 100644 --- a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/common/resource/InstanceResourceList.java +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/common/resource/InstanceResourceList.java @@ -19,116 +19,120 @@ */ package org.onap.so.bpmn.common.resource; +import com.google.common.reflect.TypeToken; import com.google.gson.Gson; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import org.onap.so.bpmn.core.domain.GroupResource; import org.onap.so.bpmn.core.domain.Resource; import org.onap.so.bpmn.core.domain.ResourceType; +import org.onap.so.bpmn.core.domain.VnfResource; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Optional; public class InstanceResourceList { - private static List>> convertUUIReqTOStd(final String uuiRequest, + private static Map>> convertUUIReqTOStd(final JsonObject reqInputJsonObj, List seqResourceList) { - List>> normalizedList = new ArrayList<>(); + Map>> normalizedRequest = new HashMap<>(); + for (Resource r : seqResourceList) { - Gson gson = new Gson(); - JsonObject servJsonObject = gson.fromJson(uuiRequest, JsonObject.class); + if (r.getResourceType() == ResourceType.VNF) { + String pk = getPrimaryKey(r); - JsonObject reqInputJsonObj = servJsonObject.getAsJsonObject("service").getAsJsonObject("parameters") - .getAsJsonObject("requestInputs"); + JsonElement vfNode = reqInputJsonObj.get(pk); - // iterate all node in requestInputs - Iterator> iterator = reqInputJsonObj.entrySet().iterator(); - - while (iterator.hasNext()) { // iterate all _list - Map.Entry entry = iterator.next(); - - // truncate "_list" from key and keep only the - String key = entry.getKey().substring(0, entry.getKey().indexOf("_list")); - - // all the element represent VF will contain "_list". - if (key.contains("_list")) { - // this will return list of vf of same type - // e.g. vf_list [{vf1}, {vf2}] - Iterator vfsIterator = entry.getValue().getAsJsonArray().iterator(); - - while (vfsIterator.hasNext()) { // iterate all [] inside vf_list - JsonObject vfObject = vfsIterator.next().getAsJsonObject(); - List tmpGrpsHolder = new ArrayList<>(); - - // iterate vfObject to get groups(vfc) - // currently each vfc represented by one group. - Iterator> vfIterator = vfObject.entrySet().iterator(); - while (vfIterator.hasNext()) { // iterate all property inside a VF - Map.Entry vfEntry = vfIterator.next(); - - // property name for vfc input will always carry "_list" - if (vfEntry.getKey().contains("_list")) { - // truncate "_list" from key and keep only the - String vfcName = vfEntry.getKey().substring(0, vfEntry.getKey().indexOf("_list")); - GroupResource grpRes = getGroupResource(vfcName, seqResourceList); - // A _list can contain more than one vfc of same type - Iterator vfcsIterator = vfEntry.getValue().getAsJsonArray().iterator(); - - while (vfcsIterator.hasNext()) { // iterate all the vfcs inside _list - tmpGrpsHolder.add(grpRes); - } + // if the service property is type of array then it + // means it is a VF resource + if (vfNode instanceof JsonArray) { + + for (int i = 0; i < ((JsonArray) vfNode).size(); i++) { + List> groupsList = normalizedRequest.get(pk); + if (groupsList == null) { + groupsList = new ArrayList<>(); + normalizedRequest.put(pk, groupsList); } + + groupsList.add(new ArrayList<>()); } - List seqGrpResourceList = seqGrpResource(tmpGrpsHolder, seqResourceList); - HashMap> entryNormList = new HashMap<>(); - entryNormList.put(key, seqGrpResourceList); - normalizedList.add(entryNormList); } - } - } - return normalizedList; - } + } else if (r.getResourceType() == ResourceType.GROUP) { + String sk = getPrimaryKey(r); - private static List seqGrpResource(List grpResources, List resourceList) { - List seqGrpResList = new ArrayList<>(); - for (Resource r : resourceList) { - if (r.getResourceType() != ResourceType.GROUP) { - continue; - } - for (GroupResource g : grpResources) { - if (r.getModelInfo().getModelName().equalsIgnoreCase(g.getModelInfo().getModelName())) { - seqGrpResList.add(g); + for (String pk : normalizedRequest.keySet()) { + JsonArray vfs = reqInputJsonObj.getAsJsonArray(pk); + + for (int i = 0; i < vfs.size(); i++) { + + JsonElement vfcsNode = vfs.get(i).getAsJsonObject().get(sk); + if (vfcsNode instanceof JsonArray) { + + List groupResources = normalizedRequest.get(pk).get(i); + + if (groupResources == null) { + groupResources = new ArrayList<>(); + normalizedRequest.get(pk).add(i, groupResources); + } + + for (int j = 0; j < ((JsonArray) vfcsNode).size(); j++) { + groupResources.add((GroupResource) r); + } + } + } } } } - return seqGrpResList; + return normalizedRequest; } - private static GroupResource getGroupResource(String vfcName, List seqRessourceList) { - for (Resource r : seqRessourceList) { - if (r.getResourceType() == ResourceType.GROUP) { - // Currently only once vnfc is added to group - return ((GroupResource) r).getVnfcs().get(0).getModelInfo().getModelName().contains(vfcName) - ? (GroupResource) r - : null; - } + // this method returns key from resource input + // e.g. {\"sdwansite_emails\" : \"[sdwansiteresource_list(PK), INDEX, sdwansite_emails]|default\", + // ....} + // it will return sdwansiteresource_list + private static String getPrimaryKey(Resource resource) { + String pk = ""; + + String resourceInput = ""; + if (resource instanceof VnfResource) { + resourceInput = ((VnfResource) resource).getResourceInput(); + } else if (resource instanceof GroupResource) { + resourceInput = ((GroupResource) resource).getVnfcs().get(0).getResourceInput(); } - return null; + + Gson gson = new Gson(); + Type type = new TypeToken>() {}.getType(); + Map map = gson.fromJson(resourceInput, type); + + Optional pkOpt = map.values().stream().filter(e -> e.contains("[")).map(e -> e.replace("[", "")) + .map(e -> e.split(",")[0]).findFirst(); + + return pkOpt.isPresent() ? pkOpt.get() : ""; } - private static List convertToInstanceResourceList(List>> normalizedReq, + private static List convertToInstanceResourceList(Map>> normalizedReq, List seqResourceList) { List flatResourceList = new ArrayList<>(); for (Resource r : seqResourceList) { if (r.getResourceType() == ResourceType.VNF) { - for (Map> entry : normalizedReq) { - if (r.getModelInfo().getModelName().equalsIgnoreCase(entry.keySet().iterator().next())) { - flatResourceList.add(r); - flatResourceList.addAll(entry.get(entry.keySet().iterator().next())); + String primaryKey = getPrimaryKey(r); + for (String pk : normalizedReq.keySet()) { + + if (primaryKey.equalsIgnoreCase(pk)) { + + List> vfs = normalizedReq.get(pk); + + vfs.stream().forEach(e -> { + flatResourceList.add(r); + flatResourceList.addAll(e); + }); } } } @@ -139,22 +143,13 @@ public class InstanceResourceList { public static List getInstanceResourceList(final List seqResourceList, final String uuiRequest) { + Gson gson = new Gson(); + JsonObject servJsonObject = gson.fromJson(uuiRequest, JsonObject.class); + JsonObject reqInputJsonObj = servJsonObject.getAsJsonObject("service").getAsJsonObject("parameters") + .getAsJsonObject("requestInputs"); + // this will convert UUI request to normalized form - List>> normalizedReq = convertUUIReqTOStd(uuiRequest, seqResourceList); - - // now UUI json req is normalized to - // [ - // { VFB1 : [GrpA1, GrA2, GrB1]}, - // { VFB2 : [GrpA1, GrB1]}, - // { VFA1 : [GrpC1]} - // ] - // now sequence according to VF order (Group is already sequenced). - // After sequence it will look like : - // [ - // { VFA1 : [GrpA1, GrA2, GrB1]}, - // { VFA2 : [GrpA1, GrB1]}, - // { VFB1 : [GrpC1]} - // ] - return convertToInstanceResourceList(normalizedReq, seqResourceList); + Map>> normalizedRequest = convertUUIReqTOStd(reqInputJsonObj, seqResourceList); + return convertToInstanceResourceList(normalizedRequest, seqResourceList); } } -- cgit 1.2.3-korg