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.GenericVnf;
50 import org.onap.aai.domain.yang.GenericVnfs;
51 import org.onap.aai.domain.yang.Relationship;
52 import org.onap.aai.domain.yang.Vserver;
53 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
54 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
55 import org.onap.so.adapters.vnfmadapter.extclients.aai.OamIpAddressSource;
56 import org.onap.so.adapters.vnfmadapter.extclients.aai.OamIpAddressSource.OamIpAddressType;
57 import org.onap.so.adapters.vnfmadapter.extclients.vim.model.AccessInfo;
58 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
59 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum;
60 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationComputeResource;
61 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinks;
62 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance;
63 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfIdentifierCreationNotification;
64 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification;
65 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationEnum;
66 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
67 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
68 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201Links;
69 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201LinksSelf;
70 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
71 import org.onap.so.client.aai.AAIResourcesClient;
72 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
73 import org.springframework.beans.factory.annotation.Autowired;
74 import org.springframework.beans.factory.annotation.Qualifier;
75 import org.springframework.boot.test.context.SpringBootTest;
76 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
77 import org.springframework.boot.test.mock.mockito.MockBean;
78 import org.springframework.boot.web.server.LocalServerPort;
79 import org.springframework.http.HttpStatus;
80 import org.springframework.http.MediaType;
81 import org.springframework.http.ResponseEntity;
82 import org.springframework.test.context.ActiveProfiles;
83 import org.springframework.test.context.junit4.SpringRunner;
84 import org.springframework.test.web.client.MockRestServiceServer;
85 import org.springframework.web.client.RestTemplate;
87 @RunWith(SpringRunner.class)
88 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
89 @ActiveProfiles("test")
90 public class Sol003LcnControllerTest {
92 private static final String CLOUD_OWNER = "myTestCloudOwner";
93 private static final String REGION = "myTestRegion";
94 private static final String TENANT_ID = "myTestTenantId";
99 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
100 private RestTemplate testRestTemplate;
101 private MockRestServiceServer mockRestServer;
104 private AAIResourcesClient aaiResourcesClient;
107 private Sol003LcnContoller controller;
108 private final Gson gson = new Gson();
111 private AaiHelper aaiHelper;
114 public void setUp() throws Exception {
115 mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
119 public void lcnNotification_IdentifierCreated_Returns204() throws URISyntaxException, InterruptedException {
120 final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
121 new VnfIdentifierCreationNotification();
122 final ResponseEntity<Void> response =
123 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
124 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
128 public void lcnNotification_IdentifierDeleted_Returns204() throws URISyntaxException, InterruptedException {
129 final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
130 new VnfIdentifierCreationNotification();
131 final ResponseEntity<Void> response =
132 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
133 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
137 public void lcnNotification_InstantiateStartingOrProcessing_NoAction()
138 throws URISyntaxException, InterruptedException {
139 final VnfLcmOperationOccurrenceNotification startingNotification = new VnfLcmOperationOccurrenceNotification();
140 startingNotification.setOperation(OperationEnum.INSTANTIATE);
141 startingNotification.setOperationState(OperationStateEnum.STARTING);
143 ResponseEntity<Void> response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(startingNotification);
144 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
146 verifyZeroInteractions(aaiResourcesClient);
148 final VnfLcmOperationOccurrenceNotification processingNotification =
149 new VnfLcmOperationOccurrenceNotification();
150 processingNotification.setOperation(OperationEnum.INSTANTIATE);
151 processingNotification.setOperationState(OperationStateEnum.STARTING);
153 response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(processingNotification);
154 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
156 verifyZeroInteractions(aaiResourcesClient);
160 public void lcnNotification_InstantiateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
161 final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
162 createNotification(OperationEnum.INSTANTIATE);
163 addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.ADDED);
164 final InlineResponse201 vnfInstance = createVnfInstance();
166 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
167 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
169 final GenericVnf genericVnf = createGenericVnf("vnfmType1");
170 final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
171 listOfGenericVnfs.add(genericVnf);
172 final GenericVnfs genericVnfs = new GenericVnfs();
173 genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
174 doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
175 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
176 "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
178 final ResponseEntity<Void> response =
179 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
180 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
182 final ArgumentCaptor<Object> bodyArgument1 = ArgumentCaptor.forClass(Object.class);
183 final ArgumentCaptor<AAIResourceUri> uriArgument1 = ArgumentCaptor.forClass(AAIResourceUri.class);
185 verify(aaiResourcesClient, timeout(1000)).update(uriArgument1.capture(), bodyArgument1.capture());
187 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId",
188 uriArgument1.getAllValues().get(0).build().toString());
189 final GenericVnf updatedGenericVnf = (GenericVnf) bodyArgument1.getAllValues().get(0);
190 assertEquals("10.10.10.10", updatedGenericVnf.getIpv4OamAddress());
191 assertEquals("Created", updatedGenericVnf.getOrchestrationStatus());
193 final ArgumentCaptor<Object> bodyArgument2 = ArgumentCaptor.forClass(Object.class);
194 final ArgumentCaptor<AAIResourceUri> uriArgument2 = ArgumentCaptor.forClass(AAIResourceUri.class);
195 verify(aaiResourcesClient, timeout(1000)).create(uriArgument2.capture(), bodyArgument2.capture());
198 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
199 + TENANT_ID + "/vservers/vserver/myVnfc1",
200 uriArgument2.getAllValues().get(0).build().toString());
202 final Vserver vserver = (Vserver) bodyArgument2.getAllValues().get(0);
203 assertEquals("myVnfc1", vserver.getVserverId());
204 final Relationship relationship = vserver.getRelationshipList().getRelationship().get(0);
205 assertEquals("generic-vnf", relationship.getRelatedTo());
206 assertEquals("tosca.relationships.HostedOn", relationship.getRelationshipLabel());
207 assertEquals("/aai/v15/network/generic-vnfs/generic-vnf/myTestVnfId", relationship.getRelatedLink());
208 assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
209 assertEquals("myTestVnfId", relationship.getRelationshipData().get(0).getRelationshipValue());
213 public void lcnNotification_TerminateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
214 final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
215 createNotification(OperationEnum.TERMINATE);
216 addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.REMOVED);
218 final InlineResponse201 vnfInstance = createVnfInstance();
220 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
221 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
223 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
224 .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON));
226 final GenericVnf genericVnf = createGenericVnf("vnfmType1");
227 genericVnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
228 final List<GenericVnf> listOfGenericVnfs = new ArrayList<>();
229 listOfGenericVnfs.add(genericVnf);
230 final GenericVnfs genericVnfs = new GenericVnfs();
231 genericVnfs.getGenericVnf().addAll(listOfGenericVnfs);
233 doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(GenericVnfs.class),
234 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
235 "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
237 final ResponseEntity<Void> response =
238 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
239 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
241 final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
242 final ArgumentCaptor<AAIResourceUri> updateUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
243 verify(aaiResourcesClient, timeout(10000000)).update(updateUriArgument.capture(), genericVnfArgument.capture());
244 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", updateUriArgument.getValue().build().toString());
245 assertEquals("Assigned", genericVnfArgument.getValue().getOrchestrationStatus());
247 final ArgumentCaptor<AAIResourceUri> deleteUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
249 verify(aaiResourcesClient, timeout(10000000)).delete(deleteUriArgument.capture());
252 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
253 + TENANT_ID + "/vservers/vserver/myVnfc1",
254 deleteUriArgument.getAllValues().get(0).build().toString());
257 private VnfLcmOperationOccurrenceNotification createNotification(final OperationEnum operation) {
258 final VnfLcmOperationOccurrenceNotification notification = new VnfLcmOperationOccurrenceNotification();
259 notification.setOperation(operation);
260 notification.setOperationState(OperationStateEnum.COMPLETED);
262 final LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance linkToVnfInstance =
263 new LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance()
264 .href("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
265 final LcnVnfLcmOperationOccurrenceNotificationLinks operationLinks =
266 new LcnVnfLcmOperationOccurrenceNotificationLinks().vnfInstance(linkToVnfInstance);
267 notification.setLinks(operationLinks);
272 private void addVnfcsToNotification(final VnfLcmOperationOccurrenceNotification notification,
273 final ChangeTypeEnum changeType) {
274 final List<LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs> affectedVnfcs = new ArrayList<>();;
275 final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc =
276 new LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs();
277 vnfc.changeType(changeType);
278 final LcnVnfLcmOperationOccurrenceNotificationComputeResource computeResource =
279 new LcnVnfLcmOperationOccurrenceNotificationComputeResource();
280 computeResource.setResourceId("myVnfc1");
281 computeResource.setVimConnectionId(CLOUD_OWNER + "_" + REGION);
282 vnfc.setComputeResource(computeResource);
283 affectedVnfcs.add(vnfc);
284 notification.setAffectedVnfcs(affectedVnfcs);
287 private InlineResponse201 createVnfInstance() {
288 final InlineResponse201 vnfInstance = new InlineResponse201();
289 vnfInstance.setId("myTestVnfIdOnVnfm");
290 final InlineResponse201LinksSelf selfLink = new InlineResponse201LinksSelf();
291 selfLink.setHref("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
292 final InlineResponse201Links VnfInstancelinks = new InlineResponse201Links();
293 VnfInstancelinks.setSelf(selfLink);
294 vnfInstance.setLinks(VnfInstancelinks);
296 final Map<String, String> vnfConfigurableProperties = new HashMap<>();
297 vnfConfigurableProperties.put("vnfIpAddress", "10.10.10.10");
298 vnfInstance.setVnfConfigurableProperties(vnfConfigurableProperties);
300 final List<InlineResponse201VimConnectionInfo> vimConnectionInfo = new ArrayList<>();;
301 final InlineResponse201VimConnectionInfo vimConnection = new InlineResponse201VimConnectionInfo();
302 vimConnection.setVimId(CLOUD_OWNER + "_" + REGION);
303 vimConnection.setId(CLOUD_OWNER + "_" + REGION);
304 final AccessInfo accessInfo = new AccessInfo();
305 accessInfo.setProjectId(TENANT_ID);
306 vimConnection.setAccessInfo(accessInfo);
307 vimConnectionInfo.add(vimConnection);
308 vnfInstance.setVimConnectionInfo(vimConnectionInfo);
310 final OamIpAddressSource oamIpAddressSource =
311 new OamIpAddressSource(OamIpAddressType.CONFIGURABLE_PROPERTY, "vnfIpAddress");
312 aaiHelper.setOamIpAddressSource("myTestVnfIdOnVnfm", oamIpAddressSource);
316 private GenericVnf createGenericVnf(final String type) {
317 final GenericVnf genericVnf = new GenericVnf();
318 genericVnf.setVnfId("myTestVnfId");
319 genericVnf.setNfType(type);
323 private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
325 final String uriAsString;
327 public AaiResourceUriMatcher(final String uriAsString) {
328 this.uriAsString = uriAsString;
332 public boolean matches(final Object item) {
333 if (item instanceof AAIResourceUri) {
334 if (uriAsString.endsWith("...")) {
335 return ((AAIResourceUri) item).build().toString()
336 .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
338 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
344 public void describeTo(final Description description) {}