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