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.entities.uri.AAIResourceUri;
77 import org.springframework.beans.factory.annotation.Autowired;
78 import org.springframework.beans.factory.annotation.Qualifier;
79 import org.springframework.boot.test.context.SpringBootTest;
80 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
81 import org.springframework.boot.test.mock.mockito.MockBean;
82 import org.springframework.boot.web.server.LocalServerPort;
83 import org.springframework.http.HttpStatus;
84 import org.springframework.http.MediaType;
85 import org.springframework.http.ResponseEntity;
86 import org.springframework.test.context.ActiveProfiles;
87 import org.springframework.test.context.junit4.SpringRunner;
88 import org.springframework.test.web.client.MockRestServiceServer;
89 import org.springframework.web.client.RestTemplate;
91 @RunWith(SpringRunner.class)
92 @SpringBootTest(classes = VnfmAdapterApplication.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(CONFIGURABLE_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 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());
215 final Relationship relationship = vserver.getRelationshipList().getRelationship().get(0);
216 assertEquals("generic-vnf", relationship.getRelatedTo());
217 assertEquals("tosca.relationships.HostedOn", relationship.getRelationshipLabel());
218 assertEquals("/aai/v15/network/generic-vnfs/generic-vnf/myTestVnfId", relationship.getRelatedLink());
219 assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
220 assertEquals("myTestVnfId", relationship.getRelationshipData().get(0).getRelationshipValue());
224 public void lcnNotification_TerminateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
225 final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
226 createNotification(OperationEnum.TERMINATE);
227 addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.REMOVED);
229 final InlineResponse201 vnfInstance = createVnfInstance();
231 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
232 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
234 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
235 .andRespond(withStatus(HttpStatus.NO_CONTENT).contentType(MediaType.APPLICATION_JSON));
237 final GenericVnf genericVnf = createGenericVnf("vnfmType1");
238 addRelationshipFromGenericVnfToVnfm(genericVnf, "vnfm1");
239 genericVnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
240 final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
241 listOfGenericVnfs.add(genericVnf);
242 final GenericVnfs genericVnfs = new GenericVnfs();
243 genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
244 addRelationshipFromGenericVnfToVserver(genericVnf, "myVnfc1");
246 doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
247 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
248 "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
249 EsrVnfm vnfm = new EsrVnfm();
250 vnfm.setVnfmId("vnfm1");
251 final EsrSystemInfoList esrSystemInfoList = new EsrSystemInfoList();
252 vnfm.setEsrSystemInfoList(esrSystemInfoList);
253 doReturn(Optional.of(vnfm)).when(aaiResourcesClient).get(eq(EsrVnfm.class), MockitoHamcrest
254 .argThat(new AaiResourceUriMatcher("/external-system/esr-vnfm-list/esr-vnfm/vnfm1?depth=1")));
256 final ResponseEntity<Void> response =
257 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
258 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
260 final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
261 final ArgumentCaptor<AAIResourceUri> updateUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
262 verify(aaiResourcesClient, timeout(10000000)).update(updateUriArgument.capture(), genericVnfArgument.capture());
263 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", updateUriArgument.getValue().build().toString());
264 assertEquals("Assigned", genericVnfArgument.getValue().getOrchestrationStatus());
266 final ArgumentCaptor<AAIResourceUri> deleteUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
268 verify(aaiResourcesClient, timeout(10000000)).delete(deleteUriArgument.capture());
271 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
272 + TENANT_ID + "/vservers/vserver/myVnfc1",
273 deleteUriArgument.getAllValues().get(0).build().toString());
276 private VnfLcmOperationOccurrenceNotification createNotification(final OperationEnum operation) {
277 final VnfLcmOperationOccurrenceNotification notification = new VnfLcmOperationOccurrenceNotification();
278 notification.setOperation(operation);
279 notification.setOperationState(OperationStateEnum.COMPLETED);
281 final LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance linkToVnfInstance =
282 new LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance()
283 .href("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
284 final LcnVnfLcmOperationOccurrenceNotificationLinks operationLinks =
285 new LcnVnfLcmOperationOccurrenceNotificationLinks().vnfInstance(linkToVnfInstance);
286 notification.setLinks(operationLinks);
291 private void addVnfcsToNotification(final VnfLcmOperationOccurrenceNotification notification,
292 final ChangeTypeEnum changeType) {
293 final List<LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs> affectedVnfcs = new ArrayList<>();;
294 final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc =
295 new LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs();
296 vnfc.changeType(changeType);
297 final LcnVnfLcmOperationOccurrenceNotificationComputeResource computeResource =
298 new LcnVnfLcmOperationOccurrenceNotificationComputeResource();
299 computeResource.setResourceId("myVnfc1");
300 computeResource.setVimConnectionId(CLOUD_OWNER + "_" + REGION);
301 vnfc.setComputeResource(computeResource);
302 affectedVnfcs.add(vnfc);
303 notification.setAffectedVnfcs(affectedVnfcs);
306 private InlineResponse201 createVnfInstance() {
307 final InlineResponse201 vnfInstance = new InlineResponse201();
308 vnfInstance.setId("myTestVnfIdOnVnfm");
309 final InlineResponse201LinksSelf selfLink = new InlineResponse201LinksSelf();
310 selfLink.setHref("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
311 final InlineResponse201Links VnfInstancelinks = new InlineResponse201Links();
312 VnfInstancelinks.setSelf(selfLink);
313 vnfInstance.setLinks(VnfInstancelinks);
315 final Map<String, String> vnfConfigurableProperties = new HashMap<>();
316 vnfConfigurableProperties.put("vnfIpAddress", "10.10.10.10");
317 vnfInstance.setVnfConfigurableProperties(vnfConfigurableProperties);
319 final List<InlineResponse201VimConnectionInfo> vimConnectionInfo = new ArrayList<>();;
320 final InlineResponse201VimConnectionInfo vimConnection = new InlineResponse201VimConnectionInfo();
321 vimConnection.setVimId(CLOUD_OWNER + "_" + REGION);
322 vimConnection.setId(CLOUD_OWNER + "_" + REGION);
323 final AccessInfo accessInfo = new AccessInfo();
324 accessInfo.setProjectId(TENANT_ID);
325 vimConnection.setAccessInfo(accessInfo);
326 vimConnectionInfo.add(vimConnection);
327 vnfInstance.setVimConnectionInfo(vimConnectionInfo);
329 final OamIpAddressSource oamIpAddressSource =
330 new OamIpAddressSource(OamIpAddressType.CONFIGURABLE_PROPERTY, "vnfIpAddress");
331 aaiHelper.setOamIpAddressSource("myTestVnfIdOnVnfm", oamIpAddressSource);
335 private GenericVnf createGenericVnf(final String type) {
336 final GenericVnf genericVnf = new GenericVnf();
337 genericVnf.setVnfId("myTestVnfId");
338 genericVnf.setNfType(type);
342 private void addRelationshipFromGenericVnfToVnfm(final GenericVnf genericVnf, final String vnfmId) {
343 final Relationship relationshipToVnfm = new Relationship();
344 relationshipToVnfm.setRelatedLink("/aai/v15/external-system/esr-vnfm-list/esr-vnfm/" + vnfmId);
345 relationshipToVnfm.setRelatedTo("esr-vnfm");
346 final RelationshipData relationshipData = new RelationshipData();
347 relationshipData.setRelationshipKey("esr-vnfm.vnfm-id");
348 relationshipData.setRelationshipValue(vnfmId);
349 relationshipToVnfm.getRelationshipData().add(relationshipData);
351 if (genericVnf.getRelationshipList() == null) {
352 final RelationshipList relationshipList = new RelationshipList();
353 genericVnf.setRelationshipList(relationshipList);
355 genericVnf.getRelationshipList().getRelationship().add(relationshipToVnfm);
358 private void addRelationshipFromGenericVnfToVserver(final GenericVnf genericVnf, final String vserverId) {
359 final Relationship relationshipToVserver = new Relationship();
360 relationshipToVserver.setRelatedTo("vserver");
361 final RelationshipData relationshipData1 = new RelationshipData();
362 relationshipData1.setRelationshipKey("vserver.vserver-id");
363 relationshipData1.setRelationshipValue(vserverId);
364 relationshipToVserver.getRelationshipData().add(relationshipData1);
365 final RelationshipData relationshipData2 = new RelationshipData();
366 relationshipData2.setRelationshipKey("cloud-region.cloud-owner");
367 relationshipData2.setRelationshipValue(CLOUD_OWNER);
368 relationshipToVserver.getRelationshipData().add(relationshipData2);
369 final RelationshipData relationshipData3 = new RelationshipData();
370 relationshipData3.setRelationshipKey("cloud-region.cloud-region-id");
371 relationshipData3.setRelationshipValue(REGION);
372 relationshipToVserver.getRelationshipData().add(relationshipData3);
373 final RelationshipData relationshipData4 = new RelationshipData();
374 relationshipData4.setRelationshipKey("tenant.tenant-id");
375 relationshipData4.setRelationshipValue(TENANT_ID);
376 relationshipToVserver.getRelationshipData().add(relationshipData4);
378 if (genericVnf.getRelationshipList() == null) {
379 final RelationshipList relationshipList = new RelationshipList();
380 genericVnf.setRelationshipList(relationshipList);
382 genericVnf.getRelationshipList().getRelationship().add(relationshipToVserver);
385 private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
387 final String uriAsString;
389 public AaiResourceUriMatcher(final String uriAsString) {
390 this.uriAsString = uriAsString;
394 public boolean matches(final Object item) {
395 if (item instanceof AAIResourceUri) {
396 if (uriAsString.endsWith("...")) {
397 return ((AAIResourceUri) item).build().toString()
398 .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
400 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
406 public void describeTo(final Description description) {}