fixed tn allocate fail problem
[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 response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, NSSMF_ALLOCATE_URL,
66                 objectMapper.writeValueAsString(nbiRequest))
67
68         if (response != null) {
69             NssiResponse nssiResponse = objectMapper.readValue(response, NssiResponse.class)
70             execution.setVariable("nssiAllocateResult", nssiResponse)
71         }
72
73         execution.setVariable("serviceInfo", nbiRequest.getServiceInfo())
74         execution.setVariable("esrInfo", nbiRequest.getEsrInfo())
75     }
76
77     /**
78      * query nssi allocate status
79      * @param execution
80      */
81     void queryNSSIStatus(DelegateExecution execution) {
82         NssmfAdapterNBIRequest nbiRequest = new NssmfAdapterNBIRequest()
83         NssiResponse nssiAllocateResult = execution.getVariable("nssiAllocateResult") as NssiResponse
84         String jobId = nssiAllocateResult.getJobId()
85         String nssiId = nssiAllocateResult.getNssiId()
86
87         ServiceInfo serviceInfo = execution.getVariable("serviceInfo") as ServiceInfo
88         serviceInfo.setNssiId(nssiId)
89         EsrInfo esrInfo = execution.getVariable("esrInfo") as EsrInfo
90
91         //nbiRequest.setResponseId(jobId)
92         nbiRequest.setServiceInfo(serviceInfo)
93         nbiRequest.setEsrInfo(esrInfo)
94
95         String endpoint = String.format(NSSMF_QUERY_JOB_STATUS_URL, jobId)
96
97         String response =
98                 nssmfAdapterUtils.sendPostRequestNSSMF(execution, endpoint, objectMapper.writeValueAsString(nbiRequest))
99
100         if (response != null) {
101             JobStatusResponse jobStatusResponse = objectMapper.readValue(response, JobStatusResponse.class)
102             if (StringUtils.isBlank(nssiId)) {
103                 nssiAllocateResult.setNssiId(jobStatusResponse.getResponseDescriptor().getNssiId())
104                 execution.setVariable("nssiAllocateResult", nssiAllocateResult)
105             }
106
107             execution.setVariable("nssiAllocateStatus", jobStatusResponse)
108             if (jobStatusResponse.getResponseDescriptor().getProgress() == 100) {
109                 execution.setVariable("jobFinished", true)
110             }
111         }
112     }
113
114     void prepareUpdateOrchestrationTask(DelegateExecution execution) {
115         logger.debug("Start prepareUpdateOrchestrationTask progress")
116         String requestMethod = "PUT"
117
118         SliceTaskParamsAdapter sliceParams =
119                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
120         JobStatusResponse jobStatusResponse = execution.getVariable("nssiAllocateStatus") as JobStatusResponse
121         ResponseDescriptor response = jobStatusResponse.getResponseDescriptor()
122         SubnetType subnetType = execution.getVariable("subnetType") as SubnetType
123
124         SliceTaskInfo sliceTaskInfo = execution.getVariable("sliceTaskInfo") as SliceTaskInfo
125         sliceTaskInfo.progress = response.getProgress()
126         sliceTaskInfo.status = response.getStatus().toLowerCase()
127         sliceTaskInfo.statusDescription = response.getStatusDescription()
128         sliceTaskInfo.endPointId = response.getEndPointId()
129         updateNssiResult(sliceParams, subnetType, sliceTaskInfo)
130
131         String paramJson = sliceParams.convertToJson()
132         execution.setVariable("CSSOT_paramJson", paramJson)
133         execution.setVariable("CSSOT_requestMethod", requestMethod)
134
135         execution.setVariable("sliceTaskParams", sliceParams)
136         execution.setVariable("sliceTaskInfo", sliceTaskInfo)
137
138         logger.debug("Finish prepareUpdateOrchestrationTask progress")
139     }
140
141     private void updateNssiResult(SliceTaskParamsAdapter sliceTaskParams, SubnetType subnetType,
142                                   SliceTaskInfo sliceTaskInfo) {
143         switch (subnetType) {
144             case SubnetType.CN:
145                 sliceTaskParams.cnSliceTaskInfo = sliceTaskInfo
146                 break
147             case SubnetType.AN:
148                 sliceTaskParams.anSliceTaskInfo = sliceTaskInfo
149                 break
150             case SubnetType.TN_BH:
151                 sliceTaskParams.tnBHSliceTaskInfo = sliceTaskInfo
152                 break
153             case SubnetType.TN_FH:
154                 sliceTaskParams.tnFHSliceTaskInfo = sliceTaskInfo
155                 break
156             case SubnetType.TN_MH:
157                 sliceTaskParams.tnMHSliceTaskInfo = sliceTaskInfo
158                 break
159         }
160     }
161
162     void timeDelay(DelegateExecution execution) {
163         logger.trace("Enter timeDelay in DoAllocateNSSI()")
164         try {
165             Thread.sleep(60000)
166             int currentCycle = execution.getVariable("currentCycle") as Integer
167             currentCycle = currentCycle + 1
168             if(currentCycle >  60)
169             {
170                 logger.trace("Completed all the retry times... but still nssmf havent completed the creation process...")
171                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "NSSMF creation didnt complete by time...")
172             }
173             execution.setVariable("currentCycle", currentCycle)
174         } catch(InterruptedException e) {
175             logger.info("Time Delay exception" + e)
176         }
177         logger.trace("Exit timeDelay in DoAllocateNSSI()")
178     }
179
180 }