3a69d27f32f194762335a720dc48134c779718a0
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 TechMahindra.
6  * Copyright (C) 2019 Nokia.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import java.io.IOException;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.UUID;
30 import org.onap.so.bpmn.common.BuildingBlockExecution;
31 import org.onap.so.bpmn.infrastructure.flowspecific.exceptions.VnfNotFoundException;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
34 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
35 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
36 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
37 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
38 import org.onap.so.client.cds.beans.ConfigAssignPropertiesForVnf;
39 import org.onap.so.client.cds.beans.ConfigAssignRequestVnf;
40 import org.onap.so.client.exception.ExceptionBuilder;
41 import org.onap.so.serviceinstancebeans.Service;
42 import org.onap.so.serviceinstancebeans.Vnfs;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
47
48 /**
49  * Get vnf related data and config assign
50  */
51 @Component
52 public class ConfigAssignVnf {
53
54     private static final Logger logger = LoggerFactory.getLogger(ConfigAssignVnf.class);
55     private static final String ORIGINATOR_ID = "SO";
56     private static final String ACTION_NAME = "config-assign";
57     private static final String MODE = "sync";
58
59     private final ExtractPojosForBB extractPojosForBB;
60     private final ExceptionBuilder exceptionBuilder;
61
62     @Autowired
63     public ConfigAssignVnf(ExtractPojosForBB extractPojosForBB, ExceptionBuilder exceptionBuilder) {
64         this.extractPojosForBB = extractPojosForBB;
65         this.exceptionBuilder = exceptionBuilder;
66     }
67
68     /**
69      * Getting the vnf data, blueprint name, blueprint version etc and setting them in execution object and calling the
70      * subprocess.
71      */
72     public void preProcessAbstractCDSProcessing(BuildingBlockExecution execution) {
73         logger.info("Start preProcessAbstractCDSProcessing ");
74         try {
75             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
76             ServiceInstance serviceInstance =
77                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
78             ConfigAssignPropertiesForVnf configAssignPropertiesForVnf = new ConfigAssignPropertiesForVnf();
79             configAssignPropertiesForVnf.setServiceInstanceId(serviceInstance.getServiceInstanceId());
80             configAssignPropertiesForVnf
81                     .setServiceModelUuid(serviceInstance.getModelInfoServiceInstance().getModelUuid());
82             configAssignPropertiesForVnf
83                     .setVnfCustomizationUuid(genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid());
84             configAssignPropertiesForVnf.setVnfId(genericVnf.getVnfId());
85             configAssignPropertiesForVnf.setVnfName(genericVnf.getVnfName());
86             setUserParamsInConfigAssignPropertiesForVnf(configAssignPropertiesForVnf,
87                     execution.getGeneralBuildingBlock().getRequestContext().getRequestParameters().getUserParams(),
88                     genericVnf);
89             ConfigAssignRequestVnf configAssignRequestVnf = new ConfigAssignRequestVnf();
90             configAssignRequestVnf.setResolutionKey(genericVnf.getVnfName());
91             configAssignRequestVnf.setConfigAssignPropertiesForVnf(configAssignPropertiesForVnf);
92
93             String blueprintName = genericVnf.getModelInfoGenericVnf().getBlueprintName();
94             String blueprintVersion = genericVnf.getModelInfoGenericVnf().getBlueprintVersion();
95             logger.debug(" BlueprintName : " + blueprintName + " BlueprintVersion : " + blueprintVersion);
96
97             AbstractCDSPropertiesBean abstractCDSPropertiesBean = new AbstractCDSPropertiesBean();
98
99             abstractCDSPropertiesBean.setBlueprintName(blueprintName);
100             abstractCDSPropertiesBean.setBlueprintVersion(blueprintVersion);
101             abstractCDSPropertiesBean.setRequestObject(configAssignRequestVnf.toString());
102
103             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
104
105             abstractCDSPropertiesBean.setOriginatorId(ORIGINATOR_ID);
106             abstractCDSPropertiesBean.setRequestId(gBBInput.getRequestContext().getMsoRequestId());
107             abstractCDSPropertiesBean.setSubRequestId(UUID.randomUUID().toString());
108             abstractCDSPropertiesBean.setActionName(ACTION_NAME);
109             abstractCDSPropertiesBean.setMode(MODE);
110             execution.setVariable("executionObject", abstractCDSPropertiesBean);
111         } catch (Exception ex) {
112             logger.error("An exception occurred when creating ConfigAssignPropertiesForVnf for CDS request", ex);
113             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ex);
114         }
115     }
116
117     private void setUserParamsInConfigAssignPropertiesForVnf(ConfigAssignPropertiesForVnf configAssignProperties,
118             List<Map<String, Object>> userParamsFromRequest, GenericVnf vnf) throws Exception {
119         Service service = getServiceFromRequestUserParams(userParamsFromRequest);
120         List<Map<String, String>> instanceParamsList =
121                 getInstanceParamForVnf(service, vnf.getModelInfoGenericVnf().getModelCustomizationUuid());
122         instanceParamsList
123                 .forEach(instanceParamsMap -> instanceParamsMap.forEach(configAssignProperties::setUserParam));
124     }
125
126     private Service getServiceFromRequestUserParams(List<Map<String, Object>> userParams) throws Exception {
127         Map<String, Object> serviceMap = userParams.stream().filter(key -> key.containsKey("service")).findFirst()
128                 .orElseThrow(() -> new Exception("Can not find service in userParams section in generalBuildingBlock"));
129         return getServiceObjectFromServiceMap(serviceMap);
130     }
131
132     private Service getServiceObjectFromServiceMap(Map<String, Object> serviceMap) throws IOException {
133         ObjectMapper objectMapper = new ObjectMapper();
134         String serviceFromJson = objectMapper.writeValueAsString(serviceMap.get("service"));
135         try {
136             return objectMapper.readValue(serviceFromJson, Service.class);
137         } catch (IOException e) {
138             logger.error(String.format(
139                     "An exception occurred while converting json object to Service object. The json is: %s",
140                     serviceFromJson), e);
141             throw e;
142         }
143     }
144
145     private List<Map<String, String>> getInstanceParamForVnf(Service service, String genericVnfModelCustomizationUuid)
146             throws VnfNotFoundException {
147         Optional<Vnfs> foundedVnf = service.getResources().getVnfs().stream()
148                 .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(genericVnfModelCustomizationUuid))
149                 .findFirst();
150         if (foundedVnf.isPresent()) {
151             return foundedVnf.get().getInstanceParams();
152         } else {
153             throw new VnfNotFoundException(genericVnfModelCustomizationUuid);
154         }
155     }
156 }