363780dcc57862082804c48ea5a7f4ea5b53dbc9
[so.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.rest;
22
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.*;
40 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum;
41 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationEnum;
42 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
43 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
44 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201Links;
45 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201LinksSelf;
46 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
47 import org.onap.so.client.aai.AAIResourcesClient;
48 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.beans.factory.annotation.Qualifier;
51 import org.springframework.boot.test.context.SpringBootTest;
52 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
53 import org.springframework.boot.test.mock.mockito.MockBean;
54 import org.springframework.boot.web.server.LocalServerPort;
55 import org.springframework.http.HttpStatus;
56 import org.springframework.http.MediaType;
57 import org.springframework.http.ResponseEntity;
58 import org.springframework.test.context.ActiveProfiles;
59 import org.springframework.test.context.junit4.SpringRunner;
60 import org.springframework.test.web.client.MockRestServiceServer;
61 import org.springframework.web.client.RestTemplate;
62 import javax.inject.Inject;
63 import java.net.URI;
64 import java.net.URISyntaxException;
65 import java.util.*;
66 import static org.junit.Assert.assertEquals;
67 import static org.mockito.ArgumentMatchers.eq;
68 import static org.mockito.Mockito.*;
69 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
70 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
71 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
72 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
73
74 @RunWith(SpringRunner.class)
75 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
76 @ActiveProfiles("test")
77 public class Sol003LcnControllerTest {
78
79     private static final String CLOUD_OWNER = "myTestCloudOwner";
80     private static final String REGION = "myTestRegion";
81     private static final String TENANT_ID = "myTestTenantId";
82
83     @LocalServerPort
84     private int port;
85     @Autowired
86     @Qualifier(CONFIGURABLE_REST_TEMPLATE)
87     private RestTemplate testRestTemplate;
88     private MockRestServiceServer mockRestServer;
89
90     @MockBean
91     private AAIResourcesClient aaiResourcesClient;
92
93     @Autowired
94     private Sol003LcnContoller controller;
95     private final Gson gson = new Gson();
96
97     @Inject
98     private AaiHelper aaiHelper;
99
100     @Before
101     public void setUp() throws Exception {
102         mockRestServer = MockRestServiceServer.bindTo(testRestTemplate).build();
103     }
104
105     @Test
106     public void lcnNotification_IdentifierCreated_Returns204() throws URISyntaxException, InterruptedException {
107         final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
108                 new VnfIdentifierCreationNotification();
109         final ResponseEntity<Void> response =
110                 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
111         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
112     }
113
114     @Test
115     public void lcnNotification_IdentifierDeleted_Returns204() throws URISyntaxException, InterruptedException {
116         final VnfIdentifierCreationNotification vnfIdentifierCreationNotification =
117                 new VnfIdentifierCreationNotification();
118         final ResponseEntity<Void> response =
119                 controller.lcnVnfIdentifierCreationNotificationPost(vnfIdentifierCreationNotification);
120         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
121     }
122
123     @Test
124     public void lcnNotification_InstantiateStartingOrProcessing_NoAction()
125             throws URISyntaxException, InterruptedException {
126         final VnfLcmOperationOccurrenceNotification startingNotification = new VnfLcmOperationOccurrenceNotification();
127         startingNotification.setOperation(OperationEnum.INSTANTIATE);
128         startingNotification.setOperationState(OperationStateEnum.STARTING);
129
130         ResponseEntity<Void> response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(startingNotification);
131         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
132
133         verifyZeroInteractions(aaiResourcesClient);
134
135         final VnfLcmOperationOccurrenceNotification processingNotification =
136                 new VnfLcmOperationOccurrenceNotification();
137         processingNotification.setOperation(OperationEnum.INSTANTIATE);
138         processingNotification.setOperationState(OperationStateEnum.STARTING);
139
140         response = controller.lcnVnfLcmOperationOccurrenceNotificationPost(processingNotification);
141         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
142
143         verifyZeroInteractions(aaiResourcesClient);
144     }
145
146     @Test
147     public void lcnNotification_InstantiateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
148         final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
149                 createNotification(OperationEnum.INSTANTIATE);
150         addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.ADDED);
151         final InlineResponse201 vnfInstance = createVnfInstance();
152
153         mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
154                 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
155
156         final GenericVnf genericVnf = createGenericVnf("vnfmType1");
157         final List<GenericVnf> genericVnfs = new ArrayList<>();
158         genericVnfs.add(genericVnf);
159         doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(List.class),
160                 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
161                         "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
162
163         final ResponseEntity<Void> response =
164                 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
165         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
166
167         final ArgumentCaptor<Object> bodyArgument = ArgumentCaptor.forClass(Object.class);
168         final ArgumentCaptor<AAIResourceUri> uriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
169
170         verify(aaiResourcesClient, timeout(1000).times(2)).update(uriArgument.capture(), bodyArgument.capture());
171
172         assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId",
173                 uriArgument.getAllValues().get(0).build().toString());
174         final GenericVnf updatedGenericVnf = (GenericVnf) bodyArgument.getAllValues().get(0);
175         assertEquals("10.10.10.10", updatedGenericVnf.getIpv4OamAddress());
176         assertEquals("Created", updatedGenericVnf.getOrchestrationStatus());
177
178         assertEquals(
179                 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
180                         + TENANT_ID + "/vservers/vserver/myVnfc1",
181                 uriArgument.getAllValues().get(1).build().toString());
182
183         final Vserver vserver = (Vserver) bodyArgument.getAllValues().get(1);
184         assertEquals("myVnfc1", vserver.getVserverId());
185         final Relationship relationship = vserver.getRelationshipList().getRelationship().get(0);
186         assertEquals("generic-vnf", relationship.getRelatedTo());
187         assertEquals("tosca.relationships.HostedOn", relationship.getRelationshipLabel());
188         assertEquals("/aai/v15/network/generic-vnfs/generic-vnf/myTestVnfId", relationship.getRelatedLink());
189         assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
190         assertEquals("myTestVnfId", relationship.getRelationshipData().get(0).getRelationshipValue());
191     }
192
193     @Test
194     public void lcnNotification_TerminateCompleted_AaiUpdated() throws URISyntaxException, InterruptedException {
195         final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification =
196                 createNotification(OperationEnum.TERMINATE);
197         addVnfcsToNotification(vnfLcmOperationOccurrenceNotification, ChangeTypeEnum.REMOVED);
198
199         final InlineResponse201 vnfInstance = createVnfInstance();
200
201         mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
202                 .andRespond(withSuccess(gson.toJson(vnfInstance), MediaType.APPLICATION_JSON));
203
204         mockRestServer.expect(requestTo(new URI("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm")))
205                 .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON));
206
207         final GenericVnf genericVnf = createGenericVnf("vnfmType1");
208         genericVnf.setSelflink("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
209         final List<GenericVnf> genericVnfs = new ArrayList<>();
210         genericVnfs.add(genericVnf);
211         doReturn(Optional.of(genericVnfs)).when(aaiResourcesClient).get(eq(List.class),
212                 MockitoHamcrest.argThat(new AaiResourceUriMatcher(
213                         "/network/generic-vnfs?selflink=http%3A%2F%2Fvnfm%3A8080%2Fvnfs%2FmyTestVnfIdOnVnfm")));
214
215         final ResponseEntity<Void> response =
216                 controller.lcnVnfLcmOperationOccurrenceNotificationPost(vnfLcmOperationOccurrenceNotification);
217         assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
218
219         final ArgumentCaptor<GenericVnf> genericVnfArgument = ArgumentCaptor.forClass(GenericVnf.class);
220         final ArgumentCaptor<AAIResourceUri> updateUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
221         verify(aaiResourcesClient, timeout(10000000)).update(updateUriArgument.capture(), genericVnfArgument.capture());
222         assertEquals("/network/generic-vnfs/generic-vnf/myTestVnfId", updateUriArgument.getValue().build().toString());
223         assertEquals("Assigned", genericVnfArgument.getValue().getOrchestrationStatus());
224
225         final ArgumentCaptor<AAIResourceUri> deleteUriArgument = ArgumentCaptor.forClass(AAIResourceUri.class);
226
227         verify(aaiResourcesClient, timeout(10000000)).delete(deleteUriArgument.capture());
228
229         assertEquals(
230                 "/cloud-infrastructure/cloud-regions/cloud-region/" + CLOUD_OWNER + "/" + REGION + "/tenants/tenant/"
231                         + TENANT_ID + "/vservers/vserver/myVnfc1",
232                 deleteUriArgument.getAllValues().get(0).build().toString());
233     }
234
235     private VnfLcmOperationOccurrenceNotification createNotification(final OperationEnum operation) {
236         final VnfLcmOperationOccurrenceNotification notification = new VnfLcmOperationOccurrenceNotification();
237         notification.setOperation(operation);
238         notification.setOperationState(OperationStateEnum.COMPLETED);
239
240         final LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance linkToVnfInstance =
241                 new LcnVnfLcmOperationOccurrenceNotificationLinksVnfInstance()
242                         .href("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
243         final LcnVnfLcmOperationOccurrenceNotificationLinks operationLinks =
244                 new LcnVnfLcmOperationOccurrenceNotificationLinks().vnfInstance(linkToVnfInstance);
245         notification.setLinks(operationLinks);
246
247         return notification;
248     }
249
250     private void addVnfcsToNotification(final VnfLcmOperationOccurrenceNotification notification,
251             final ChangeTypeEnum changeType) {
252         final List<LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs> affectedVnfcs = new ArrayList<>();;
253         final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc =
254                 new LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs();
255         vnfc.changeType(changeType);
256         final LcnVnfLcmOperationOccurrenceNotificationComputeResource computeResource =
257                 new LcnVnfLcmOperationOccurrenceNotificationComputeResource();
258         computeResource.setResourceId("myVnfc1");
259         computeResource.setVimConnectionId(CLOUD_OWNER + "_" + REGION);
260         vnfc.setComputeResource(computeResource);
261         affectedVnfcs.add(vnfc);
262         notification.setAffectedVnfcs(affectedVnfcs);
263     }
264
265     private InlineResponse201 createVnfInstance() {
266         final InlineResponse201 vnfInstance = new InlineResponse201();
267         vnfInstance.setId("myTestVnfIdOnVnfm");
268         final InlineResponse201LinksSelf selfLink = new InlineResponse201LinksSelf();
269         selfLink.setHref("http://vnfm:8080/vnfs/myTestVnfIdOnVnfm");
270         final InlineResponse201Links VnfInstancelinks = new InlineResponse201Links();
271         VnfInstancelinks.setSelf(selfLink);
272         vnfInstance.setLinks(VnfInstancelinks);
273
274         final Map<String, String> vnfConfigurableProperties = new HashMap<>();
275         vnfConfigurableProperties.put("vnfIpAddress", "10.10.10.10");
276         vnfInstance.setVnfConfigurableProperties(vnfConfigurableProperties);
277
278         final List<InlineResponse201VimConnectionInfo> vimConnectionInfo = new ArrayList<>();;
279         final InlineResponse201VimConnectionInfo vimConnection = new InlineResponse201VimConnectionInfo();
280         vimConnection.setVimId(CLOUD_OWNER + "_" + REGION);
281         vimConnection.setId(CLOUD_OWNER + "_" + REGION);
282         final AccessInfo accessInfo = new AccessInfo();
283         accessInfo.setProjectId(TENANT_ID);
284         vimConnection.setAccessInfo(accessInfo);
285         vimConnectionInfo.add(vimConnection);
286         vnfInstance.setVimConnectionInfo(vimConnectionInfo);
287
288         final OamIpAddressSource oamIpAddressSource =
289                 new OamIpAddressSource(OamIpAddressType.CONFIGURABLE_PROPERTY, "vnfIpAddress");
290         aaiHelper.setOamIpAddressSource("myTestVnfIdOnVnfm", oamIpAddressSource);
291         return vnfInstance;
292     }
293
294     private GenericVnf createGenericVnf(final String type) {
295         final GenericVnf genericVnf = new GenericVnf();
296         genericVnf.setVnfId("myTestVnfId");
297         genericVnf.setNfType(type);
298         return genericVnf;
299     }
300
301     private class AaiResourceUriMatcher extends BaseMatcher<AAIResourceUri> {
302
303         final String uriAsString;
304
305         public AaiResourceUriMatcher(final String uriAsString) {
306             this.uriAsString = uriAsString;
307         }
308
309         @Override
310         public boolean matches(final Object item) {
311             if (item instanceof AAIResourceUri) {
312                 if (uriAsString.endsWith("...")) {
313                     return ((AAIResourceUri) item).build().toString()
314                             .startsWith(uriAsString.substring(0, uriAsString.indexOf("...")));
315                 }
316                 return ((AAIResourceUri) item).build().toString().equals(uriAsString);
317             }
318             return false;
319         }
320
321         @Override
322         public void describeTo(final Description description) {}
323
324     }
325
326 }