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.vnfmadapter.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;
33 import com.google.gson.Gson;
35 import java.net.URISyntaxException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.Optional;
41 import javax.inject.Inject;
42 import org.hamcrest.BaseMatcher;
43 import org.hamcrest.Description;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.ArgumentCaptor;
48 import org.mockito.hamcrest.MockitoHamcrest;
49 import org.onap.aai.domain.yang.EsrSystemInfoList;
50 import org.onap.aai.domain.yang.EsrVnfm;
51 import org.onap.aai.domain.yang.GenericVnf;
52 import org.onap.aai.domain.yang.GenericVnfs;
53 import org.onap.aai.domain.yang.Relationship;
54 import org.onap.aai.domain.yang.RelationshipData;
55 import org.onap.aai.domain.yang.RelationshipList;
56 import org.onap.aai.domain.yang.Vserver;
57 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
58 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
59 import org.onap.so.adapters.vnfmadapter.extclients.aai.OamIpAddressSource;
60 import org.onap.so.adapters.vnfmadapter.extclients.aai.OamIpAddressSource.OamIpAddressType;
61 import org.onap.so.adapters.vnfmadapter.extclients.vim.model.AccessInfo;
62 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
63 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum;
64 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationComputeResource;
65 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinks;
66 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance;
67 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfIdentifierCreationNotification;
68 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification;
69 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationEnum;
70 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
71 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
72 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201Links;
73 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201LinksSelf;
74 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
75 import org.onap.so.client.aai.AAIResourcesClient;
76 import org.onap.so.client.aai.AAIVersion;
77 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
78 import org.springframework.beans.factory.annotation.Autowired;
79 import org.springframework.beans.factory.annotation.Qualifier;
80 import org.springframework.boot.test.context.SpringBootTest;
81 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
82 import org.springframework.boot.test.mock.mockito.MockBean;
83 import org.springframework.boot.web.server.LocalServerPort;
84 import org.springframework.http.HttpStatus;
85 import org.springframework.http.MediaType;
86 import org.springframework.http.ResponseEntity;
87 import org.springframework.test.context.ActiveProfiles;
88 import org.springframework.test.context.junit4.SpringRunner;
89 import org.springframework.test.web.client.MockRestServiceServer;
90 import org.springframework.web.client.RestTemplate;
92 @RunWith(SpringRunner.class)
93 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
94 @ActiveProfiles("test")
95 public class Sol003LcnControllerTest {
97 private static final String CLOUD_OWNER = "myTestCloudOwner";
98 private static final String REGION = "myTestRegion";
99 private static final String TENANT_ID = "myTestTenantId";
104 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
105 private RestTemplate testRestTemplate;
106 private MockRestServiceServer mockRestServer;
109 private AAIResourcesClient aaiResourcesClient;
112 private Sol003LcnContoller controller;
113 private final Gson gson = new Gson();
116 private AaiHelper aaiHelper;
119 public void setUp() throws Exception {
120 mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
124 public void lcnNotification_IdentifierCreated_Returns204() throws URISyntaxException, InterruptedException {
125 final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
126 new VnfIdentifierCreationNotification();
127 final ResponseEntity<Void> response =
128 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
129 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
133 public void lcnNotification_IdentifierDeleted_Returns204() throws URISyntaxException, InterruptedException {
134 final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
135 new VnfIdentifierCreationNotification();
136 final ResponseEntity<Void> response =
137 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
138 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
142 public void lcnNotification_InstantiateStartingOrProcessing_NoAction()
143 throws URISyntaxException, InterruptedException {
144 final VnfLcmOperationOccurrenceNotification startingNotification = new VnfLcmOperationOccurrenceNotification();
145 startingNotification.setOperation(OperationEnum.INSTANTIATE);
146 startingNotification.setOperationState(OperationStateEnum.STARTING);
148 ResponseEntity<Void> response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(startingNotification);
149 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
151 verifyZeroInteractions(aaiResourcesClient);
153 final VnfLcmOperationOccurrenceNotification processingNotification =
154 new VnfLcmOperationOccurrenceNotification();
155 processingNotification.setOperation(OperationEnum.INSTANTIATE);
156 processingNotification.setOperationState(OperationStateEnum.STARTING);
158 response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(processingNotification);
159 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
161 verifyZeroInteractions(aaiResourcesClient);
165 public void lcnNotification_InstantiateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
166 final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
167 createNotification(OperationEnum.INSTANTIATE);
168 addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.ADDED);
169 final InlineResponse201 vnfInstance = createVnfInstance();
171 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
172 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
174 final GenericVnf genericVnf = createGenericVnf("vnfmType1");
175 addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm1");
176 final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
177 listOfGenericVnfs.add(genericVnf);
178 final GenericVnfs genericVnfs = new GenericVnfs();
179 genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
180 doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
181 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
182 "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
183 EsrVnfm vnfm = new EsrVnfm();
184 vnfm.setVnfmId("vnfm1");
185 final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
186 vnfm.setEsrSystemInfoList(esrSystemInfoList);
187 doReturn(Optional.of(vnfm)).when(aaiResourcesClient).get(eq(EsrVnfm.class), MockitoHamcrest
188 .argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/vnfm1?depth=1")));
190 final ResponseEntity<Void> response =
191 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
192 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
194 final ArgumentCaptor<Object> bodyArgument1 = ArgumentCaptor.forClass(Object.class);
195 final ArgumentCaptor<AAIResourceUri> uriArgument1 = ArgumentCaptor.forClass(AAIResourceUri.class);
197 verify(aaiResourcesClient, timeout(1000)).update(uriArgument1.capture(), bodyArgument1.capture());
199 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId",
200 uriArgument1.getAllValues().get(0).build().toString());
201 final GenericVnf updatedGenericVnf = (GenericVnf) bodyArgument1.getAllValues().get(0);
202 assertEquals("10.10.10.10", updatedGenericVnf.getIpv4OamAddress());
203 assertEquals("Created", updatedGenericVnf.getOrchestrationStatus());
205 final ArgumentCaptor<Object> bodyArgument2 = ArgumentCaptor.forClass(Object.class);
206 final ArgumentCaptor<AAIResourceUri> uriArgument2 = ArgumentCaptor.forClass(AAIResourceUri.class);
207 verify(aaiResourcesClient, timeout(1000)).create(uriArgument2.capture(), bodyArgument2.capture());
210 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
211 + TENANT_ID + "/vservers/vserver/myVnfc1",
212 uriArgument2.getAllValues().get(0).build().toString());
214 final Vserver vserver = (Vserver) bodyArgument2.getAllValues().get(0);
215 assertEquals("myVnfc1", vserver.getVserverId());
217 final ArgumentCaptor<AAIResourceUri> uriArgument1Connect = ArgumentCaptor.forClass(AAIResourceUri.class);
218 final ArgumentCaptor<AAIResourceUri> uriArgument2Connect = ArgumentCaptor.forClass(AAIResourceUri.class);
219 verify(aaiResourcesClient, timeout(1000)).connect(uriArgument1Connect.capture(), uriArgument2Connect.capture());
221 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
222 + TENANT_ID + "/vservers/vserver/myVnfc1",
223 uriArgument1Connect.getAllValues().get(0).build().toString());
224 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId",
225 uriArgument2Connect.getAllValues().get(0).build().toString());
229 public void lcnNotification_TerminateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
230 final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
231 createNotification(OperationEnum.TERMINATE);
232 addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.REMOVED);
234 final InlineResponse201 vnfInstance = createVnfInstance();
236 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
237 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
239 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
240 .andRespond(withStatus(HttpStatus.NO_CONTENT).contentType(MediaType.APPLICATION_JSON));
242 final GenericVnf genericVnf = createGenericVnf("vnfmType1");
243 addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm1");
244 genericVnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
245 final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
246 listOfGenericVnfs.add(genericVnf);
247 final GenericVnfs genericVnfs = new GenericVnfs();
248 genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
249 addRelationshipFromGenericVnfToVserver(genericVnf, "myVnfc1");
251 doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
252 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
253 "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
254 EsrVnfm vnfm = new EsrVnfm();
255 vnfm.setVnfmId("vnfm1");
256 final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
257 vnfm.setEsrSystemInfoList(esrSystemInfoList);
258 doReturn(Optional.of(vnfm)).when(aaiResourcesClient).get(eq(EsrVnfm.class), MockitoHamcrest
259 .argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/vnfm1?depth=1")));
261 final ResponseEntity<Void> response =
262 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
263 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
265 final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
266 final ArgumentCaptor<AAIResourceUri> updateUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
267 verify(aaiResourcesClient, timeout(10000000)).update(updateUriArgument.capture(), genericVnfArgument.capture());
268 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", updateUriArgument.getValue().build().toString());
269 assertEquals("Assigned", genericVnfArgument.getValue().getOrchestrationStatus());
271 final ArgumentCaptor<AAIResourceUri> deleteUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
273 verify(aaiResourcesClient, timeout(10000000)).delete(deleteUriArgument.capture());
276 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
277 + TENANT_ID + "/vservers/vserver/myVnfc1",
278 deleteUriArgument.getAllValues().get(0).build().toString());
281 private VnfLcmOperationOccurrenceNotification createNotification(final OperationEnum operation) {
282 final VnfLcmOperationOccurrenceNotification notification = new VnfLcmOperationOccurrenceNotification();
283 notification.setOperation(operation);
284 notification.setOperationState(OperationStateEnum.COMPLETED);
286 final LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance linkToVnfInstance =
287 new LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance()
288 .href("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
289 final LcnVnfLcmOperationOccurrenceNotificationLinks operationLinks =
290 new LcnVnfLcmOperationOccurrenceNotificationLinks().vnfInstance(linkToVnfInstance);
291 notification.setLinks(operationLinks);
296 private void addVnfcsToNotification(final VnfLcmOperationOccurrenceNotification notification,
297 final ChangeTypeEnum changeType) {
298 final List<LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs> affectedVnfcs = new ArrayList<>();;
299 final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc =
300 new LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs();
301 vnfc.changeType(changeType);
302 final LcnVnfLcmOperationOccurrenceNotificationComputeResource computeResource =
303 new LcnVnfLcmOperationOccurrenceNotificationComputeResource();
304 computeResource.setResourceId("myVnfc1");
305 computeResource.setVimConnectionId(CLOUD_OWNER + "_" + REGION);
306 vnfc.setComputeResource(computeResource);
307 affectedVnfcs.add(vnfc);
308 notification.setAffectedVnfcs(affectedVnfcs);
311 private InlineResponse201 createVnfInstance() {
312 final InlineResponse201 vnfInstance = new InlineResponse201();
313 vnfInstance.setId("myTestVnfIdOnVnfm");
314 final InlineResponse201LinksSelf selfLink = new InlineResponse201LinksSelf();
315 selfLink.setHref("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
316 final InlineResponse201Links VnfInstancelinks = new InlineResponse201Links();
317 VnfInstancelinks.setSelf(selfLink);
318 vnfInstance.setLinks(VnfInstancelinks);
320 final Map<String, String> vnfConfigurableProperties = new HashMap<>();
321 vnfConfigurableProperties.put("vnfIpAddress", "10.10.10.10");
322 vnfInstance.setVnfConfigurableProperties(vnfConfigurableProperties);
324 final List<InlineResponse201VimConnectionInfo> vimConnectionInfo = new ArrayList<>();;
325 final InlineResponse201VimConnectionInfo vimConnection = new InlineResponse201VimConnectionInfo();
326 vimConnection.setVimId(CLOUD_OWNER + "_" + REGION);
327 vimConnection.setId(CLOUD_OWNER + "_" + REGION);
328 final AccessInfo accessInfo = new AccessInfo();
329 accessInfo.setProjectId(TENANT_ID);
330 vimConnection.setAccessInfo(accessInfo);
331 vimConnectionInfo.add(vimConnection);
332 vnfInstance.setVimConnectionInfo(vimConnectionInfo);
334 final OamIpAddressSource oamIpAddressSource =
335 new OamIpAddressSource(OamIpAddressType.CONFIGURABLE_PROPERTY, "vnfIpAddress");
336 aaiHelper.setOamIpAddressSource("myTestVnfIdOnVnfm", oamIpAddressSource);
340 private GenericVnf createGenericVnf(final String type) {
341 final GenericVnf genericVnf = new GenericVnf();
342 genericVnf.setVnfId("myTestVnfId");
343 genericVnf.setNfType(type);
347 private void addRelationshipFromGenericVnfToVnfm(final GenericVnf genericVnf, final String vnfmId) {
348 final Relationship relationshipToVnfm = new Relationship();
349 relationshipToVnfm.setRelatedLink("/aai/v15/external-system/esr-vnfm-list/esr-vnfm/" + vnfmId);
350 relationshipToVnfm.setRelatedTo("esr-vnfm");
351 final RelationshipData relationshipData = new RelationshipData();
352 relationshipData.setRelationshipKey("esr-vnfm.vnfm-id");
353 relationshipData.setRelationshipValue(vnfmId);
354 relationshipToVnfm.getRelationshipData().add(relationshipData);
356 if (genericVnf.getRelationshipList() == null) {
357 final RelationshipList relationshipList = new RelationshipList();
358 genericVnf.setRelationshipList(relationshipList);
360 genericVnf.getRelationshipList().getRelationship().add(relationshipToVnfm);
363 private void addRelationshipFromGenericVnfToVserver(final GenericVnf genericVnf, final String vserverId) {
364 final Relationship relationshipToVserver = new Relationship();
365 relationshipToVserver.setRelatedTo("vserver");
366 final RelationshipData relationshipData1 = new RelationshipData();
367 relationshipData1.setRelationshipKey("vserver.vserver-id");
368 relationshipData1.setRelationshipValue(vserverId);
369 relationshipToVserver.getRelationshipData().add(relationshipData1);
370 final RelationshipData relationshipData2 = new RelationshipData();
371 relationshipData2.setRelationshipKey("cloud-region.cloud-owner");
372 relationshipData2.setRelationshipValue(CLOUD_OWNER);
373 relationshipToVserver.getRelationshipData().add(relationshipData2);
374 final RelationshipData relationshipData3 = new RelationshipData();
375 relationshipData3.setRelationshipKey("cloud-region.cloud-region-id");
376 relationshipData3.setRelationshipValue(REGION);
377 relationshipToVserver.getRelationshipData().add(relationshipData3);
378 final RelationshipData relationshipData4 = new RelationshipData();
379 relationshipData4.setRelationshipKey("tenant.tenant-id");
380 relationshipData4.setRelationshipValue(TENANT_ID);
381 relationshipToVserver.getRelationshipData().add(relationshipData4);
383 if (genericVnf.getRelationshipList() == null) {
384 final RelationshipList relationshipList = new RelationshipList();
385 genericVnf.setRelationshipList(relationshipList);
387 genericVnf.getRelationshipList().getRelationship().add(relationshipToVserver);
390 private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
392 final String uriAsString;
394 public AaiResourceUriMatcher(final String uriAsString) {
395 this.uriAsString = uriAsString;
399 public boolean matches(final Object item) {
400 if (item instanceof AAIResourceUri) {
401 if (uriAsString.endsWith("...")) {
402 return ((AAIResourceUri) item).build().toString()
403 .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
405 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
411 public void describeTo(final Description description) {}