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