e1b963cc4d21bf3f34229763f377c5c6db14f349
[clamp.git] / src / test / java / org / onap / clamp / clds / it / sdc / controller / installer / CsarInstallerItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Modifications copyright (c) 2019 Nokia
9  * ===================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END============================================
22  * ===================================================================
23  *
24  */
25
26 package org.onap.clamp.clds.it.sdc.controller.installer;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertTrue;
32 import static org.junit.Assert.fail;
33
34 import java.io.IOException;
35 import java.nio.charset.StandardCharsets;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
41 import org.apache.commons.io.IOUtils;
42 import org.apache.commons.lang3.RandomStringUtils;
43 import org.json.JSONException;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mockito;
47 import org.onap.clamp.clds.dao.CldsDao;
48 import org.onap.clamp.clds.exception.policy.PolicyModelException;
49 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
50 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
51 import org.onap.clamp.clds.model.CldsModel;
52 import org.onap.clamp.clds.model.CldsTemplate;
53 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
54 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
55 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
56 import org.onap.clamp.clds.sdc.controller.installer.CsarInstallerImpl;
57 import org.onap.clamp.clds.util.ResourceFileUtil;
58 import org.onap.sdc.api.notification.INotificationData;
59 import org.onap.sdc.api.notification.IResourceInstance;
60 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
61 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
62 import org.onap.sdc.toscaparser.api.elements.Metadata;
63 import org.skyscreamer.jsonassert.JSONAssert;
64 import org.springframework.beans.factory.annotation.Autowired;
65 import org.springframework.beans.factory.annotation.Qualifier;
66 import org.springframework.boot.test.context.SpringBootTest;
67 import org.springframework.test.context.ActiveProfiles;
68 import org.springframework.test.context.junit4.SpringRunner;
69
70 @RunWith(SpringRunner.class)
71 @SpringBootTest
72 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller")
73 public class CsarInstallerItCase {
74
75     private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92";
76     private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec";
77     private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad";
78     private static final String RESOURCE_INSTANCE_NAME_RESOURCE1 = "ResourceInstanceName1";
79     private static final String RESOURCE_INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2";
80
81     @Autowired
82     @Qualifier("oldModelInstaller")
83     private CsarInstaller csarInstaller;
84
85     @Autowired
86     private CldsDao cldsDao;
87
88     @Test(expected = SdcArtifactInstallerException.class)
89     public void testInstallTheCsarFail() throws SdcArtifactInstallerException, SdcToscaParserException,
90         CsarHandlerException, IOException, InterruptedException, PolicyModelException {
91         CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
92         BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
93         Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(Mockito.mock(IResourceInstance.class));
94         Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
95         blueprintMap.put("resourceid", blueprintArtifact);
96         Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
97         Mockito.when(blueprintArtifact.getDcaeBlueprint()).thenReturn(
98             IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/not-recognized.yaml"),
99                 StandardCharsets.UTF_8));
100         csarInstaller.installTheCsar(csarHandler);
101         fail("Should have raised an SdcArtifactInstallerException");
102     }
103
104     private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
105         String blueprintFilePath, String artifactName, String invariantServiceUuid) throws IOException {
106         IResourceInstance resource = Mockito.mock(IResourceInstance.class);
107         Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
108         Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
109         BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
110         Mockito.when(blueprintArtifact.getDcaeBlueprint())
111             .thenReturn(ResourceFileUtil.getResourceAsString(blueprintFilePath));
112         Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(artifactName);
113         Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
114         Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
115         return blueprintArtifact;
116     }
117
118     private CsarHandler buildFakeCsarHandler(String generatedName) throws IOException {
119         // Create fake notification
120         INotificationData notificationData = Mockito.mock(INotificationData.class);
121         Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
122         // Create fake resource in notification
123         CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
124         List<IResourceInstance> listResources = new ArrayList<>();
125         Mockito.when(notificationData.getResources()).thenReturn(listResources);
126         Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
127         Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
128         // Create fake blueprint artifact 1 on resource1
129         BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1,
130             INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", "tca.yaml", INVARIANT_SERVICE_UUID);
131         listResources.add(blueprintArtifact.getResourceAttached());
132         blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
133         // Create fake blueprint artifact 2 on resource2
134         blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
135             "example/sdc/blueprint-dcae/tca_2.yaml", "tca_2.yaml", INVARIANT_SERVICE_UUID);
136         listResources.add(blueprintArtifact.getResourceAttached());
137         blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
138
139         // Create fake blueprint artifact 3 on resource 1 so that it's possible to
140         // test multiple CL deployment per Service/vnf
141         blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1, INVARIANT_RESOURCE1_UUID,
142             "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID);
143         blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
144
145         // Build fake csarhandler
146         Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
147         // Build fake csar Helper
148         ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
149         Metadata data = Mockito.mock(Metadata.class);
150         Mockito.when(data.getValue("name")).thenReturn(generatedName);
151         Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
152         Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
153         return csarHandler;
154     }
155
156     @Test
157     public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException,
158         CsarHandlerException, IOException, InterruptedException, PolicyModelException {
159         String generatedName = RandomStringUtils.randomAlphanumeric(5);
160         CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
161         assertFalse(csarInstaller.isCsarAlreadyDeployed(csarHandler));
162         csarInstaller.installTheCsar(csarHandler);
163         assertTrue(csarInstaller.isCsarAlreadyDeployed(csarHandler));
164     }
165
166     @Test
167     public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException,
168         CsarHandlerException, IOException, JSONException, InterruptedException, PolicyModelException {
169         String generatedName = RandomStringUtils.randomAlphanumeric(5);
170         CsarHandler csar = buildFakeCsarHandler(generatedName);
171         csarInstaller.installTheCsar(csar);
172         CldsModel cldsModel1 = verifyClosedLoopModelLoadedInDb(csar, "tca.yaml");
173         JSONAssert.assertEquals(
174             IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json"),
175                 StandardCharsets.UTF_8),
176             cldsModel1.getPropText(), true);
177         CldsModel cldsModel2 = verifyClosedLoopModelLoadedInDb(csar, "tca_2.yaml");
178         JSONAssert.assertEquals(IOUtils.toString(
179             ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca-2.json"),
180             StandardCharsets.UTF_8), cldsModel2.getPropText(), true);
181         CldsModel cldsModel3 = verifyClosedLoopModelLoadedInDb(csar, "tca_3.yaml");
182         JSONAssert.assertEquals(IOUtils.toString(
183             ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca-3.json"),
184             StandardCharsets.UTF_8), cldsModel3.getPropText(), true);
185     }
186
187     private CldsModel verifyClosedLoopModelLoadedInDb(CsarHandler csar, String artifactName)
188         throws SdcArtifactInstallerException {
189
190         // Get the template back from DB
191         CldsTemplate templateFromDb = CldsTemplate.retrieve(cldsDao, CsarInstallerImpl.TEMPLATE_NAME_PREFIX
192             + CsarInstallerImpl.buildModelName(csar, csar.getMapOfBlueprints().get(artifactName)), false);
193         assertNotNull(templateFromDb);
194         assertNotNull(templateFromDb.getBpmnText());
195         assertNotNull(templateFromDb.getImageText());
196         assertNotNull(templateFromDb.getPropText());
197         assertTrue(templateFromDb.getPropText().contains("global")
198             && templateFromDb.getPropText().contains("node_templates:"));
199         assertEquals(templateFromDb.getName(), CsarInstallerImpl.TEMPLATE_NAME_PREFIX
200             + CsarInstallerImpl.buildModelName(csar, csar.getMapOfBlueprints().get(artifactName)));
201         // Get the Model back from DB
202         CldsModel modelFromDb = CldsModel.retrieve(cldsDao,
203             CsarInstallerImpl.buildModelName(csar, csar.getMapOfBlueprints().get(artifactName)), true);
204         assertNotNull(modelFromDb);
205         assertNotNull(modelFromDb.getBpmnText());
206         assertNotNull(modelFromDb.getImageText());
207         assertNotNull(modelFromDb.getPropText());
208         assertTrue(modelFromDb.getPropText().contains("policy_id"));
209         assertEquals(CsarInstallerImpl.buildModelName(csar, csar.getMapOfBlueprints().get(artifactName)),
210             modelFromDb.getName());
211         assertEquals(CsarInstallerImpl.CONTROL_NAME_PREFIX, modelFromDb.getControlNamePrefix());
212         return modelFromDb;
213     }
214 }