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