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