aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DoAllocateNSSI.groovy
blob: 6981d9432466b409d64e6059deed7e3cdb717d31 (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
package org.onap.so.bpmn.infrastructure.scripts

import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.commons.lang3.StringUtils
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.onap.so.beans.nsmf.EsrInfo
import org.onap.so.beans.nsmf.JobStatusResponse
import org.onap.so.beans.nsmf.NssiResponse
import org.onap.so.beans.nsmf.NssmfAdapterNBIRequest
import org.onap.so.beans.nsmf.ResponseDescriptor
import org.onap.so.beans.nsmf.ServiceInfo
import org.onap.so.beans.nsmf.SliceTaskInfo
import org.onap.so.beans.nsmf.SliceTaskParamsAdapter
import org.onap.so.beans.nsmf.oof.SubnetType
import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
import org.onap.so.bpmn.common.scripts.ExceptionUtil
import org.onap.so.bpmn.common.scripts.NssmfAdapterUtils
import org.onap.so.bpmn.core.json.JsonUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory


class DoAllocateNSSI extends AbstractServiceTaskProcessor {

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

    ExceptionUtil exceptionUtil = new ExceptionUtil()

    JsonUtils jsonUtil = new JsonUtils()

    ObjectMapper objectMapper = new ObjectMapper()

    private NssmfAdapterUtils nssmfAdapterUtils = new NssmfAdapterUtils(httpClientFactory, jsonUtil)

    private static final NSSMF_ALLOCATE_URL = "/api/rest/provMns/v1/NSS/SliceProfiles"

    private static final NSSMF_QUERY_JOB_STATUS_URL = "/api/rest/provMns/v1/NSS/jobs/%s"

    @Override
    void preProcessRequest(DelegateExecution execution) {
        logger.trace("Enter preProcessRequest()")

        NssmfAdapterNBIRequest nbiRequest = execution.getVariable("nbiRequest") as NssmfAdapterNBIRequest

        execution.setVariable("currentCycle", 0)
        boolean isNSIOptionAvailable = execution.getVariable("isNSIOptionAvailable") as Boolean

        if (!isNSIOptionAvailable) {
            nbiRequest.serviceInfo.setActionType("allocate")
        } else if (StringUtils.isBlank(nbiRequest.serviceInfo.nssiId)){
            nbiRequest.serviceInfo.setActionType("allocate")
        } else {
            nbiRequest.serviceInfo.setActionType("modify")
        }
        execution.setVariable("nbiRequest", nbiRequest)
        logger.trace("Exit preProcessRequest")
    }

    /**
     * send Create Request NSSMF
     * @param execution
     */
    void sendCreateRequestNSSMF(DelegateExecution execution) {
        NssmfAdapterNBIRequest nbiRequest = execution.getVariable("nbiRequest") as NssmfAdapterNBIRequest
        String nssmfRequest = objectMapper.writeValueAsString(nbiRequest)
        logger.debug("sendCreateRequestNSSMF: " + nssmfRequest)

        String response = nssmfAdapterUtils.sendPostRequestNSSMF(execution, NSSMF_ALLOCATE_URL, nssmfRequest)

        if (response != null) {
            NssiResponse nssiResponse = objectMapper.readValue(response, NssiResponse.class)
            execution.setVariable("nssiAllocateResult", nssiResponse)
        }

        execution.setVariable("serviceInfo", nbiRequest.getServiceInfo())
        execution.setVariable("esrInfo", nbiRequest.getEsrInfo())
    }

    /**
     * query nssi allocate status
     * @param execution
     */
    void queryNSSIStatus(DelegateExecution execution) {
        NssmfAdapterNBIRequest nbiRequest = new NssmfAdapterNBIRequest()
        NssiResponse nssiAllocateResult = execution.getVariable("nssiAllocateResult") as NssiResponse
        String jobId = nssiAllocateResult.getJobId()
        String nssiId = nssiAllocateResult.getNssiId()

        ServiceInfo serviceInfo = execution.getVariable("serviceInfo") as ServiceInfo
        serviceInfo.setNssiId(nssiId)
        EsrInfo esrInfo = execution.getVariable("esrInfo") as EsrInfo

        //nbiRequest.setResponseId(jobId)
        nbiRequest.setServiceInfo(serviceInfo)
        nbiRequest.setEsrInfo(esrInfo)

        String endpoint = String.format(NSSMF_QUERY_JOB_STATUS_URL, jobId)

        String response =
                nssmfAdapterUtils.sendPostRequestNSSMF(execution, endpoint, objectMapper.writeValueAsString(nbiRequest))

        logger.debug("nssmf response nssiAllocateStatus:" + response)

        if (response != null) {
            JobStatusResponse jobStatusResponse = objectMapper.readValue(response, JobStatusResponse.class)
            if (StringUtils.isBlank(nssiId)) {
                nssiAllocateResult.setNssiId(jobStatusResponse.getResponseDescriptor().getNssiId())
                execution.setVariable("nssiAllocateResult", nssiAllocateResult)
            }

            execution.setVariable("nssiAllocateStatus", jobStatusResponse)
            if (jobStatusResponse.getResponseDescriptor().getProgress() == 100) {
                nssiAllocateResult.setNssiId(jobStatusResponse.getResponseDescriptor().getNssiId())
                execution.setVariable("jobFinished", true)
            }
        }
    }

    void prepareUpdateOrchestrationTask(DelegateExecution execution) {
        logger.debug("Start prepareUpdateOrchestrationTask progress")
        String requestMethod = "PUT"

        SliceTaskParamsAdapter sliceParams =
                execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
        JobStatusResponse jobStatusResponse = execution.getVariable("nssiAllocateStatus") as JobStatusResponse
        ResponseDescriptor response = jobStatusResponse.getResponseDescriptor()
        SubnetType subnetType = execution.getVariable("subnetType") as SubnetType

        SliceTaskInfo sliceTaskInfo = execution.getVariable("sliceTaskInfo") as SliceTaskInfo
        sliceTaskInfo.progress = response.getProgress()
        sliceTaskInfo.status = response.getStatus().toLowerCase()
        sliceTaskInfo.statusDescription = response.getStatusDescription()
        updateNssiResult(sliceParams, subnetType, sliceTaskInfo)

        execution.setVariable("CSSOT_paramJson", objectMapper.writeValueAsString(sliceParams))
        execution.setVariable("CSSOT_requestMethod", requestMethod)

        execution.setVariable("sliceTaskParams", sliceParams)
        execution.setVariable("sliceTaskInfo", sliceTaskInfo)

        logger.debug("Finish prepareUpdateOrchestrationTask progress")
    }

    private void updateNssiResult(SliceTaskParamsAdapter sliceTaskParams, SubnetType subnetType,
                                  SliceTaskInfo sliceTaskInfo) {
        switch (subnetType) {
            case SubnetType.CN:
                sliceTaskParams.cnSliceTaskInfo = sliceTaskInfo
                break
            case SubnetType.AN:
                sliceTaskParams.anSliceTaskInfo = sliceTaskInfo
                break
            case SubnetType.TN_BH:
                sliceTaskParams.tnBHSliceTaskInfo = sliceTaskInfo
                break
            case SubnetType.TN_FH:
                sliceTaskParams.tnFHSliceTaskInfo = sliceTaskInfo
                break
            case SubnetType.TN_MH:
                sliceTaskParams.tnMHSliceTaskInfo = sliceTaskInfo
                break
        }
    }

    void timeDelay(DelegateExecution execution) {
        logger.trace("Enter timeDelay in DoAllocateNSSI()")
        try {
            Thread.sleep(60000)
            int currentCycle = execution.getVariable("currentCycle") as Integer
            currentCycle = currentCycle + 1
            if(currentCycle >  60)
            {
                logger.trace("Completed all the retry times... but still nssmf havent completed the creation process...")
                exceptionUtil.buildAndThrowWorkflowException(execution, 500, "NSSMF creation didnt complete by time...")
            }
            execution.setVariable("currentCycle", currentCycle)
        } catch(InterruptedException e) {
            logger.info("Time Delay exception" + e)
        }
        logger.trace("Exit timeDelay in DoAllocateNSSI()")
    }

}