Fixed issues in AN NSSMF for modify & Deallocate 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 "deallocate":
79                                                 String sliceProfile = execution.getVariable("additionalProperties")
80                                                 execution.setVariable("sliceProfile", sliceProfile)
81                                                 break
82                                         case "reconfigure":
83                                                 String resourceConfig = execution.getVariable("additionalProperties")
84                                                 execution.setVariable("resourceConfig", resourceConfig)
85                                                 break
86                                         default:
87                                                 logger.debug("Invalid modify Action")
88                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid modify Action : "+modifyAction)
89                                 }
90                         }
91                         List<String> snssaiList = execution.getVariable("snssaiList")
92                         String sliceProfileId = execution.getVariable("sliceProfileId")
93                         if (isBlank(sliceProfileId) || (snssaiList.empty)) {
94                                 msg = "Mandatory fields are empty"
95                                 logger.debug(msg)
96                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
97                         } else {
98                                 execution.setVariable("sliceProfileId", sliceProfileId)
99                                 execution.setVariable("snssaiList", snssaiList)
100                                 execution.setVariable("snssai", snssaiList.get(0))
101                         }
102                         
103                 } catch(BpmnError e) {
104                         throw e
105                 } catch(Exception ex) {
106                         msg = "Exception in DoModifyAccessNssi.preProcessRequest " + ex.getMessage()
107                         logger.debug(msg)
108                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
109                 }
110                 logger.debug(Prefix + "preProcessRequest Exit")
111         }
112         
113         def createSdnrRequest = { DelegateExecution execution ->
114                 logger.debug(Prefix+"createSdnrRequest method start")
115                 String callbackUrl = UrnPropertiesReader.getVariable("mso.workflow.message.endpoint") + "/AsyncSdnrResponse/"+execution.getVariable("msoRequestId")
116                 String modifyAction = execution.getVariable("modifyAction")
117                 String sdnrRequest = buildSdnrAllocateRequest(execution, modifyAction, "InstantiateRANSlice", callbackUrl)
118                 execution.setVariable("createNSSI_sdnrRequest", sdnrRequest)
119                 execution.setVariable("createNSSI_timeout", "PT10M")
120                 execution.setVariable("createNSSI_correlator", execution.getVariable("msoRequestId"))
121                 execution.setVariable("createNSSI_messageType", "AsyncSdnrResponse");
122         }
123         
124         def processSdnrResponse = { DelegateExecution execution ->
125                 logger.debug(Prefix+"processSdnrResponse method start")
126                 String SDNRResponse = execution.getVariable("SDNR_asyncCallbackResponse")
127                 String status = jsonUtil.getJsonValue(SDNRResponse, "status")
128                 if(status.equalsIgnoreCase("success")) {
129                         String nfIds = jsonUtil.getJsonValue(SDNRResponse, "nfIds")
130                         execution.setVariable("ranNfIdsJson", nfIds)
131                         execution.setVariable("ranNfStatus", status)
132                 }else {
133                         String reason = jsonUtil.getJsonValue(SDNRResponse, "reason")
134                         logger.error("received failed status from SDNR "+ reason)
135                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000,"received failed status from SDNR "+ reason)
136                 }
137                 logger.debug("response from SDNR "+SDNRResponse)
138         }
139         
140         private String buildSdnrAllocateRequest(DelegateExecution execution, String action, String rpcName, String callbackUrl) {
141                 
142                 String requestId = execution.getVariable("msoRequestId")
143                 Instant time = Instant.now()
144                 Map<String,Object> sliceProfile = new HashMap<>()
145                 JsonObject response = new JsonObject()
146                 JsonObject body = new JsonObject()
147                 JsonObject input = new JsonObject()
148                 JsonObject commonHeader = new JsonObject()
149                 JsonObject payload = new JsonObject()
150                 JsonObject payloadInput = new JsonObject()
151                 JsonParser parser = new JsonParser()
152                 if(action.equals("allocate")) {
153                         sliceProfile = objectMapper.readValue(execution.getVariable("sliceProfile"), Map.class)
154                         sliceProfile.put("sliceProfileId", execution.getVariable("sliceProfileId"))
155                         sliceProfile.put("maxNumberofConns", sliceProfile.get("maxNumberofPDUSessions"))
156                         sliceProfile.put("uLThptPerSlice", sliceProfile.get("expDataRateUL"))
157                         sliceProfile.put("dLThptPerSlice", sliceProfile.get("expDataRateDL"))
158                         action = "modify-"+action
159                         payloadInput.add("additionalproperties", new JsonObject())
160                 }else if(action.equals("deallocate")) {
161                         action = "modify-"+action
162                         sliceProfile.put("sliceProfileId", execution.getVariable("sliceProfileId"))
163                         sliceProfile.put("sNSSAI", execution.getVariable("snssai"))
164                         payloadInput.add("additionalproperties", new JsonObject())
165                 }else if(action.equals("reconfigure")) {
166                         sliceProfile.put("sliceProfileId", execution.getVariable("sliceProfileId"))
167                         sliceProfile.put("sNSSAI", execution.getVariable("snssai"))
168                         JsonObject resourceconfig = new JsonObject()
169                         resourceconfig.add("resourceConfig", (JsonObject) parser.parse(execution.getVariable("resourceConfig")))
170                         payloadInput.add("additionalproperties", resourceconfig)
171                 }
172                 commonHeader.addProperty("timestamp", time.toString())
173                 commonHeader.addProperty("api-ver", "1.0")
174                 commonHeader.addProperty("request-id", requestId)
175                 commonHeader.addProperty("sub-request-id", "1")
176                 commonHeader.add("flags", new JsonObject())
177                 payloadInput.addProperty("sliceProfile", sliceProfile.toString())
178                 payloadInput.addProperty("RANNFNSSIId", execution.getVariable("serviceInstanceID"))
179                 payloadInput.addProperty("callbackURL", callbackUrl)
180                 payload.add("input", payloadInput)
181                 input.add("common-header", commonHeader)
182                 input.addProperty("action", action)
183                 input.addProperty("payload", payload.toString())
184                 body.add("input", input)
185                 response.add("body", body)
186                 response.addProperty("version", "1.0")
187                 response.addProperty("rpc-name", rpcName)
188                 response.addProperty("correlation-id", requestId+"-1")
189                 response.addProperty("type", "request")
190                 return response.toString()
191         }
192         
193 }