85ecaaa3ea38b13adcf6022ed3c0fa68b90646c1
[so.git] / asdc-controller / src / test / java / org / onap / so / asdc / client / ASDCControllerITTest.java
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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.so.asdc.client;
21
22 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
23 import static com.github.tomakehurst.wiremock.client.WireMock.post;
24 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.fail;
29
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.UUID;
34
35 import javax.persistence.EntityNotFoundException;
36
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Ignore;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.TestName;
43 import org.onap.so.asdc.BaseTest;
44 import org.onap.so.asdc.client.exceptions.ASDCControllerException;
45 import org.onap.so.asdc.client.test.emulators.ArtifactInfoImpl;
46 import org.onap.so.asdc.client.test.emulators.DistributionClientEmulator;
47 import org.onap.so.asdc.client.test.emulators.NotificationDataImpl;
48 import org.onap.so.asdc.client.test.emulators.ResourceInfoImpl;
49 import org.onap.so.db.catalog.beans.PnfResource;
50 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
51 import org.onap.so.db.catalog.beans.Service;
52 import org.onap.so.db.catalog.beans.ToscaCsar;
53 import org.onap.so.db.catalog.beans.VnfResource;
54 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
55 import org.onap.so.db.catalog.data.repository.PnfCustomizationRepository;
56 import org.onap.so.db.catalog.data.repository.PnfResourceRepository;
57 import org.onap.so.db.catalog.data.repository.ServiceRepository;
58 import org.onap.so.db.catalog.data.repository.ToscaCsarRepository;
59 import org.onap.so.db.catalog.data.repository.VnfCustomizationRepository;
60 import org.onap.so.db.catalog.data.repository.VnfResourceRepository;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63 import org.springframework.beans.factory.annotation.Autowired;
64 import org.springframework.transaction.annotation.Transactional;
65
66 /**
67  * This is used to run some basic integration test(BIT) for ASDC controller. It will test the csar install and all the
68  * testing csar files are located underneath src/main/resources/download folder,
69  *
70  * PNF csar: service-Testservice140-csar.csar VNF csar: service-Svc140-VF-csar.csar
71  */
72 @Transactional
73 public class ASDCControllerITTest extends BaseTest {
74
75     private Logger logger = LoggerFactory.getLogger(ASDCControllerITTest.class);
76
77     @Rule
78     public TestName testName = new TestName();
79
80     private String serviceUuid;
81     private String serviceInvariantUuid;
82
83     /**
84      * Random UUID served as distribution UUID.
85      */
86     private String distributionId;
87     private String artifactUuid;
88
89     @Autowired
90     private ASDCController asdcController;
91
92     @Autowired
93     private PnfResourceRepository pnfResourceRepository;
94
95     @Autowired
96     private PnfCustomizationRepository pnfCustomizationRepository;
97
98     @Autowired
99     private VnfResourceRepository vnfResourceRepository;
100
101     @Autowired
102     private VnfCustomizationRepository vnfCustomizationRepository;
103
104     @Autowired
105     private ToscaCsarRepository toscaCsarRepository;
106
107     @Autowired
108     private ServiceRepository serviceRepository;
109
110     private DistributionClientEmulator distributionClient;
111
112     @Before
113     public void setUp() {
114         distributionId = UUID.randomUUID().toString();
115         artifactUuid = UUID.randomUUID().toString();
116         logger.info("Using distributionId: {}, artifactUUID: {} for testcase: {}", distributionId, artifactUuid,
117             testName.getMethodName());
118
119         distributionClient = new DistributionClientEmulator();
120         distributionClient.setResourcePath("src/test/resources");
121         asdcController.setDistributionClient(distributionClient);
122         try {
123             asdcController.initASDC();
124         } catch (ASDCControllerException e) {
125             logger.error(e.getMessage(), e);
126             fail(e.getMessage());
127         }
128     }
129
130     @After
131     public void shutDown() {
132         try {
133             asdcController.closeASDC();
134         } catch (ASDCControllerException e) {
135             logger.error(e.getMessage(), e);
136             fail(e.getMessage());
137         }
138     }
139
140     /**
141      * Mock the AAI using wireshark.
142      */
143     private void initMockAaiServer(final String serviceUuid, final String serviceInvariantUuid) {
144         String modelEndpoint = "/aai/v15/service-design-and-creation/models/model/" + serviceInvariantUuid
145             + "/model-vers/model-ver/" + serviceUuid + "?depth=0";
146
147         stubFor(post(urlEqualTo(modelEndpoint)).willReturn(ok()));
148     }
149
150     /**
151      * Test with service-Testservice140-csar.csar.
152      */
153     @Test
154     public void treatNotification_ValidPnfResource_ExpectedOutput() {
155
156         /**
157          * service UUID/invariantUUID from global metadata in service-Testservice140-template.yml.
158          */
159         String serviceUuid = "efaea486-561f-4159-9191-a8d3cb346728";
160         String serviceInvariantUuid = "f2edfbf4-bb0a-4fe7-a57a-71362d4b0b23";
161
162         initMockAaiServer(serviceUuid, serviceInvariantUuid);
163
164         NotificationDataImpl notificationData = new NotificationDataImpl();
165         notificationData.setServiceUUID(serviceUuid);
166         notificationData.setDistributionID(distributionId);
167         notificationData.setServiceInvariantUUID(serviceInvariantUuid);
168         notificationData.setServiceVersion("1.0");
169
170         ResourceInfoImpl resourceInfo = constructPnfResourceInfo();
171         List<ResourceInfoImpl> resourceInfoList = new ArrayList<>();
172         resourceInfoList.add(resourceInfo);
173         notificationData.setResources(resourceInfoList);
174
175         ArtifactInfoImpl artifactInfo = constructPnfServiceArtifact();
176         List<ArtifactInfoImpl> artifactInfoList = new ArrayList<>();
177         artifactInfoList.add(artifactInfo);
178         notificationData.setServiceArtifacts(artifactInfoList);
179
180         try {
181             asdcController.treatNotification(notificationData);
182             logger.info("Checking the database for PNF ingestion");
183
184             /**
185              * Check the tosca csar entity, it should be the same as provided from NotficationData.
186              */
187             ToscaCsar toscaCsar = toscaCsarRepository.findById(artifactUuid)
188                 .orElseThrow(() -> new EntityNotFoundException("Tosca csar: " + artifactUuid + " not found"));
189             assertEquals("tosca csar UUID", artifactUuid, toscaCsar.getArtifactUUID());
190             assertEquals("tosca csar name", "service-Testservice140-csar.csar", toscaCsar.getName());
191             assertEquals("tosca csar version", "1.0", toscaCsar.getVersion());
192             assertNull("tosca csar descrption", toscaCsar.getDescription());
193             assertEquals("tosca csar checksum", "MANUAL_RECORD", toscaCsar.getArtifactChecksum());
194             assertEquals("toscar csar URL", "/download/service-Testservice140-csar.csar", toscaCsar.getUrl());
195
196             /**
197              * Check the service entity, it should be the same as global metadata information in service-Testservice140-template.yml inside csar.
198              */
199             Service service = serviceRepository.findById(serviceUuid)
200                 .orElseThrow(() -> new EntityNotFoundException("Service: " + serviceUuid + " not found"));
201             assertEquals("model UUID", "efaea486-561f-4159-9191-a8d3cb346728", service.getModelUUID());
202             assertEquals("model name", "TestService140", service.getModelName());
203             assertEquals("model invariantUUID", "f2edfbf4-bb0a-4fe7-a57a-71362d4b0b23",
204                 service.getModelInvariantUUID());
205             assertEquals("model version", "1.0", service.getModelVersion());
206             assertEquals("description", "Test Service for extended attributes of PNF resource",
207                 service.getDescription().trim());
208             assertEquals("tosca csar artifact UUID", artifactUuid, service.getCsar().getArtifactUUID());
209             assertEquals("service type", "Network", service.getServiceType());
210             assertEquals("service role", "nfv", service.getServiceRole());
211             assertEquals("environment context", "General_Revenue-Bearing", service.getEnvironmentContext());
212             assertEquals("service category", "Network Service", service.getCategory());
213             assertNull("workload context", service.getWorkloadContext());
214             assertEquals("resource order", "Test140PNF", service.getResourceOrder());
215
216             /**
217              * Check PNF resource, it should be the same as metadata in the topology template in service-Testservice140-template.yml
218              * OR
219              * global metadata in the resource-Test140pnf-template.yml
220              */
221             String pnfResourceKey = "9c54e269-122b-4e8a-8b2a-6eac849b441a";
222             PnfResource pnfResource = pnfResourceRepository.findById(pnfResourceKey)
223                 .orElseThrow(() -> new EntityNotFoundException("PNF resource:" + pnfResourceKey + " not found"));
224             assertNull("orchestration mode", pnfResource.getOrchestrationMode());
225             assertEquals("Description", "Oracle", pnfResource.getDescription().trim());
226             assertEquals("model UUID", pnfResourceKey, pnfResource.getModelUUID());
227             assertEquals("model invariant UUID", "d832a027-75f3-455d-9de4-f02fcdee7e7e",
228                 pnfResource.getModelInvariantUUID());
229             assertEquals("model version", "1.0", pnfResource.getModelVersion());
230             assertEquals("model name", "Test140PNF", pnfResource.getModelName());
231             assertEquals("tosca node type", "org.openecomp.resource.pnf.Test140pnf", pnfResource.getToscaNodeType());
232             assertEquals("resource category", "Application L4+", pnfResource.getCategory());
233             assertEquals("resource sub category", "Call Control", pnfResource.getSubCategory());
234
235             /**
236              * Check PNF resource customization, it should be the same as metadata in the topology template in service-Testservice140-template.yml
237              * OR
238              * global metadata in the resource-Test140pnf-template.yml
239              */
240             String pnfCustomizationKey = "428a3d73-f962-4cc2-ba62-2483c45d6b12";
241             PnfResourceCustomization pnfCustomization = pnfCustomizationRepository
242                 .findById(pnfCustomizationKey).orElseThrow(
243                     () -> new EntityNotFoundException(
244                         "PNF resource customization: " + pnfCustomizationKey + " not found"));
245             assertEquals("model customizationUUID", pnfCustomizationKey, pnfCustomization.getModelCustomizationUUID());
246             assertEquals("model instance name", "Test140PNF 0", pnfCustomization.getModelInstanceName());
247             assertEquals("NF type", "", pnfCustomization.getNfType());
248             assertEquals("NF Role", "nf", pnfCustomization.getNfRole());
249             assertEquals("NF function", "nf", pnfCustomization.getNfFunction());
250             assertEquals("NF naming code", "", pnfCustomization.getNfNamingCode());
251             assertEquals("PNF resource model UUID", pnfResourceKey, pnfCustomization.getPnfResources().getModelUUID());
252             assertEquals("Multi stage design", "", pnfCustomization.getMultiStageDesign());
253             assertNull("resource input", pnfCustomization.getResourceInput());
254             assertEquals("cds blueprint name(sdnc_model_name property)", pnfCustomization.getBlueprintName(),
255                 pnfCustomization.getBlueprintName());
256             assertEquals("cds blueprint version(sdnc_model_version property)", pnfCustomization.getBlueprintVersion(),
257                 pnfCustomization.getBlueprintVersion());
258
259             /**
260              * Check the pnf resource customization with service mapping
261              */
262             List<PnfResourceCustomization> pnfCustList = service.getPnfCustomizations();
263             assertEquals("PNF resource customization entity", 1, pnfCustList.size());
264             assertEquals(pnfCustomizationKey, pnfCustList.get(0).getModelCustomizationUUID());
265
266         } catch (Exception e) {
267             logger.info(e.getMessage(), e);
268             fail(e.getMessage());
269         }
270     }
271
272     private ArtifactInfoImpl constructPnfServiceArtifact() {
273         ArtifactInfoImpl artifactInfo = new ArtifactInfoImpl();
274         artifactInfo.setArtifactType(ASDCConfiguration.TOSCA_CSAR);
275         artifactInfo.setArtifactURL("/download/service-Testservice140-csar.csar");
276         artifactInfo.setArtifactName("service-Testservice140-csar.csar");
277         artifactInfo.setArtifactVersion("1.0");
278         artifactInfo.setArtifactUUID(artifactUuid);
279         return artifactInfo;
280     }
281
282     /**
283      * Construct the PnfResourceInfo based on the resource-Test140Pnf-template.yml from
284      * service-Testservice140-csar.csar.
285      */
286     private ResourceInfoImpl constructPnfResourceInfo() {
287         ResourceInfoImpl resourceInfo = new ResourceInfoImpl();
288         resourceInfo.setResourceInstanceName("Test140PNF");
289         resourceInfo.setResourceInvariantUUID("d832a027-75f3-455d-9de4-f02fcdee7e7e");
290         resourceInfo.setResoucreType("PNF");
291         resourceInfo.setCategory("Application L4+");
292         resourceInfo.setSubcategory("Call Control");
293         resourceInfo.setResourceUUID("9c54e269-122b-4e8a-8b2a-6eac849b441a");
294         resourceInfo.setResourceCustomizationUUID("428a3d73-f962-4cc2-ba62-2483c45d6b12");
295         resourceInfo.setResourceVersion("1.0");
296         return resourceInfo;
297     }
298
299     /**
300      * Testing with the service-Svc140-VF-csar.csar.
301      */
302     @Test
303     @Ignore
304     public void treatNotification_ValidVnfResource_ExpectedOutput() {
305
306         /**
307          * service UUID/invariantUUID from global metadata in resource-Testvf140-template.yml.
308          */
309         String serviceUuid = "28944a37-de3f-46ec-9c60-b57036fbd26d";
310         String serviceInvariantUuid = "9e900d3e-1e2e-4124-a5c2-4345734dc9de";
311
312         initMockAaiServer(serviceUuid, serviceInvariantUuid);
313
314         NotificationDataImpl notificationData = new NotificationDataImpl();
315         notificationData.setServiceUUID(serviceUuid);
316         notificationData.setDistributionID(distributionId);
317         notificationData.setServiceInvariantUUID(serviceInvariantUuid);
318         notificationData.setServiceVersion("1.0");
319
320         ResourceInfoImpl resourceInfo = constructVnfResourceInfo();
321         List<ResourceInfoImpl> resourceInfoList = new ArrayList<>();
322         resourceInfoList.add(resourceInfo);
323         notificationData.setResources(resourceInfoList);
324
325         ArtifactInfoImpl artifactInfo = constructVnfServiceArtifact();
326         List<ArtifactInfoImpl> artifactInfoList = new ArrayList<>();
327         artifactInfoList.add(artifactInfo);
328         notificationData.setServiceArtifacts(artifactInfoList);
329
330         try {
331             asdcController.treatNotification(notificationData);
332             logger.info("Checking the database for VNF ingestion");
333
334             /**
335              * Check the tosca csar entity, it should be the same as provided from NotficationData.
336              */
337             ToscaCsar toscaCsar = toscaCsarRepository.findById(artifactUuid)
338                 .orElseThrow(() -> new EntityNotFoundException("Tosca csar: " + artifactUuid + " not found"));
339             assertEquals("tosca csar UUID", artifactUuid, toscaCsar.getArtifactUUID());
340             assertEquals("tosca csar name", "service-Svc140-VF-csar.csar", toscaCsar.getName());
341             assertEquals("tosca csar version", "1.0", toscaCsar.getVersion());
342             assertNull("tosca csar descrption", toscaCsar.getDescription());
343             assertEquals("tosca csar checksum", "MANUAL_RECORD", toscaCsar.getArtifactChecksum());
344             assertEquals("toscar csar URL", "/download/service-Svc140-VF-csar.csar", toscaCsar.getUrl());
345
346             /**
347              * Check the service entity, it should be the same as global metadata information in service-Testservice140-template.yml inside csar.
348              */
349             Service service = serviceRepository.findById(serviceUuid)
350                 .orElseThrow(() -> new EntityNotFoundException("Service: " + serviceUuid + " not found"));
351             assertEquals("model UUID", serviceUuid, service.getModelUUID());
352             assertEquals("model name", "SVC140", service.getModelName());
353             assertEquals("model invariantUUID", serviceInvariantUuid,
354                 service.getModelInvariantUUID());
355             assertEquals("model version", "1.0", service.getModelVersion());
356             assertEquals("description", "SVC140",
357                 service.getDescription().trim());
358             assertEquals("tosca csar artifact UUID", artifactUuid, service.getCsar().getArtifactUUID());
359             assertEquals("service type", "ST", service.getServiceType());
360             assertEquals("service role", "Sr", service.getServiceRole());
361             assertEquals("environment context", "General_Revenue-Bearing", service.getEnvironmentContext());
362             assertEquals("service category", "Network Service", service.getCategory());
363             assertNull("workload context", service.getWorkloadContext());
364             assertEquals("resource order", "TestVF140", service.getResourceOrder());
365
366             /**
367              * Check VNF resource, it should be the same as metadata in the topology template in service-Testservice140-template.yml
368              * OR
369              * global metadata in the resource-Testservice140-template.yml
370              */
371             String vnfResourceKey = "d20d3ea9-2f54-4071-8b5c-fd746dde245e";
372             VnfResource vnfResource = vnfResourceRepository.findById(vnfResourceKey)
373                 .orElseThrow(() -> new EntityNotFoundException("VNF resource:" + vnfResourceKey + " not found"));
374             assertEquals("orchestration mode", "HEAT", vnfResource.getOrchestrationMode());
375             assertEquals("Description", "TestPNF140", vnfResource.getDescription().trim());
376             assertEquals("model UUID", vnfResourceKey, vnfResource.getModelUUID());
377             assertEquals("model invariant UUID", "7a4bffa2-fac5-4b8b-b348-0bdf313a1aeb",
378                 vnfResource.getModelInvariantUUID());
379             assertEquals("model version", "1.0", vnfResource.getModelVersion());
380             assertEquals("model name", "TestVF140", vnfResource.getModelName());
381             assertEquals("tosca node type", "org.openecomp.resource.vf.Testvf140", vnfResource.getToscaNodeType());
382             assertEquals("resource category", "Application L4+", vnfResource.getCategory());
383             assertEquals("resource sub category", "Database", vnfResource.getSubCategory());
384
385             /**
386              * Check VNF resource customization, it should be the same as metadata in the topology template in service-Testservice140-template.yml
387              * OR
388              * global metadata in the resource-Testservice140-template.yml
389              */
390             String vnfCustomizationKey = "ca1c8455-8ce2-4a76-a037-3f4cf01cffa0";
391             VnfResourceCustomization vnfCustomization = vnfCustomizationRepository
392                 .findById(vnfCustomizationKey).orElseThrow(
393                     () -> new EntityNotFoundException(
394                         "VNF resource customization: " + vnfCustomizationKey + " not found"));
395             assertEquals("model customizationUUID", vnfCustomizationKey, vnfCustomization.getModelCustomizationUUID());
396             assertEquals("model instance name", "TestVF140 0", vnfCustomization.getModelInstanceName());
397             assertNull("NF type", vnfCustomization.getNfType());
398             assertNull("NF Role", vnfCustomization.getNfRole());
399             assertNull("NF function", vnfCustomization.getNfFunction());
400             assertNull("NF naming code", vnfCustomization.getNfNamingCode());
401             assertEquals("VNF resource model UUID", vnfResourceKey, vnfCustomization.getVnfResources().getModelUUID());
402             assertEquals("Multi stage design", "false", vnfCustomization.getMultiStageDesign());
403             assertNull("resource input", vnfCustomization.getResourceInput());
404             assertEquals("cds blueprint name(sdnc_model_name property)", vnfCustomization.getBlueprintName(),
405                 vnfCustomization.getBlueprintName());
406             assertEquals("cds blueprint version(sdnc_model_version property)", vnfCustomization.getBlueprintVersion(),
407                 vnfCustomization.getBlueprintVersion());
408             /**
409              * Check the vnf resource customization with service mapping
410              */
411             List<VnfResourceCustomization> vnfCustList = service.getVnfCustomizations();
412             assertEquals("VNF resource customization entity", 1, vnfCustList.size());
413             assertEquals(vnfCustomizationKey, vnfCustList.get(0).getModelCustomizationUUID());
414         } catch (Exception e) {
415             logger.info(e.getMessage(), e);
416             fail(e.getMessage());
417         }
418     }
419
420     private ArtifactInfoImpl constructVnfServiceArtifact() {
421         ArtifactInfoImpl artifactInfo = new ArtifactInfoImpl();
422         artifactInfo.setArtifactType(ASDCConfiguration.TOSCA_CSAR);
423         artifactInfo.setArtifactURL("/download/service-Svc140-VF-csar.csar");
424         artifactInfo.setArtifactName("service-Svc140-VF-csar.csar");
425         artifactInfo.setArtifactVersion("1.0");
426         artifactInfo.setArtifactUUID(artifactUuid);
427         return artifactInfo;
428     }
429
430     /**
431      * Construct the PnfResourceInfo based on the resource-Testvf140-template.yml from service-Svc140-VF-csar.csar.
432      */
433     private ResourceInfoImpl constructVnfResourceInfo() {
434         ResourceInfoImpl resourceInfo = new ResourceInfoImpl();
435         resourceInfo.setResourceInstanceName("TestVF140");
436         resourceInfo.setResourceInvariantUUID("7a4bffa2-fac5-4b8b-b348-0bdf313a1aeb");
437         resourceInfo.setResoucreType("VF");
438         resourceInfo.setCategory("Application L4+");
439         resourceInfo.setSubcategory("Database");
440         resourceInfo.setResourceUUID("d20d3ea9-2f54-4071-8b5c-fd746dde245e");
441         resourceInfo.setResourceCustomizationUUID("ca1c8455-8ce2-4a76-a037-3f4cf01cffa0");
442         resourceInfo.setResourceVersion("1.0");
443         resourceInfo.setArtifacts(Collections.EMPTY_LIST);
444         return resourceInfo;
445     }
446 }