2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.buildingblock;
23 import static org.apache.commons.lang3.StringUtils.*;
25 import java.time.Duration;
26 import java.util.List;
28 import java.util.UUID;
30 import org.apache.commons.lang.SerializationUtils;
31 import org.camunda.bpm.engine.delegate.BpmnError;
32 import org.json.JSONArray;
33 import org.json.JSONObject;
34 import org.onap.so.bpmn.common.BuildingBlockExecution;
35 import org.onap.so.bpmn.core.json.JsonUtils;
36 import org.onap.so.bpmn.servicedecomposition.bbobjects.AllottedResource;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceProxy;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.VpnBondingLink;
44 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
45 import org.onap.so.bpmn.servicedecomposition.generalobjects.License;
46 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
47 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
48 import org.onap.so.bpmn.servicedecomposition.homingobjects.Candidate;
49 import org.onap.so.bpmn.servicedecomposition.homingobjects.CandidateType;
50 import org.onap.so.bpmn.servicedecomposition.homingobjects.SolutionCandidates;
51 import org.onap.so.bpmn.servicedecomposition.homingobjects.SolutionInfo;
52 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoMetadata;
53 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoServiceInstance;
54 import org.onap.so.client.exception.BadResponseException;
55 import org.onap.so.client.exception.ExceptionBuilder;
57 import org.onap.so.client.sniro.SniroClient;
58 import static org.onap.so.client.sniro.SniroValidator.*;
60 import org.onap.so.client.sniro.beans.SniroManagerRequest;
61 import org.onap.so.db.catalog.beans.OrchestrationStatus;
62 import org.onap.so.logger.MsoLogger;
63 import org.springframework.beans.factory.annotation.Autowired;
64 import org.springframework.core.env.Environment;
65 import org.springframework.stereotype.Component;
66 import org.springframework.web.util.UriUtils;
70 * The sniro homing building block obtains licensing and homing solutions for a given
71 * resource or set of resources.
76 @Component("SniroHoming")
77 public class SniroHomingV2 {
79 private static final MsoLogger log = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SniroHomingV2.class);
80 private JsonUtils jsonUtils = new JsonUtils();
82 private Environment env;
84 private SniroClient client;
86 private ExceptionBuilder exceptionUtil;
87 private static final String MODEL_NAME = "modelName";
88 private static final String MODEL_INVARIANT_ID = "modelInvariantId";
89 private static final String MODEL_VERSION_ID = "modelVersionId";
90 private static final String MODEL_VERSION = "modelVersion";
91 private static final String SERVICE_RESOURCE_ID = "serviceResourceId";
92 private static final String RESOURCE_MODULE_NAME = "resourceModuleName";
93 private static final String RESOURCE_MODEL_INFO = "resourceModelInfo";
94 private static final String IDENTIFIER_TYPE = "identifierType";
95 private static final String INVENTORY_TYPE = "inventoryType";
96 private static final String SOLUTIONS = "solutions";
97 private static final String RESOURCE_MISSING_DATA = "Resource does not contain: ";
98 private static final String SERVICE_MISSING_DATA = "Service Instance does not contain: ";
99 private static final String UNPROCESSABLE = "422";
100 private static final int INTERNAL = 500;
103 * Generates the request payload then sends to sniro manager to perform homing and
104 * licensing for the provided demands
108 public void callSniro(BuildingBlockExecution execution){
109 log.trace("Started Sniro Homing Call Sniro");
111 GeneralBuildingBlock bb = execution.getGeneralBuildingBlock();
113 RequestContext requestContext = bb.getRequestContext();
114 RequestParameters requestParams = requestContext.getRequestParameters();
115 String requestId = requestContext.getMsoRequestId();
117 ServiceInstance serviceInstance = bb.getCustomer().getServiceSubscription().getServiceInstances().get(0);
118 Customer customer = bb.getCustomer();
120 String timeout = execution.getVariable("timeout");
121 if(isBlank(timeout)){
122 timeout = env.getProperty("sniro.manager.timeout", "PT30M");
125 SniroManagerRequest request = new SniroManagerRequest(); //TODO Add additional pojos for each section
127 JSONObject requestInfo = buildRequestInfo(requestId, timeout);
128 request.setRequestInformation(requestInfo.toString());
130 JSONObject serviceInfo = buildServiceInfo(serviceInstance);
131 request.setServiceInformation(serviceInfo.toString());
133 JSONObject placementInfo = buildPlacementInfo(customer, requestParams);
135 JSONArray placementDemands = buildPlacementDemands(serviceInstance);
136 placementInfo.put("placementDemands", placementDemands);
137 request.setPlacementInformation(placementInfo.toString());
139 JSONObject licenseInfo = new JSONObject();
141 JSONArray licenseDemands = buildLicenseDemands(serviceInstance);
142 licenseInfo.put("licenseDemands", licenseDemands);
143 request.setLicenseInformation(licenseInfo.toString());
145 if(placementDemands.length() > 0 || licenseDemands.length() > 0){
146 client.postDemands(request);
148 log.debug(SERVICE_MISSING_DATA + "resources eligible for homing or licensing");
149 throw new BpmnError(UNPROCESSABLE, SERVICE_MISSING_DATA + "resources eligible for homing or licensing");
152 //Variables for ReceiveWorkflowMessage subflow
153 execution.setVariable("asyncCorrelator", requestId);
154 execution.setVariable("asyncMessageType", "SNIROResponse");
155 execution.setVariable("asyncTimeout", timeout);
157 log.trace("Completed Sniro Homing Call Sniro");
159 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(e.getErrorCode()), e.getMessage());
160 }catch(BadResponseException e){
161 exceptionUtil.buildAndThrowWorkflowException(execution, 400, e.getMessage());
163 exceptionUtil.buildAndThrowWorkflowException(execution, INTERNAL, "Internal Error - occurred while preparing sniro request: " + e.getMessage());
168 * Validates, processes, and sets the homing and licensing solutions that are returned by
172 * @param asyncResponse
174 public void processSolution(BuildingBlockExecution execution, String asyncResponse){
175 log.trace("Started Sniro Homing Process Solution");
177 //TODO improve handling multiple solutions but is dependent on sniro enhancing api + work with sniro conductor to improve "inventoryType" representation
178 validateSolution(asyncResponse);
179 ServiceInstance serviceInstance = execution.getGeneralBuildingBlock().getCustomer().getServiceSubscription().getServiceInstances().get(0);
181 log.debug("Processing sniro manager asyncronous response");
182 JSONObject response = new JSONObject(asyncResponse);
183 if(response.has(SOLUTIONS)){
184 JSONObject allSolutions = response.getJSONObject(SOLUTIONS);
185 if(allSolutions.has("placementSolutions")){
186 JSONArray placementSolutions = allSolutions.getJSONArray("placementSolutions");
187 for(int i = 0; i < placementSolutions.length(); i++){
188 JSONArray placements = placementSolutions.getJSONArray(i);
189 processPlacementSolution(serviceInstance, placements, i);
192 if(allSolutions.has("licenseSolutions")){
193 JSONArray licenseSolutions = allSolutions.getJSONArray("licenseSolutions");
194 if(licenseSolutions.length() > 0){
195 processLicenseSolution(serviceInstance, licenseSolutions);
199 throw new BpmnError(UNPROCESSABLE, "Sniro Managers response does not contain: " + SOLUTIONS);
202 execution.setVariable("generalBuildingBlock", execution.getGeneralBuildingBlock());
204 log.trace("Completed Sniro Homing Process Solution");
206 exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(e.getErrorCode()), e.getMessage());
207 }catch(BadResponseException e){
208 exceptionUtil.buildAndThrowWorkflowException(execution, 400, e.getMessage());
210 exceptionUtil.buildAndThrowWorkflowException(execution, INTERNAL, "Internal Error - occurred while processing sniro asynchronous response: " + e.getMessage());
215 * Builds the request information section for the homing/licensing request
219 private JSONObject buildRequestInfo(String requestId, String timeout) throws Exception{
220 log.trace("Building request information");
221 JSONObject requestInfo = new JSONObject();
222 if(requestId != null){
223 String host = env.getProperty("mso.workflow.message.endpoint");
224 String callbackUrl = host + "/" + UriUtils.encodePathSegment("SNIROResponse", "UTF-8") + "/" + UriUtils.encodePathSegment(requestId, "UTF-8");
226 Duration d = Duration.parse(timeout);
227 long timeoutSeconds = d.getSeconds();
229 requestInfo.put("transactionId", requestId).put("requestId", requestId).put("callbackUrl", callbackUrl).put("sourceId", "mso").put("requestType", "create")
230 .put("timeout", timeoutSeconds);
232 throw new BpmnError(UNPROCESSABLE, "Request Context does not contain: requestId");
238 * Builds the request information section for the homing/licensing request
241 private JSONObject buildServiceInfo(ServiceInstance serviceInstance){
242 log.trace("Building service information");
243 JSONObject info = new JSONObject();
244 ModelInfoServiceInstance modelInfo = serviceInstance.getModelInfoServiceInstance();
245 if(isNotBlank(modelInfo.getModelInvariantUuid()) && isNotBlank(modelInfo.getModelUuid())){
246 info.put("serviceInstanceId", serviceInstance.getServiceInstanceId());
247 if(modelInfo.getServiceType() != null && modelInfo.getServiceType().length() > 0){ //temp solution
248 info.put("serviceName", modelInfo.getServiceType());
250 info.put("modelInfo", buildModelInfo(serviceInstance.getModelInfoServiceInstance()));
252 throw new BpmnError(UNPROCESSABLE, SERVICE_MISSING_DATA + MODEL_VERSION_ID + ", " + MODEL_INVARIANT_ID);
258 * Builds initial section of placement info for the homing/licensing request
261 private JSONObject buildPlacementInfo(Customer customer, RequestParameters requestParams){
262 JSONObject placementInfo = new JSONObject();
263 if(customer != null){
264 log.debug("Adding subscriber to placement information");
265 placementInfo.put("subscriberInfo", new JSONObject().put("globalSubscriberId", customer.getGlobalCustomerId()).put("subscriberName", customer.getSubscriberName()).put("subscriberCommonSiteId", customer.getSubscriberCommonSiteId()));
266 if(requestParams != null){
267 log.debug("Adding request parameters to placement information");
268 placementInfo.put("requestParameters", new JSONObject(requestParams.toJsonString()));
271 throw new BpmnError(UNPROCESSABLE, SERVICE_MISSING_DATA + "customer");
273 return placementInfo;
278 * Builds the placement demand list for the homing/licensing request
281 private JSONArray buildPlacementDemands(ServiceInstance serviceInstance){
282 log.trace("Building placement information demands");
283 JSONArray placementDemands = new JSONArray();
285 List<AllottedResource> allottedResourceList = serviceInstance.getAllottedResources();
286 if(!allottedResourceList.isEmpty()){
287 log.debug("Adding allotted resources to placement demands list");
288 for(AllottedResource ar : allottedResourceList){
289 if(isBlank(ar.getId())){
290 ar.setId(UUID.randomUUID().toString());
292 JSONObject demand = buildDemand(ar.getId(), ar.getModelInfoAllottedResource());
293 addCandidates(ar, demand);
294 placementDemands.put(demand);
297 List<VpnBondingLink> vpnBondingLinkList = serviceInstance.getVpnBondingLinks();
298 if(!vpnBondingLinkList.isEmpty()){
299 log.debug("Adding vpn bonding links to placement demands list");
300 for(VpnBondingLink vbl:vpnBondingLinkList){
301 List<ServiceProxy> serviceProxyList = vbl.getServiceProxies();
302 for(ServiceProxy sp : serviceProxyList){
303 if(isBlank(sp.getId())){
304 sp.setId(UUID.randomUUID().toString());
306 JSONObject demand = buildDemand(sp.getId(), sp.getModelInfoServiceProxy());
307 addCandidates(sp, demand);
308 placementDemands.put(demand);
312 return placementDemands;
316 * Builds the license demand list for the homing/licensing request
319 private JSONArray buildLicenseDemands(ServiceInstance serviceInstance){
320 log.trace("Building license information");
321 JSONArray licenseDemands = new JSONArray();
322 List<GenericVnf> vnfList = serviceInstance.getVnfs();
323 if(!vnfList.isEmpty()){
324 log.debug("Adding vnfs to license demands list");
325 for(GenericVnf vnf : vnfList){
326 JSONObject demand = buildDemand(vnf.getVnfId(), vnf.getModelInfoGenericVnf());
327 licenseDemands.put(demand);
330 return licenseDemands;
334 * Builds a single demand object
337 private JSONObject buildDemand(String id, ModelInfoMetadata metadata){
338 log.debug("Building demand for service or resource: " + id);
339 JSONObject demand = new JSONObject();
340 if(isNotBlank(id) && isNotBlank(metadata.getModelInstanceName())){
341 demand.put(SERVICE_RESOURCE_ID, id);
342 demand.put(RESOURCE_MODULE_NAME, metadata.getModelInstanceName());
343 demand.put(RESOURCE_MODEL_INFO, buildModelInfo(metadata));
345 throw new BpmnError(UNPROCESSABLE, RESOURCE_MISSING_DATA + "modelInstanceName");
351 * Builds the resource model info section
354 private JSONObject buildModelInfo(ModelInfoMetadata metadata){
355 JSONObject object = new JSONObject();
356 String invariantUuid = metadata.getModelInvariantUuid();
357 String modelUuid = metadata.getModelUuid();
358 if(isNotBlank(invariantUuid) && isNotBlank(modelUuid)){
359 object.put(MODEL_INVARIANT_ID, invariantUuid).put(MODEL_VERSION_ID, modelUuid).put(MODEL_NAME, metadata.getModelName()).put(MODEL_VERSION, metadata.getModelVersion());
360 }else if(isNotBlank(invariantUuid)){
361 throw new BpmnError(UNPROCESSABLE, RESOURCE_MISSING_DATA + MODEL_VERSION_ID);
363 throw new BpmnError(UNPROCESSABLE, RESOURCE_MISSING_DATA + MODEL_INVARIANT_ID);
369 * Adds required, excluded, and existing candidates to a demand
372 private void addCandidates(SolutionCandidates candidates, JSONObject demand){
373 List<Candidate> required = candidates.getRequiredCandidates();
374 List<Candidate> excluded = candidates.getExcludedCandidates();
375 if(!required.isEmpty()){
376 demand.put("requiredCandidates", required);
378 if(!excluded.isEmpty()){
379 demand.put("excludedCandidates", excluded);
381 //TODO support existing candidates
385 * Processes the license solutions and sets to the corresponding generic vnf
388 private void processLicenseSolution(ServiceInstance serviceInstance, JSONArray licenseSolutions){
389 List<GenericVnf> vnfs = serviceInstance.getVnfs();
391 log.debug("Processing the license solution");
392 for(int i = 0; i < licenseSolutions.length(); i++){
393 JSONObject licenseSolution = licenseSolutions.getJSONObject(i);
394 for(GenericVnf vnf:vnfs){
395 if(licenseSolution.getString(SERVICE_RESOURCE_ID).equals(vnf.getVnfId())){
396 License license = new License();
397 JSONArray entitlementPools = licenseSolution.getJSONArray("entitlementPoolUUID");
398 List<String> entitlementPoolsList = jsonUtils.StringArrayToList(entitlementPools);
399 license.setEntitlementPoolUuids(entitlementPoolsList);
400 JSONArray licenseKeys = licenseSolution.getJSONArray("licenseKeyGroupUUID");
401 List<String> licenseKeysList = jsonUtils.StringArrayToList(licenseKeys);
402 license.setLicenseKeyGroupUuids(licenseKeysList);
404 vnf.setLicense(license);
411 * Processes a placement solution list then correlates and sets each placement solution
412 * to its corresponding resource
415 private void processPlacementSolution(ServiceInstance serviceInstance, JSONArray placements, int i){
416 List<VpnBondingLink> links = serviceInstance.getVpnBondingLinks();
417 List<AllottedResource> allottes = serviceInstance.getAllottedResources();
418 List<GenericVnf> vnfs = serviceInstance.getVnfs();
420 log.debug("Processing placement solution " + i+1);
421 for(int p = 0; p < placements.length(); p++){
422 JSONObject placement = placements.getJSONObject(p);
423 SolutionInfo solutionInfo = new SolutionInfo();
424 solutionInfo.setSolutionId(i + 1);
426 for(VpnBondingLink vbl:links){
427 List<ServiceProxy> proxies = vbl.getServiceProxies();
428 for(ServiceProxy sp:proxies){
429 if(placement.getString(SERVICE_RESOURCE_ID).equals(sp.getId())){
432 VpnBondingLink vblNew = (VpnBondingLink) SerializationUtils.clone(vbl);
433 vblNew.setVpnBondingLinkId(UUID.randomUUID().toString());
436 links.get(links.size() - 1).getServiceProxy(sp.getId()).setServiceInstance(setSolution(solutionInfo, placement));
438 sp.setServiceInstance(setSolution(solutionInfo, placement));
444 for(AllottedResource ar:allottes){
445 if(placement.getString(SERVICE_RESOURCE_ID).equals(ar.getId())){
446 ar.setParentServiceInstance(setSolution(solutionInfo, placement));
450 for(GenericVnf vnf:vnfs){
451 if(placement.getString(SERVICE_RESOURCE_ID).equals(vnf.getVnfId())){
452 ServiceInstance si = setSolution(solutionInfo, placement);
453 serviceInstance.setSolutionInfo(si.getSolutionInfo());
454 serviceInstance.getVnfs().add(si.getVnfs().get(0));
464 * Creates and sets necessary pojos with placement solution data for a given demand
467 private ServiceInstance setSolution(SolutionInfo solutionInfo, JSONObject placement){
468 log.debug("Mapping placement solution");
469 String invalidMessage = "Sniro Managers Response contains invalid: ";
471 JSONObject solution = placement.getJSONObject("solution");
472 String identifierType = solution.getString(IDENTIFIER_TYPE);
473 List<String> identifiersList = jsonUtils.StringArrayToList(solution.getJSONArray("identifiers").toString());
474 String identifierValue = identifiersList.get(0);
476 JSONArray assignments = placement.getJSONArray("assignmentInfo");
477 Map<String, String> assignmentsMap = jsonUtils.entryArrayToMap(assignments.toString(), "key", "value");
478 solutionInfo.setRehome(Boolean.parseBoolean(assignmentsMap.get("isRehome")));
479 String type = placement.getString(INVENTORY_TYPE);
481 ServiceInstance si = new ServiceInstance();
482 CloudRegion cloud = setCloud(assignmentsMap);
483 if(type.equals("service")){
484 if(identifierType.equals(CandidateType.SERVICE_INSTANCE_ID.toString())){
485 solutionInfo.setHomed(true);
486 si.setServiceInstanceId(identifierValue);
487 si.setOrchestrationStatus(OrchestrationStatus.CREATED);
488 cloud.setLcpCloudRegionId(assignmentsMap.get("cloudRegionId"));
489 if(assignmentsMap.containsKey("vnfHostName")){
490 log.debug("Resources has been homed to a vnf");
491 GenericVnf vnf = setVnf(assignmentsMap);
492 vnf.setCloudRegion(cloud);
493 si.getVnfs().add(vnf);
495 }else if(assignmentsMap.containsKey("primaryPnfName")){
496 log.debug("Resources has been homed to a pnf");
497 Pnf priPnf = setPnf(assignmentsMap, "primary");
498 priPnf.setCloudRegion(cloud);
499 si.getPnfs().add(priPnf);
500 if(assignmentsMap.containsKey("secondaryPnfName")){
501 Pnf secPnf = setPnf(assignmentsMap, "secondary");
502 secPnf.setCloudRegion(cloud);
503 si.getPnfs().add(secPnf);
507 log.debug(invalidMessage + IDENTIFIER_TYPE);
508 throw new BpmnError(UNPROCESSABLE, invalidMessage + IDENTIFIER_TYPE);
510 }else if(type.equals("cloud")){
511 if(identifierType.equals(CandidateType.CLOUD_REGION_ID.toString())){
512 log.debug("Resources has been homed to a cloud region");
513 cloud.setLcpCloudRegionId(identifierValue);
514 solutionInfo.setHomed(false);
515 solutionInfo.setTargetedCloudRegion(cloud);
516 si.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
518 log.debug(invalidMessage + IDENTIFIER_TYPE);
519 throw new BpmnError(UNPROCESSABLE, invalidMessage + IDENTIFIER_TYPE);
522 log.debug(invalidMessage + INVENTORY_TYPE);
523 throw new BpmnError(UNPROCESSABLE, invalidMessage + INVENTORY_TYPE);
525 si.setSolutionInfo(solutionInfo);
530 * Sets the cloud data to a cloud region object
533 private CloudRegion setCloud(Map<String, String> assignmentsMap){
534 CloudRegion cloud = new CloudRegion();
535 cloud.setCloudOwner(assignmentsMap.get("cloudOwner"));
536 cloud.setCloudRegionVersion(assignmentsMap.get("aicVersion"));
537 cloud.setComplex(assignmentsMap.get("aicClli"));
542 * Sets the vnf data to a generic vnf object
545 private GenericVnf setVnf(Map<String, String> assignmentsMap){
546 GenericVnf vnf = new GenericVnf();
547 vnf.setOrchestrationStatus(OrchestrationStatus.CREATED);
548 vnf.setVnfName(assignmentsMap.get("vnfHostName"));
549 vnf.setVnfId(assignmentsMap.get("vnfId"));
554 * Sets the pnf data to a pnf object
557 private Pnf setPnf(Map<String, String> assignmentsMap, String role){
560 pnf.setOrchestrationStatus(OrchestrationStatus.CREATED);
561 pnf.setPnfName(assignmentsMap.get(role + "PnfName"));