Code changes in BPMN infra for RAN Slice Use case
[so.git] / bpmn / so-bpmn-moi / src / main / java / org / onap / so / bpmn / moi / tasks / EnrichGBBTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (c) 2022 Deutsche telekom
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21
22 package org.onap.so.bpmn.moi.tasks;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.camunda.bpm.engine.delegate.DelegateExecution;
26 import org.onap.so.bpmn.common.BuildingBlockExecution;
27 import org.onap.so.bpmn.common.DelegateExecutionImpl;
28 import org.onap.so.bpmn.common.InjectExecution;
29 import org.onap.so.bpmn.common.scripts.ExceptionUtil;
30 import org.onap.so.bpmn.common.scripts.OofUtils;
31 import org.onap.so.bpmn.core.json.JsonUtils;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
33 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
34 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoServiceInstance;
35 import org.onap.so.db.catalog.beans.Service;
36 import org.onap.so.db.catalog.client.CatalogDbClient;
37 import org.onap.so.moi.Attributes;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45
46 @Component
47 public class EnrichGBBTask {
48
49     private static final String EXECUTE_BB_VAR_NAME = "buildingBlock";
50     private static final String GBB_INPUT_VAR_NAME = "gBBInput";
51
52     ExceptionUtil exceptionUtil = new ExceptionUtil();
53     JsonUtils jsonUtil = new JsonUtils();
54
55     @Autowired
56     CatalogDbClient catalogDbClient;
57
58     InjectExecution injectExecution = new InjectExecution();
59
60
61     private static final Logger LOGGER = LoggerFactory.getLogger(EnrichGBBTask.class);
62     private static final ObjectMapper mapper = new ObjectMapper();
63     OofUtils oofUtils = new OofUtils(null);
64
65     public void prepareOofRequest(DelegateExecution execution) throws Exception {
66
67         String msoReqId = (String) execution.getVariable("mso-request-id");
68
69
70         BuildingBlockExecution gBuildingBlockExecution =
71                 (BuildingBlockExecution) execution.getVariable("gBuildingBlockExecution");
72
73         DelegateExecutionImpl gbbEx = injectExecution.execute(execution,
74                 (DelegateExecutionImpl) execution.getVariable("gBuildingBlockExecution"));
75
76
77         GeneralBuildingBlock generalBuildingBlock = (GeneralBuildingBlock) execution.getVariable("gBBInput");
78
79         List<Map<String, Object>> mapUserParams =
80                 generalBuildingBlock.getRequestContext().getRequestParameters().getUserParams();
81
82         Attributes attributes = null;
83
84         for (Map<String, Object> userParamData : mapUserParams) {
85             if (userParamData.get("nssi") != null) {
86                 Map<String, Object> mapParam = (Map<String, Object>) userParamData.get("nssi");
87                 LOGGER.info(">>> mapParam: {}", mapParam);
88                 attributes = mapper.convertValue(mapParam, Attributes.class);
89             }
90         }
91         // Attributes attributes = new ObjectMapper().convertValue(attrMap, Attributes.class);
92         Integer latency = attributes.getSliceProfileList().get(0).getRANSliceSubnetProfile().getLatency();
93         Integer areaTrafficCapDL =
94                 attributes.getSliceProfileList().get(0).getRANSliceSubnetProfile().getAreaTrafficCapDL();
95
96
97         String requestId = generalBuildingBlock.getRequestContext().getMsoRequestId();
98
99         // API Path
100         String apiPath = "/api/oof/v1/selection/nsst";
101         LOGGER.debug("API path for NSST Selection: " + apiPath);
102         execution.setVariable("NSST_apiPath", apiPath);
103
104         // Setting correlator as requestId
105         execution.setVariable("NSST_correlator", requestId);
106
107         // Setting messageType for all Core slice as an
108         String messageType = "an";
109         execution.setVariable("NSST_messageType", messageType);
110
111         String timeout = "PT30M";
112         execution.setVariable("NSST_timeout", timeout);
113
114         Map<String, Object> profileInfo = new HashMap<>();
115         profileInfo.put("latency", latency);
116         profileInfo.put("areaTrafficCapDL", areaTrafficCapDL);
117
118         String oofRequest = oofUtils.buildSelectNSTRequest(requestId, messageType, profileInfo);
119         LOGGER.debug("**** OOfRequest for NSST Selection: " + oofRequest);
120         execution.setVariable("NSST_oofRequest", oofRequest);
121     }
122
123
124     public void processOOFAsyncResponse(DelegateExecution execution) {
125         GeneralBuildingBlock generalBuildingBlock = (GeneralBuildingBlock) execution.getVariable("gBBInput");
126
127         LOGGER.debug(">>>> generalBuildingBlock Initial: {}", generalBuildingBlock);
128
129         LOGGER.debug(" **** Enter EnrichGBB ::: processOOFAsyncResponse ****");
130         String OOFResponse = (String) execution.getVariable("NSST_asyncCallbackResponse");
131         String requestStatus = jsonUtil.getJsonValue(OOFResponse, "requestStatus");
132         LOGGER.debug("NSST OOFResponse is: " + OOFResponse);
133         execution.setVariable("OOFResponse", OOFResponse);
134         String solutions = "";
135         if (requestStatus.equals("completed")) {
136             List solutionsList = jsonUtil.StringArrayToList(jsonUtil.getJsonValue(OOFResponse, "solutions"));
137             if (solutionsList != null && !solutionsList.isEmpty()) {
138                 solutions = (String) solutionsList.get(0);
139             }
140         } else {
141             String statusMessage = jsonUtil.getJsonValue(OOFResponse, "statusMessage");
142             LOGGER.error("received failed status from oof " + statusMessage);
143             LOGGER.debug("received failed status from oof " + statusMessage);
144         }
145
146         LOGGER.debug(">>>>>> solutions: {}", solutions);
147
148         String nsstId = jsonUtil.getJsonValue(solutions, "UUID");
149         LOGGER.info(">>> nsstId:{} ", nsstId);
150
151         Service service = catalogDbClient.getServiceByModelUUID(nsstId);
152
153         LOGGER.info("Service from CatalogDB: {}", service);
154
155         LOGGER.debug(">>> Map Incoming Values to GBB");
156
157         ServiceInstance serviceInstance = generalBuildingBlock.getServiceInstance();
158
159         ModelInfoServiceInstance modelInfoServiceInstanceFromGBB = serviceInstance.getModelInfoServiceInstance();
160
161         if (modelInfoServiceInstanceFromGBB == null) {
162             String modelInvariantId = service.getModelInvariantUUID();
163             String modelVersion = service.getModelVersion();
164             String serviceType = service.getServiceType();
165             String serviceRole = service.getServiceRole();
166             String controllerActor = service.getControllerActor();
167             String blueprintName = service.getBlueprintName();
168             String blueprintVersion = service.getBlueprintVersion();
169
170             ModelInfoServiceInstance modelInfoServiceInstance = new ModelInfoServiceInstance();
171             modelInfoServiceInstance.setModelUuid("ad2233a2-6e3f-42cf-8c60-04e614031383");
172             modelInfoServiceInstance.setModelInvariantUuid("38730fb9-bfbb-4a78-88f8-b4f6823197b6");
173             modelInfoServiceInstance.setModelVersion("1.0");
174             modelInfoServiceInstance.setServiceRole("AN");
175             modelInfoServiceInstance.setServiceType("eMBB");
176             modelInfoServiceInstance.setBlueprintVersion("1.0.0");
177             modelInfoServiceInstance.setControllerActor("CDS");
178             modelInfoServiceInstance.setBlueprintName("Hello_World_CBA");
179             modelInfoServiceInstance.setServiceType(serviceType);
180             modelInfoServiceInstance.setServiceRole(serviceRole);
181             modelInfoServiceInstance.setControllerActor(controllerActor);
182             modelInfoServiceInstance.setBlueprintName(blueprintName);
183             modelInfoServiceInstance.setBlueprintVersion(blueprintVersion);
184             modelInfoServiceInstance.setModelInvariantUuid(modelInvariantId);
185             modelInfoServiceInstance.setModelUuid(nsstId);
186             modelInfoServiceInstance.setModelVersion(modelVersion);
187
188             serviceInstance.setModelInfoServiceInstance(modelInfoServiceInstance);
189
190             serviceInstance.setModelInfoServiceInstance(modelInfoServiceInstance);
191
192
193         }
194
195         LOGGER.info(">>> ServiceInstance: {}", serviceInstance);
196
197         generalBuildingBlock.setServiceInstance(serviceInstance);
198
199         LOGGER.debug("generalBuildingBlock: {}", generalBuildingBlock);
200
201         execution.setVariable(GBB_INPUT_VAR_NAME, generalBuildingBlock);
202         BuildingBlockExecution gBuildingBlockExecution1 = new DelegateExecutionImpl(execution);
203         execution.setVariable("gBuildingBlockExecution", gBuildingBlockExecution1);
204
205         // execution.setVariable("gBuildingBlockExecution", gBBExecution);
206         LOGGER.debug(" **** Exit EnrichBB ::: processOOFAsyncResponse ****");
207     }
208 }