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