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