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