summaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DoModifyRanNfNssi.groovy
blob: bb8e516c3b600bc5b928d065a3d2f142c7447d7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 # Copyright (c) 2020, Wipro Limited.
 #
 # Licensed under the Apache License, Version 2.0 (the "License")
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
 #       http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.bpmn.infrastructure.scripts

import org.camunda.bpm.engine.delegate.BpmnError
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
import org.onap.so.bpmn.common.scripts.ExceptionUtil
import org.onap.so.bpmn.core.json.JsonUtils
import com.fasterxml.jackson.databind.ObjectMapper
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import java.time.Instant

import static org.apache.commons.lang3.StringUtils.isBlank
import org.onap.so.bpmn.core.UrnPropertiesReader

class DoModifyRanNfNssi extends AbstractServiceTaskProcessor {

	String Prefix="MANNFNSS_"
	ExceptionUtil exceptionUtil = new ExceptionUtil()
	JsonUtils jsonUtil = new JsonUtils()
	ObjectMapper objectMapper = new ObjectMapper();
	AnNssmfUtils anNssmfUtils = new AnNssmfUtils()

	private static final Logger logger = LoggerFactory.getLogger(DoModifyRanNfNssi.class)

	@Override
	void preProcessRequest(DelegateExecution execution) {
		logger.debug(Prefix + "preProcessRequest Start")
		execution.setVariable("prefix", Prefix)
		execution.setVariable("startTime", System.currentTimeMillis())
		def msg
		try {

			logger.debug("input variables : msoRequestId - "+execution.getVariable("msoRequestId")+
					" globalSubscriberId - "+execution.getVariable("globalSubscriberId")+
					" serviceInstanceID - "+execution.getVariable("serviceInstanceID")+
					" subscriptionServiceType - "+execution.getVariable("subscriptionServiceType")+
					" sliceProfileId - "+execution.getVariable("sliceProfileId")+
					" snssaiList - "+execution.getVariable("snssaiList")+
					" modifyAction - "+execution.getVariable("modifyAction"))

			//validate RAN NF slice subnet inputs

			String modifyAction = execution.getVariable("modifyAction")
			if (isBlank(modifyAction)) {
				msg = "Input modifyAction is null"
				logger.debug(msg)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
			} else {
				execution.setVariable("modifyAction", modifyAction)
				switch(modifyAction) {
					case "allocate":
						String sliceProfile = execution.getVariable("additionalProperties")
						execution.setVariable("sliceProfile", sliceProfile)
						break
                                        case "deallocate":
                                                String sliceProfile = execution.getVariable("additionalProperties")
                                                execution.setVariable("sliceProfile", sliceProfile)
                                                break
					case "reconfigure":
                                                String resourceConfig = execution.getVariable("additionalProperties")
						execution.setVariable("resourceConfig", resourceConfig)
						break
					default:
						logger.debug("Invalid modify Action")
						exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid modify Action : "+modifyAction)
				}
			}
			List<String> snssaiList = execution.getVariable("snssaiList")
			String sliceProfileId = execution.getVariable("sliceProfileId")
			if (isBlank(sliceProfileId) || (snssaiList.empty)) {
				msg = "Mandatory fields are empty"
				logger.debug(msg)
				exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
			} else {
				execution.setVariable("sliceProfileId", sliceProfileId)
				execution.setVariable("snssaiList", snssaiList)
				execution.setVariable("snssai", snssaiList.get(0))
			}
			
		} catch(BpmnError e) {
			throw e
		} catch(Exception ex) {
			msg = "Exception in DoModifyAccessNssi.preProcessRequest " + ex.getMessage()
			logger.debug(msg)
			exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
		}
		logger.debug(Prefix + "preProcessRequest Exit")
	}
	
	def createSdnrRequest = { DelegateExecution execution ->
		logger.debug(Prefix+"createSdnrRequest method start")
		String callbackUrl = UrnPropertiesReader.getVariable("mso.workflow.message.endpoint") + "/AsyncSdnrResponse/"+execution.getVariable("msoRequestId")
		String modifyAction = execution.getVariable("modifyAction")
		String sdnrRequest = buildSdnrAllocateRequest(execution, modifyAction, "InstantiateRANSlice", callbackUrl)
		execution.setVariable("createNSSI_sdnrRequest", sdnrRequest)
		execution.setVariable("createNSSI_timeout", "PT10M")
		execution.setVariable("createNSSI_correlator", execution.getVariable("msoRequestId"))
		execution.setVariable("createNSSI_messageType", "AsyncSdnrResponse");
	}
	
