Merge "Add new service in API-Handler for 3gpp service instances"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / cnf / tasks / CnfAdapterCreateTasks.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.adapter.cnf.tasks;
24
25 import java.io.IOException;
26 import java.util.HashMap;
27 import java.util.Map;
28 import org.onap.so.bpmn.common.BuildingBlockExecution;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
33 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
34 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
35 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
36 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
37 import org.onap.so.client.adapter.cnf.CnfAdapterClient;
38 import org.onap.so.client.adapter.cnf.entities.InstanceRequest;
39 import org.onap.so.client.adapter.cnf.entities.InstanceResponse;
40 import org.onap.so.client.adapter.vnf.mapper.AttributeNameValue;
41 import org.onap.so.client.adapter.vnf.mapper.Attributes;
42 import org.onap.so.client.adapter.vnf.mapper.VnfAdapterVfModuleObjectMapper;
43 import org.onap.so.client.exception.ExceptionBuilder;
44 import org.onap.so.openstack.utils.MsoMulticloudUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.stereotype.Component;
49 import com.fasterxml.jackson.core.JsonParseException;
50 import com.fasterxml.jackson.databind.JsonMappingException;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52
53 @Component
54 public class CnfAdapterCreateTasks {
55     private static final Logger logger = LoggerFactory.getLogger(CnfAdapterCreateTasks.class);
56     public static final String SDNCQUERY_RESPONSE = "SDNCQueryResponse_";
57
58     @Autowired
59     private ExtractPojosForBB extractPojosForBB;
60     @Autowired
61     private ExceptionBuilder exceptionUtil;
62     @Autowired
63     private CnfAdapterClient cnfAdapterClient;
64     @Autowired
65     private VnfAdapterVfModuleObjectMapper vfModuleMapper;
66
67     private ObjectMapper mapper = new ObjectMapper();
68
69     /**
70      * This method is used for creating the request for an Instance in Multicloud K8s Plugin.
71      *
72      * @param execution
73      * @return
74      */
75     public void createInstance(BuildingBlockExecution execution) {
76         try {
77             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
78             ServiceInstance serviceInstance =
79                     gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0);
80             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
81             VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
82             RequestContext requestContext = gBBInput.getRequestContext();
83             CloudRegion cloudRegion = gBBInput.getCloudRegion();
84             String sdncVfModuleQueryResponse = execution.getVariable(SDNCQUERY_RESPONSE + vfModule.getVfModuleId());
85             String sdncVnfQueryResponse = execution.getVariable(SDNCQUERY_RESPONSE + genericVnf.getVnfId());
86             Map<String, Object> paramsMap = vfModuleMapper.buildVfModuleParamsMap(requestContext, serviceInstance,
87                     genericVnf, vfModule, sdncVnfQueryResponse, sdncVfModuleQueryResponse);
88             Map<String, String> sdncDirectives = getSdncDirectives(paramsMap);
89             InstanceRequest createInstanceRequest = createInstanceRequest(vfModule, cloudRegion, sdncDirectives);
90             InstanceResponse response = cnfAdapterClient.createVfModule(createInstanceRequest);
91             execution.setVariable("heatStackId", response.getId());
92         } catch (Exception ex) {
93             logger.error("Exception occurred", ex);
94             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
95         }
96     }
97
98     protected Map<String, String> getSdncDirectives(Map<String, Object> paramsMap)
99             throws JsonParseException, JsonMappingException, IOException {
100         Map<String, String> sdncDirectivesMap = new HashMap<>();
101         String sdncDirectivesString = (String) paramsMap.get(MsoMulticloudUtils.SDNC_DIRECTIVES);
102         Attributes sdncDirectives = mapper.readValue(sdncDirectivesString, Attributes.class);
103         for (AttributeNameValue nameVal : sdncDirectives.getAttributes()) {
104             sdncDirectivesMap.put(nameVal.getAttributeName(), (String) nameVal.getAttributeValue());
105         }
106         return sdncDirectivesMap;
107     }
108
109     protected InstanceRequest createInstanceRequest(VfModule vfModule, CloudRegion cloudRegion,
110             Map<String, String> sdncDirectives) {
111         InstanceRequest request = new InstanceRequest();
112         request.setRbName(vfModule.getModelInfoVfModule().getModelInvariantUUID());
113         request.setRbVersion(vfModule.getModelInfoVfModule().getModelUUID());
114         request.setCloudRegion(cloudRegion.getLcpCloudRegionId());
115         request.setReleaseName(vfModule.getVfModuleId());
116         request.setProfileName(sdncDirectives.get("k8s-rb-profile-name"));
117         request.setOverrideValues(sdncDirectives);
118         return request;
119     }
120
121 }