2eebde06873b87a31dfada018f78607d14a99c2d
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / cnf / tasks / CnfAdapterUpgradeTasks.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.UpgradeInstanceRequest;
39 import org.onap.so.client.adapter.cnf.entities.UpgradeInstanceResponse;
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.core.JsonProcessingException;
51 import com.fasterxml.jackson.databind.JsonMappingException;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53
54 @Component
55 public class CnfAdapterUpgradeTasks {
56     private static final Logger logger = LoggerFactory.getLogger(CnfAdapterUpgradeTasks.class);
57
58     public static final String SDNCQUERY_RESPONSE = "SDNCQueryResponse_";
59
60     @Autowired
61     private ExtractPojosForBB extractPojosForBB;
62     @Autowired
63     private ExceptionBuilder exceptionUtil;
64     @Autowired
65     private CnfAdapterClient cnfAdapterClient;
66     @Autowired
67     private VnfAdapterVfModuleObjectMapper vfModuleMapper;
68
69     private ObjectMapper mapper = new ObjectMapper();
70
71     /**
72      * This method is used for updating the request for an Instance in Multicloud K8s Plugin.
73      *
74      * @param execution
75      * @return
76      */
77     public void upgradeInstance(BuildingBlockExecution execution) {
78         try {
79             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
80             ServiceInstance serviceInstance =
81                     gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0);
82             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
83             VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
84             RequestContext requestContext = gBBInput.getRequestContext();
85             CloudRegion cloudRegion = gBBInput.getCloudRegion();
86             String sdncVfModuleQueryResponse = execution.getVariable(SDNCQUERY_RESPONSE + vfModule.getVfModuleId());
87             String sdncVnfQueryResponse = execution.getVariable(SDNCQUERY_RESPONSE + genericVnf.getVnfId());
88             Map<String, Object> paramsMap = vfModuleMapper.buildVfModuleParamsMap(requestContext, serviceInstance,
89                     genericVnf, vfModule, sdncVnfQueryResponse, sdncVfModuleQueryResponse);
90             Map<String, String> sdncDirectives = getSdncDirectives(paramsMap);
91             UpgradeInstanceRequest upgradeInstanceRequest =
92                     upgradeInstanceRequest(vfModule, cloudRegion, sdncDirectives);
93             UpgradeInstanceResponse response = cnfAdapterClient.upgradeVfModule(upgradeInstanceRequest);
94             execution.setVariable("heatStackId", response.getId());
95         } catch (Exception ex) {
96             logger.error("Exception occurred", ex);
97             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
98         }
99     }
100
101     protected Map<String, String> getSdncDirectives(Map<String, Object> paramsMap)
102             throws JsonParseException, JsonMappingException, IOException {
103         Map<String, String> sdncDirectivesMap = new HashMap<>();
104         String sdncDirectivesString = (String) paramsMap.get(MsoMulticloudUtils.SDNC_DIRECTIVES);
105         Attributes sdncDirectives = mapper.readValue(sdncDirectivesString, Attributes.class);
106         for (AttributeNameValue nameVal : sdncDirectives.getAttributes()) {
107             sdncDirectivesMap.put(nameVal.getAttributeName(), (String) nameVal.getAttributeValue());
108         }
109         return sdncDirectivesMap;
110     }
111
112     protected UpgradeInstanceRequest upgradeInstanceRequest(VfModule vfModule, CloudRegion cloudRegion,
113             Map<String, String> sdncDirectives) {
114
115         UpgradeInstanceRequest request = new UpgradeInstanceRequest();
116
117         request.setModelInvariantId(vfModule.getModelInfoVfModule().getModelInvariantUUID());
118         request.setModelCustomizationId(vfModule.getModelInfoVfModule().getModelCustomizationUUID());
119         request.setCloudRegion(cloudRegion.getLcpCloudRegionId());
120         request.setVfModuleUUID(vfModule.getVfModuleId());
121         request.setProfileName(sdncDirectives.get("k8s-rb-profile-name"));
122         request.setLabels(sdncDirectives);
123         if (sdncDirectives.containsKey("k8s-rb-instance-status-check"))
124             request.setStatusCheck(sdncDirectives.get("k8s-rb-instance-status-check").equalsIgnoreCase("true"));
125         request.setOverrideValues(sdncDirectives);
126         return request;
127     }
128
129 }