2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.etsisol003adapter.lcm.rest;
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.timeout;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.verifyZeroInteractions;
29 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.VnfmRestTemplateConfiguration;
30 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
31 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
32 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
34 import java.net.URISyntaxException;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
39 import java.util.Optional;
40 import javax.inject.Inject;
41 import org.hamcrest.BaseMatcher;
42 import org.hamcrest.Description;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.ArgumentCaptor;
47 import org.mockito.hamcrest.MockitoHamcrest;
48 import org.onap.aai.domain.yang.EsrSystemInfoList;
49 import org.onap.aai.domain.yang.EsrVnfm;
50 import org.onap.aai.domain.yang.GenericVnf;
51 import org.onap.aai.domain.yang.GenericVnfs;
52 import org.onap.aai.domain.yang.Relationship;
53 import org.onap.aai.domain.yang.RelationshipData;
54 import org.onap.aai.domain.yang.RelationshipList;
55 import org.onap.aai.domain.yang.Vserver;
56 import org.onap.aaiclient.client.aai.AAIResourcesClient;
57 import org.onap.aaiclient.client.aai.entities.uri.AAIBaseResourceUri;
58 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
59 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.aai.AaiHelper;
60 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.aai.OamIpAddressSource;
61 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.aai.OamIpAddressSource.OamIpAddressType;
62 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vim.model.AccessInfo;
63 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.InlineResponse201;
64 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.InlineResponse201Links;
65 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.InlineResponse201LinksSelf;
66 import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
67 import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
68 import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum;
69 import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationComputeResource;
70 import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinks;
71 import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance;
72 import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfIdentifierCreationNotification;
73 import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification;
74 import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationEnum;
75 import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
76 import org.springframework.beans.factory.annotation.Autowired;
77 import org.springframework.beans.factory.annotation.Qualifier;
78 import org.springframework.boot.test.context.SpringBootTest;
79 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
80 import org.springframework.boot.test.mock.mockito.MockBean;
81 import org.springframework.boot.web.server.LocalServerPort;
82 import org.springframework.http.HttpStatus;
83 import org.springframework.http.MediaType;
84 import org.springframework.http.ResponseEntity;
85 import org.springframework.test.context.ActiveProfiles;
86 import org.springframework.test.context.junit4.SpringRunner;
87 import org.springframework.test.web.client.MockRestServiceServer;
88 import org.springframework.web.client.RestTemplate;
89 import com.google.gson.Gson;
91 @RunWith(SpringRunner.class)
92 @SpringBootTest(classes = TestApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
93 @ActiveProfiles("test")
94 public class Sol003LcnControllerTest {
96 private static final String CLOUD_OWNER = "myTestCloudOwner";
97 private static final String REGION = "myTestRegion";
98 private static final String TENANT_ID = "myTestTenantId";
103 @Qualifier(VnfmRestTemplateConfiguration.SOL003_LCM_REST_TEMPLATE)
104 private RestTemplate testRestTemplate;
105 private MockRestServiceServer mockRestServer;
108 private AAIResourcesClient aaiResourcesClient;
111 private Sol003LcnContoller controller;
112 private final Gson gson = new Gson();
115 private AaiHelper aaiHelper;
118 public void setUp() throws Exception {
119 mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
123 public void lcnNotification_IdentifierCreated_Returns204() throws URISyntaxException, InterruptedException {
124 final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
125 new VnfIdentifierCreationNotification();
126 final ResponseEntity<Void> response =
127 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
128 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
132 public void lcnNotification_IdentifierDeleted_Returns204() throws URISyntaxException, InterruptedException {
133 final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
134 new VnfIdentifierCreationNotification();
135 final ResponseEntity<Void> response =
136 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
137 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
141 public void lcnNotification_InstantiateStartingOrProcessing_NoAction()
142 throws URISyntaxException, InterruptedException {
143 final VnfLcmOperationOccurrenceNotification startingNotification = new VnfLcmOperationOccurrenceNotification();
144 startingNotification.setOperation(OperationEnum.INSTANTIATE);
145 startingNotification.setOperationState(OperationStateEnum.STARTING);
147 ResponseEntity<Void> response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(startingNotification);
148 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
150 verifyZeroInteractions(aaiResourcesClient);
152 final VnfLcmOperationOccurrenceNotification processingNotification =
153 new VnfLcmOperationOccurrenceNotification();
154 processingNotification.setOperation(OperationEnum.INSTANTIATE);
155 processingNotification.setOperationState(OperationStateEnum.STARTING);
157 response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(processingNotification);
158 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
160 verifyZeroInteractions(aaiResourcesClient);
164 public void lcnNotification_InstantiateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
165 final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
166 createNotification(OperationEnum.INSTANTIATE);
167 addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.ADDED);
168 final InlineResponse201 vnfInstance = createVnfInstance();
170 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
171 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
173 final GenericVnf genericVnf = createGenericVnf("vnfmType1");
174 addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm1");
175 final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
176 listOfGenericVnfs.add(genericVnf);
177 final GenericVnfs genericVnfs = new GenericVnfs();
178 genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
179 doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
180 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
181 "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
182 final EsrVnfm vnfm = new EsrVnfm();
183 vnfm.setVnfmId("vnfm1");
184 final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
185 vnfm.setEsrSystemInfoList(esrSystemInfoList);
186 doReturn(Optional.of(vnfm)).when(aaiResourcesClient).get(eq(EsrVnfm.class), MockitoHamcrest
187 .argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/vnfm1?depth=1")));
189 final ResponseEntity<Void> response =
190 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
191 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
193 final ArgumentCaptor<Object> bodyArgument1 = ArgumentCaptor.forClass(Object.class);
194 final ArgumentCaptor<AAIResourceUri> uriArgument1 = ArgumentCaptor.forClass(AAIResourceUri.class);
196 verify(aaiResourcesClient, timeout(1000)).update(uriArgument1.capture(), bodyArgument1.capture());
198 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId",
199 uriArgument1.getAllValues().get(0).build().toString());
200 final GenericVnf updatedGenericVnf = (GenericVnf) bodyArgument1.getAllValues().get(0);
201 assertEquals("10.10.10.10", updatedGenericVnf.getIpv4OamAddress());
202 assertEquals("Created", updatedGenericVnf.getOrchestrationStatus());
204 final ArgumentCaptor<Object> bodyArgument2 = ArgumentCaptor.forClass(Object.class);
205 final ArgumentCaptor<AAIResourceUri> uriArgument2 = ArgumentCaptor.forClass(AAIResourceUri.class);
206 verify(aaiResourcesClient, timeout(1000)).create(uriArgument2.capture(), bodyArgument2.capture());
209 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
210 + TENANT_ID + "/vservers/vserver/myVnfc1",
211 uriArgument2.getAllValues().get(0).build().toString());
213 final Vserver vserver = (Vserver) bodyArgument2.getAllValues().get(0);
214 assertEquals("myVnfc1", vserver.getVserverId());
216 final ArgumentCaptor<AAIResourceUri> uriArgument1Connect = ArgumentCaptor.forClass(AAIResourceUri.class);
217 final ArgumentCaptor<AAIResourceUri> uriArgument2Connect = ArgumentCaptor.forClass(AAIResourceUri.class);
218 verify(aaiResourcesClient, timeout(1000)).connect(uriArgument1Connect.capture(), uriArgument2Connect.capture());
220 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
221 + TENANT_ID + "/vservers/vserver/myVnfc1",
222 uriArgument1Connect.getAllValues().get(0).build().toString());
223 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId",
224 uriArgument2Connect.getAllValues().get(0).build().toString());
228 public void lcnNotification_TerminateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
229 final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
230 createNotification(OperationEnum.TERMINATE);
231 addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.REMOVED);
233 final InlineResponse201 vnfInstance = createVnfInstance();
235 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
236 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
238 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
239 .andRespond(withStatus(HttpStatus.NO_CONTENT).contentType(MediaType.APPLICATION_JSON));
241 final GenericVnf genericVnf = createGenericVnf("vnfmType1");
242 addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm1");
243 genericVnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
244 final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
245 listOfGenericVnfs.add(genericVnf);
246 final GenericVnfs genericVnfs = new GenericVnfs();
247 genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
248 addRelationshipFromGenericVnfToVserver(genericVnf, "myVnfc1");
250 doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
251 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
252 "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
253 final EsrVnfm vnfm = new EsrVnfm();
254 vnfm.setVnfmId("vnfm1");
255 final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
256 vnfm.setEsrSystemInfoList(esrSystemInfoList);
257 doReturn(Optional.of(vnfm)).when(aaiResourcesClient).get(eq(EsrVnfm.class), MockitoHamcrest
258 .argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/vnfm1?depth=1")));
260 final ResponseEntity<Void> response =
261 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
262 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
264 final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
265 final ArgumentCaptor<AAIResourceUri> updateUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
266 verify(aaiResourcesClient, timeout(10000000)).update(updateUriArgument.capture(), genericVnfArgument.capture());
267 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", updateUriArgument.getValue().build().toString());
268 assertEquals("Assigned", genericVnfArgument.getValue().getOrchestrationStatus());
270 final ArgumentCaptor<AAIResourceUri> deleteUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
272 verify(aaiResourcesClient, timeout(10000000)).delete(deleteUriArgument.capture());
275 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
276 + TENANT_ID + "/vservers/vserver/myVnfc1",
277 deleteUriArgument.getAllValues().get(0).build().toString());
280 private VnfLcmOperationOccurrenceNotification createNotification(final OperationEnum operation) {
281 final VnfLcmOperationOccurrenceNotification notification = new VnfLcmOperationOccurrenceNotification();
282 notification.setOperation(operation);
283 notification.setOperationState(OperationStateEnum.COMPLETED);
285 final LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance linkToVnfInstance =
286 new LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance()
287 .href("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
288 final LcnVnfLcmOperationOccurrenceNotificationLinks operationLinks =
289 new LcnVnfLcmOperationOccurrenceNotificationLinks().vnfInstance(linkToVnfInstance);
290 notification.setLinks(operationLinks);
295 private void addVnfcsToNotification(final VnfLcmOperationOccurrenceNotification notification,
296 final ChangeTypeEnum changeType) {
297 final List<LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs> affectedVnfcs = new ArrayList<>();;
298 final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc =
299 new LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs();
300 vnfc.changeType(changeType);
301 final LcnVnfLcmOperationOccurrenceNotificationComputeResource computeResource =
302 new LcnVnfLcmOperationOccurrenceNotificationComputeResource();
303 computeResource.setResourceId("myVnfc1");
304 computeResource.setVimConnectionId(CLOUD_OWNER + "_" + REGION);
305 vnfc.setComputeResource(computeResource);
306 affectedVnfcs.add(vnfc);
307 notification.setAffectedVnfcs(affectedVnfcs);
310 private InlineResponse201 createVnfInstance() {
311 final InlineResponse201 vnfInstance = new InlineResponse201();
312 vnfInstance.setId("myTestVnfIdOnVnfm");
313 final InlineResponse201LinksSelf selfLink = new InlineResponse201LinksSelf();
314 selfLink.setHref("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
315 final InlineResponse201Links VnfInstancelinks = new InlineResponse201Links();
316 VnfInstancelinks.setSelf(selfLink);
317 vnfInstance.setLinks(VnfInstancelinks);
319 final Map<String, String> vnfConfigurableProperties = new HashMap<>();
320 vnfConfigurableProperties.put("vnfIpAddress", "10.10.10.10");
321 vnfInstance.setVnfConfigurableProperties(vnfConfigurableProperties);
323 final List<InlineResponse201VimConnectionInfo> vimConnectionInfo = new ArrayList<>();;
324 final InlineResponse201VimConnectionInfo vimConnection = new InlineResponse201VimConnectionInfo();
325 vimConnection.setVimId(CLOUD_OWNER + "_" + REGION);
326 vimConnection.setId(CLOUD_OWNER + "_" + REGION);
327 final AccessInfo accessInfo = new AccessInfo();
328 accessInfo.setProjectId(TENANT_ID);
329 vimConnection.setAccessInfo(accessInfo);
330 vimConnectionInfo.add(vimConnection);
331 vnfInstance.setVimConnectionInfo(vimConnectionInfo);
333 final OamIpAddressSource oamIpAddressSource =
334 new OamIpAddressSource(OamIpAddressType.CONFIGURABLE_PROPERTY, "vnfIpAddress");
335 aaiHelper.setOamIpAddressSource("myTestVnfIdOnVnfm", oamIpAddressSource);
339 private GenericVnf createGenericVnf(final String type) {
340 final GenericVnf genericVnf = new GenericVnf();
341 genericVnf.setVnfId("myTestVnfId");
342 genericVnf.setNfType(type);
346 private void addRelationshipFromGenericVnfToVnfm(final GenericVnf genericVnf, final String vnfmId) {
347 final Relationship relationshipToVnfm = new Relationship();
348 relationshipToVnfm.setRelatedLink("/aai/v15/external-system/esr-vnfm-list/esr-vnfm/" + vnfmId);
349 relationshipToVnfm.setRelatedTo("esr-vnfm");
350 final RelationshipData relationshipData = new RelationshipData();
351 relationshipData.setRelationshipKey("esr-vnfm.vnfm-id");
352 relationshipData.setRelationshipValue(vnfmId);
353 relationshipToVnfm.getRelationshipData().add(relationshipData);
355 if (genericVnf.getRelationshipList() == null) {
356 final RelationshipList relationshipList = new RelationshipList();
357 genericVnf.setRelationshipList(relationshipList);
359 genericVnf.getRelationshipList().getRelationship().add(relationshipToVnfm);
362 private void addRelationshipFromGenericVnfToVserver(final GenericVnf genericVnf, final String vserverId) {
363 final Relationship relationshipToVserver = new Relationship();
364 relationshipToVserver.setRelatedTo("vserver");
365 final RelationshipData relationshipData1 = new RelationshipData();
366 relationshipData1.setRelationshipKey("vserver.vserver-id");
367 relationshipData1.setRelationshipValue(vserverId);
368 relationshipToVserver.getRelationshipData().add(relationshipData1);
369 final RelationshipData relationshipData2 = new RelationshipData();
370 relationshipData2.setRelationshipKey("cloud-region.cloud-owner");
371 relationshipData2.setRelationshipValue(CLOUD_OWNER);
372 relationshipToVserver.getRelationshipData().add(relationshipData2);
373 final RelationshipData relationshipData3 = new RelationshipData();
374 relationshipData3.setRelationshipKey("cloud-region.cloud-region-id");
375 relationshipData3.setRelationshipValue(REGION);
376 relationshipToVserver.getRelationshipData().add(relationshipData3);
377 final RelationshipData relationshipData4 = new RelationshipData();
378 relationshipData4.setRelationshipKey("tenant.tenant-id");
379 relationshipData4.setRelationshipValue(TENANT_ID);
380 relationshipToVserver.getRelationshipData().add(relationshipData4);
382 if (genericVnf.getRelationshipList() == null) {
383 final RelationshipList relationshipList = new RelationshipList();
384 genericVnf.setRelationshipList(relationshipList);
386 genericVnf.getRelationshipList().getRelationship().add(relationshipToVserver);
389 private class AaiResourceUriMatcher extends BaseMatcher<AAIBaseResourceUri<?, ?>> {
391 final String uriAsString;
393 public AaiResourceUriMatcher(final String uriAsString) {
394 this.uriAsString = uriAsString;
398 public boolean matches(final Object item) {
399 if (item instanceof AAIBaseResourceUri<?, ?>) {
400 if (uriAsString.endsWith("...")) {
401 return ((AAIBaseResourceUri<?, ?>) item).build().toString()
402 .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
404 return ((AAIBaseResourceUri<?, ?>) item).build().toString().equals(uriAsString);
410 public void describeTo(final Description description) {}