01aefe2054d77b1d716a3ed5010ad8a4d8980ddb
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020  Tech Mahindra
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 package org.onap.so.bpmn.infrastructure.scripts
22 import org.apache.commons.collections.map.HashedMap
23 import org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.so.beans.nsmf.SliceTaskParams
26 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
27 import org.onap.so.bpmn.common.scripts.CatalogDbUtils
28 import org.onap.so.bpmn.common.scripts.CatalogDbUtilsFactory
29 import org.onap.so.bpmn.common.scripts.ExceptionUtil
30 import org.onap.so.bpmn.core.domain.ServiceDecomposition
31 import org.onap.so.bpmn.core.domain.ServiceProxy
32 import org.onap.so.bpmn.core.json.JsonUtils
33 import org.onap.so.db.request.beans.OperationStatus
34 import org.onap.so.db.request.beans.ResourceOperationStatus
35 import org.onap.so.serviceinstancebeans.ModelInfo
36 import org.onap.so.bpmn.core.UrnPropertiesReader
37 import org.slf4j.Logger
38 import org.slf4j.LoggerFactory
39 import static org.apache.commons.lang3.StringUtils.*;
40 import com.fasterxml.jackson.databind.ObjectMapper
41 import groovy.json.JsonSlurper
42 import javax.ws.rs.core.Response
43 import org.onap.so.bpmn.common.scripts.OofUtils
44 import org.onap.so.bpmn.common.scripts.RequestDBUtil
45
46 class DoAllocateCoreNSSI extends AbstractServiceTaskProcessor {
47     String Prefix="DACNSSI_"
48     private static final Logger logger = LoggerFactory.getLogger( DoAllocateCoreNSSI.class);
49     ExceptionUtil exceptionUtil = new ExceptionUtil()
50     CatalogDbUtils catalogDbUtils = new CatalogDbUtilsFactory().create()
51     JsonUtils jsonUtil = new JsonUtils()
52     RequestDBUtil requestDBUtil = new RequestDBUtil()
53     ObjectMapper mapper = new ObjectMapper()
54     OofUtils oofUtils = new OofUtils()
55     void preProcessRequest(DelegateExecution execution) {
56         logger.debug(Prefix+" **** Enter DoAllocateCoreNSSI ::: preProcessRequest ****")
57         execution.setVariable("prefix", Prefix)
58         String msg = ""
59         //Get SliceProfile from sliceParams JSON
60         String sliceProfile = jsonUtil.getJsonValue(execution.getVariable("sliceParams"), "sliceProfile")
61         if (isBlank(sliceProfile)) {
62             msg = "Slice Profile is null"
63             exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
64         } else {
65             execution.setVariable("sliceProfile", sliceProfile)
66         }
67         String coreServiceInstanceId = UUID.randomUUID().toString()
68         execution.setVariable("coreServiceInstanceId", coreServiceInstanceId)
69         logger.debug(Prefix+" **** Exit DoAllocateCoreNSSI ::: preProcessRequest ****")
70     }
71
72     void getNSSTName(DelegateExecution execution){
73         logger.debug(Prefix+" **** Enter DoAllocateCoreNSSI ::: getNSSTName ****")
74         String nsstModelInvariantUuid = execution.getVariable("modelInvariantUuid")
75         try{
76             String json = catalogDbUtils.getServiceResourcesByServiceModelInvariantUuidString(execution, nsstModelInvariantUuid)
77             logger.debug("***** JSON Response is: "+json)
78             String nsstName = jsonUtil.getJsonValue(json, "serviceResources.modelInfo.modelName") ?: ""
79             List serviceProxyList = jsonUtil.StringArrayToList(jsonUtil.getJsonValue(json, "serviceResources.serviceProxy"))
80             String networkServiceModelInfo = serviceProxyList.get(0)
81             execution.setVariable("networkServiceModelInfo", networkServiceModelInfo)
82             logger.debug("***** nsstName is: "+ nsstName)
83             execution.setVariable("nsstName",nsstName)
84         }catch(BpmnError e){
85             throw e
86         } catch (Exception ex){
87             String msg = "Exception in preProcessRequest " + ex.getMessage()
88             logger.debug(msg)
89             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
90         }
91         logger.debug(Prefix+" **** Exit DoAllocateCoreNSSI ::: getNSSTName ****")
92     }
93
94     void prepareOOFRequest(DelegateExecution execution){
95         logger.debug(Prefix+" **** Enter DoAllocateCoreNSSI ::: prepareOOFRequest ****")
96         //API Path
97         String apiPath =  "/api/oof/selection/nssi/v1"
98         logger.debug("API path for DoAllocateCoreNSSI: "+apiPath)
99         execution.setVariable("NSSI_apiPath", apiPath)
100         //Setting correlator as requestId
101         String requestId = execution.getVariable("msoRequestId")
102         execution.setVariable("NSSI_correlator", requestId)
103         //Setting messageType for all Core slice as cn
104         String messageType = "cn"
105         execution.setVariable("NSSI_messageType", messageType)
106         String timeout = "PT30M"
107         execution.setVariable("NSSI_timeout", timeout)
108         Map<String, Object> profileInfo = mapper.readValue(execution.getVariable("sliceProfile"), Map.class)
109         String nsstModelUuid = execution.getVariable("modelUuid")
110         String nsstModelInvariantUuid = execution.getVariable("modelInvariantUuid")
111         String nsstName = execution.getVariable("nsstName")
112         String oofRequest = oofUtils.buildSelectNSSIRequest(requestId, messageType, nsstModelUuid, nsstModelInvariantUuid, nsstName, profileInfo)
113         logger.debug("**** OOfRequest for Core Slice: "+oofRequest)
114         execution.setVariable("NSSI_oofRequest", oofRequest)
115         logger.debug(Prefix+" **** Exit DoAllocateCoreNSSI ::: prepareOOFRequest ****")
116     }
117
118     void processOOFAsyncResponse(DelegateExecution execution) {
119         logger.debug(Prefix+ " **** Enter DoAllocateCoreNSSI ::: processOOFAsyncResponse ****")
120         String OOFResponse = execution.getVariable("NSSI_asyncCallbackResponse")
121         String requestStatus = jsonUtil.getJsonValue(OOFResponse, "requestStatus")
122         logger.debug("NSSI OOFResponse is: " + OOFResponse)
123         execution.setVariable("OOFResponse", OOFResponse)
124         String solutions =""
125         if(requestStatus.equals("completed")) {
126             List solutionsList = jsonUtil.StringArrayToList(jsonUtil.getJsonValue(OOFResponse, "solutions"))
127             if(solutionsList!=null && !solutionsList.isEmpty() ) {
128                 solutions = solutionsList.get(0)
129             }
130         } else {
131             String statusMessage = jsonUtil.getJsonValue(OOFResponse, "statusMessage")
132             logger.error("received failed status from oof "+ statusMessage)
133             exceptionUtil.buildAndThrowWorkflowException(execution, 7000,"Received a failed Async Response from OOF : "+statusMessage)
134         }
135         execution.setVariable("solutions", solutions)
136         logger.debug(Prefix+" **** Exit DoAllocateCoreNSSI ::: processOOFAsyncResponse ****")
137     }
138
139     void prepareFailedOperationStatusUpdate(DelegateExecution execution){
140         logger.debug(Prefix + " **** Enter DoAllocateCoreNSSI ::: prepareFailedOperationStatusUpdate ****")
141         String serviceId = execution.getVariable("nsiId")
142         String jobId = execution.getVariable("jobId")
143         String nsiId = execution.getVariable("nsiId")
144         String nssiId = execution.getVariable("nssiId")
145         String operationType = "ALLOCATE"
146         logger.debug("serviceId: "+serviceId+" jobId: "+jobId+" nsiId: "+nsiId+" operationType: "+operationType)
147         String modelUuid= execution.getVariable("modelUuid")
148         ResourceOperationStatus resourceOperationStatus = new ResourceOperationStatus()
149         resourceOperationStatus.setServiceId(serviceId)
150         resourceOperationStatus.setJobId(jobId)
151         resourceOperationStatus.setOperationId(jobId)
152         resourceOperationStatus.setResourceTemplateUUID(modelUuid)
153         resourceOperationStatus.setOperType(operationType)
154         resourceOperationStatus.setProgress("0")
155         resourceOperationStatus.setStatus("failed")
156         resourceOperationStatus.setStatusDescription("Core NSSI Allocate Failed")
157         requestDBUtil.prepareUpdateResourceOperationStatus(execution, resourceOperationStatus)
158         logger.debug(Prefix + " **** Exit DoAllocateCoreNSSI ::: prepareFailedOperationStatusUpdate ****")
159     }
160 }