f65dde1af5f7eff274af622da6e8e7b479d55035
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / buildingblock / OofHomingV2.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.buildingblock;
22
23 import org.apache.commons.lang.SerializationUtils;
24 import org.camunda.bpm.engine.delegate.BpmnError;
25 import java.util.ArrayList;
26 import org.json.JSONArray;
27 import org.json.JSONObject;
28 import org.onap.so.bpmn.common.BuildingBlockExecution;
29 import org.onap.so.bpmn.core.json.JsonUtils;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.AllottedResource;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
34 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
35 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
36 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceProxy;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.VpnBondingLink;
38 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
39 import org.onap.so.bpmn.servicedecomposition.generalobjects.License;
40 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
41 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
42 import org.onap.so.bpmn.servicedecomposition.homingobjects.Candidate;
43 import org.onap.so.bpmn.servicedecomposition.homingobjects.CandidateType;
44 import org.onap.so.bpmn.servicedecomposition.homingobjects.SolutionCandidates;
45 import org.onap.so.bpmn.servicedecomposition.homingobjects.SolutionInfo;
46 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoMetadata;
47 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoServiceInstance;
48 import org.onap.so.client.exception.BadResponseException;
49 import org.onap.so.client.exception.ExceptionBuilder;
50 import org.onap.so.client.oof.OofClient;
51 import org.onap.so.client.oof.OofValidator;
52 import org.onap.so.client.oof.beans.ModelInfo;
53 import org.onap.so.client.oof.beans.OofRequest;
54 import org.onap.so.client.oof.beans.OofRequestParameters;
55 import org.onap.so.client.oof.beans.PlacementDemand;
56 import org.onap.so.client.oof.beans.PlacementInfo;
57 import org.onap.so.client.oof.beans.RequestInfo;
58 import org.onap.so.client.oof.beans.ResourceModelInfo;
59 import org.onap.so.client.oof.beans.ServiceInfo;
60 import org.onap.so.client.oof.beans.SubscriberInfo;
61 import org.onap.so.db.catalog.beans.OrchestrationStatus;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64 import org.springframework.beans.factory.annotation.Autowired;
65 import org.springframework.core.env.Environment;
66 import org.springframework.stereotype.Component;
67 import org.springframework.web.util.UriUtils;
68
69 import java.time.Duration;
70 import java.util.List;
71 import java.util.Map;
72 import java.util.UUID;
73
74 import static org.apache.commons.lang3.StringUtils.isBlank;
75 import static org.apache.commons.lang3.StringUtils.isNotBlank;
76
77
78 /**
79  * The oof homing building block obtains licensing and homing solutions for a given
80  * resource or set of resources.
81  *
82  */
83 @Component("OofHoming")
84 public class OofHomingV2 {
85
86     private static final Logger logger = LoggerFactory.getLogger(OofHomingV2.class);
87     private JsonUtils jsonUtils = new JsonUtils();
88     @Autowired
89     private Environment env;
90     @Autowired
91     private OofClient client;
92     @Autowired
93     private OofValidator oofValidator;
94     @Autowired
95     private ExceptionBuilder exceptionUtil;
96     private static final String MODEL_NAME = "modelName";
97     private static final String MODEL_INVARIANT_ID = "modelInvariantId";
98     private static final String MODEL_VERSION_ID = "modelVersionId";
99     private static final String MODEL_VERSION = "modelVersion";
100     private static final String SERVICE_RESOURCE_ID = "serviceResourceId";
101     private static final String RESOURCE_MODULE_NAME = "resourceModuleName";
102     private static final String RESOURCE_MODEL_INFO = "resourceModelInfo";
103     private static final String IDENTIFIER_TYPE = "identifierType";
104     private static final String INVENTORY_TYPE = "inventoryType";
105     private static final String SOLUTIONS = "solutions";
106     private static final String RESOURCE_MISSING_DATA = "Resource does not contain: ";
107     private static final String SERVICE_MISSING_DATA = "Service Instance does not contain: ";
108     private static final String UNPROCESSABLE = "422";
109     private static final int INTERNAL = 500;
110
111     /**
112      * Generates the request payload then sends to Oof to perform homing and
113      * licensing for the provided demands
114      *
115      * @param execution
116      */
117     public void callOof(BuildingBlockExecution execution){
118         logger.trace("Started Sniro Homing Call Sniro");
119         try{
120             GeneralBuildingBlock bb = execution.getGeneralBuildingBlock();
121
122             RequestContext requestContext = bb.getRequestContext();
123             RequestParameters requestParams = requestContext.getRequestParameters();
124             String requestId = requestContext.getMsoRequestId();
125
126             ServiceInstance serviceInstance = bb.getCustomer().getServiceSubscription().getServiceInstances().get(0);
127             Customer customer = bb.getCustomer();
128
129             String timeout = execution.getVariable("timeout");
130             if(isBlank(timeout)){
131                 timeout = env.getProperty("oof.timeout", "PT30M");
132             }
133
134             OofRequest request = new OofRequest();
135
136             RequestInfo requestInfo = (RequestInfo)buildRequestInfo(requestId, timeout);
137             request.setRequestInformation(requestInfo);
138
139             ServiceInfo serviceInfo = buildServiceInfo(serviceInstance);
140             request.setServiceInformation(serviceInfo);
141
142             PlacementInfo placementInfo = buildPlacementInfo(customer, requestParams);
143
144             ArrayList<PlacementDemand> placementDemands = buildPlacementDemands(serviceInstance);
145             placementInfo.setPlacementDemands(placementDemands);
146             request.setPlacementInformation(placementInfo);
147
148             JSONObject licenseInfo = new JSONObject();
149
150             JSONArray licenseDemands = buildLicenseDemands(serviceInstance);
151             licenseInfo.put("licenseDemands", licenseDemands);
152             request.setLicenseInformation(licenseInfo.toString());
153
154             if(placementDemands.size() > 0 || licenseDemands.length() > 0){
155                 client.postDemands(request);
156             }else{
157                 logger.debug(SERVICE_MISSING_DATA + " resources eligible for homing or licensing");
158                 throw new BpmnError(UNPROCESSABLE, SERVICE_MISSING_DATA + " resources eligible for homing or licensing");
159             }
160
161             //Variables for ReceiveWorkflowMessage subflow
162             execution.setVariable("asyncCorrelator", requestId);
163             execution.setVariable("asyncMessageType", "OofResponse");
164             execution.setVariable("asyncTimeout", timeout);
165
166             logger.trace("Completed Oof Homing Call Oof");
167         }catch(BpmnError e){
168             exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(e.getErrorCode()), e.getMessage());
169         }catch(BadResponseException e){
170             exceptionUtil.buildAndThrowWorkflowException(execution, 400, e.getMessage());
171         }catch(Exception e){
172             exceptionUtil.buildAndThrowWorkflowException(execution, INTERNAL, "Internal Error - occurred while preparing oof request: " + e.getMessage());
173         }
174     }
175
176     /**
177      * Validates, processes, and sets the homing and licensing solutions that are returned by
178      * Oof
179      *
180      * @param execution
181      * @param asyncResponse
182      */
183     public void processSolution(BuildingBlockExecution execution, String asyncResponse){
184         logger.trace("Started Oof Homing Process Solution");
185         try{
186             oofValidator.validateSolution(asyncResponse);
187             ServiceInstance serviceInstance = execution.getGeneralBuildingBlock().getCustomer().getServiceSubscription().getServiceInstances().get(0);
188
189             logger.debug("Processing Oof asyncronous response");
190             JSONObject response = new JSONObject(asyncResponse);
191             if(response.has(SOLUTIONS)){
192                 JSONObject allSolutions = response.getJSONObject(SOLUTIONS);
193                 if(allSolutions.has("placementSolutions")){
194                     JSONArray placementSolutions = allSolutions.getJSONArray("placementSolutions");
195                     for(int i = 0; i < placementSolutions.length(); i++){
196                         JSONArray placements = placementSolutions.getJSONArray(i);
197                         processPlacementSolution(serviceInstance, placements, i);
198                     }
199                 }
200                 if(allSolutions.has("licenseSolutions")){
201                     JSONArray licenseSolutions = allSolutions.getJSONArray("licenseSolutions");
202                     if(licenseSolutions.length() > 0){
203                         processLicenseSolution(serviceInstance, licenseSolutions);
204                     }
205                 }
206             }else{
207                 throw new BpmnError(UNPROCESSABLE, "Oof response does not contain: " + SOLUTIONS);
208             }
209
210             execution.setVariable("generalBuildingBlock", execution.getGeneralBuildingBlock());
211
212             logger.trace("Completed Oof Homing Process Solution");
213         }catch(BpmnError e){
214             exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(e.getErrorCode()), e.getMessage());
215         }catch(BadResponseException e){
216             exceptionUtil.buildAndThrowWorkflowException(execution, 400, e.getMessage());
217         }catch(Exception e){
218             exceptionUtil.buildAndThrowWorkflowException(execution, INTERNAL, "Internal Error - occurred while processing Oof asynchronous response: " + e.getMessage());
219         }
220     }
221
222     /**
223      * Builds the request information section for the homing/licensing request
224      *
225      * @throws Exception
226      */
227     private RequestInfo buildRequestInfo(String requestId, String timeout) throws Exception{
228         logger.trace("Building request information");
229         RequestInfo requestInfo = new RequestInfo();
230         if(requestId != null){
231             String host = env.getProperty("mso.workflow.message.endpoint");
232             String callbackUrl = host + "/" + UriUtils.encodePathSegment("OofResponse", "UTF-8") + "/" + UriUtils.encodePathSegment(requestId, "UTF-8");
233
234             Duration d = Duration.parse(timeout);
235             long timeoutSeconds = d.getSeconds();
236
237             requestInfo.setTransactionId(requestId);
238             requestInfo.setRequestId(requestId);
239             requestInfo.setCallbackUrl(callbackUrl);
240             requestInfo.setSourceId("mso");
241             requestInfo.setRequestType("create");
242             requestInfo.setTimeout(timeoutSeconds);
243         } else{
244             throw new BpmnError(UNPROCESSABLE, "Request Context does not contain: requestId");
245         }
246         return requestInfo;
247     }
248
249     /**
250      * Builds the request information section for the homing/licensing request
251      *
252      */
253     private ServiceInfo buildServiceInfo(ServiceInstance serviceInstance){
254         logger.trace("Building service information");
255         ServiceInfo info = new ServiceInfo();
256         ModelInfoServiceInstance modelInfo = serviceInstance.getModelInfoServiceInstance();
257         if(isNotBlank(modelInfo.getModelInvariantUuid()) && isNotBlank(modelInfo.getModelUuid())){
258             info.setServiceInstanceId(serviceInstance.getServiceInstanceId());
259             if(modelInfo.getServiceType() != null && modelInfo.getServiceType().length() > 0){ //temp solution
260                 info.setServiceName(modelInfo.getServiceType());
261             }
262             info.setModelInfo(buildModelInfo(serviceInstance.getModelInfoServiceInstance()));
263         }else{
264             throw new BpmnError(UNPROCESSABLE, SERVICE_MISSING_DATA + MODEL_VERSION_ID + ", " + MODEL_INVARIANT_ID);
265         }
266         return info;
267     }
268
269     /**
270      * Builds initial section of placement info for the homing/licensing request
271      *
272      */
273     private PlacementInfo buildPlacementInfo(Customer customer, RequestParameters requestParams){
274         PlacementInfo placementInfo = new PlacementInfo();
275         if(customer != null){
276             logger.debug("Adding subscriber to placement information");
277             SubscriberInfo subscriberInfo = new SubscriberInfo();
278             subscriberInfo.setGlobalSubscriberId(customer.getGlobalCustomerId());
279             subscriberInfo.setSubscriberName(customer.getSubscriberName());
280             subscriberInfo.setSubscriberCommonSiteId(customer.getSubscriberCommonSiteId());
281             placementInfo.setSubscriberInfo(subscriberInfo);
282             if(requestParams != null){
283                 logger.debug("Adding request parameters to placement information");
284                 OofRequestParameters oofRequestParams = new OofRequestParameters();
285                 for (Map requestParam : requestParams.getUserParams()){
286                     if (requestParam.containsKey("customerLatitude")){
287                         oofRequestParams.setCustomerLatitude(requestParam.get("customerLatitude").toString());
288                     }
289                     if (requestParam.containsKey("customerLongitude")){
290                         oofRequestParams.setCustomerLongitude(requestParam.get("customerLongitude").toString());
291                     }
292                     if (requestParam.containsKey("customerName")){
293                         oofRequestParams.setCustomerName(requestParam.get("customerName").toString());
294                     }
295                 }
296                 placementInfo.setRequestParameters(oofRequestParams);
297             }
298         }else{
299             throw new BpmnError(UNPROCESSABLE, SERVICE_MISSING_DATA + "customer");
300         }
301         return placementInfo;
302
303     }
304
305     /**
306      * Builds the placement demand list for the homing/licensing request
307      *
308      */
309     private ArrayList<PlacementDemand> buildPlacementDemands(ServiceInstance serviceInstance){
310         logger.trace("Building placement information demands");
311         ArrayList<PlacementDemand> placementDemands = new ArrayList();
312
313         List<AllottedResource> allottedResourceList = serviceInstance.getAllottedResources();
314         if(!allottedResourceList.isEmpty()){
315             logger.debug("Adding allotted resources to placement demands list");
316             for(AllottedResource ar : allottedResourceList){
317                 if(isBlank(ar.getId())){
318                     ar.setId(UUID.randomUUID().toString());
319                 }
320                 PlacementDemand demand = buildDemand(ar.getId(), ar.getModelInfoAllottedResource());
321                 //addCandidates(ar, demand);
322                 placementDemands.add(demand);
323             }
324         }
325         List<VpnBondingLink> vpnBondingLinkList = serviceInstance.getVpnBondingLinks();
326         if(!vpnBondingLinkList.isEmpty()){
327             logger.debug("Adding vpn bonding links to placement demands list");
328             for(VpnBondingLink vbl:vpnBondingLinkList){
329                 List<ServiceProxy> serviceProxyList = vbl.getServiceProxies();
330                 for(ServiceProxy sp : serviceProxyList){
331                     if(isBlank(sp.getId())){
332                         sp.setId(UUID.randomUUID().toString());
333                     }
334                     PlacementDemand demand = buildDemand(sp.getId(), sp.getModelInfoServiceProxy());
335                     //addCandidates(sp, demand);
336                     placementDemands.add(demand);
337                 }
338             }
339         }
340         return placementDemands;
341     }
342
343     /**
344      * Builds the license demand list for the homing/licensing request
345      *
346      */
347     private JSONArray buildLicenseDemands(ServiceInstance serviceInstance){
348         logger.trace("Building license information");
349         JSONArray licenseDemands = new JSONArray();
350         List<GenericVnf> vnfList = serviceInstance.getVnfs();
351         if(!vnfList.isEmpty()){
352             logger.debug("Adding vnfs to license demands list");
353             for(GenericVnf vnf : vnfList){
354                 JSONObject demand = buildLicenseDemand(vnf.getVnfId(), vnf.getModelInfoGenericVnf());
355                 licenseDemands.put(demand);
356             }
357         }
358         return licenseDemands;
359     }
360
361     /**
362      * Builds a single license demand object
363      *
364      */
365     private JSONObject buildLicenseDemand(String id, ModelInfoMetadata metadata){
366         logger.debug("Building demand for service or resource: " + id);
367         JSONObject demand = new JSONObject();
368         if(isNotBlank(id) && isNotBlank(metadata.getModelInstanceName())){
369             demand.put(SERVICE_RESOURCE_ID, id);
370             demand.put(RESOURCE_MODULE_NAME, metadata.getModelInstanceName());
371             demand.put(RESOURCE_MODEL_INFO, buildModelInfo(metadata));
372         }else{
373             throw new BpmnError(UNPROCESSABLE, RESOURCE_MISSING_DATA + "modelInstanceName");
374         }
375         return demand;
376     }
377
378     /**
379      * Builds a single demand object
380      *
381      */
382     private PlacementDemand buildDemand(String id, ModelInfoMetadata metadata){
383         logger.debug("Building demand for service or resource: " + id);
384         PlacementDemand placementDemand = new PlacementDemand();
385         if(isNotBlank(id) && isNotBlank(metadata.getModelInstanceName())){
386             placementDemand.setServiceResourceId(id);
387             placementDemand.setResourceModuleName(metadata.getModelInstanceName());
388             placementDemand.setResourceModelInfo((ResourceModelInfo) buildModelInfo(metadata));
389         }else{
390             throw new BpmnError(UNPROCESSABLE, RESOURCE_MISSING_DATA + "modelInstanceName");
391         }
392         return placementDemand;
393     }
394
395     /**
396      * Builds the resource model info section
397      *
398      */
399     private ModelInfo buildModelInfo(ModelInfoMetadata metadata){
400         ModelInfo modelInfo = new ModelInfo();
401         String invariantUuid = metadata.getModelInvariantUuid();
402         String modelUuid = metadata.getModelUuid();
403         if(isNotBlank(invariantUuid) && isNotBlank(modelUuid)){
404             modelInfo.setModelInvariantId(invariantUuid);
405             modelInfo.setModelVersionId(modelUuid);
406             modelInfo.setModelName(metadata.getModelName());
407             modelInfo.setModelVersion(metadata.getModelVersion());
408         }else if(isNotBlank(invariantUuid)){
409             throw new BpmnError(UNPROCESSABLE, RESOURCE_MISSING_DATA + MODEL_VERSION_ID);
410         }else{
411             throw new BpmnError(UNPROCESSABLE, RESOURCE_MISSING_DATA + MODEL_INVARIANT_ID);
412         }
413         return modelInfo;
414     }
415
416     /**
417      * Adds required, excluded, and existing candidates to a demand
418      *
419      */
420     private void addCandidates(SolutionCandidates candidates, JSONObject demand){
421         List<Candidate> required = candidates.getRequiredCandidates();
422         List<Candidate> excluded = candidates.getExcludedCandidates();
423         if(!required.isEmpty()){
424             demand.put("requiredCandidates", required);
425         }
426         if(!excluded.isEmpty()){
427             demand.put("excludedCandidates", excluded);
428         }
429         //TODO support existing candidates
430     }
431
432     /**
433      * Processes the license solutions and sets to the corresponding generic vnf
434      *
435      */
436     private void processLicenseSolution(ServiceInstance serviceInstance, JSONArray licenseSolutions){
437         List<GenericVnf> vnfs = serviceInstance.getVnfs();
438
439         logger.debug("Processing the license solution");
440         for(int i = 0; i < licenseSolutions.length(); i++){
441             JSONObject licenseSolution = licenseSolutions.getJSONObject(i);
442             for(GenericVnf vnf:vnfs){
443                 if(licenseSolution.getString(SERVICE_RESOURCE_ID).equals(vnf.getVnfId())){
444                     License license = new License();
445                     JSONArray entitlementPools = licenseSolution.getJSONArray("entitlementPoolUUID");
446                     List<String> entitlementPoolsList = jsonUtils.StringArrayToList(entitlementPools);
447                     license.setEntitlementPoolUuids(entitlementPoolsList);
448                     JSONArray licenseKeys = licenseSolution.getJSONArray("licenseKeyGroupUUID");
449                     List<String> licenseKeysList = jsonUtils.StringArrayToList(licenseKeys);
450                     license.setLicenseKeyGroupUuids(licenseKeysList);
451
452                     vnf.setLicense(license);
453                 }
454             }
455         }
456     }
457
458     /**
459      * Processes a placement solution list then correlates and sets each placement solution
460      * to its corresponding resource
461      *
462      */
463     private void processPlacementSolution(ServiceInstance serviceInstance, JSONArray placements, int i){
464         List<VpnBondingLink> links = serviceInstance.getVpnBondingLinks();
465         List<AllottedResource> allottes = serviceInstance.getAllottedResources();
466         List<GenericVnf> vnfs = serviceInstance.getVnfs();
467
468         logger.debug("Processing placement solution " + i+1);
469         for(int p = 0; p < placements.length(); p++){
470             JSONObject placement = placements.getJSONObject(p);
471             SolutionInfo solutionInfo = new SolutionInfo();
472             solutionInfo.setSolutionId(i + 1);
473             search: {
474                 for(VpnBondingLink vbl:links){
475                     List<ServiceProxy> proxies = vbl.getServiceProxies();
476                     for(ServiceProxy sp:proxies){
477                         if(placement.getString(SERVICE_RESOURCE_ID).equals(sp.getId())){
478                             if(i > 0){
479                                 if(p % 2 == 0){
480                                     VpnBondingLink vblNew = (VpnBondingLink) SerializationUtils.clone(vbl);
481                                     vblNew.setVpnBondingLinkId(UUID.randomUUID().toString());
482                                     links.add(vblNew);
483                                 }
484                                 links.get(links.size() - 1).getServiceProxy(sp.getId()).setServiceInstance(setSolution(solutionInfo, placement));
485                             }else{
486                                 sp.setServiceInstance(setSolution(solutionInfo, placement));
487                             }
488                             break search;
489                         }
490                     }
491                 }
492                 for(AllottedResource ar:allottes){
493                     if(placement.getString(SERVICE_RESOURCE_ID).equals(ar.getId())){
494                         ar.setParentServiceInstance(setSolution(solutionInfo, placement));
495                         break search;
496                     }
497                 }
498                 for(GenericVnf vnf:vnfs){
499                     if(placement.getString(SERVICE_RESOURCE_ID).equals(vnf.getVnfId())){
500                         ServiceInstance si = setSolution(solutionInfo, placement);
501                         serviceInstance.setSolutionInfo(si.getSolutionInfo());
502                         serviceInstance.getVnfs().add(si.getVnfs().get(0));
503                         break search;
504                     }
505                 }
506             }
507         }
508     }
509
510
511     /**
512      * Creates and sets necessary pojos with placement solution data for a given demand
513      *
514      */
515     private ServiceInstance setSolution(SolutionInfo solutionInfo, JSONObject placement){
516         logger.debug("Mapping placement solution");
517         String invalidMessage = "Oof Response contains invalid: ";
518
519         JSONObject solution = placement.getJSONObject("solution");
520         String identifierType = solution.getString(IDENTIFIER_TYPE);
521         List<String> identifiersList = jsonUtils.StringArrayToList(solution.getJSONArray("identifiers").toString());
522         String identifierValue = identifiersList.get(0);
523
524         JSONArray assignments = placement.getJSONArray("assignmentInfo");
525         Map<String, String> assignmentsMap = jsonUtils.entryArrayToMap(assignments.toString(), "key", "value");
526         solutionInfo.setRehome(Boolean.parseBoolean(assignmentsMap.get("isRehome")));
527         String type = placement.getString(INVENTORY_TYPE);
528
529         ServiceInstance si = new ServiceInstance();
530         CloudRegion cloud = setCloud(assignmentsMap);
531         if(type.equals("service")){
532             if(identifierType.equals(CandidateType.SERVICE_INSTANCE_ID.toString())){
533                 solutionInfo.setHomed(true);
534                 si.setServiceInstanceId(identifierValue);
535                 si.setOrchestrationStatus(OrchestrationStatus.CREATED);
536                 cloud.setLcpCloudRegionId(assignmentsMap.get("cloudRegionId"));
537                 if(assignmentsMap.containsKey("vnfHostName")){
538                     logger.debug("Resources has been homed to a vnf");
539                     GenericVnf vnf = setVnf(assignmentsMap);
540                     vnf.setCloudRegion(cloud);
541                     si.getVnfs().add(vnf);
542
543                 }else if(assignmentsMap.containsKey("primaryPnfName")){
544                     logger.debug("Resources has been homed to a pnf");
545                     Pnf priPnf = setPnf(assignmentsMap, "primary");
546                     priPnf.setCloudRegion(cloud);
547                     si.getPnfs().add(priPnf);
548                     if(assignmentsMap.containsKey("secondaryPnfName")){
549                         Pnf secPnf = setPnf(assignmentsMap, "secondary");
550                         secPnf.setCloudRegion(cloud);
551                         si.getPnfs().add(secPnf);
552                     }
553                 }
554             }else{
555                 logger.debug(invalidMessage + IDENTIFIER_TYPE);
556                 throw new BpmnError(UNPROCESSABLE, invalidMessage + IDENTIFIER_TYPE);
557             }
558         }else if(type.equals("cloud")){
559             if(identifierType.equals(CandidateType.CLOUD_REGION_ID.toString())){
560                 logger.debug("Resources has been homed to a cloud region");
561                 cloud.setLcpCloudRegionId(identifierValue);
562                 solutionInfo.setHomed(false);
563                 solutionInfo.setTargetedCloudRegion(cloud);
564                 si.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
565             }else{
566                 logger.debug(invalidMessage + IDENTIFIER_TYPE);
567                 throw new BpmnError(UNPROCESSABLE, invalidMessage + IDENTIFIER_TYPE);
568             }
569         }else{
570             logger.debug(invalidMessage + INVENTORY_TYPE);
571             throw new BpmnError(UNPROCESSABLE, invalidMessage + INVENTORY_TYPE);
572         }
573         si.setSolutionInfo(solutionInfo);
574         return si;
575     }
576
577     /**
578      * Sets the cloud data to a cloud region object
579      *
580      */
581     private CloudRegion setCloud(Map<String, String> assignmentsMap){
582         CloudRegion cloud = new CloudRegion();
583         cloud.setCloudOwner(assignmentsMap.get("cloudOwner"));
584         cloud.setCloudRegionVersion(assignmentsMap.get("aicVersion"));
585         cloud.setComplex(assignmentsMap.get("aicClli"));
586         return cloud;
587     }
588
589     /**
590      * Sets the vnf data to a generic vnf object
591      *
592      */
593     private GenericVnf setVnf(Map<String, String> assignmentsMap){
594         GenericVnf vnf = new GenericVnf();
595         vnf.setOrchestrationStatus(OrchestrationStatus.CREATED);
596         vnf.setVnfName(assignmentsMap.get("vnfHostName"));
597         vnf.setVnfId(assignmentsMap.get("vnfId"));
598         return vnf;
599     }
600
601     /**
602      * Sets the pnf data to a pnf object
603      *
604      */
605     private Pnf setPnf(Map<String, String> assignmentsMap, String role){
606         Pnf pnf = new Pnf();
607         pnf.setRole(role);
608         pnf.setOrchestrationStatus(OrchestrationStatus.CREATED);
609         pnf.setPnfName(assignmentsMap.get(role + "PnfName"));
610         return pnf;
611     }
612
613
614
615 }