64c36e70261a76693febd1c4b562f861d6ea5ec8
[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      * Query NSST name from CatalogDB
73      * @param execution
74      */
75     void getNSSTName(DelegateExecution execution){
76         logger.debug(Prefix+" **** Enter DoAllocateCoreNSSI ::: getNSSTName ****")
77         String nsstModelInvariantUuid = execution.getVariable("modelInvariantUuid")
78         try{
79             String json = catalogDbUtils.getServiceResourcesByServiceModelInvariantUuidString(execution, nsstModelInvariantUuid)
80             logger.debug("***** JSON Response is: "+json)
81             String nsstName = jsonUtil.getJsonValue(json, "serviceResources.modelInfo.modelName") ?: ""
82             String networkServiceModelInfo = jsonUtil.getJsonValue(json, "serviceResources.serviceProxy.modelInfo") ?: ""
83
84             execution.setVariable("networkServiceModelInfo", networkServiceModelInfo)
85             logger.debug("***** nsstName is: "+ nsstName)
86             execution.setVariable("nsstName",nsstName)
87         }catch(BpmnError e){
88             throw e
89         } catch (Exception ex){
90             String msg = "Exception in preProcessRequest " + ex.getMessage()
91             logger.debug(msg)
92             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
93         }
94         logger.debug(Prefix+" **** Exit DoAllocateCoreNSSI ::: getNSSTName ****")
95     }
96     void prepareOOFRequest(DelegateExecution execution){
97         logger.debug(Prefix+" **** Enter DoAllocateCoreNSSI ::: prepareOOFRequest ****")
98         //API Path
99         String apiPath =  "/api/oof/selection/nssi/v1"
100         logger.debug("API path for DoAllocateCoreNSSI: "+apiPath)
101         execution.setVariable("NSSI_apiPath", apiPath)
102         //Setting correlator as requestId
103         String requestId = execution.getVariable("msoRequestId")
104         execution.setVariable("NSSI_correlator", requestId)
105         //Setting messageType for all Core slice as cn
106         String messageType = "cn"
107         execution.setVariable("NSSI_messageType", messageType)
108         //Is there any specific timeout we have to set or else we don't need to send
109         //if blank will be set default value in DoHandleOofRequest
110         String timeout = "PT30M"
111         execution.setVariable("NSSI_timeout", timeout)
112         Map<String, Object> profileInfo = mapper.readValue(execution.getVariable("sliceProfile"), Map.class)
113         String nsstModelUuid = execution.getVariable("modelUuid")
114         String nsstModelInvariantUuid = execution.getVariable("modelInvariantUuid")
115         String nsstName = execution.getVariable("nsstName")
116         String oofRequest = oofUtils.buildSelectNSSIRequest(requestId, messageType, nsstModelUuid, nsstModelInvariantUuid, nsstName, profileInfo)
117         logger.debug("**** OOfRequest for Core Slice: "+oofRequest)
118         execution.setVariable("NSSI_oofRequest", oofRequest)
119         logger.debug(Prefix+" **** Exit DoAllocateCoreNSSI ::: prepareOOFRequest ****")
120     }
121
122     void processOOFAsyncResponse(DelegateExecution execution) {
123         logger.debug(Prefix+ " **** Enter DoAllocateCoreNSSI ::: processOOFAsyncResponse ****")
124         String OOFResponse = execution.getVariable("NSSI_asyncCallbackResponse")
125         String requestStatus = jsonUtil.getJsonValue(OOFResponse, "requestStatus")
126         logger.debug("NSSI OOFResponse is: " + OOFResponse)
127         execution.setVariable("OOFResponse", OOFResponse)
128         String solutions =""
129         if(requestStatus.equals("completed")) {
130             solutions = jsonUtil.getJsonValue(OOFResponse, "solutions")
131         } else {
132             String statusMessage = jsonUtil.getJsonValue(OOFResponse, "statusMessage")
133             logger.error("received failed status from oof "+ statusMessage)
134             exceptionUtil.buildAndThrowWorkflowException(execution, 7000,"Received a failed Async Response from OOF : "+statusMessage)
135         }
136         execution.setVariable("solutions", solutions)
137         logger.debug(Prefix+" **** Exit DoAllocateCoreNSSI ::: processOOFAsyncResponse ****")
138     }
139
140     void prepareFailedOperationStatusUpdate(DelegateExecution execution){
141         logger.debug(Prefix + " **** Enter DoAllocateCoreNSSI ::: prepareFailedOperationStatusUpdate ****")
142         String serviceId = execution.getVariable("nssiId")
143         String jobId = execution.getVariable("jobId")
144         String nsiId = execution.getVariable("nsiId")
145         String operationType = execution.getVariable("operationType")
146         ResourceOperationStatus resourceOperationStatus = new ResourceOperationStatus()
147         resourceOperationStatus.setServiceId(serviceId)
148         resourceOperationStatus.setOperationId(jobId)
149         resourceOperationStatus.setResourceTemplateUUID(nsiId)
150         resourceOperationStatus.setOperType(operationType)
151         resourceOperationStatus.setProgress(0)
152         resourceOperationStatus.setStatus("failed")
153         resourceOperationStatus.setStatusDescription("Core NSSI Allocate Failed")
154         requestDBUtil.prepareUpdateResourceOperationStatus(execution, resourceOperationStatus)
155         logger.debug(Prefix + " **** Exit DoAllocateCoreNSSI ::: prepareFailedOperationStatusUpdate ****")
156     }
157 }