From 417bd28022619432588cc58679e68d893c62c617 Mon Sep 17 00:00:00 2001 From: aleemraja Date: Thu, 20 Aug 2020 19:06:27 +0530 Subject: Passing Instance params for SO Macro request - Added Support for Instance params for SO Macro request at Service level and VNF level. - Added JUnit Test cases for ExecutionTask and ToscaInfoProcessor. - Updated timeout for StatusResourceTest. Issue-ID: EXTAPI-369 Change-Id: Ibbbbbd2d7021c16ea6291cc899e310ae26200999 Signed-off-by: aleemraja --- .../ServiceSpecificationResource.java | 1 + .../apis/servicecatalog/ToscaInfosProcessor.java | 43 ++++++++- .../serviceorder/workflow/PostSoProcessor.java | 104 ++++++++++++++++++++- 3 files changed, 141 insertions(+), 7 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/onap/nbi/apis/servicecatalog/ServiceSpecificationResource.java b/src/main/java/org/onap/nbi/apis/servicecatalog/ServiceSpecificationResource.java index f9f5e07..128bc6c 100644 --- a/src/main/java/org/onap/nbi/apis/servicecatalog/ServiceSpecificationResource.java +++ b/src/main/java/org/onap/nbi/apis/servicecatalog/ServiceSpecificationResource.java @@ -49,6 +49,7 @@ public class ServiceSpecificationResource extends ResourceManagement { ArrayList> resourseSpecificationMap= (ArrayList>) response.get("resourceSpecification"); for (Map map : resourseSpecificationMap) { map.remove("childResourceSpecification"); + map.remove("serviceInstanceParams"); map.remove("InstanceSpecification"); } response.put("resourceSpecification", resourseSpecificationMap); diff --git a/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java b/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java index 586d0b5..67f157e 100644 --- a/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java +++ b/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java @@ -22,6 +22,7 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import org.onap.nbi.apis.serviceorder.model.consumer.VFModelInfo; @@ -35,6 +36,7 @@ import org.onap.sdc.tosca.parser.impl.SdcPropertyNames; import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; import org.onap.sdc.toscaparser.api.NodeTemplate; import org.onap.sdc.toscaparser.api.elements.Metadata; +import org.onap.sdc.toscaparser.api.functions.GetInput; import org.onap.sdc.toscaparser.api.parameters.Input; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -137,11 +139,12 @@ public class ToscaInfosProcessor { List vfEntityList = sdcCsarHelper.getEntity(EntityQuery.newBuilder(SdcTypes.VF).build(), TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE).build(), false); + Map groupProperties = null; Map listOfInstanceParameters = new HashMap<>(); if (!vfEntityList.isEmpty()) { IEntityDetails iEntityDetails = vfEntityList.get(0); - Map groupProperties = iEntityDetails.getProperties(); + groupProperties = iEntityDetails.getProperties(); for (String key : groupProperties.keySet()) { org.onap.sdc.toscaparser.api.Property property = groupProperties.get(key); @@ -254,11 +257,45 @@ public class ToscaInfosProcessor { if (!vfModuleEntityList.isEmpty()) { resourceSpecification.put("childResourceSpecification", listOfVfModelInfo); } - resourceSpecification.put("InstanceSpecification", listOfInstanceParameters); - + HashMap serviceInstanceParams = getServiceLevelInstanceParams(inputs); + resourceSpecification.put("serviceInstanceParams", serviceInstanceParams); + HashMap vnfInstanceParams = getUserDefinedVFLevelInstanceParams(groupProperties, listOfInstanceParameters); + resourceSpecification.put("InstanceSpecification", vnfInstanceParams); } } } + + // Get List of Service Level InputParams as Key Value + private HashMap getServiceLevelInstanceParams(List listOfServiceLevelInputs) { + + HashMap serviceLevelInstanceParams = new HashMap<>(); + + for (Input input : listOfServiceLevelInputs) { + serviceLevelInstanceParams.put(input.getName(), input.getDefault()); + } + + return serviceLevelInstanceParams; + } + + private HashMap getUserDefinedVFLevelInstanceParams( + Map groupProperties, Map listOfVFLevelInputs) { + + HashMap vnfLevelInstanceParams = new HashMap<>(); + + for (Entry entry : groupProperties.entrySet()) { + + org.onap.sdc.toscaparser.api.Property property = entry.getValue(); + + if ((property.getValue().getClass() == GetInput.class)) { + GetInput getInput = (GetInput) property.getValue(); + listOfVFLevelInputs.put(getInput.getInputName(), getInput.result()); + listOfVFLevelInputs.remove(property.getName()); + } + } + + return (HashMap) listOfVFLevelInputs; + } + private static String testNull(Object object) { if (object == null) { diff --git a/src/main/java/org/onap/nbi/apis/serviceorder/workflow/PostSoProcessor.java b/src/main/java/org/onap/nbi/apis/serviceorder/workflow/PostSoProcessor.java index e590afc..17b72e4 100644 --- a/src/main/java/org/onap/nbi/apis/serviceorder/workflow/PostSoProcessor.java +++ b/src/main/java/org/onap/nbi/apis/serviceorder/workflow/PostSoProcessor.java @@ -20,6 +20,8 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; import org.onap.nbi.apis.servicecatalog.ServiceSpecificationService; import org.onap.nbi.apis.serviceorder.SoClient; @@ -248,21 +250,31 @@ public class PostSoProcessor { Map resourseSpecificationMap = resourseSpecificationArray.get(0); + Map serviceInstanceParam = (Map) resourseSpecificationMap.get("serviceInstanceParams"); Map instanceSpecification = (Map) resourseSpecificationMap.get("InstanceSpecification"); ArrayList childResourceSpecification = (ArrayList) resourseSpecificationMap .get("childResourceSpecification"); + + HashMap instanceParamsFromServiceCharacteristics = retrieveInstanceParamsFromServiceCharacteristics( + orderItem.getService().getServiceCharacteristic()); + + HashMap instanceParams = (HashMap) buildAndDistinguishServiceAndVnfLevelParams( + instanceParamsFromServiceCharacteristics, instanceSpecification, serviceInstanceParam); + HashMap vnfInstanceParams = (HashMap) instanceParams.get("vnf"); List serviceObject = new ArrayList<>(); ArrayList vnfInstanceParam = new ArrayList<>(); //Differentiating vnf with cnf(Can be discussed and improved) if (instanceSpecification.get("public_net_id") != null) { + Map instanceParam = new HashMap<>(); + //Merge instanceSpecification with vnfInstanceParams + instanceSpecification.putAll(vnfInstanceParams); vnfInstanceParam.add(instanceSpecification); } else { - Map instanceParam = new HashMap<>(); - instanceParam.put("k8s-rb-profile-name", k8sRbProfileName); - vnfInstanceParam.add(instanceParam); + vnfInstanceParams.put("k8s-rb-profile-name", k8sRbProfileName); + vnfInstanceParam.add(vnfInstanceParams); } List resSpec = (ArrayList) sdcInfos.get("resourceSpecification"); @@ -349,7 +361,7 @@ public class PostSoProcessor { // For now it is empty to comply with so request List> listOfServiceLevelInstanceParams = new ArrayList<>(); - Map serviceInstanceParams= new HashMap<>(); + Map serviceInstanceParams = (HashMap) instanceParams.get("service"); listOfServiceLevelInstanceParams.add(serviceInstanceParams); Map serviceData = new HashMap<>(); @@ -563,5 +575,89 @@ public class PostSoProcessor { return userParams; } + + /** + * Build a list of InstanceParams for the SO Macro request by browsing a list of + * ServiceCharacteristics + */ + private HashMap retrieveInstanceParamsFromServiceCharacteristics( + List characteristics) { + + HashMap instanceParams = new HashMap<>(); + + if (!CollectionUtils.isEmpty(characteristics)) { + for (ServiceCharacteristic characteristic : characteristics) { + // Check is the characteristic is of type object, if proceed as before to allow + // for + // backwards compatibility. + if (characteristic.getValueType() != null && !characteristic.getValueType().isEmpty() + && characteristic.getValueType().equals("object")) { + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonNode = null; + try { + jsonNode = mapper.readTree(characteristic.getValue().getServiceCharacteristicValue()); + } catch (IOException e) { + LOGGER.error("Failed to read object json {} , exception is ", + characteristic.getValue().getServiceCharacteristicValue(), e.getMessage()); + } + ObjectNode objectNode = (ObjectNode) jsonNode; + Iterator> iter = objectNode.fields(); + while (iter.hasNext()) { + Map.Entry entry = iter.next(); + if (!entry.getValue().isArray()) { + instanceParams.put(entry.getKey(), entry.getValue().asText()); + } else { + ArrayNode arrayNode = (ArrayNode) entry.getValue(); + String arrayNodeValueString = arrayNode.toString(); + instanceParams.put(entry.getKey(), arrayNodeValueString); + } + } + } else { + instanceParams.put(characteristic.getName(), + characteristic.getValue().getServiceCharacteristicValue()); + } + } + } + + return instanceParams; + } + + /** + * Build and distinguish InstanceParams at VNF Level and Service level and overwrite values from ServiceOrder JSON Request. + * Can be used as buildAndDistinguishServiceAndVnfLevelParams.get("vnf"); or buildAndDistinguishServiceAndVnfLevelParams.get("cnf"); + */ + private Map buildAndDistinguishServiceAndVnfLevelParams( + Map instanceParamsFromServiceCharacteristic, Map existingVNFParams, + Map existingServiceParams) { + + //To be used by passing key as "vnf" or "service" for respective instanceParams + Map serviceAndVNFLevelInstanceParams = new HashMap<>(); + + Map resultVNFParams = new HashMap<>(); + Map resultServiceParams = new HashMap<>(); + + // First Filter VNF level Params From Service Characteristics and overwrite + // values + resultVNFParams = instanceParamsFromServiceCharacteristic.entrySet().stream() + .filter(entry -> existingVNFParams.containsKey(entry.getKey())) + .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + + //Add it as VNF level Params + serviceAndVNFLevelInstanceParams.put("vnf", resultVNFParams); + + // Filter VNF level Params From Service Level + existingServiceParams.entrySet().removeIf(e -> existingVNFParams.containsKey(e.getKey())); + + // Filter Service level Params From Service Characteristics and overwrite values + resultServiceParams = instanceParamsFromServiceCharacteristic.entrySet().stream() + .filter(entry -> existingServiceParams.containsKey(entry.getKey())) + .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + + //Add it as Service level params + serviceAndVNFLevelInstanceParams.put("service", resultServiceParams); + + return serviceAndVNFLevelInstanceParams; + + } } \ No newline at end of file -- cgit 1.2.3-korg