	def processSdnrResponse = { DelegateExecution execution ->
		logger.debug(Prefix+"processSdnrResponse method start")
		String SDNRResponse = execution.getVariable("SDNR_asyncCallbackResponse")
		String status = jsonUtil.getJsonValue(SDNRResponse, "status")
		if(status.equalsIgnoreCase("success")) {
			String nfIds = jsonUtil.getJsonValue(SDNRResponse, "nfIds")
			execution.setVariable("ranNfIdsJson", nfIds)
			execution.setVariable("ranNfStatus", status)
		}else {
			String reason = jsonUtil.getJsonValue(SDNRResponse, "reason")
			logger.error("received failed status from SDNR "+ reason)
			exceptionUtil.buildAndThrowWorkflowException(execution, 7000,"received failed status from SDNR "+ reason)
		}
		logger.debug("response from SDNR "+SDNRResponse)
	}
	
	private String buildSdnrAllocateRequest(DelegateExecution execution, String action, String rpcName, String callbackUrl) {
		
		String requestId = execution.getVariable("msoRequestId")
                Instant time = Instant.now()
                Map<String,Object> sliceProfile = new HashMap<>()
		JsonObject response = new JsonObject()
		JsonObject body = new JsonObject()
		JsonObject input = new JsonObject()
		JsonObject commonHeader = new JsonObject()
		JsonObject payload = new JsonObject()
		JsonObject payloadInput = new JsonObject()
                JsonParser parser = new JsonParser()
		if(action.equals("allocate")) {
			sliceProfile = objectMapper.readValue(execution.getVariable("sliceProfile"), Map.class)
			sliceProfile.put("sliceProfileId", execution.getVariable("sliceProfileId"))
			sliceProfile.put("maxNumberofConns", sliceProfile.get("maxNumberofPDUSessions"))
			sliceProfile.put("uLThptPerSlice", sliceProfile.get("expDataRateUL"))
			sliceProfile.put("dLThptPerSlice", sliceProfile.get("expDataRateDL"))
			action = "modify-"+action
			payloadInput.add("additionalproperties", new JsonObject())
		}else if(action.equals("deallocate")) {
			action = "modify-"+action
			sliceProfile.put("sliceProfileId", execution.getVariable("sliceProfileId"))
			sliceProfile.put("sNSSAI", execution.getVariable("snssai"))
			payloadInput.add("additionalproperties", new JsonObject())
		}else if(action.equals("reconfigure")) {
			sliceProfile.put("sliceProfileId", execution.getVariable("sliceProfileId"))
			sliceProfile.put("sNSSAI", execution.getVariable("snssai"))
			JsonObject resourceconfig = new JsonObject()
			resourceconfig.add("resourceConfig", (JsonObject) parser.parse(execution.getVariable("resourceConfig")))
			payloadInput.add("additionalproperties", resourceconfig)
		}
		commonHeader.addProperty("timestamp", time.toString())
		commonHeader.addProperty("api-ver", "1.0")
		commonHeader.addProperty("request-id", requestId)
		commonHeader.addProperty("sub-request-id", "1")
		commonHeader.add("flags", new JsonObject())
		payloadInput.addProperty("sliceProfile", sliceProfile.toString())
		payloadInput.addProperty("RANNFNSSIId", execution.getVariable("serviceInstanceID"))
		payloadInput.addProperty("callbackURL", callbackUrl)
		payload.add("input", payloadInput)
		input.add("common-header", commonHeader)
		input.addProperty("action", action)
		input.addProperty("payload", payload.toString())
		body.add("input", input)
		response.add("body", body)
		response.addProperty("version", "1.0")
		response.addProperty("rpc-name", rpcName)
		response.addProperty("correlation-id", requestId+"-1")
		response.addProperty("type", "request")
		return response.toString()
	}
	
}