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