9413e8ef2ed2585e8cc9505a28d097c274dae7ef
[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.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.UUID;
29 import org.onap.so.bpmn.common.BuildingBlockExecution;
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.entities.GeneralBuildingBlock;
33 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
34 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
35 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
36 import org.onap.so.client.cds.beans.ConfigAssignPropertiesForVnf;
37 import org.onap.so.client.cds.beans.ConfigAssignRequestVnf;
38 import org.onap.so.client.exception.ExceptionBuilder;
39 import org.onap.so.serviceinstancebeans.Service;
40 import org.onap.so.serviceinstancebeans.Vnfs;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Component;
45
46 /**
47  * Get vnf related data and config assign
48  */
49 @Component
50 public class ConfigAssignVnf {
51
52     private static final Logger logger = LoggerFactory.getLogger(ConfigAssignVnf.class);
53     private static final String ORIGINATOR_ID = "SO";
54     private static final String ACTION_NAME = "config-assign";
55     private static final String MODE = "sync";
56
57     private final ExtractPojosForBB extractPojosForBB;
58     private final ExceptionBuilder exceptionBuilder;
59
60     @Autowired
61     public ConfigAssignVnf(ExtractPojosForBB extractPojosForBB, ExceptionBuilder exceptionBuilder) {
62         this.extractPojosForBB = extractPojosForBB;
63         this.exceptionBuilder = exceptionBuilder;
64     }
65
66     /**
67      * Getting the vnf data, blueprint name, blueprint version etc and setting them in execution object and calling the
68      * subprocess.
69      */
70     public void preProcessAbstractCDSProcessing(BuildingBlockExecution execution) {
71         logger.info("Start preProcessAbstractCDSProcessing ");
72         try {
73             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
74             ServiceInstance serviceInstance =
75                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
76             ConfigAssignPropertiesForVnf configAssignPropertiesForVnf = new ConfigAssignPropertiesForVnf();
77             configAssignPropertiesForVnf.setServiceInstanceId(serviceInstance.getServiceInstanceId());
78             configAssignPropertiesForVnf
79                     .setServiceModelUuid(serviceInstance.getModelInfoServiceInstance().getModelUuid());
80             configAssignPropertiesForVnf
81                     .setVnfCustomizationUuid(genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid());
82             configAssignPropertiesForVnf.setVnfId(genericVnf.getVnfId());
83             configAssignPropertiesForVnf.setVnfName(genericVnf.getVnfName());
84             setUserParamsInConfigAssignPropertiesForVnf(configAssignPropertiesForVnf,
85                     execution.getGeneralBuildingBlock().getRequestContext().getRequestParameters().getUserParams(),
86                     genericVnf);
87             ConfigAssignRequestVnf configAssignRequestVnf = new ConfigAssignRequestVnf();
88             configAssignRequestVnf.setResolutionKey(genericVnf.getVnfName());
89             configAssignRequestVnf.setConfigAssignPropertiesForVnf(configAssignPropertiesForVnf);
90
91             String blueprintName = genericVnf.getModelInfoGenericVnf().getBlueprintName();
92             String blueprintVersion = genericVnf.getModelInfoGenericVnf().getBlueprintVersion();
93             logger.debug(" BlueprintName : " + blueprintName + " BlueprintVersion : " + blueprintVersion);
94
95             AbstractCDSPropertiesBean abstractCDSPropertiesBean = new AbstractCDSPropertiesBean();
96
97             abstractCDSPropertiesBean.setBlueprintName(blueprintName);
98             abstractCDSPropertiesBean.setBlueprintVersion(blueprintVersion);
99             abstractCDSPropertiesBean.setRequestObject(configAssignRequestVnf.toString());
100
101             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
102
103             abstractCDSPropertiesBean.setOriginatorId(ORIGINATOR_ID);
104             abstractCDSPropertiesBean.setRequestId(gBBInput.getRequestContext().getMsoRequestId());
105             abstractCDSPropertiesBean.setSubRequestId(UUID.randomUUID().toString());
106             abstractCDSPropertiesBean.setActionName(ACTION_NAME);
107             abstractCDSPropertiesBean.setMode(MODE);
108             execution.setVariable("executionObject", abstractCDSPropertiesBean);
109         } catch (Exception ex) {
110             logger.error("An exception occurred when creating ConfigAssignPropertiesForVnf for CDS request", ex);
111             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ex);
112         }
113     }
114
115     private void setUserParamsInConfigAssignPropertiesForVnf(ConfigAssignPropertiesForVnf configAssignProperties,
116             List<Map<String, Object>> userParamsFromRequest, GenericVnf vnf) throws Exception {
117         Service service = getServiceFromRequestUserParams(userParamsFromRequest);
118         List<Map<String, String>> instanceParamsList =
119                 getInstanceParamForVnf(service, vnf.getModelInfoGenericVnf().getModelCustomizationUuid());
120         instanceParamsList
121                 .forEach(instanceParamsMap -> instanceParamsMap.forEach(configAssignProperties::setUserParam));
122     }
123
124     private Service getServiceFromRequestUserParams(List<Map<String, Object>> userParams) throws Exception {
125         Map<String, Object> serviceMap = userParams.stream().filter(key -> key.containsKey("service")).findFirst()
126                 .orElseThrow(() -> new Exception("Can not find service in userParams section in generalBuildingBlock"));
127         return convertServiceFromJsonToServiceObject((String) serviceMap.get("service"));
128     }
129
130     private Service convertServiceFromJsonToServiceObject(String serviceFromJson) throws Exception {
131         try {
132             return new ObjectMapper().readValue(serviceFromJson, Service.class);
133         } catch (Exception e) {
134             logger.error(String.format(
135                     "An exception occurred while converting json object to Service object. The json is: %s",
136                     serviceFromJson), e);
137             throw e;
138         }
139     }
140
141     private List<Map<String, String>> getInstanceParamForVnf(Service service, String genericVnfModelCustomizationUuid)
142             throws Exception {
143         Optional<Vnfs> foundedVnf = service.getResources().getVnfs().stream()
144                 .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(genericVnfModelCustomizationUuid))
145                 .findFirst();
146         if (foundedVnf.isPresent()) {
147             return foundedVnf.get().getInstanceParams();
148         } else {
149             throw new Exception(String.format("Can not find vnf for genericVnfModelCustomizationUuid: %s",
150                     genericVnfModelCustomizationUuid));
151         }
152     }
153
154 }