4aa9c119cfae4ea55ed971b3f1736a3ed986dc0d
[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     private static final ObjectMapper objectMapper = new ObjectMapper();
59
60     private final ExtractPojosForBB extractPojosForBB;
61     private final ExceptionBuilder exceptionBuilder;
62
63     @Autowired
64     public ConfigAssignVnf(ExtractPojosForBB extractPojosForBB, ExceptionBuilder exceptionBuilder) {
65         this.extractPojosForBB = extractPojosForBB;
66         this.exceptionBuilder = exceptionBuilder;
67     }
68
69     /**
70      * Getting the vnf data, blueprint name, blueprint version etc and setting them in execution object and calling the
71      * subprocess.
72      */
73     public void preProcessAbstractCDSProcessing(BuildingBlockExecution execution) {
74         logger.info("Start preProcessAbstractCDSProcessing ");
75         try {
76             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
77             ServiceInstance serviceInstance =
78                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
79             ConfigAssignPropertiesForVnf configAssignPropertiesForVnf = new ConfigAssignPropertiesForVnf();
80             configAssignPropertiesForVnf.setServiceInstanceId(serviceInstance.getServiceInstanceId());
81             configAssignPropertiesForVnf
82                     .setServiceModelUuid(serviceInstance.getModelInfoServiceInstance().getModelUuid());
83             configAssignPropertiesForVnf
84                     .setVnfCustomizationUuid(genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid());
85             configAssignPropertiesForVnf.setVnfId(genericVnf.getVnfId());
86             configAssignPropertiesForVnf.setVnfName(genericVnf.getVnfName());
87             setUserParamsInConfigAssignPropertiesForVnf(configAssignPropertiesForVnf,
88                     execution.getGeneralBuildingBlock().getRequestContext().getRequestParameters().getUserParams(),
89                     genericVnf);
90             ConfigAssignRequestVnf configAssignRequestVnf = new ConfigAssignRequestVnf();
91             configAssignRequestVnf.setResolutionKey(genericVnf.getVnfName());
92             configAssignRequestVnf.setConfigAssignPropertiesForVnf(configAssignPropertiesForVnf);
93
94             String blueprintName = genericVnf.getModelInfoGenericVnf().getBlueprintName();
95             String blueprintVersion = genericVnf.getModelInfoGenericVnf().getBlueprintVersion();
96             logger.debug(" BlueprintName : " + blueprintName + " BlueprintVersion : " + blueprintVersion);
97
98             AbstractCDSPropertiesBean abstractCDSPropertiesBean = new AbstractCDSPropertiesBean();
99
100             abstractCDSPropertiesBean.setBlueprintName(blueprintName);
101             abstractCDSPropertiesBean.setBlueprintVersion(blueprintVersion);
102             abstractCDSPropertiesBean.setRequestObject(configAssignRequestVnf.toString());
103
104             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
105
106             abstractCDSPropertiesBean.setOriginatorId(ORIGINATOR_ID);
107             abstractCDSPropertiesBean.setRequestId(gBBInput.getRequestContext().getMsoRequestId());
108             abstractCDSPropertiesBean.setSubRequestId(UUID.randomUUID().toString());
109             abstractCDSPropertiesBean.setActionName(ACTION_NAME);
110             abstractCDSPropertiesBean.setMode(MODE);
111             execution.setVariable("executionObject", abstractCDSPropertiesBean);
112         } catch (Exception ex) {
113             logger.error("An exception occurred when creating ConfigAssignPropertiesForVnf for CDS request", ex);
114             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ex);
115         }
116     }
117
118     private void setUserParamsInConfigAssignPropertiesForVnf(ConfigAssignPropertiesForVnf configAssignProperties,
119             List<Map<String, Object>> userParamsFromRequest, GenericVnf vnf) throws Exception {
120         Service service = getServiceFromRequestUserParams(userParamsFromRequest);
121         List<Map<String, String>> instanceParamsList =
122                 getInstanceParamForVnf(service, vnf.getModelInfoGenericVnf().getModelCustomizationUuid());
123         instanceParamsList
124                 .forEach(instanceParamsMap -> instanceParamsMap.forEach(configAssignProperties::setUserParam));
125     }
126
127     private Service getServiceFromRequestUserParams(List<Map<String, Object>> userParams) throws Exception {
128         Map<String, Object> serviceMap = userParams.stream().filter(key -> key.containsKey("service")).findFirst()
129                 .orElseThrow(() -> new Exception("Can not find service in userParams section in generalBuildingBlock"));
130         return getServiceObjectFromServiceMap(serviceMap);
131     }
132
133     private Service getServiceObjectFromServiceMap(Map<String, Object> serviceMap) throws IOException {
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 }