Fix issues in Modify RAN-NF NSSI reconfigure flow
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoModifyRanNfNssi.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2020, Wipro Limited.
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
23 import org.camunda.bpm.engine.delegate.BpmnError
24 import org.slf4j.Logger
25 import org.slf4j.LoggerFactory
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
28 import org.onap.so.bpmn.common.scripts.ExceptionUtil
29 import org.onap.so.bpmn.core.json.JsonUtils
30 import com.fasterxml.jackson.databind.ObjectMapper
31 import com.google.gson.JsonObject
32 import com.google.gson.JsonParser
33 import java.time.Instant
34
35 import static org.apache.commons.lang3.StringUtils.isBlank
36 import org.onap.so.bpmn.core.UrnPropertiesReader
37
38 class DoModifyRanNfNssi extends AbstractServiceTaskProcessor {
39
40         String Prefix="MANNFNSS_"
41         ExceptionUtil exceptionUtil = new ExceptionUtil()
42         JsonUtils jsonUtil = new JsonUtils()
43         ObjectMapper objectMapper = new ObjectMapper();
44         AnNssmfUtils anNssmfUtils = new AnNssmfUtils()
45
46         private static final Logger logger = LoggerFactory.getLogger(DoModifyRanNfNssi.class)
47
48         @Override
49         void preProcessRequest(DelegateExecution execution) {
50                 logger.debug(Prefix + "preProcessRequest Start")
51                 execution.setVariable("prefix", Prefix)
52                 execution.setVariable("startTime", System.currentTimeMillis())
53                 def msg
54                 try {
55
56                         logger.debug("input variables : msoRequestId - "+execution.getVariable("msoRequestId")+
57                                         " globalSubscriberId - "+execution.getVariable("globalSubscriberId")+
58                                         " serviceInstanceID - "+execution.getVariable("serviceInstanceID")+
59                                         " subscriptionServiceType - "+execution.getVariable("subscriptionServiceType")+
60                                         " sliceProfileId - "+execution.getVariable("sliceProfileId")+
61                                         " snssaiList - "+execution.getVariable("snssaiList")+
62                                         " modifyAction - "+execution.getVariable("modifyAction"))
63
64                         //validate RAN NF slice subnet inputs
65
66                         String modifyAction = execution.getVariable("modifyAction")
67                         if (isBlank(modifyAction)) {
68                                 msg = "Input modifyAction is null"
69                                 logger.debug(msg)
70                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
71                         } else {
72                                 execution.setVariable("modifyAction", modifyAction)
73                                 switch(modifyAction) {
74                                         case "allocate":
75                                                 String sliceProfile = execution.getVariable("additionalProperties")
76                                                 execution.setVariable("sliceProfile", sliceProfile)
77                                                 break
78                                         case "reconfigure":
79                                                 String resourceConfig = execution.getVariable("additionalProperties")
80                                                 execution.setVariable("resourceConfig", resourceConfig)
81                                                 break
82                                         default:
83                                                 logger.debug("Invalid modify Action")
84                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid modify Action : "+modifyAction)
85                                 }
86                         }
87                         List<String> snssaiList = execution.getVariable("snssaiList")
88                         String sliceProfileId = execution.getVariable("sliceProfileId")
89                         if (isBlank(sliceProfileId) || (snssaiList.empty)) {
90                                 msg = "Mandatory fields are empty"
91                                 logger.debug(msg)
92                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
93                         } else {
94                                 execution.setVariable("sliceProfileId", sliceProfileId)
95                                 execution.setVariable("snssaiList", snssaiList)
96                                 execution.setVariable("snssai", snssaiList.get(0))
97                         }
98                         
99                 } catch(BpmnError e) {
100                         throw e
101                 } catch(Exception ex) {
102                         msg = "Exception in DoModifyAccessNssi.preProcessRequest " + ex.getMessage()
103                         logger.debug(msg)
104                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
105                 }
106                 logger.debug(Prefix + "preProcessRequest Exit")
107         }
108         
109         def createSdnrRequest = { DelegateExecution execution ->
110                 logger.debug(Prefix+"createSdnrRequest method start")
111                 String callbackUrl = UrnPropertiesReader.getVariable("mso.workflow.message.endpoint") + "/AsyncSdnrResponse/"+execution.getVariable("msoRequestId")
112                 String modifyAction = execution.getVariable("modifyAction")
113                 String sdnrRequest = buildSdnrAllocateRequest(execution, modifyAction, "instantiateRANSlice", callbackUrl)
114                 execution.setVariable("createNSSI_sdnrRequest", sdnrRequest)
115                 execution.setVariable("createNSSI_timeout", "PT10M")
116                 execution.setVariable("createNSSI_correlator", execution.getVariable("msoRequestId"))
117                 execution.setVariable("createNSSI_messageType", "AsyncSdnrResponse");
118         }
119         
120         def processSdnrResponse = { DelegateExecution execution ->
121                 logger.debug(Prefix+"processSdnrResponse method start")
122                 String SDNRResponse = execution.getVariable("SDNR_asyncCallbackResponse")
123                 String status = jsonUtil.getJsonValue(SDNRResponse, "status")
124                 if(status.equalsIgnoreCase("success")) {
125                         String nfIds = jsonUtil.getJsonValue(SDNRResponse, "nfIds")
126                         execution.setVariable("ranNfIdsJson", nfIds)
127                         execution.setVariable("ranNfStatus", status)
128                 }else {
129                         String reason = jsonUtil.getJsonValue(SDNRResponse, "reason")
130                         logger.error("received failed status from SDNR "+ reason)
131                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000,"received failed status from SDNR "+ reason)
132                 }
133                 logger.debug("response from SDNR "+SDNRResponse)
134         }
135         
136         private String buildSdnrAllocateRequest(DelegateExecution execution, String action, String rpcName, String callbackUrl) {
137                 
138                 String requestId = execution.getVariable("msoRequestId")
139                 Instant time = Instant.now()
140                 Map<String,Object> sliceProfile = new HashMap<>()
141                 JsonObject response = new JsonObject()
142                 JsonObject body = new JsonObject()
143                 JsonObject input = new JsonObject()
144                 JsonObject commonHeader = new JsonObject()
145                 JsonObject payload = new JsonObject()
146                 JsonObject payloadInput = new JsonObject()
147                 JsonParser parser = new JsonParser()
148                 if(action.equals("allocate")) {
149                         sliceProfile = objectMapper.readValue(execution.getVariable("sliceProfile"), Map.class)
150                         sliceProfile.put("sliceProfileId", execution.getVariable("sliceProfileId"))
151                         sliceProfile.put("maxNumberofConns", sliceProfile.get("maxNumberofPDUSessions"))
152                         sliceProfile.put("uLThptPerSlice", sliceProfile.get("expDataRateUL"))
153                         sliceProfile.put("dLThptPerSlice", sliceProfile.get("expDataRateDL"))
154                         action = "modify-"+action
155                         payloadInput.add("additionalproperties", new JsonObject())
156                 }else if(action.equals("deallocate")) {
157                         action = "modify-"+action
158                         sliceProfile.put("sliceProfileId", execution.getVariable("sliceProfileId"))
159                         sliceProfile.put("sNSSAI", execution.getVariable("snssai"))
160                         payloadInput.add("additionalproperties", new JsonObject())
161                 }else if(action.equals("reconfigure")) {
162                         sliceProfile.put("sliceProfileId", execution.getVariable("sliceProfileId"))
163                         sliceProfile.put("sNSSAI", execution.getVariable("snssai"))
164                         JsonObject resourceconfig = new JsonObject()
165                         resourceconfig.add("resourceConfig", (JsonObject) parser.parse(execution.getVariable("resourceConfig")))
166                         payloadInput.add("additionalproperties", resourceconfig)
167                 }
168                 commonHeader.addProperty("timestamp", time.toString())
169                 commonHeader.addProperty("api-ver", "1.0")
170                 commonHeader.addProperty("request-id", requestId)
171                 commonHeader.addProperty("sub-request-id", "1")
172                 commonHeader.add("flags", new JsonObject())
173                 payloadInput.addProperty("sliceProfile", sliceProfile.toString())
174                 payloadInput.addProperty("RANNFNSSIId", execution.getVariable("serviceInstanceID"))
175                 payloadInput.addProperty("callbackURL", callbackUrl)
176                 payload.add("input", payloadInput)
177                 input.add("common-header", commonHeader)
178                 input.addProperty("action", action)
179                 input.addProperty("payload", payload.toString())
180                 body.add("input", input)
181                 response.add("body", body)
182                 response.addProperty("version", "1.0")
183                 response.addProperty("rpc-name", rpcName)
184                 response.addProperty("correlation-id", requestId+"-1")
185                 response.addProperty("type", "request")
186                 return response.toString()
187         }
188         
189 }