9f2ad5b537fac3c04afa791c934b82e64149442b
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateCommunicationService.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.aai.domain.yang.CommunicationServiceProfile
26 import org.onap.aai.domain.yang.ServiceInstance
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 org.onap.aaiclient.client.aai.AAIObjectType
31 import org.onap.aaiclient.client.aai.AAIResourcesClient
32 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
33 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
34 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
35 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
36 import org.slf4j.Logger
37 import org.slf4j.LoggerFactory
38
39 import static org.apache.commons.lang3.StringUtils.isBlank
40
41 /**
42  * This groovy class supports the <class>DoCreateCommunicationService.bpmn</class> process.
43  * AlaCarte flow for 1702 ServiceInstance Create
44  *
45  */
46 class DoCreateCommunicationService extends AbstractServiceTaskProcessor{
47     String Prefix="DCCS_"
48     ExceptionUtil exceptionUtil = new ExceptionUtil()
49     JsonUtils jsonUtil = new JsonUtils()
50     AAIResourcesClient client = new AAIResourcesClient()
51
52     private static final Logger logger = LoggerFactory.getLogger( DoCreateCommunicationService.class)
53
54     @Override
55      void preProcessRequest(DelegateExecution execution) {
56         logger.trace("start preProcessRequest")
57         execution.setVariable("prefix", Prefix)
58         String msg = ""
59         try {
60             String serviceInstanceId = execution.getVariable("serviceInstanceId")
61             if (isBlank(serviceInstanceId)) {
62                 msg = "Input serviceInstanceId' is null"
63                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
64             }
65
66             String globalSubscriberId = execution.getVariable("globalSubscriberId")
67             if (isBlank(globalSubscriberId)) {
68                 msg = "Input globalSubscriberId' is null"
69                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
70             }
71
72             String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
73             if (isBlank(subscriptionServiceType)) {
74                 msg = "Input subscriptionServiceType' is null"
75                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
76             }
77
78
79         } catch(BpmnError e) {
80             throw e
81         } catch(Exception ex) {
82             msg = "Exception in preProcessRequest " + ex.getMessage()
83             logger.debug(msg)
84             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
85         }
86         logger.trace("Exit preProcessRequest")
87     }
88
89     /**
90      * create communication service, generate S-NSSAI Id and communication service profile
91      * 1.create communication service profile
92      *
93      */
94     def createCommunicationServiceProfile = { DelegateExecution execution ->
95         logger.trace("createCSandServiceProfile")
96         String msg = ""
97         String serviceInstanceId = execution.getVariable("serviceInstanceId")
98         String globalSubscriberId = execution.getVariable("globalSubscriberId")
99         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
100         try {
101             // String sNSSAI_id = execution.getVariable("sNSSAI_id")
102             // create communication service profile
103             String profileId = UUID.randomUUID().toString()
104             execution.setVariable("communicationProfileId", profileId)
105
106             def csInputMap = execution.getVariable("csInputMap") as Map<String, ?>
107             Integer latency = csInputMap.get("latency") as Integer
108             Integer maxNumberOfUEs = csInputMap.get("maxNumberofUEs") as Integer
109             Integer expDataRateDL = csInputMap.get("expDataRateDL") as Integer
110             Integer expDataRateUL = csInputMap.get("expDataRateUL") as Integer
111             String coverageArea = csInputMap.get("coverageAreaTAList")
112             String uEMobilityLevel = csInputMap.get("uEMobilityLevel")
113             String resourceSharingLevel = csInputMap.get("resourceSharingLevel")
114
115             CommunicationServiceProfile csp = new CommunicationServiceProfile()
116             csp.setProfileId(profileId)
117
118             csp.setLatency(latency)
119             csp.setMaxNumberOfUEs(maxNumberOfUEs)
120             csp.setUeMobilityLevel(uEMobilityLevel)
121             csp.setResourceSharingLevel(resourceSharingLevel)
122             csp.setExpDataRateDL(expDataRateDL)
123             csp.setExpDataRateUL(expDataRateUL)
124             csp.setCoverageAreaList(coverageArea)
125
126             execution.setVariable("communicationServiceInstanceProfile", csp)
127
128             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(serviceInstanceId).communicationServiceProfile(profileId))
129             client.create(uri, csp)
130
131
132         } catch (BpmnError e) {
133             throw e
134         } catch (Exception ex) {
135             msg = "Exception in createCSandServiceProfile " + ex.getMessage()
136             logger.debug(msg)
137             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
138         }
139         logger.trace("exit createCSandServiceProfile")
140     }
141
142
143     /**
144      * create communication service, generate S-NSSAI Id
145      * 1.generate S-NSSAI Id
146      * 2.create communication service
147      *
148      */
149     def createCommunicationService = { DelegateExecution execution ->
150         logger.trace("create communication service")
151         String msg
152         String serviceInstanceId = execution.getVariable("serviceInstanceId")
153         try {
154             //generate S-NSSAI Id and communication service profile
155             String sNSSAI_id = generateNSSAI(serviceInstanceId)
156
157             execution.setVariable("sNSSAI_id", sNSSAI_id)
158             // create communication service
159             String serviceInstanceName = execution.getVariable("serviceInstanceName")
160             String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
161             String csServiceType = execution.getVariable("csServiceType")
162             String aaiServiceRole = "communication-service"
163
164             String oStatus = "processing"
165             String uuiRequest = execution.getVariable("uuiRequest")
166             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
167             String modelUuid = execution.getVariable("modelUuid")
168             String useInterval = execution.getVariable("useInterval")
169             String globalSubscriberId = execution.getVariable("globalSubscriberId")
170
171             // create service
172             ServiceInstance csi = new ServiceInstance()
173             csi.setServiceInstanceName(serviceInstanceName)
174             csi.setServiceType(csServiceType)
175             csi.setServiceRole(aaiServiceRole)
176             csi.setOrchestrationStatus(oStatus)
177             csi.setModelInvariantId(modelInvariantUuid)
178             csi.setModelVersionId(modelUuid)
179             csi.setInputParameters(uuiRequest)
180             csi.setWorkloadContext(useInterval)
181             csi.setEnvironmentContext(sNSSAI_id)
182
183             //timestamp format YYYY-MM-DD hh:mm:ss
184             csi.setCreatedAt(new Date(System.currentTimeMillis()).format("yyyy-MM-dd HH:mm:ss", TimeZone.getDefault()))
185
186             execution.setVariable("communicationServiceInstance", csi)
187
188             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(serviceInstanceId))
189             client.create(uri, csi)
190
191         } catch (BpmnError e) {
192             throw e
193         } catch (Exception ex) {
194             msg = "Exception in communication service " + ex.getMessage()
195             logger.debug(msg)
196             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
197         }
198         logger.trace("exit communication service")
199     }
200
201     private static generateNSSAI = { final String instanceId ->
202         int h, res
203         res = (instanceId == null) ? 0 : (h = instanceId.hashCode()) ^ (h >>> 16)
204         res = res >>> 1
205         return "01-" + Integer.toHexString(res).toUpperCase()
206     }
207 }