2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Telecom Italia
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.scripts
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.junit.Before
26 import org.mockito.Mockito
27 import org.onap.aai.domain.yang.v19.AllottedResource
28 import org.onap.aai.domain.yang.v19.ServiceInstance
29 import org.onap.aai.domain.yang.v19.SliceProfile
30 import org.onap.aai.domain.yang.v19.SliceProfiles
31 import org.onap.aaiclient.client.aai.entities.AAIEdgeLabel
32 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
33 import org.onap.aaiclient.client.aai.entities.Relationships
34 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
35 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
36 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
37 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
38 import org.onap.so.bpmn.common.scripts.MsoGroovyTest
39 import org.springframework.data.domain.Slice
41 import static org.junit.Assert.assertNotNull
42 import static org.junit.Assert.assertTrue
43 import static org.mockito.Mockito.*
45 class DoModifyCoreNSSITest extends MsoGroovyTest {
48 void init() throws IOException {
49 super.init("DoModifyCoreNSSITest")
54 void testPreProcessRequest() {
56 String nssiId = "5G-999"
57 when(mockExecution.getVariable("serviceInstanceID")).thenReturn(nssiId)
59 String nsiId = "5G-777"
60 when(mockExecution.getVariable("nsiId")).thenReturn(nsiId)
62 String snssai = "S-NSSAI"
63 String snssaiList = "[ \"${snssai}\" ]"
64 String sliceProfileId = "slice-profile-id"
65 String modifyAction = "allocate"
66 String sliceParams = "{\n" +
67 "\"sliceProfileId\":\"${sliceProfileId}\",\"snssaiList\":${snssaiList}\n" +
68 ",\"modifyAction\":\"${modifyAction}\"\n" +
70 when(mockExecution.getVariable("sliceParams")).thenReturn(sliceParams)
72 DoModifyCoreNSSI obj = new DoModifyCoreNSSI()
73 obj.preProcessRequest(mockExecution)
76 currentNSSI.put("nssiId", nssiId)
77 currentNSSI.put("nsiId", nsiId)
78 currentNSSI.put("sliceProfile", sliceParams)
79 currentNSSI.put("S-NSSAI", snssai)
80 currentNSSI.put("sliceProfileId", sliceProfileId)
82 Mockito.verify(mockExecution,times(1)).setVariable("currentNSSI", currentNSSI)
88 void testCalculateSNSSAISliceProfileInstanceHasToBeDeleted() {
90 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
92 when(mockExecution.getVariable("isCreateSliceProfileInstance")).thenReturn("false")
94 String theSNSSAI = "theS-NSSAI"
96 currentNSSI.put("S-NSSAI", theSNSSAI)
98 String sliceProfileId = "the-slice-profile-id"
99 currentNSSI['sliceProfileId'] = sliceProfileId
101 List<SliceProfile> associatedProfiles = new ArrayList<>()
102 SliceProfile sliceProfile1 = new SliceProfile()
103 sliceProfile1.setProfileId(sliceProfileId)
104 sliceProfile1.setSNssai(theSNSSAI)
106 SliceProfile sliceProfile2 = new SliceProfile()
107 sliceProfile2.setProfileId("sp-2")
108 sliceProfile2.setSNssai(theSNSSAI)
110 SliceProfile sliceProfile3 = new SliceProfile()
111 sliceProfile3.setProfileId("sp-3")
112 sliceProfile3.setSNssai("snssai2")
114 associatedProfiles.add(sliceProfile1)
115 associatedProfiles.add(sliceProfile2)
116 associatedProfiles.add(sliceProfile3)
118 currentNSSI.put("associatedProfiles", associatedProfiles)
120 when(mockExecution.getVariable("isCreateSliceProfileInstance" )).thenReturn(false)
122 DoModifyCoreNSSI obj = new DoModifyCoreNSSI()
123 obj.calculateSNSSAI(mockExecution)
125 List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
126 SliceProfile sliceProfileContainsSNSSAI = (SliceProfile)currentNSSI.get("sliceProfileS-NSSAI")
128 assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == (associatedProfiles.size() - 1)))
129 assertNotNull("Slice Profile which contains given S-NSSAI not found", sliceProfileContainsSNSSAI)
130 assertTrue("Wrong Slice Profile", sliceProfileContainsSNSSAI.getSNssai().equals(theSNSSAI))
135 void testCalculateSNSSAISliceProfileInstanceHasToBeCreated() {
136 def currentNSSI = [:]
137 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
139 when(mockExecution.getVariable("isCreateSliceProfileInstance")).thenReturn("true")
141 String theSNSSAI = "theS-NSSAI"
143 currentNSSI.put("S-NSSAI", theSNSSAI)
145 List<SliceProfile> associatedProfiles = new ArrayList<>()
146 SliceProfile sliceProfile1 = new SliceProfile()
147 sliceProfile1.setSNssai("snssai1")
149 SliceProfile sliceProfile2 = new SliceProfile()
150 sliceProfile2.setSNssai("snssai2")
152 associatedProfiles.add(sliceProfile1)
153 associatedProfiles.add(sliceProfile2)
155 int sizeBefore = associatedProfiles.size()
157 currentNSSI.put("associatedProfiles", associatedProfiles)
159 DoModifyCoreNSSI obj = new DoModifyCoreNSSI()
160 obj.calculateSNSSAI(mockExecution)
162 List<SliceProfile> snssais = (List<SliceProfile>)currentNSSI.get("S-NSSAIs")
164 assertTrue("Either snssais doesn't exist or size is incorrect", (snssais != null && snssais.size() == (sizeBefore + 1)))
170 void testCreateSliceProfileInstance() {
171 def currentNSSI = [:]
173 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
175 String sliceProfileId = "sliceProfileId"
177 currentNSSI.put("sliceProfile", "{\"sliceProfileId\":\"slice-profile-id\",\"snssaiList\":[\"S-NSSAI\"],\"expDataRateUL\":\"12\",\"expDataRateDL\":\"5\"," +
178 "\"activityFactor\":\"2\",\"resourceSharingLevel\":\"resource-sharing-level\",\"uEMobilityLevel\":\"ue-mobility-level\",\"coverageAreaTAList\":\"coverage-area-ta-list\"," +
179 "\"maxNumberofUEs\":\"10000\",\"latency\":\"7\"}")
180 currentNSSI.put("sliceProfileId", sliceProfileId)
182 List<String> snssais = new ArrayList<>()
183 snssais.add("s-nssai")
184 currentNSSI.put("S-NSSAIs", snssais)
186 DoModifyCoreNSSI spy = spy(DoModifyCoreNSSI.class)
187 when(spy.getAAIClient()).thenReturn(client)
189 String globalSubscriberId = "globalSubscriberId"
190 String subscriptionServiceType = "subscription-service-type"
191 String nssiId = "nssiId"
193 when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
194 when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
196 currentNSSI.put("nssiId", nssiId)
198 ServiceInstance sliceProfileInstance = new ServiceInstance()
199 sliceProfileInstance.setServiceInstanceId(UUID.randomUUID().toString())
201 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).
202 serviceInstance(sliceProfileInstance.getServiceInstanceId()))
204 SliceProfile sliceProfile = new SliceProfile()
205 sliceProfile.setProfileId(sliceProfileId)
207 doNothing().when(client).create(uri, sliceProfileInstance)
209 spy.createSliceProfileInstance(mockExecution)
211 assertTrue("Slice Profile Instance Id doesn't exist", (currentNSSI.get("createdSliceProfileInstanceId")) != null)
217 void testAssociateSliceProfileInstanceWithNSSI() {
218 def currentNSSI = [:]
220 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
222 DoModifyCoreNSSI spy = spy(DoModifyCoreNSSI.class)
223 when(spy.getAAIClient()).thenReturn(client)
225 String sliceProfileId = "sliceProfileId"
227 when(mockExecution.getVariable("sliceProfileID")).thenReturn(sliceProfileId)
229 String nssiId = "5G-999"
230 currentNSSI.put("nssiId", nssiId)
232 String globalSubscriberId = "globalSubscriberId"
233 String subscriptionServiceType = "subscriptionServiceType"
235 String sliceProfileInstanceId = "slice-rpofile-instance-id"
236 currentNSSI.put("createdSliceProfileInstanceId", sliceProfileInstanceId)
238 AllottedResource allottedResource = new AllottedResource()
240 String allottedResourceId = UUID.randomUUID().toString()
242 allottedResource.setId(allottedResourceId)
244 AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(sliceProfileInstanceId).allottedResource(allottedResourceId))
245 doNothing().when(client).create(allottedResourceUri, allottedResource)
247 currentNSSI.put("allottedResourceUri", allottedResourceUri)
249 when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
250 when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
252 currentNSSI.put("sliceProfileId", sliceProfileId)
254 SliceProfile sliceProfile = new SliceProfile()
255 currentNSSI.put("createdSliceProfile", sliceProfile)
257 AAIResourceUri sliceProfileInstanceUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(sliceProfileInstanceId))
259 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
261 ServiceInstance sliceProfileInstance = new ServiceInstance()
262 sliceProfileInstance.setServiceInstanceId(sliceProfileInstanceId)
263 Optional<ServiceInstance> sliceProfileInstanceOpt = Optional.of(sliceProfileInstance)
265 when(client.get(ServiceInstance.class, sliceProfileInstanceUri)).thenReturn(sliceProfileInstanceOpt)
266 doNothing().when(client).update(sliceProfileInstanceUri, sliceProfileInstance)
268 ServiceInstance nssi = new ServiceInstance()
269 nssi.setServiceInstanceId(nssiId)
270 nssi.setSliceProfiles(new SliceProfiles())
271 currentNSSI.put("nssi", nssi)
273 doNothing().when(client).connect(nssiUri, sliceProfileInstanceUri, AAIEdgeLabel.USES)
275 spy.associateSliceProfileInstanceWithNSSI(mockExecution)
281 void testGetNSSIAssociatedProfilesCreateSLiceProfileEmptySLiceProfiles() {
282 invokeGetNSSIAssociatedProfiles("true", true)
286 void testGetNSSIAssociatedProfilesCreateSLiceProfileNonEmptySliceProfiles() {
287 invokeGetNSSIAssociatedProfiles("true", false)
291 void testGetNSSIAssociatedProfilesDeleteSLiceProfile() {
292 invokeGetNSSIAssociatedProfiles("false", false)
295 void invokeGetNSSIAssociatedProfiles(String isCreateSliceProfileInstance, boolean isEmptySliceProfiles) {
296 def currentNSSI = [:]
297 when(mockExecution.getVariable("currentNSSI")).thenReturn(currentNSSI)
299 String globalSubscriberId = "5GCustomer"
300 String subscriptionServiceType = "5G"
301 when(mockExecution.getVariable("globalSubscriberId")).thenReturn(globalSubscriberId)
302 when(mockExecution.getVariable("subscriptionServiceType")).thenReturn(subscriptionServiceType)
304 when(mockExecution.getVariable("isCreateSliceProfileInstance")).thenReturn(isCreateSliceProfileInstance)
306 String nssiId = "5G-999"
307 ServiceInstance nssi = new ServiceInstance()
308 nssi.setServiceInstanceId(nssiId)
309 currentNSSI.put("nssiId", nssiId)
311 String theSLiceProfileId = "slice-profile-id"
312 currentNSSI.put("sliceProfileId", theSLiceProfileId)
314 SliceProfiles sliceProfiles = new SliceProfiles()
316 List<SliceProfile> slProfiles = sliceProfiles.getSliceProfile()
318 if(!isEmptySliceProfiles) {
319 SliceProfile sp1 = new SliceProfile()
320 sp1.setProfileId(theSLiceProfileId)
322 slProfiles.add(new SliceProfile())
325 //nssi.setSliceProfiles(sliceProfiles)
326 currentNSSI.put("nssi", nssi)
328 DoModifyCoreNSSI spy = spy(DoModifyCoreNSSI.class)
329 when(spy.getAAIClient()).thenReturn(client)
331 AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
333 AAIResultWrapper wrapperMock = mock(AAIResultWrapper.class) //new AAIResultWrapper(json)
334 Relationships rsMock = mock(Relationships.class)
335 Optional<Relationships> orsMock = Optional.of(rsMock)
336 List<AAIResourceUri> allottedUris = new ArrayList<>()
337 AAIResourceUri allottedUri = AAIUriFactory.createResourceUri(Types.ALLOTTED_RESOURCE.getFragment("allotted-id"))
338 allottedUris.add(allottedUri)
340 when(client.get(nssiUri)).thenReturn(wrapperMock)
341 when(wrapperMock.getRelationships()).thenReturn(orsMock)
342 when(rsMock.getRelatedUris(Types.ALLOTTED_RESOURCE)).thenReturn(allottedUris)
344 String sliceProfileInstanceId = "slice-profile-instance-id"
345 ServiceInstance sliceProfileInstance = new ServiceInstance()
346 sliceProfileInstance.setServiceInstanceId(sliceProfileInstanceId)
347 sliceProfileInstance.setServiceRole("slice-profile-instance")
349 List<AAIResourceUri> sliceProfileInstanceUris = new ArrayList<>()
350 AAIResourceUri sliceProfileInstanceUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(sliceProfileInstance.getServiceInstanceId()))
351 sliceProfileInstanceUris.add(sliceProfileInstanceUri)
353 Optional<ServiceInstance> sliceProfileInstanceOpt = Optional.of(sliceProfileInstance)
355 when(client.get(allottedUri)).thenReturn(wrapperMock)
356 when(rsMock.getRelatedUris(Types.SERVICE_INSTANCE)).thenReturn(sliceProfileInstanceUris)
357 when(client.get(ServiceInstance.class, sliceProfileInstanceUri)).thenReturn(sliceProfileInstanceOpt)
360 SliceProfiles sps = new SliceProfiles()
361 sps.getSliceProfile().addAll(slProfiles)
362 sliceProfileInstance.setSliceProfiles(sps)
364 Optional<SliceProfiles> sliceProfilesOpt = Optional.of(sps)
366 org.onap.aaiclient.client.generated.fluentbuilders.SliceProfiles sliceProfilesType =
367 AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(sliceProfileInstance.getServiceInstanceId()).sliceProfiles()
369 def sliceProfilesUri = AAIUriFactory.createResourceUri(sliceProfilesType)
370 when(client.exists(sliceProfilesUri)).thenReturn(true)
372 when(client.get(sliceProfilesUri)).thenReturn(wrapperMock)
374 when(wrapperMock.asBean(SliceProfiles.class)).thenReturn(sliceProfilesOpt)
376 spy.getNSSIAssociatedProfiles(mockExecution)
378 List<SliceProfile> associatedProfiles = (List<SliceProfile>)currentNSSI.get("associatedProfiles")
379 int expectedSize = sps.getSliceProfile().size()
381 if(!isEmptySliceProfiles) {
382 assertTrue("sliceProfileInstanceUri not found in contect Map", currentNSSI.get("sliceProfileInstanceUri") != null)
385 assertTrue("Either associatedProfiles doesn't exist or size is incorrect", (associatedProfiles != null && associatedProfiles.size() == expectedSize))