a784cbee6e7c7bf258d9519858c00de5fc9efd91
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoAllocateNSSI.groovy
1 package org.onap.so.bpmn.infrastructure.scripts
2
3 import com.fasterxml.jackson.databind.ObjectMapper
4 import org.apache.commons.lang3.StringUtils
5 import org.camunda.bpm.engine.delegate.DelegateExecution
6 import org.onap.so.beans.nsmf.EsrInfo
7 import org.onap.so.beans.nsmf.JobStatusResponse
8 import org.onap.so.beans.nsmf.NssiResponse
9 import org.onap.so.beans.nsmf.NssmfAdapterNBIRequest
10 import org.onap.so.beans.nsmf.ResponseDescriptor
11 import org.onap.so.beans.nsmf.ServiceInfo
12 import org.onap.so.beans.nsmf.SliceTaskInfo
13 import org.onap.so.beans.nsmf.SliceTaskParamsAdapter
14 import org.onap.so.beans.nsmf.oof.SubnetType
15 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
16 import org.onap.so.bpmn.common.scripts.ExceptionUtil
17 import org.onap.so.bpmn.common.scripts.NssmfAdapterUtils
18 import org.onap.so.bpmn.core.json.JsonUtils
19 import org.slf4j.Logger
20 import org.slf4j.LoggerFactory
21
22
23 class DoAllocateNSSI extends AbstractServiceTaskProcessor {
24
25     private static final Logger logger = LoggerFactory.getLogger(DoAllocateNSSI.class);
26
27     ExceptionUtil exceptionUtil = new ExceptionUtil()
28
29     JsonUtils jsonUtil = new JsonUtils()
30
31     ObjectMapper objectMapper = new ObjectMapper()
32
33     private NssmfAdapterUtils nssmfAdapterUtils = new NssmfAdapterUtils(httpClientFactory, jsonUtil)
34
35     private static final NSSMF_ALLOCATE_URL = "/api/rest/provMns/v1/NSS/SliceProfiles"
36
37     private static final NSSMF_QUERY_JOB_STATUS_URL = "/api/rest/provMns/v1/NSS/jobs/%s"
38
39     @Override
40     void preProcessRequest(DelegateExecution execution) {
41         logger.trace("Enter preProcessRequest()")
42
43         NssmfAdapterNBIRequest nbiRequest = execution.getVariable("nbiRequest") as NssmfAdapterNBIRequest
44
45         execution.setVariable("currentCycle", 0)
46         boolean isNSIOptionAvailable = execution.getVariable("isNSIOptionAvailable") as Boolean
47
48         if (!isNSIOptionAvailable) {
49             nbiRequest.serviceInfo.setActionType("allocate")
50         } else if (StringUtils.isBlank(nbiRequest.serviceInfo.nssiId)){
51             nbiRequest.serviceInfo.setActionType("allocate")
52         } else {
53             nbiRequest.serviceInfo.setActionType("modify")
54         }
55         execution.setVariable("nbiRequest", nbiRequest)
56         logger.trace("Exit preProcessRequest")
57     }
58
59     /**
60      * send Create Request NSSMF
61      * @param execution
62      */
63     void sendCreateRequestNSSMF(DelegateExecution execution) {
64         NssmfAdapterNBIRequest nbiRequest = execution.getVariable("nbiRequest") as NssmfAdapterNBIRequest
65         String nssmfRequest = objectMapper.writeValueAsString(nbiRequest)
66         logger.debug("sendCreateRequestNSSMF: " + nssmfRequest)
67
68         String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, NSSMF_ALLOCATE_URL, nssmfRequest)
69
70         if (response != null) {
71             NssiResponse nssiResponse = objectMapper.readValue(response, NssiResponse.class)
72             execution.setVariable("nssiAllocateResult", nssiResponse)
73         }
74
75         execution.setVariable("serviceInfo", nbiRequest.getServiceInfo())
76         execution.setVariable("esrInfo", nbiRequest.getEsrInfo())
77     }
78
79     /**
80      * query nssi allocate status
81      * @param execution
82      */
83     void queryNSSIStatus(DelegateExecution execution) {
84         NssmfAdapterNBIRequest nbiRequest = new NssmfAdapterNBIRequest()
85         NssiResponse nssiAllocateResult = execution.getVariable("nssiAllocateResult") as NssiResponse
86         String jobId = nssiAllocateResult.getJobId()
87         String nssiId = nssiAllocateResult.getNssiId()
88
89         ServiceInfo serviceInfo = execution.getVariable("serviceInfo") as ServiceInfo
90         serviceInfo.setNssiId(nssiId)
91         EsrInfo esrInfo = execution.getVariable("esrInfo") as EsrInfo
92
93         //nbiRequest.setResponseId(jobId)
94         nbiRequest.setServiceInfo(serviceInfo)
95         nbiRequest.setEsrInfo(esrInfo)
96
97         String endpoint = String.format(NSSMF_QUERY_JOB_STATUS_URL, jobId)
98
99         String response =
100                 nssmfAdapterUtils.sendPostRequestNSSMF(execution, endpoint, objectMapper.writeValueAsString(nbiRequest))
101
102         logger.debug("nssmf response nssiAllocateStatus:" + response)
103
104         if (response != null) {
105             JobStatusResponse jobStatusResponse = objectMapper.readValue(response, JobStatusResponse.class)
106             if (StringUtils.isBlank(nssiId)) {
107                 nssiAllocateResult.setNssiId(jobStatusResponse.getResponseDescriptor().getNssiId())
108                 execution.setVariable("nssiAllocateResult", nssiAllocateResult)
109             }
110
111             execution.setVariable("nssiAllocateStatus", jobStatusResponse)
112             if (jobStatusResponse.getResponseDescriptor().getProgress() == 100) {
113                 execution.setVariable("jobFinished", true)
114             }
115         }
116     }
117
118     void prepareUpdateOrchestrationTask(DelegateExecution execution) {
119         logger.debug("Start prepareUpdateOrchestrationTask progress")
120         String requestMethod = "PUT"
121
122         SliceTaskParamsAdapter sliceParams =
123                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
124         JobStatusResponse jobStatusResponse = execution.getVariable("nssiAllocateStatus") as JobStatusResponse
125         ResponseDescriptor response = jobStatusResponse.getResponseDescriptor()
126         SubnetType subnetType = execution.getVariable("subnetType") as SubnetType
127
128         SliceTaskInfo sliceTaskInfo = execution.getVariable("sliceTaskInfo") as SliceTaskInfo
129         sliceTaskInfo.progress = response.getProgress()
130         sliceTaskInfo.status = response.getStatus().toLowerCase()
131         sliceTaskInfo.statusDescription = response.getStatusDescription()
132         updateNssiResult(sliceParams, subnetType, sliceTaskInfo)
133
134         execution.setVariable("CSSOT_paramJson", objectMapper.writeValueAsString(sliceParams))
135         execution.setVariable("CSSOT_requestMethod", requestMethod)
136
137         execution.setVariable("sliceTaskParams", sliceParams)
138         execution.setVariable("sliceTaskInfo", sliceTaskInfo)
139
140         logger.debug("Finish prepareUpdateOrchestrationTask progress")
141     }
142
143     private void updateNssiResult(SliceTaskParamsAdapter sliceTaskParams, SubnetType subnetType,
144                                   SliceTaskInfo sliceTaskInfo) {
145         switch (subnetType) {
146             case SubnetType.CN:
147                 sliceTaskParams.cnSliceTaskInfo = sliceTaskInfo
148                 break
149             case SubnetType.AN:
150                 sliceTaskParams.anSliceTaskInfo = sliceTaskInfo
151                 break
152             case SubnetType.TN_BH:
153                 sliceTaskParams.tnBHSliceTaskInfo = sliceTaskInfo
154                 break
155             case SubnetType.TN_FH:
156                 sliceTaskParams.tnFHSliceTaskInfo = sliceTaskInfo
157                 break
158             case SubnetType.TN_MH:
159                 sliceTaskParams.tnMHSliceTaskInfo = sliceTaskInfo
160                 break
161         }
162     }
163
164     void timeDelay(DelegateExecution execution) {
165         logger.trace("Enter timeDelay in DoAllocateNSSI()")
166         try {
167             Thread.sleep(60000)
168             int currentCycle = execution.getVariable("currentCycle") as Integer
169             currentCycle = currentCycle + 1
170             if(currentCycle >  60)
171             {
172                 logger.trace("Completed all the retry times... but still nssmf havent completed the creation process...")
173                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "NSSMF creation didnt complete by time...")
174             }
175             execution.setVariable("currentCycle", currentCycle)
176         } catch(InterruptedException e) {
177             logger.info("Time Delay exception" + e)
178         }
179         logger.trace("Exit timeDelay in DoAllocateNSSI()")
180     }
181
182 }