Merge "Removed MsoLogger from 'so-bpmn-tasks'"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / buildingblock / SniroHomingV2.java
index 6b89387..893c67d 100644 (file)
@@ -4,6 +4,8 @@
  * ================================================================================
  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -23,6 +25,7 @@ package org.onap.so.bpmn.buildingblock;
 import static org.apache.commons.lang3.StringUtils.*;
 
 import java.time.Duration;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
@@ -57,9 +60,17 @@ import org.onap.so.client.exception.ExceptionBuilder;
 import org.onap.so.client.sniro.SniroClient;
 import static org.onap.so.client.sniro.SniroValidator.*;
 
+import org.onap.so.client.sniro.beans.Demand;
+import org.onap.so.client.sniro.beans.LicenseInfo;
+import org.onap.so.client.sniro.beans.ModelInfo;
+import org.onap.so.client.sniro.beans.PlacementInfo;
+import org.onap.so.client.sniro.beans.RequestInfo;
+import org.onap.so.client.sniro.beans.ServiceInfo;
 import org.onap.so.client.sniro.beans.SniroManagerRequest;
+import org.onap.so.client.sniro.beans.SubscriberInfo;
 import org.onap.so.db.catalog.beans.OrchestrationStatus;
-import org.onap.so.logger.MsoLogger;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.env.Environment;
 import org.springframework.stereotype.Component;
@@ -76,7 +87,7 @@ import org.springframework.web.util.UriUtils;
 @Component("SniroHoming")
 public class SniroHomingV2 {
 
-       private static final MsoLogger log = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SniroHomingV2.class);
+       private static final Logger logger =  LoggerFactory.getLogger(SniroHomingV2.class);
        private JsonUtils jsonUtils = new JsonUtils();
        @Autowired
        private Environment env;
