Merge "split single and plural graph inventory uris"
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateSliceServiceInstance.groovy
1 package org.onap.so.bpmn.infrastructure.scripts
2
3
4 import org.onap.aai.domain.yang.AllottedResource
5
6 import static org.apache.commons.lang3.StringUtils.*;
7
8 import org.camunda.bpm.engine.delegate.BpmnError
9 import org.camunda.bpm.engine.delegate.DelegateExecution
10 import org.onap.aai.domain.yang.OwningEntity
11 import org.onap.aai.domain.yang.ServiceProfile;
12 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
13 import org.onap.so.bpmn.common.scripts.CatalogDbUtils
14 import org.onap.so.bpmn.common.scripts.CatalogDbUtilsFactory
15 import org.onap.so.bpmn.common.scripts.ExceptionUtil
16 import org.onap.so.bpmn.common.scripts.MsoUtils
17 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
18 import org.onap.so.bpmn.core.RollbackData
19 import org.onap.so.bpmn.core.UrnPropertiesReader
20 import org.onap.so.bpmn.core.WorkflowException
21 import org.onap.so.bpmn.core.domain.ModelInfo
22 import org.onap.so.bpmn.core.domain.ServiceDecomposition
23 import org.onap.so.bpmn.core.domain.ServiceInstance
24 import org.onap.so.bpmn.core.json.JsonUtils
25 import org.onap.so.bpmn.infrastructure.aai.groovyflows.AAICreateResources
26 import org.onap.so.client.aai.AAIObjectType
27 import org.onap.so.client.aai.AAIResourcesClient
28 import org.onap.so.client.aai.entities.uri.AAIResourceUri
29 import org.onap.so.client.aai.entities.uri.AAIUriFactory
30 import org.slf4j.Logger
31 import org.slf4j.LoggerFactory
32
33
34
35 class DoCreateSliceServiceInstance extends AbstractServiceTaskProcessor{
36
37     private static final Logger logger = LoggerFactory.getLogger( DoCreateSliceServiceInstance.class);
38     JsonUtils jsonUtil = new JsonUtils()
39
40     ExceptionUtil exceptionUtil = new ExceptionUtil()
41
42     CatalogDbUtils catalogDbUtils = new CatalogDbUtilsFactory().create()
43
44     /**
45      * Pre Process the BPMN Flow Request
46      * Inclouds:
47      * generate the nsOperationKey
48      * generate the nsParameters
49      */
50     void preProcessRequest (DelegateExecution execution) {
51         String msg = ""
52         logger.trace("Enter preProcessRequest()")
53         //Need update
54         //1. Prepare service parameter.
55         //2. Prepare slice profile parameters.
56
57         String sliceserviceInstanceId = execution.getVariable("serviceInstanceId")
58         String allottedResourceId = UUID.randomUUID().toString()
59         execution.setVariable("sliceserviceInstanceId", sliceserviceInstanceId)
60         execution.setVariable("allottedResourceId", allottedResourceId)
61
62         String uuiRequest = execution.getVariable("uuiRequest")
63         String modelInvariantUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceInvariantUuid")
64         String modelUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceUuid")
65         //here modelVersion is not set, we use modelUuid to decompose the service.
66         def isDebugLogEnabled = true
67         execution.setVariable("serviceInstanceId",sliceserviceInstanceId)
68         execution.setVariable("isDebugLogEnabled",isDebugLogEnabled)
69         String serviceModelInfo = """{
70             "modelInvariantUuid":"${modelInvariantUuid}",
71             "modelUuid":"${modelUuid}",
72             "modelVersion":""
73              }"""
74         execution.setVariable("serviceModelInfo", serviceModelInfo)
75
76         logger.trace("Exit preProcessRequest")
77     }
78
79
80     void createServiceProfile(DelegateExecution execution) {
81
82         String sliceserviceInstanceId = execution.getVariable("sliceserviceInstanceId")
83         Map<String, Object> serviceProfileMap = execution.getVariable("serviceProfile")
84         String serviceProfileID = UUID.randomUUID().toString()
85         ServiceProfile serviceProfile = new ServiceProfile();
86         serviceProfile.setProfileId(serviceProfileID)
87         serviceProfile.setLatency(Integer.parseInt(serviceProfileMap.get("latency").toString()))
88         serviceProfile.setMaxNumberOfUEs(Integer.parseInt(serviceProfileMap.get("maxNumberofUEs").toString()))
89         serviceProfile.setCoverageAreaTAList(serviceProfileMap.get("coverageAreaTAList").toString())
90         serviceProfile.setUeMobilityLevel(serviceProfileMap.get("uEMobilityLevel").toString())
91         serviceProfile.setResourceSharingLevel(serviceProfileMap.get("resourceSharingLevel").toString())
92         serviceProfile.setExpDataRateUL(Integer.parseInt(serviceProfileMap.get("expDataRateUL").toString()))
93         serviceProfile.setExpDataRateDL(Integer.parseInt(serviceProfileMap.get("expDataRateDL").toString()))
94         serviceProfile.setAreaTrafficCapUL(Integer.parseInt(serviceProfileMap.get("areaTrafficCapUL").toString()))
95         serviceProfile.setAreaTrafficCapDL(Integer.parseInt(serviceProfileMap.get("areaTrafficCapDL").toString()))
96         serviceProfile.setActivityFactor(Integer.parseInt(serviceProfileMap.get("activityFactor").toString()))
97
98         serviceProfile.setJitter(0)
99         serviceProfile.setSurvivalTime(0)
100         serviceProfile.setCsAvailability(new Object())
101         serviceProfile.setReliability(new Object())
102         serviceProfile.setExpDataRate(0)
103         serviceProfile.setTrafficDensity(0)
104         serviceProfile.setConnDensity(0)
105         try {
106             AAIResourcesClient client = new AAIResourcesClient()
107             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_PROFILE, execution.getVariable("globalSubscriberId"),
108                     execution.getVariable("subscriptionServiceType"), sliceserviceInstanceId, serviceProfileID)
109             client.create(uri, serviceProfile)
110
111         } catch (BpmnError e) {
112             throw e
113         } catch (Exception ex) {
114             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
115             logger.info(msg)
116             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
117         }
118     }
119
120     void instantiateSliceService(DelegateExecution execution) {
121
122         ServiceDecomposition serviceDecomposition= execution.getVariable("sliceServiceDecomposition")
123         String uuiRequest = execution.getVariable("uuiRequest")
124         ModelInfo modelInfo = serviceDecomposition.getModelInfo()
125         String serviceRole = "e2eslice-service"
126         String serviceType = execution.getVariable("serviceType")
127         Map<String, Object> serviceProfile = execution.getVariable("serviceProfile")
128         String ssInstanceId = execution.getVariable("serviceInstanceId")
129         try {
130             org.onap.aai.domain.yang.ServiceInstance ss = new org.onap.aai.domain.yang.ServiceInstance()
131             ss.setServiceInstanceId(ssInstanceId)
132             String sliceInstanceName = execution.getVariable("serviceInstanceName")
133             ss.setServiceInstanceName(sliceInstanceName)
134             ss.setServiceType(serviceType)
135             String serviceStatus = "deactivated"
136             ss.setOrchestrationStatus(serviceStatus)
137             String modelInvariantUuid = modelInfo.getModelInvariantUuid()
138             String modelUuid = modelInfo.getModelUuid()
139             ss.setModelInvariantId(modelInvariantUuid)
140             ss.setModelVersionId(modelUuid)
141             String serviceInstanceLocationid = serviceProfile.get("plmnIdList")
142             ss.setServiceInstanceLocationId(serviceInstanceLocationid)
143             String snssai = serviceProfile.get("sNSSAI")
144             ss.setEnvironmentContext(snssai)
145             ss.setServiceRole(serviceRole)
146             AAIResourcesClient client = new AAIResourcesClient()
147             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, execution.getVariable("globalSubscriberId"), execution.getVariable("subscriptionServiceType"), ssInstanceId)
148             client.create(uri, ss)
149         } catch (BpmnError e) {
150             throw e
151         } catch (Exception ex) {
152             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
153             logger.info(msg)
154             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
155         }
156
157
158         def rollbackData = execution.getVariable("RollbackData")
159         if (rollbackData == null) {
160             rollbackData = new RollbackData();
161         }
162         //rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
163         rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
164         rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", ssInstanceId)
165         rollbackData.put("SERVICEINSTANCE", "subscriptionServiceType", execution.getVariable("subscriptionServiceType"))
166         rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))
167         execution.setVariable("rollbackData", rollbackData)
168         execution.setVariable("RollbackData", rollbackData)
169         logger.debug("RollbackData:" + rollbackData)
170
171     }
172
173
174     void createAllottedResource(DelegateExecution execution) {
175         String serviceInstanceId = execution.getVariable('sliceserviceInstanceId')
176
177         AAIResourcesClient resourceClient = new AAIResourcesClient()
178         AAIResourceUri ssServiceuri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
179
180 //        try {
181 //
182 //            if(resourceClient.exists(ssServiceuri)){
183 //                execution.setVariable("ssi_resourceLink", uri.build().toString())
184 //            }else{
185 //                exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai to " +
186 //                        "associate allotted resource for service :"+serviceInstanceId)
187 //            }
188 //        }catch(BpmnError e) {
189 //            throw e;
190 //        }catch (Exception ex){
191 //            String msg = "Exception in getServiceInstance. " + ex.getMessage()
192 //            logger.debug(msg)
193 //            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
194 //        }
195
196         try {
197             String allottedResourceId = execution.getVariable("allottedResourceId")
198             ServiceDecomposition serviceDecomposition = execution.getVariable("sliceServiceDecomposition")
199             List<org.onap.so.bpmn.core.domain.AllottedResource> allottedResourceList = serviceDecomposition.getAllottedResources()
200             for(org.onap.so.bpmn.core.domain.AllottedResource allottedResource : allottedResourceList)
201             {
202                 //AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceFromParentURI(ssServiceuri, AAIObjectType.ALLOTTED_RESOURCE, allottedResourceId)
203                 AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIObjectType.ALLOTTED_RESOURCE,
204                         execution.getVariable("globalSubscriberId"),execution.getVariable("subscriptionServiceType"),
205                         execution.getVariable("sliceserviceInstanceId"), allottedResourceId)
206                 execution.setVariable("allottedResourceUri", allottedResourceUri)
207                 String arType = allottedResource.getAllottedResourceType()
208                 String arRole = allottedResource.getAllottedResourceRole()
209                 String modelInvariantId = allottedResource.getModelInfo().getModelInvariantUuid()
210                 String modelVersionId = allottedResource.getModelInfo().getModelUuid()
211
212                 org.onap.aai.domain.yang.AllottedResource resource = new org.onap.aai.domain.yang.AllottedResource()
213                 resource.setId(allottedResourceId)
214                 resource.setType(arType)
215                 resource.setAllottedResourceName("Allotted_"+ execution.getVariable("serviceInstanceName"))
216                 resource.setRole(arRole)
217                 resource.setModelInvariantId(modelInvariantId)
218                 resource.setModelVersionId(modelVersionId)
219                 getAAIClient().create(allottedResourceUri, resource)
220                 //AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceFromExistingURI(AAIObjectType.SERVICE_INSTANCE, UriBuilder.fromPath(ssServiceuri).build())
221                 //getAAIClient().connect(allottedResourceUri,ssServiceuri)
222             }
223             //execution.setVariable("aaiARPath", allottedResourceUri.build().toString());
224
225         }catch (Exception ex) {
226             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception in createAaiAR " + ex.getMessage())
227         }
228     }
229
230 }