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.etsi.sol003.adapter.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 static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
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.so.adapters.etsi.sol003.adapter.lcm.extclients.aai.AaiHelper;
57 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.aai.OamIpAddressSource;
58 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.aai.OamIpAddressSource.OamIpAddressType;
59 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vim.model.AccessInfo;
60 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.InlineResponse201;
61 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.InlineResponse201Links;
62 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.InlineResponse201LinksSelf;
63 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
64 import org.onap.so.adapters.etsi.sol003.adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
65 import org.onap.so.adapters.etsi.sol003.adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum;
66 import org.onap.so.adapters.etsi.sol003.adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationComputeResource;
67 import org.onap.so.adapters.etsi.sol003.adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinks;
68 import org.onap.so.adapters.etsi.sol003.adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance;
69 import org.onap.so.adapters.etsi.sol003.adapter.lcm.lcn.model.VnfIdentifierCreationNotification;
70 import org.onap.so.adapters.etsi.sol003.adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification;
71 import org.onap.so.adapters.etsi.sol003.adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationEnum;
72 import org.onap.so.adapters.etsi.sol003.adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
73 import org.onap.aaiclient.client.aai.AAIResourcesClient;
74 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
75 import org.springframework.beans.factory.annotation.Autowired;
76 import org.springframework.beans.factory.annotation.Qualifier;
77 import org.springframework.boot.test.context.SpringBootTest;
78 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
79 import org.springframework.boot.test.mock.mockito.MockBean;
80 import org.springframework.boot.web.server.LocalServerPort;
81 import org.springframework.http.HttpStatus;
82 import org.springframework.http.MediaType;
83 import org.springframework.http.ResponseEntity;
84 import org.springframework.test.context.ActiveProfiles;
85 import org.springframework.test.context.junit4.SpringRunner;
86 import org.springframework.test.web.client.MockRestServiceServer;
87 import org.springframework.web.client.RestTemplate;
88 import com.google.gson.Gson;
90 @RunWith(SpringRunner.class)
91 @SpringBootTest(classes = TestApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
92 @ActiveProfiles("test")
93 public class Sol003LcnControllerTest {
95 private static final String CLOUD_OWNER = "myTestCloudOwner";
96 private static final String REGION = "myTestRegion";
97 private static final String TENANT_ID = "myTestTenantId";
102 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
103 private RestTemplate testRestTemplate;
104 private MockRestServiceServer mockRestServer;
107 private AAIResourcesClient aaiResourcesClient;
110 private Sol003LcnContoller controller;
111 private final Gson gson = new Gson();
114 private AaiHelper aaiHelper;
117 public void setUp() throws Exception {
118 mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
122 public void lcnNotification_IdentifierCreated_Returns204() throws URISyntaxException, InterruptedException {
123 final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
124 new VnfIdentifierCreationNotification();
125 final ResponseEntity<Void> response =
126 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
127 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
131 public void lcnNotification_IdentifierDeleted_Returns204() throws URISyntaxException, InterruptedException {
132 final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
133 new VnfIdentifierCreationNotification();
134 final ResponseEntity<Void> response =
135 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
136 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
140 public void lcnNotification_InstantiateStartingOrProcessing_NoAction()
141 throws URISyntaxException, InterruptedException {
142 final VnfLcmOperationOccurrenceNotification startingNotification = new VnfLcmOperationOccurrenceNotification();
143 startingNotification.setOperation(OperationEnum.INSTANTIATE);
144 startingNotification.setOperationState(OperationStateEnum.STARTING);
146 ResponseEntity<Void> response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(startingNotification);
147 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
149 verifyZeroInteractions(aaiResourcesClient);
151 final VnfLcmOperationOccurrenceNotification processingNotification =
152 new VnfLcmOperationOccurrenceNotification();
153 processingNotification.setOperation(OperationEnum.INSTANTIATE);
154 processingNotification.setOperationState(OperationStateEnum.STARTING);
156 response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(processingNotification);
157 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
159 verifyZeroInteractions(aaiResourcesClient);
163 public void lcnNotification_InstantiateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
164 final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
165 createNotification(OperationEnum.INSTANTIATE);
166 addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.ADDED);
167 final InlineResponse201 vnfInstance = createVnfInstance();
169 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
170 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
172 final GenericVnf genericVnf = createGenericVnf("vnfmType1");
173 addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm1");
174 final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
175 listOfGenericVnfs.add(genericVnf);
176 final GenericVnfs genericVnfs = new GenericVnfs();
177 genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
178 doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
179 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
180 "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
181 EsrVnfm vnfm = new EsrVnfm();
182 vnfm.setVnfmId("vnfm1");
183 final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
184 vnfm.setEsrSystemInfoList(esrSystemInfoList);
185 doReturn(Optional.of(vnfm)).when(aaiResourcesClient).get(eq(EsrVnfm.class), MockitoHamcrest
186 .argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/vnfm1?depth=1")));
188 final ResponseEntity<Void> response =
189 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
190 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
192 final ArgumentCaptor<Object> bodyArgument1 = ArgumentCaptor.forClass(Object.class);
193 final ArgumentCaptor<AAIResourceUri> uriArgument1 = ArgumentCaptor.forClass(AAIResourceUri.class);
195 verify(aaiResourcesClient, timeout(1000)).update(uriArgument1.capture(), bodyArgument1.capture());
197 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId",
198 uriArgument1.getAllValues().get(0).build().toString());
199 final GenericVnf updatedGenericVnf = (GenericVnf) bodyArgument1.getAllValues().get(0);
200 assertEquals("10.10.10.10", updatedGenericVnf.getIpv4OamAddress());
201 assertEquals("Created", updatedGenericVnf.getOrchestrationStatus());
203 final ArgumentCaptor<Object> bodyArgument2 = ArgumentCaptor.forClass(Object.class);
204 final ArgumentCaptor<AAIResourceUri> uriArgument2 = ArgumentCaptor.forClass(AAIResourceUri.class);
205 verify(aaiResourcesClient, timeout(1000)).create(uriArgument2.capture(), bodyArgument2.capture());
208 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
209 + TENANT_ID + "/vservers/vserver/myVnfc1",
210 uriArgument2.getAllValues().get(0).build().toString());
212 final Vserver vserver = (Vserver) bodyArgument2.getAllValues().get(0);
213 assertEquals("myVnfc1", vserver.getVserverId());
215 final ArgumentCaptor<AAIResourceUri> uriArgument1Connect = ArgumentCaptor.forClass(AAIResourceUri.class);
216 final ArgumentCaptor<AAIResourceUri> uriArgument2Connect = ArgumentCaptor.forClass(AAIResourceUri.class);
217 verify(aaiResourcesClient, timeout(1000)).connect(uriArgument1Connect.capture(), uriArgument2Connect.capture());
219 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
220 + TENANT_ID + "/vservers/vserver/myVnfc1",
221 uriArgument1Connect.getAllValues().get(0).build().toString());
222 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId",
223 uriArgument2Connect.getAllValues().get(0).build().toString());
227 public void lcnNotification_TerminateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
228 final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
229 createNotification(OperationEnum.TERMINATE);
230 addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.REMOVED);
232 final InlineResponse201 vnfInstance = createVnfInstance();
234 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
235 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
237 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
238 .andRespond(withStatus(HttpStatus.NO_CONTENT).contentType(MediaType.APPLICATION_JSON));
240 final GenericVnf genericVnf = createGenericVnf("vnfmType1");
241 addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm1");
242 genericVnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
243 final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
244 listOfGenericVnfs.add(genericVnf);
245 final GenericVnfs genericVnfs = new GenericVnfs();
246 genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
247 addRelationshipFromGenericVnfToVserver(genericVnf, "myVnfc1");
249 doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
250 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
251 "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
252 EsrVnfm vnfm = new EsrVnfm();
253 vnfm.setVnfmId("vnfm1");
254 final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
255 vnfm.setEsrSystemInfoList(esrSystemInfoList);
256 doReturn(Optional.of(vnfm)).when(aaiResourcesClient).get(eq(EsrVnfm.class), MockitoHamcrest
257 .argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/vnfm1?depth=1")));
259 final ResponseEntity<Void> response =
260 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
261 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
263 final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
264 final ArgumentCaptor<AAIResourceUri> updateUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
265 verify(aaiResourcesClient, timeout(10000000)).update(updateUriArgument.capture(), genericVnfArgument.capture());
266 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", updateUriArgument.getValue().build().toString());
267 assertEquals("Assigned", genericVnfArgument.getValue().getOrchestrationStatus());
269 final ArgumentCaptor<AAIResourceUri> deleteUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
271 verify(aaiResourcesClient, timeout(10000000)).delete(deleteUriArgument.capture());
274 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
275 + TENANT_ID + "/vservers/vserver/myVnfc1",
276 deleteUriArgument.getAllValues().get(0).build().toString());
279 private VnfLcmOperationOccurrenceNotification createNotification(final OperationEnum operation) {
280 final VnfLcmOperationOccurrenceNotification notification = new VnfLcmOperationOccurrenceNotification();
281 notification.setOperation(operation);
282 notification.setOperationState(OperationStateEnum.COMPLETED);
284 final LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance linkToVnfInstance =
285 new LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance()
286 .href("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
287 final LcnVnfLcmOperationOccurrenceNotificationLinks operationLinks =
288 new LcnVnfLcmOperationOccurrenceNotificationLinks().vnfInstance(linkToVnfInstance);
289 notification.setLinks(operationLinks);
294 private void addVnfcsToNotification(final VnfLcmOperationOccurrenceNotification notification,
295 final ChangeTypeEnum changeType) {
296 final List<LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs> affectedVnfcs = new ArrayList<>();;
297 final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc =
298 new LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs();
299 vnfc.changeType(changeType);
300 final LcnVnfLcmOperationOccurrenceNotificationComputeResource computeResource =
301 new LcnVnfLcmOperationOccurrenceNotificationComputeResource();
302 computeResource.setResourceId("myVnfc1");
303 computeResource.setVimConnectionId(CLOUD_OWNER + "_" + REGION);
304 vnfc.setComputeResource(computeResource);
305 affectedVnfcs.add(vnfc);
306 notification.setAffectedVnfcs(affectedVnfcs);
309 private InlineResponse201 createVnfInstance() {
310 final InlineResponse201 vnfInstance = new InlineResponse201();
311 vnfInstance.setId("myTestVnfIdOnVnfm");
312 final InlineResponse201LinksSelf selfLink = new InlineResponse201LinksSelf();
313 selfLink.setHref("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
314 final InlineResponse201Links VnfInstancelinks = new InlineResponse201Links();
315 VnfInstancelinks.setSelf(selfLink);
316 vnfInstance.setLinks(VnfInstancelinks);
318 final Map<String, String> vnfConfigurableProperties = new HashMap<>();
319 vnfConfigurableProperties.put("vnfIpAddress", "10.10.10.10");
320 vnfInstance.setVnfConfigurableProperties(vnfConfigurableProperties);
322 final List<InlineResponse201VimConnectionInfo> vimConnectionInfo = new ArrayList<>();;
323 final InlineResponse201VimConnectionInfo vimConnection = new InlineResponse201VimConnectionInfo();
324 vimConnection.setVimId(CLOUD_OWNER + "_" + REGION);
325 vimConnection.setId(CLOUD_OWNER + "_" + REGION);
326 final AccessInfo accessInfo = new AccessInfo();
327 accessInfo.setProjectId(TENANT_ID);
328 vimConnection.setAccessInfo(accessInfo);
329 vimConnectionInfo.add(vimConnection);
330 vnfInstance.setVimConnectionInfo(vimConnectionInfo);
332 final OamIpAddressSource oamIpAddressSource =
333 new OamIpAddressSource(OamIpAddressType.CONFIGURABLE_PROPERTY, "vnfIpAddress");
334 aaiHelper.setOamIpAddressSource("myTestVnfIdOnVnfm", oamIpAddressSource);
338 private GenericVnf createGenericVnf(final String type) {
339 final GenericVnf genericVnf = new GenericVnf();
340 genericVnf.setVnfId("myTestVnfId");
341 genericVnf.setNfType(type);
345 private void addRelationshipFromGenericVnfToVnfm(final GenericVnf genericVnf, final String vnfmId) {
346 final Relationship relationshipToVnfm = new Relationship();
347 relationshipToVnfm.setRelatedLink("/aai/v15/external-system/esr-vnfm-list/esr-vnfm/" + vnfmId);
348 relationshipToVnfm.setRelatedTo("esr-vnfm");
349 final RelationshipData relationshipData = new RelationshipData();
350 relationshipData.setRelationshipKey("esr-vnfm.vnfm-id");
351 relationshipData.setRelationshipValue(vnfmId);
352 relationshipToVnfm.getRelationshipData().add(relationshipData);
354 if (genericVnf.getRelationshipList() == null) {
355 final RelationshipList relationshipList = new RelationshipList();
356 genericVnf.setRelationshipList(relationshipList);
358 genericVnf.getRelationshipList().getRelationship().add(relationshipToVnfm);
361 private void addRelationshipFromGenericVnfToVserver(final GenericVnf genericVnf, final String vserverId) {
362 final Relationship relationshipToVserver = new Relationship();
363 relationshipToVserver.setRelatedTo("vserver");
364 final RelationshipData relationshipData1 = new RelationshipData();
365 relationshipData1.setRelationshipKey("vserver.vserver-id");
366 relationshipData1.setRelationshipValue(vserverId);
367 relationshipToVserver.getRelationshipData().add(relationshipData1);
368 final RelationshipData relationshipData2 = new RelationshipData();
369 relationshipData2.setRelationshipKey("cloud-region.cloud-owner");
370 relationshipData2.setRelationshipValue(CLOUD_OWNER);
371 relationshipToVserver.getRelationshipData().add(relationshipData2);
372 final RelationshipData relationshipData3 = new RelationshipData();
373 relationshipData3.setRelationshipKey("cloud-region.cloud-region-id");
374 relationshipData3.setRelationshipValue(REGION);
375 relationshipToVserver.getRelationshipData().add(relationshipData3);
376 final RelationshipData relationshipData4 = new RelationshipData();
377 relationshipData4.setRelationshipKey("tenant.tenant-id");
378 relationshipData4.setRelationshipValue(TENANT_ID);
379 relationshipToVserver.getRelationshipData().add(relationshipData4);
381 if (genericVnf.getRelationshipList() == null) {
382 final RelationshipList relationshipList = new RelationshipList();
383 genericVnf.setRelationshipList(relationshipList);
385 genericVnf.getRelationshipList().getRelationship().add(relationshipToVserver);
388 private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
390 final String uriAsString;
392 public AaiResourceUriMatcher(final String uriAsString) {
393 this.uriAsString = uriAsString;
397 public boolean matches(final Object item) {
398 if (item instanceof AAIResourceUri) {
399 if (uriAsString.endsWith("...")) {
400 return ((AAIResourceUri) item).build().toString()
401 .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
403 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
409 public void describeTo(final Description description) {}