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