@@ -106,7 +117,7 @@ public class SniroHomingV2 {
         * @param execution
         */
        public void callSniro(BuildingBlockExecution execution){
-               log.debug("Started Sniro Homing Call Sniro");
+               logger.debug("Started Sniro Homing Call Sniro");
                try{
                        GeneralBuildingBlock bb = execution.getGeneralBuildingBlock();
 
@@ -122,30 +133,30 @@ public class SniroHomingV2 {
                                timeout = env.getProperty("sniro.manager.timeout", "PT30M");
                        }
 
-                       SniroManagerRequest request = new SniroManagerRequest(); //TODO Add additional pojos for each section
+                       SniroManagerRequest request = new SniroManagerRequest();
 
-                       JSONObject requestInfo = buildRequestInfo(requestId, timeout);
-                       request.setRequestInformation(requestInfo.toString());
+                       RequestInfo requestInfo = buildRequestInfo(requestId, timeout);
+                       request.setRequestInformation(requestInfo);
 
-                       JSONObject serviceInfo = buildServiceInfo(serviceInstance);
-                       request.setServiceInformation(serviceInfo.toString());
+                       ServiceInfo serviceInfo = buildServiceInfo(serviceInstance);
+                       request.setServiceInformation(serviceInfo);
 
-                       JSONObject placementInfo = buildPlacementInfo(customer, requestParams);
+                       PlacementInfo placementInfo = buildPlacementInfo(customer, requestParams);
 
-                       JSONArray placementDemands = buildPlacementDemands(serviceInstance);
-                       placementInfo.put("placementDemands", placementDemands);
-                       request.setPlacementInformation(placementInfo.toString());
+                       List<Demand> placementDemands = buildPlacementDemands(serviceInstance);
+                       placementInfo.setDemands(placementDemands);
+                       request.setPlacementInformation(placementInfo);
 
-                       JSONObject licenseInfo = new JSONObject();
+                       LicenseInfo licenseInfo = new LicenseInfo();
 
-                       JSONArray licenseDemands = buildLicenseDemands(serviceInstance);
-                       licenseInfo.put("licenseDemands", licenseDemands);
-                       request.setLicenseInformation(licenseInfo.toString());
+                       List<Demand> licenseDemands = buildLicenseDemands(serviceInstance);
+                       licenseInfo.setDemands(licenseDemands);
+                       request.setLicenseInformation(licenseInfo);
 
-                       if(placementDemands.length() > 0 || licenseDemands.length() > 0){
+                       if(placementDemands.size() > 0 || licenseDemands.size() > 0){
                                client.postDemands(request);
                        }else{
-                               log.debug(SERVICE_MISSING_DATA + "resources eligible for homing or licensing");
+                               logger.debug(SERVICE_MISSING_DATA + "resources eligible for homing or licensing");
                                throw new BpmnError(UNPROCESSABLE, SERVICE_MISSING_DATA + "resources eligible for homing or licensing");
                        }
 
@@ -154,12 +165,15 @@ public class SniroHomingV2 {
                        execution.setVariable("asyncMessageType", "SNIROResponse");
                        execution.setVariable("asyncTimeout", timeout);
 
-                       log.trace("Completed Sniro Homing Call Sniro");
+                       logger.trace("Completed Sniro Homing Call Sniro");
                }catch(BpmnError e){
+                       logger.error("Exception occurred", e);
                        exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(e.getErrorCode()), e.getMessage());
                }catch(BadResponseException e){
+                       logger.error("Exception occurred", e);
                        exceptionUtil.buildAndThrowWorkflowException(execution, 400, e.getMessage());
                }catch(Exception e){
+                       logger.error("Exception occurred", e);
                        exceptionUtil.buildAndThrowWorkflowException(execution, INTERNAL, "Internal Error - occurred while preparing sniro request: " + e.getMessage());
                }
        }
@@ -172,13 +186,13 @@ public class SniroHomingV2 {
         * @param asyncResponse
         */
        public void processSolution(BuildingBlockExecution execution, String asyncResponse){
-               log.trace("Started Sniro Homing Process Solution");
+               logger.trace("Started Sniro Homing Process Solution");
                try{
                        //TODO improve handling multiple solutions but is dependent on sniro enhancing api + work with sniro conductor to improve "inventoryType" representation
                        validateSolution(asyncResponse);
                        ServiceInstance serviceInstance = execution.getGeneralBuildingBlock().getCustomer().getServiceSubscription().getServiceInstances().get(0);
 
-                       log.debug("Processing sniro manager asyncronous response");
+                       logger.debug("Processing sniro manager asyncronous response");
                        JSONObject response = new JSONObject(asyncResponse);
                        if(response.has(SOLUTIONS)){
                                JSONObject allSolutions = response.getJSONObject(SOLUTIONS);
@@ -201,12 +215,15 @@ public class SniroHomingV2 {
 
                        execution.setVariable("generalBuildingBlock", execution.getGeneralBuildingBlock());
 
-                       log.trace("Completed Sniro Homing Process Solution");
+                       logger.trace("Completed Sniro Homing Process Solution");
                }catch(BpmnError e){
+                       logger.error("Exception occurred", e);
                        exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(e.getErrorCode()), e.getMessage());
                }catch(BadResponseException e){
+                       logger.error("Exception occurred", e);
                        exceptionUtil.buildAndThrowWorkflowException(execution, 400, e.getMessage());
                }catch(Exception e){
+                       logger.error("Exception occurred", e);
                        exceptionUtil.buildAndThrowWorkflowException(execution, INTERNAL, "Internal Error - occurred while processing sniro asynchronous response: " + e.getMessage());
                }
        }
@@ -216,18 +233,21 @@ public class SniroHomingV2 {
         *
         * @throws Exception
         */
-       private JSONObject buildRequestInfo(String requestId, String timeout) throws Exception{
-               log.trace("Building request information");
-               JSONObject requestInfo = new JSONObject();
+       private RequestInfo buildRequestInfo(String requestId, String timeout) throws Exception{
+               logger.trace("Building request information");
+               RequestInfo requestInfo = new RequestInfo();
                if(requestId != null){
                        String host = env.getProperty("mso.workflow.message.endpoint");
                        String callbackUrl = host + "/" + UriUtils.encodePathSegment("SNIROResponse", "UTF-8") + "/" + UriUtils.encodePathSegment(requestId, "UTF-8");
 
                        Duration d = Duration.parse(timeout);
-                       long timeoutSeconds = d.getSeconds();
 
-                       requestInfo.put("transactionId", requestId).put("requestId", requestId).put("callbackUrl", callbackUrl).put("sourceId", "mso").put("requestType", "create")
-                                       .put("timeout", timeoutSeconds);
+                       requestInfo.setTransactionId(requestId);
+                       requestInfo.setRequestId(requestId);
+                       requestInfo.setCallbackUrl(callbackUrl);
+                       requestInfo.setRequestType("create");
+                       requestInfo.setTimeout(d.getSeconds());
+
                } else{
                        throw new BpmnError(UNPROCESSABLE, "Request Context does not contain: requestId");
                }
@@ -238,19 +258,19 @@ public class SniroHomingV2 {
         * Builds the request information section for the homing/licensing request
         *
         */
-       private JSONObject buildServiceInfo(ServiceInstance serviceInstance){
-               log.trace("Building service information");
-               JSONObject info = new JSONObject();
+       private ServiceInfo buildServiceInfo(ServiceInstance serviceInstance){
+               logger.trace("Building service information");
+               ServiceInfo info = new ServiceInfo();
                ModelInfoServiceInstance modelInfo = serviceInstance.getModelInfoServiceInstance();
                if(isNotBlank(modelInfo.getModelInvariantUuid()) && isNotBlank(modelInfo.getModelUuid())){
-                       info.put("serviceInstanceId", serviceInstance.getServiceInstanceId());
+                       info.setServiceInstanceId(serviceInstance.getServiceInstanceId());
                        if(modelInfo.getServiceType() != null && modelInfo.getServiceType().length() > 0){ //temp solution
-                               info.put("serviceName", modelInfo.getServiceType());
+                               info.setServiceName(modelInfo.getServiceType());
                        }
                        if(modelInfo.getServiceRole() != null){
-                               info.put("serviceRole", modelInfo.getServiceRole());
+                               info.setServiceRole(modelInfo.getServiceRole());
                        }
-                       info.put("modelInfo", buildModelInfo(serviceInstance.getModelInfoServiceInstance()));
+                       info.setModelInfo(buildModelInfo(modelInfo));
                }else{
                        throw new BpmnError(UNPROCESSABLE, SERVICE_MISSING_DATA + MODEL_VERSION_ID + ", " + MODEL_INVARIANT_ID);
                }
@@ -261,14 +281,18 @@ public class SniroHomingV2 {
         * Builds initial section of placement info for the homing/licensing request
         *
         */
-       private JSONObject buildPlacementInfo(Customer customer, RequestParameters requestParams){
-               JSONObject placementInfo = new JSONObject();
+       private PlacementInfo buildPlacementInfo(Customer customer, RequestParameters requestParams){
+               PlacementInfo placementInfo = new PlacementInfo();
                if(customer != null){
-                       log.debug("Adding subscriber to placement information");
-                       placementInfo.put("subscriberInfo", new JSONObject().put("globalSubscriberId", customer.getGlobalCustomerId()).put("subscriberName", customer.getSubscriberName()).put("subscriberCommonSiteId", customer.getSubscriberCommonSiteId()));
+                       logger.debug("Adding subscriber to placement information");
+                       SubscriberInfo subscriber = new SubscriberInfo();
+                       subscriber.setGlobalSubscriberId(customer.getGlobalCustomerId());
+                       subscriber.setSubscriberName(customer.getSubscriberName());
+                       subscriber.setSubscriberCommonSiteId(customer.getSubscriberCommonSiteId());
+                       placementInfo.setSubscriberInfo(subscriber);
                        if(requestParams != null){
-                               log.debug("Adding request parameters to placement information");
-                               placementInfo.put("requestParameters", new JSONObject(requestParams.toJsonString()));
+                               logger.debug("Adding request parameters to placement information");
+                               placementInfo.setRequestParameters(requestParams.toJsonString());
                        }
                }else{
                        throw new BpmnError(UNPROCESSABLE, SERVICE_MISSING_DATA + "customer");
@@ -281,34 +305,34 @@ public class SniroHomingV2 {
         * Builds the placement demand list for the homing/licensing request
         *
         */
-       private JSONArray buildPlacementDemands(ServiceInstance serviceInstance){
-               log.trace("Building placement information demands");
-               JSONArray placementDemands = new JSONArray();
+       private List<Demand> buildPlacementDemands(ServiceInstance serviceInstance){
+               logger.trace("Building placement information demands");
+               List<Demand> placementDemands = new ArrayList<Demand>();
 
                List<AllottedResource> allottedResourceList = serviceInstance.getAllottedResources();
                if(!allottedResourceList.isEmpty()){
-                       log.debug("Adding allotted resources to placement demands list");
+                       logger.debug("Adding allotted resources to placement demands list");
                        for(AllottedResource ar : allottedResourceList){
                                if(isBlank(ar.getId())){
                                        ar.setId(UUID.randomUUID().toString());
                                }
-                               JSONObject demand = buildDemand(ar.getId(), ar.getModelInfoAllottedResource());
+                               Demand demand = buildDemand(ar.getId(), ar.getModelInfoAllottedResource());
                                addCandidates(ar, demand);
-                               placementDemands.put(demand);
+                               placementDemands.add(demand);
                        }
                }
                List<VpnBondingLink> vpnBondingLinkList = serviceInstance.getVpnBondingLinks();
                if(!vpnBondingLinkList.isEmpty()){
-                       log.debug("Adding vpn bonding links to placement demands list");
+                       logger.debug("Adding vpn bonding links to placement demands list");
                        for(VpnBondingLink vbl:vpnBondingLinkList){
                                List<ServiceProxy> serviceProxyList = vbl.getServiceProxies();
                                for(ServiceProxy sp : serviceProxyList){
                                        if(isBlank(sp.getId())){
                                                sp.setId(UUID.randomUUID().toString());
                                        }
-                                       JSONObject demand = buildDemand(sp.getId(), sp.getModelInfoServiceProxy());
+                                       Demand demand = buildDemand(sp.getId(), sp.getModelInfoServiceProxy());
                                        addCandidates(sp, demand);
-                                       placementDemands.put(demand);
+                                       placementDemands.add(demand);
                                }
                        }
                }
@@ -319,15 +343,15 @@ public class SniroHomingV2 {
         * Builds the license demand list for the homing/licensing request
         *
         */
-       private JSONArray buildLicenseDemands(ServiceInstance serviceInstance){
-               log.trace("Building license information");
-               JSONArray licenseDemands = new JSONArray();
+       private List<Demand> buildLicenseDemands(ServiceInstance serviceInstance){
+               logger.trace("Building license information");
+               List<Demand> licenseDemands = new ArrayList<Demand>();
                List<GenericVnf> vnfList = serviceInstance.getVnfs();
                if(!vnfList.isEmpty()){
-                       log.debug("Adding vnfs to license demands list");
+                       logger.debug("Adding vnfs to license demands list");
                        for(GenericVnf vnf : vnfList){
-                               JSONObject demand = buildDemand(vnf.getVnfId(), vnf.getModelInfoGenericVnf());
-                               licenseDemands.put(demand);
+                               Demand demand = buildDemand(vnf.getVnfId(), vnf.getModelInfoGenericVnf());
+                               licenseDemands.add(demand);
                        }
                }
                return licenseDemands;
@@ -337,13 +361,13 @@ public class SniroHomingV2 {
         * Builds a single demand object
         *
         */
-       private JSONObject buildDemand(String id, ModelInfoMetadata metadata){
-               log.debug("Building demand for service or resource: " + id);
-               JSONObject demand = new JSONObject();
+       private Demand buildDemand(String id, ModelInfoMetadata metadata){
+               logger.debug("Building demand for service or resource: " + id);
+               Demand demand = new Demand();
                if(isNotBlank(id) && isNotBlank(metadata.getModelInstanceName())){
-                       demand.put(SERVICE_RESOURCE_ID, id);
-                       demand.put(RESOURCE_MODULE_NAME, metadata.getModelInstanceName());
-                       demand.put(RESOURCE_MODEL_INFO, buildModelInfo(metadata));
+                       demand.setServiceResourceId(id);
+                       demand.setResourceModuleName(metadata.getModelInstanceName());
+                       demand.setModelInfo(buildModelInfo(metadata));
                }else{
                        throw new BpmnError(UNPROCESSABLE, RESOURCE_MISSING_DATA + "modelInstanceName");
                }
@@ -354,12 +378,15 @@ public class SniroHomingV2 {
         * Builds the resource model info section
         *
         */
-       private JSONObject buildModelInfo(ModelInfoMetadata metadata){
-               JSONObject object = new JSONObject();
+       private ModelInfo buildModelInfo(ModelInfoMetadata metadata){
+               ModelInfo object = new ModelInfo();
                String invariantUuid = metadata.getModelInvariantUuid();
                String modelUuid = metadata.getModelUuid();
                if(isNotBlank(invariantUuid) && isNotBlank(modelUuid)){
-                       object.put(MODEL_INVARIANT_ID, invariantUuid).put(MODEL_VERSION_ID, modelUuid).put(MODEL_NAME, metadata.getModelName()).put(MODEL_VERSION, metadata.getModelVersion());
+                       object.setModelInvariantId(invariantUuid);
+                       object.setModelVersionId(modelUuid);
+                       object.setModelName(metadata.getModelName());
+                       object.setModelVersion(metadata.getModelVersion());
                }else if(isNotBlank(invariantUuid)){
                        throw new BpmnError(UNPROCESSABLE, RESOURCE_MISSING_DATA + MODEL_VERSION_ID);
                }else{
@@ -372,14 +399,30 @@ public class SniroHomingV2 {
         * Adds required, excluded, and existing candidates to a demand
         *
         */
-       private void addCandidates(SolutionCandidates candidates, JSONObject demand){
+       private void addCandidates(SolutionCandidates candidates, Demand demand){
                List<Candidate> required = candidates.getRequiredCandidates();
                List<Candidate> excluded = candidates.getExcludedCandidates();
                if(!required.isEmpty()){
-                       demand.put("requiredCandidates", required);
+                       List<org.onap.so.client.sniro.beans.Candidate> cans = new ArrayList<org.onap.so.client.sniro.beans.Candidate>();
+                       for(Candidate c:required){
+                               org.onap.so.client.sniro.beans.Candidate can = new org.onap.so.client.sniro.beans.Candidate();
+                               can.setIdentifierType(c.getIdentifierType());
+                               can.setIdentifiers(c.getIdentifiers());
+                               can.setCloudOwner(c.getCloudOwner());
+                               cans.add(can);
+                       }
+                       demand.setRequiredCandidates(cans);
                }
                if(!excluded.isEmpty()){
-                       demand.put("excludedCandidates", excluded);
+                       List<org.onap.so.client.sniro.beans.Candidate> cans = new ArrayList<org.onap.so.client.sniro.beans.Candidate>();
+                       for(Candidate c:excluded){
+                               org.onap.so.client.sniro.beans.Candidate can = new org.onap.so.client.sniro.beans.Candidate();
+                               can.setIdentifierType(c.getIdentifierType());
+                               can.setIdentifiers(c.getIdentifiers());
+                               can.setCloudOwner(c.getCloudOwner());
+                               cans.add(can);
+                       }
+                       demand.setExcludedCandidates(cans);
                }
                //TODO support existing candidates
        }
@@ -391,7 +434,7 @@ public class SniroHomingV2 {
        private void processLicenseSolution(ServiceInstance serviceInstance, JSONArray licenseSolutions){
                List<GenericVnf> vnfs = serviceInstance.getVnfs();
 
-               log.debug("Processing the license solution");
+               logger.debug("Processing the license solution");
                for(int i = 0; i < licenseSolutions.length(); i++){
                        JSONObject licenseSolution = licenseSolutions.getJSONObject(i);
                        for(GenericVnf vnf:vnfs){
@@ -420,7 +463,7 @@ public class SniroHomingV2 {
                List<AllottedResource> allottes = serviceInstance.getAllottedResources();
                List<GenericVnf> vnfs = serviceInstance.getVnfs();
 
-               log.debug("Processing placement solution " + i+1);
+               logger.debug("Processing placement solution " + i+1);
                for(int p = 0; p < placements.length(); p++){
                        JSONObject placement = placements.getJSONObject(p);
                        SolutionInfo solutionInfo = new SolutionInfo();
@@ -468,7 +511,7 @@ public class SniroHomingV2 {
         *
         */
        private ServiceInstance setSolution(SolutionInfo solutionInfo, JSONObject placement){
-               log.debug("Mapping placement solution");
+               logger.debug("Mapping placement solution");
                String invalidMessage = "Sniro Managers Response contains invalid: ";
 
                JSONObject solution = placement.getJSONObject("solution");
@@ -490,13 +533,13 @@ public class SniroHomingV2 {
                                si.setOrchestrationStatus(OrchestrationStatus.CREATED);
                                cloud.setLcpCloudRegionId(assignmentsMap.get("cloudRegionId"));
                                if(assignmentsMap.containsKey("vnfHostName")){
-                                       log.debug("Resources has been homed to a vnf");
+                                       logger.debug("Resources has been homed to a vnf");
                                        GenericVnf vnf = setVnf(assignmentsMap);
                                        vnf.setCloudRegion(cloud);
                                        si.getVnfs().add(vnf);
 
                                }else if(assignmentsMap.containsKey("primaryPnfName")){
-                                       log.debug("Resources has been homed to a pnf");
+                                       logger.debug("Resources has been homed to a pnf");
                                        Pnf priPnf = setPnf(assignmentsMap, "primary");
                                        priPnf.setCloudRegion(cloud);
                                        si.getPnfs().add(priPnf);
@@ -507,22 +550,22 @@ public class SniroHomingV2 {
                                        }
                                }
                        }else{
-                               log.debug(invalidMessage + IDENTIFIER_TYPE);
+                               logger.debug(invalidMessage + IDENTIFIER_TYPE);
                                throw new BpmnError(UNPROCESSABLE, invalidMessage + IDENTIFIER_TYPE);
                        }
                }else if(type.equals("cloud")){
                        if(identifierType.equals(CandidateType.CLOUD_REGION_ID.toString())){
-                               log.debug("Resources has been homed to a cloud region");
+                               logger.debug("Resources has been homed to a cloud region");
                                cloud.setLcpCloudRegionId(identifierValue);
                                solutionInfo.setHomed(false);
                                solutionInfo.setTargetedCloudRegion(cloud);
                                si.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
                        }else{
-                               log.debug(invalidMessage + IDENTIFIER_TYPE);
+                               logger.debug(invalidMessage + IDENTIFIER_TYPE);
                                throw new BpmnError(UNPROCESSABLE, invalidMessage + IDENTIFIER_TYPE);
                        }
                }else{
-                       log.debug(invalidMessage + INVENTORY_TYPE);
+                       logger.debug(invalidMessage + INVENTORY_TYPE);
                        throw new BpmnError(UNPROCESSABLE, invalidMessage + INVENTORY_TYPE);
                }
                si.setSolutionInfo(solutionInfo);