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 com.google.gson.Gson;
24 import org.hamcrest.BaseMatcher;
25 import org.hamcrest.Description;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.hamcrest.MockitoHamcrest;
31 import org.onap.aai.domain.yang.GenericVnf;
32 import org.onap.aai.domain.yang.Relationship;
33 import org.onap.aai.domain.yang.Vserver;
34 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
35 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
36 import org.onap.so.adapters.vnfmadapter.extclients.aai.OamIpAddressSource;
37 import org.onap.so.adapters.vnfmadapter.extclients.aai.OamIpAddressSource.OamIpAddressType;
38 import org.onap.so.adapters.vnfmadapter.extclients.vim.model.AccessInfo;
39 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
40 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum;
41 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationComputeResource;
42 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinks;
43 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance;
44 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfIdentifierCreationNotification;
45 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification;
46 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationEnum;
47 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
48 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
49 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201Links;
50 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201LinksSelf;
51 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
52 import org.onap.so.client.aai.AAIResourcesClient;
53 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.beans.factory.annotation.Qualifier;
56 import org.springframework.boot.test.context.SpringBootTest;
57 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
58 import org.springframework.boot.test.mock.mockito.MockBean;
59 import org.springframework.boot.web.server.LocalServerPort;
60 import org.springframework.http.HttpStatus;
61 import org.springframework.http.MediaType;
62 import org.springframework.http.ResponseEntity;
63 import org.springframework.test.context.ActiveProfiles;
64 import org.springframework.test.context.junit4.SpringRunner;
65 import org.springframework.test.web.client.MockRestServiceServer;
66 import org.springframework.web.client.RestTemplate;
67 import javax.inject.Inject;
69 import java.net.URISyntaxException;
70 import java.util.ArrayList;
71 import java.util.HashMap;
72 import java.util.List;
74 import java.util.Optional;
75 import static org.junit.Assert.assertEquals;
76 import static org.mockito.ArgumentMatchers.eq;
77 import static org.mockito.Mockito.timeout;
78 import static org.mockito.Mockito.verifyZeroInteractions;
79 import static org.mockito.Mockito.doReturn;
80 import static org.mockito.Mockito.verify;
81 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
82 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
83 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
84 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
86 @RunWith(SpringRunner.class)
87 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
88 @ActiveProfiles("test")
89 public class Sol003LcnControllerTest {
91 private static final String CLOUD_OWNER = "myTestCloudOwner";
92 private static final String REGION = "myTestRegion";
93 private static final String TENANT_ID = "myTestTenantId";
98 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
99 private RestTemplate testRestTemplate;
100 private MockRestServiceServer mockRestServer;
103 private AAIResourcesClient aaiResourcesClient;
106 private Sol003LcnContoller controller;
107 private final Gson gson = new Gson();
110 private AaiHelper aaiHelper;
113 public void setUp() throws Exception {
114 mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
118 public void lcnNotification_IdentifierCreated_Returns204() throws URISyntaxException, InterruptedException {
119 final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
120 new VnfIdentifierCreationNotification();
121 final ResponseEntity<Void> response =
122 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
123 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
127 public void lcnNotification_IdentifierDeleted_Returns204() throws URISyntaxException, InterruptedException {
128 final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
129 new VnfIdentifierCreationNotification();
130 final ResponseEntity<Void> response =
131 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
132 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
136 public void lcnNotification_InstantiateStartingOrProcessing_NoAction()
137 throws URISyntaxException, InterruptedException {
138 final VnfLcmOperationOccurrenceNotification startingNotification = new VnfLcmOperationOccurrenceNotification();
139 startingNotification.setOperation(OperationEnum.INSTANTIATE);
140 startingNotification.setOperationState(OperationStateEnum.STARTING);
142 ResponseEntity<Void> response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(startingNotification);
143 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
145 verifyZeroInteractions(aaiResourcesClient);
147 final VnfLcmOperationOccurrenceNotification processingNotification =
148 new VnfLcmOperationOccurrenceNotification();
149 processingNotification.setOperation(OperationEnum.INSTANTIATE);
150 processingNotification.setOperationState(OperationStateEnum.STARTING);
152 response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(processingNotification);
153 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
155 verifyZeroInteractions(aaiResourcesClient);
159 public void lcnNotification_InstantiateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
160 final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
161 createNotification(OperationEnum.INSTANTIATE);
162 addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.ADDED);
163 final InlineResponse201 vnfInstance = createVnfInstance();
165 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
166 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
168 final GenericVnf genericVnf = createGenericVnf("vnfmType1");
169 final List<GenericVnf> genericVnfs = new ArrayList<>();
170 genericVnfs.add(genericVnf);
171 doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(List.class),
172 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
173 "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
175 final ResponseEntity<Void> response =
176 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
177 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
179 final ArgumentCaptor<Object> bodyArgument = ArgumentCaptor.forClass(Object.class);
180 final ArgumentCaptor<AAIResourceUri> uriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
182 verify(aaiResourcesClient, timeout(1000).times(2)).update(uriArgument.capture(), bodyArgument.capture());
184 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId",
185 uriArgument.getAllValues().get(0).build().toString());
186 final GenericVnf updatedGenericVnf = (GenericVnf) bodyArgument.getAllValues().get(0);
187 assertEquals("10.10.10.10", updatedGenericVnf.getIpv4OamAddress());
188 assertEquals("Created", updatedGenericVnf.getOrchestrationStatus());
191 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
192 + TENANT_ID + "/vservers/vserver/myVnfc1",
193 uriArgument.getAllValues().get(1).build().toString());
195 final Vserver vserver = (Vserver) bodyArgument.getAllValues().get(1);
196 assertEquals("myVnfc1", vserver.getVserverId());
197 final Relationship relationship = vserver.getRelationshipList().getRelationship().get(0);
198 assertEquals("generic-vnf", relationship.getRelatedTo());
199 assertEquals("tosca.relationships.HostedOn", relationship.getRelationshipLabel());
200 assertEquals("/aai/v15/network/generic-vnfs/generic-vnf/myTestVnfId", relationship.getRelatedLink());
201 assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
202 assertEquals("myTestVnfId", relationship.getRelationshipData().get(0).getRelationshipValue());
206 public void lcnNotification_TerminateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
207 final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
208 createNotification(OperationEnum.TERMINATE);
209 addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.REMOVED);
211 final InlineResponse201 vnfInstance = createVnfInstance();
213 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
214 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
216 mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
217 .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON));
219 final GenericVnf genericVnf = createGenericVnf("vnfmType1");
220 genericVnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
221 final List<GenericVnf> genericVnfs = new ArrayList<>();
222 genericVnfs.add(genericVnf);
223 doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(List.class),
224 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
225 "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
227 final ResponseEntity<Void> response =
228 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
229 assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
231 final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
232 final ArgumentCaptor<AAIResourceUri> updateUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
233 verify(aaiResourcesClient, timeout(10000000)).update(updateUriArgument.capture(), genericVnfArgument.capture());
234 assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", updateUriArgument.getValue().build().toString());
235 assertEquals("Assigned", genericVnfArgument.getValue().getOrchestrationStatus());
237 final ArgumentCaptor<AAIResourceUri> deleteUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
239 verify(aaiResourcesClient, timeout(10000000)).delete(deleteUriArgument.capture());
242 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
243 + TENANT_ID + "/vservers/vserver/myVnfc1",
244 deleteUriArgument.getAllValues().get(0).build().toString());
247 private VnfLcmOperationOccurrenceNotification createNotification(final OperationEnum operation) {
248 final VnfLcmOperationOccurrenceNotification notification = new VnfLcmOperationOccurrenceNotification();
249 notification.setOperation(operation);
250 notification.setOperationState(OperationStateEnum.COMPLETED);
252 final LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance linkToVnfInstance =
253 new LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance()
254 .href("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
255 final LcnVnfLcmOperationOccurrenceNotificationLinks operationLinks =
256 new LcnVnfLcmOperationOccurrenceNotificationLinks().vnfInstance(linkToVnfInstance);
257 notification.setLinks(operationLinks);
262 private void addVnfcsToNotification(final VnfLcmOperationOccurrenceNotification notification,
263 final ChangeTypeEnum changeType) {
264 final List<LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs> affectedVnfcs = new ArrayList<>();;
265 final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc =
266 new LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs();
267 vnfc.changeType(changeType);
268 final LcnVnfLcmOperationOccurrenceNotificationComputeResource computeResource =
269 new LcnVnfLcmOperationOccurrenceNotificationComputeResource();
270 computeResource.setResourceId("myVnfc1");
271 computeResource.setVimConnectionId(CLOUD_OWNER + "_" + REGION);
272 vnfc.setComputeResource(computeResource);
273 affectedVnfcs.add(vnfc);
274 notification.setAffectedVnfcs(affectedVnfcs);
277 private InlineResponse201 createVnfInstance() {
278 final InlineResponse201 vnfInstance = new InlineResponse201();
279 vnfInstance.setId("myTestVnfIdOnVnfm");
280 final InlineResponse201LinksSelf selfLink = new InlineResponse201LinksSelf();
281 selfLink.setHref("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
282 final InlineResponse201Links VnfInstancelinks = new InlineResponse201Links();
283 VnfInstancelinks.setSelf(selfLink);
284 vnfInstance.setLinks(VnfInstancelinks);
286 final Map<String, String> vnfConfigurableProperties = new HashMap<>();
287 vnfConfigurableProperties.put("vnfIpAddress", "10.10.10.10");
288 vnfInstance.setVnfConfigurableProperties(vnfConfigurableProperties);
290 final List<InlineResponse201VimConnectionInfo> vimConnectionInfo = new ArrayList<>();;
291 final InlineResponse201VimConnectionInfo vimConnection = new InlineResponse201VimConnectionInfo();
292 vimConnection.setVimId(CLOUD_OWNER + "_" + REGION);
293 vimConnection.setId(CLOUD_OWNER + "_" + REGION);
294 final AccessInfo accessInfo = new AccessInfo();
295 accessInfo.setProjectId(TENANT_ID);
296 vimConnection.setAccessInfo(accessInfo);
297 vimConnectionInfo.add(vimConnection);
298 vnfInstance.setVimConnectionInfo(vimConnectionInfo);
300 final OamIpAddressSource oamIpAddressSource =
301 new OamIpAddressSource(OamIpAddressType.CONFIGURABLE_PROPERTY, "vnfIpAddress");
302 aaiHelper.setOamIpAddressSource("myTestVnfIdOnVnfm", oamIpAddressSource);
306 private GenericVnf createGenericVnf(final String type) {
307 final GenericVnf genericVnf = new GenericVnf();
308 genericVnf.setVnfId("myTestVnfId");
309 genericVnf.setNfType(type);
313 private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
315 final String uriAsString;
317 public AaiResourceUriMatcher(final String uriAsString) {
318 this.uriAsString = uriAsString;
322 public boolean matches(final Object item) {
323 if (item instanceof AAIResourceUri) {
324 if (uriAsString.endsWith("...")) {
325 return ((AAIResourceUri) item).build().toString()
326 .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
328 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
334 public void describeTo(final Description description) {}