Replaced all tabs with spaces in java and pom.xml
[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 import java.time.Duration;
69 import java.util.List;
70 import java.util.Map;
71 import java.util.UUID;
72 import static org.apache.commons.lang3.StringUtils.isBlank;
73 import static org.apache.commons.lang3.StringUtils.isNotBlank;
74
75
76 /**
77  * The oof homing building block obtains licensing and homing solutions for a given resource or set of resources.
78  *
79  */
80 @Component("OofHoming")
81 public class OofHomingV2 {
82
83     private static final Logger logger = LoggerFactory.getLogger(OofHomingV2.class);
84     private JsonUtils jsonUtils = new JsonUtils();
85     @Autowired
86     private Environment env;
87     @Autowired
88     private OofClient client;
89     @Autowired
90     private OofValidator oofValidator;
91     @Autowired
92     private ExceptionBuilder exceptionUtil;
93     private static final String MODEL_NAME = "modelName";
94     private static final String MODEL_INVARIANT_ID = "modelInvariantId";
95     private static final String MODEL_VERSION_ID = "modelVersionId";
96     private static final String MODEL_VERSION = "modelVersion";
97     private static final String SERVICE_RESOURCE_ID = "serviceResourceId";
98     private static final String RESOURCE_MODULE_NAME = "resourceModuleName";
99     private static final String RESOURCE_MODEL_INFO = "resourceModelInfo";
100     private static final String IDENTIFIER_TYPE = "identifierType";
101     private static final String INVENTORY_TYPE = "inventoryType";
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 Sniro Homing Call Sniro");
115         try {
116             GeneralBuildingBlock bb = execution.getGeneralBuildingBlock();
117
118             RequestContext requestContext = bb.getRequestContext();
119             RequestParameters requestParams = requestContext.getRequestParameters();
120             String requestId = requestContext.getMsoRequestId();
121
122             ServiceInstance serviceInstance = bb.getCustomer().getServiceSubscription().getServiceInstances().get(0);
123             Customer customer = bb.getCustomer();
124
125             String timeout = execution.getVariable("timeout");
126             if (isBlank(timeout)) {
127                 timeout = env.getProperty("oof.timeout", "PT30M");
128             }
129
130             OofRequest request = new OofRequest();
131
132             RequestInfo requestInfo = (RequestInfo) buildRequestInfo(requestId, timeout);
133             request.setRequestInformation(requestInfo);
134
135             ServiceInfo serviceInfo = buildServiceInfo(serviceInstance);
136             request.setServiceInformation(serviceInfo);
137
138             PlacementInfo placementInfo = buildPlacementInfo(customer, requestParams);
139
140             ArrayList<PlacementDemand> placementDemands = buildPlacementDemands(serviceInstance);
141             placementInfo.setPlacementDemands(placementDemands);
142             request.setPlacementInformation(placementInfo);
143
144             JSONObject licenseInfo = new JSONObject();
145
146             JSONArray licenseDemands = buildLicenseDemands(serviceInstance);
147             licenseInfo.put("licenseDemands", licenseDemands);
148             request.setLicenseInformation(licenseInfo.toString());
149
150             if (placementDemands.size() > 0 || licenseDemands.length() > 0) {
151                 client.postDemands(request);
152             } else {
153                 logger.debug(SERVICE_MISSING_DATA + " resources eligible for homing or licensing");
154                 throw new BpmnError(UNPROCESSABLE,
155                         SERVICE_MISSING_DATA + " resources eligible for homing or licensing");
156             }
157
158             // Variables for ReceiveWorkflowMessage subflow
159             execution.setVariable("asyncCorrelator", requestId);
160             execution.setVariable("asyncMessageType", "OofResponse");
161             execution.setVariable("asyncTimeout", timeout);
162
163             logger.trace("Completed Oof Homing Call Oof");
164         } catch (BpmnError e) {
165             exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(e.getErrorCode()), e.getMessage());
166         } catch (BadResponseException e) {
167             exceptionUtil.buildAndThrowWorkflowException(execution, 400, e.getMessage());
168         } catch (Exception e) {
169             exceptionUtil.buildAndThrowWorkflowException(execution, INTERNAL,
170                     "Internal Error - occurred while preparing oof request: " + e.getMessage());
171         }
172     }
173
174     /**
175      * Validates, processes, and sets the homing and licensing solutions that are returned by Oof
176      *
177      * @param execution
178      * @param asyncResponse
179      */
180     public void processSolution(BuildingBlockExecution execution, String asyncResponse) {
181         logger.trace("Started Oof Homing Process Solution");
182         try {
183             oofValidator.validateSolution(asyncResponse);
184             ServiceInstance serviceInstance = execution.getGeneralBuildingBlock().getCustomer().getServiceSubscription()
185                     .getServiceInstances().get(0);
186
187             logger.debug("Processing Oof asyncronous response");
188             JSONObject response = new JSONObject(asyncResponse);
189             if (response.has(SOLUTIONS)) {
190                 JSONObject allSolutions = response.getJSONObject(SOLUTIONS);
191                 if (allSolutions.has("placementSolutions")) {
192                     JSONArray placementSolutions = allSolutions.getJSONArray("placementSolutions");
193                     for (int i = 0; i < placementSolutions.length(); i++) {
194                         JSONArray placements = placementSolutions.getJSONArray(i);
195                         processPlacementSolution(serviceInstance, placements, i);
196                     }
197                 }
198                 if (allSolutions.has("licenseSolutions")) {
199                     JSONArray licenseSolutions = allSolutions.getJSONArray("licenseSolutions");
200                     if (licenseSolutions.length() > 0) {
201                         processLicenseSolution(serviceInstance, licenseSolutions);
202                     }
203                 }
204             } else {
205                 throw new BpmnError(UNPROCESSABLE, "Oof response does not contain: " + SOLUTIONS);
206             }
207
208             execution.setVariable("generalBuildingBlock", execution.getGeneralBuildingBlock());
209
210             logger.trace("Completed Oof Homing Process Solution");
211         } catch (BpmnError e) {
212             exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(e.getErrorCode()), e.getMessage());
213         } catch (BadResponseException e) {
214             exceptionUtil.buildAndThrowWorkflowException(execution, 400, e.getMessage());
215         } catch (Exception e) {
216             exceptionUtil.buildAndThrowWorkflowException(execution, INTERNAL,
217                     "Internal Error - occurred while processing Oof asynchronous response: " + e.getMessage());
218         }
219     }
220
221     /**
222      * Builds the request information section for the homing/licensing request
223      *
224      * @throws Exception
225      */
226     private RequestInfo buildRequestInfo(String requestId, String timeout) throws Exception {
227         logger.trace("Building request information");
228         RequestInfo requestInfo = new RequestInfo();
229         if (requestId != null) {
230             String host = env.getProperty("mso.workflow.message.endpoint");
231             String callbackUrl = host + "/" + UriUtils.encodePathSegment("OofResponse", "UTF-8") + "/"
232                     + 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 to its corresponding
460      * 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())
485                                         .setServiceInstance(setSolution(solutionInfo, placement));
486                             } else {
487                                 sp.setServiceInstance(setSolution(solutionInfo, placement));
488                             }
489                             break search;
490                         }
491                     }
492                 }
493                 for (AllottedResource ar : allottes) {
494                     if (placement.getString(SERVICE_RESOURCE_ID).equals(ar.getId())) {
495                         ar.setParentServiceInstance(setSolution(solutionInfo, placement));
496                         break search;
497                     }
498                 }
499                 for (GenericVnf vnf : vnfs) {
500                     if (placement.getString(SERVICE_RESOURCE_ID).equals(vnf.getVnfId())) {
501                         ServiceInstance si = setSolution(solutionInfo, placement);
502                         serviceInstance.setSolutionInfo(si.getSolutionInfo());
503                         serviceInstance.getVnfs().add(si.getVnfs().get(0));
504                         break search;
505                     }
506                 }
507             }
508         }
509     }
510
511
512     /**
513      * Creates and sets necessary pojos with placement solution data for a given demand
514      *
515      */
516     private ServiceInstance setSolution(SolutionInfo solutionInfo, JSONObject placement) {
517         logger.debug("Mapping placement solution");
518         String invalidMessage = "Oof Response contains invalid: ";
519
520         JSONObject solution = placement.getJSONObject("solution");
521         String identifierType = solution.getString(IDENTIFIER_TYPE);
522         List<String> identifiersList = jsonUtils.StringArrayToList(solution.getJSONArray("identifiers").toString());
523         String identifierValue = identifiersList.get(0);
524
525         JSONArray assignments = placement.getJSONArray("assignmentInfo");
526         Map<String, String> assignmentsMap = jsonUtils.entryArrayToMap(assignments.toString(), "key", "value");
527         solutionInfo.setRehome(Boolean.parseBoolean(assignmentsMap.get("isRehome")));
528         String type = placement.getString(INVENTORY_TYPE);
529
530         ServiceInstance si = new ServiceInstance();
531         CloudRegion cloud = setCloud(assignmentsMap);
532         if (type.equals("service")) {
533             if (identifierType.equals(CandidateType.SERVICE_INSTANCE_ID.toString())) {
534                 solutionInfo.setHomed(true);
535                 si.setServiceInstanceId(identifierValue);
536                 si.setOrchestrationStatus(OrchestrationStatus.CREATED);
537                 cloud.setLcpCloudRegionId(assignmentsMap.get("cloudRegionId"));
538                 if (assignmentsMap.containsKey("vnfHostName")) {
539                     logger.debug("Resources has been homed to a vnf");
540                     GenericVnf vnf = setVnf(assignmentsMap);
541                     vnf.setCloudRegion(cloud);
542                     si.getVnfs().add(vnf);
543
544                 } else if (assignmentsMap.containsKey("primaryPnfName")) {
545                     logger.debug("Resources has been homed to a pnf");
546                     Pnf priPnf = setPnf(assignmentsMap, "primary");
547                     priPnf.setCloudRegion(cloud);
548                     si.getPnfs().add(priPnf);
549                     if (assignmentsMap.containsKey("secondaryPnfName")) {
550                         Pnf secPnf = setPnf(assignmentsMap, "secondary");
551                         secPnf.setCloudRegion(cloud);
552                         si.getPnfs().add(secPnf);
553                     }
554                 }
555             } else {
556                 logger.debug(invalidMessage + IDENTIFIER_TYPE);
557                 throw new BpmnError(UNPROCESSABLE, invalidMessage + IDENTIFIER_TYPE);
558             }
559         } else if (type.equals("cloud")) {
560             if (identifierType.equals(CandidateType.CLOUD_REGION_ID.toString())) {
561                 logger.debug("Resources has been homed to a cloud region");
562                 cloud.setLcpCloudRegionId(identifierValue);
563                 solutionInfo.setHomed(false);
564                 solutionInfo.setTargetedCloudRegion(cloud);
565                 si.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
566             } else {
567                 logger.debug(invalidMessage + IDENTIFIER_TYPE);
568                 throw new BpmnError(UNPROCESSABLE, invalidMessage + IDENTIFIER_TYPE);
569             }
570         } else {
571             logger.debug(invalidMessage + INVENTORY_TYPE);
572             throw new BpmnError(UNPROCESSABLE, invalidMessage + INVENTORY_TYPE);
573         }
574         si.setSolutionInfo(solutionInfo);
575         return si;
576     }
577
578     /**
579      * Sets the cloud data to a cloud region object
580      *
581      */
582     private CloudRegion setCloud(Map<String, String> assignmentsMap) {
583         CloudRegion cloud = new CloudRegion();
584         cloud.setCloudOwner(assignmentsMap.get("cloudOwner"));
585         cloud.setCloudRegionVersion(assignmentsMap.get("aicVersion"));
586         cloud.setComplex(assignmentsMap.get("aicClli"));
587         return cloud;
588     }
589
590     /**
591      * Sets the vnf data to a generic vnf object
592      *
593      */
594     private GenericVnf setVnf(Map<String, String> assignmentsMap) {
595         GenericVnf vnf = new GenericVnf();
596         vnf.setOrchestrationStatus(OrchestrationStatus.CREATED);
597         vnf.setVnfName(assignmentsMap.get("vnfHostName"));
598         vnf.setVnfId(assignmentsMap.get("vnfId"));
599         return vnf;
600     }
601
602     /**
603      * Sets the pnf data to a pnf object
604      *
605      */
606     private Pnf setPnf(Map<String, String> assignmentsMap, String role) {
607         Pnf pnf = new Pnf();
608         pnf.setRole(role);
609         pnf.setOrchestrationStatus(OrchestrationStatus.CREATED);
610         pnf.setPnfName(assignmentsMap.get(role + "PnfName"));
611         return pnf;
612     }
613
614
615
616 }