2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications copyright (c) 2019 Nokia
9 * Modifications Copyright (c) 2019 Samsung
10 * ================================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 * ============LICENSE_END============================================
23 * ===================================================================
27 package org.onap.clamp.loop;
29 import static org.assertj.core.api.Assertions.assertThat;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.Optional;
37 import javax.transaction.Transactional;
38 import org.apache.commons.lang3.RandomStringUtils;
39 import org.json.JSONException;
40 import org.junit.Assert;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mockito;
44 import org.onap.clamp.clds.Application;
45 import org.onap.clamp.clds.exception.sdc.controller.BlueprintParserException;
46 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
47 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
48 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
49 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
50 import org.onap.clamp.clds.util.JsonUtils;
51 import org.onap.clamp.clds.util.ResourceFileUtils;
52 import org.onap.clamp.loop.cds.CdsDataInstaller;
53 import org.onap.clamp.loop.service.ServicesRepository;
54 import org.onap.clamp.loop.template.LoopTemplate;
55 import org.onap.clamp.loop.template.LoopTemplateLoopElementModel;
56 import org.onap.clamp.loop.template.LoopTemplatesRepository;
57 import org.onap.clamp.loop.template.PolicyModelId;
58 import org.onap.clamp.loop.template.PolicyModelsRepository;
59 import org.onap.sdc.api.notification.IArtifactInfo;
60 import org.onap.sdc.api.notification.INotificationData;
61 import org.onap.sdc.api.notification.IResourceInstance;
62 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
63 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
64 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
65 import org.onap.sdc.toscaparser.api.elements.Metadata;
66 import org.skyscreamer.jsonassert.JSONAssert;
67 import org.springframework.beans.factory.annotation.Autowired;
68 import org.springframework.beans.factory.annotation.Qualifier;
69 import org.springframework.boot.test.context.SpringBootTest;
70 import org.springframework.test.annotation.Commit;
71 import org.springframework.test.context.ActiveProfiles;
72 import org.springframework.test.context.junit4.SpringRunner;
74 @RunWith(SpringRunner.class)
75 @SpringBootTest(classes = Application.class)
76 @ActiveProfiles({"clamp-default", "clamp-default-user", "clamp-sdc-controller"})
77 public class CsarInstallerItCase {
79 private static final String CSAR_ARTIFACT_NAME_CDS = "example/sdc/service_Vloadbalancerms_cds.csar";
80 private static final String CSAR_ARTIFACT_NAME_NO_CDS = "example/sdc/service_Vloadbalancerms_no_cds.csar";
81 private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92";
82 private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec";
83 private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad";
84 private static final String RESOURCE_INSTANCE_NAME_RESOURCE1 = "ResourceInstanceName1";
85 private static final String RESOURCE_INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2";
88 private LoopTemplatesRepository loopTemplatesRepo;
91 ServicesRepository serviceRepository;
94 PolicyModelsRepository policyModelsRepository;
97 @Qualifier("csarInstaller")
98 private CsarInstaller csarInstaller;
100 private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
101 String blueprintFilePath, String artifactName,
102 String invariantServiceUuid) throws IOException {
103 IResourceInstance resource = Mockito.mock(IResourceInstance.class);
104 Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
105 Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
106 BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
107 Mockito.when(blueprintArtifact.getDcaeBlueprint())
108 .thenReturn(ResourceFileUtils.getResourceAsString(blueprintFilePath));
109 Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(artifactName);
110 Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
111 Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
112 return blueprintArtifact;
115 private CsarHandler buildBadFakeCsarHandler(String generatedName, String csarFileName) throws IOException,
116 SdcToscaParserException {
118 // Build a Bad csar because the blueprint contains a link to a microservice that does not exist in the emulator
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-guilin.yaml", "tca-guilin.yaml",
131 INVARIANT_SERVICE_UUID);
132 listResources.add(blueprintArtifact.getResourceAttached());
133 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
135 // Build fake csarhandler
136 Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
137 // Build fake csar Helper
138 ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
139 Metadata data = Mockito.mock(Metadata.class);
140 Mockito.when(data.getValue("name")).thenReturn(generatedName);
141 Mockito.when(notificationData.getServiceName()).thenReturn(generatedName);
142 Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
144 // Create helper based on real csar to test policy yaml and global properties
146 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
147 String path = Thread.currentThread().getContextClassLoader().getResource(csarFileName).getFile();
148 ISdcCsarHelper sdcHelper = factory.getSdcCsarHelper(path);
149 Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcHelper);
151 // Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
152 Mockito.when(csarHandler.getPolicyModelYaml())
153 .thenReturn(Optional.ofNullable(ResourceFileUtils.getResourceAsString("tosca/tosca_example.yaml")));
157 private CsarHandler buildFakeCsarHandler(String generatedName, String csarFileName) throws IOException,
158 SdcToscaParserException {
159 // Create fake notification
160 INotificationData notificationData = Mockito.mock(INotificationData.class);
161 Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
162 // Create fake resource in notification
163 CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
164 List<IResourceInstance> listResources = new ArrayList<>();
165 Mockito.when(notificationData.getResources()).thenReturn(listResources);
166 Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
167 Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
168 // Create fake blueprint artifact 1 on resource1
169 BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1,
170 INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", "tca.yaml", INVARIANT_SERVICE_UUID);
171 listResources.add(blueprintArtifact.getResourceAttached());
172 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
173 // Create fake blueprint artifact 2 on resource2
174 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
175 "example/sdc/blueprint-dcae/tca_2.yaml", "tca_2.yaml", INVARIANT_SERVICE_UUID);
176 listResources.add(blueprintArtifact.getResourceAttached());
177 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
179 // Create fake blueprint artifact 3 on resource 1 so that it's possible to
180 // test multiple CL deployment per Service/vnf
181 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1, INVARIANT_RESOURCE1_UUID,
182 "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID);
183 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
185 // Build fake csarhandler
186 Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
187 // Build fake csar Helper
188 ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
189 Metadata data = Mockito.mock(Metadata.class);
190 Mockito.when(data.getValue("name")).thenReturn(generatedName);
191 Mockito.when(notificationData.getServiceName()).thenReturn(generatedName);
192 Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
194 // Create helper based on real csar to test policy yaml and global properties
196 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
197 String path = Thread.currentThread().getContextClassLoader().getResource(csarFileName).getFile();
198 ISdcCsarHelper sdcHelper = factory.getSdcCsarHelper(path);
199 Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcHelper);
201 // Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
202 Mockito.when(csarHandler.getPolicyModelYaml())
203 .thenReturn(Optional.ofNullable(ResourceFileUtils.getResourceAsString("tosca/tosca_example.yaml")));
209 public void testGetPolicyModelYaml() throws IOException, SdcToscaParserException, CsarHandlerException {
210 INotificationData notificationData = Mockito.mock(INotificationData.class);
211 IArtifactInfo serviceArtifacts = Mockito.mock(IArtifactInfo.class);
212 Mockito.when(serviceArtifacts.getArtifactType()).thenReturn("TOSCA_CSAR");
213 List<IArtifactInfo> serviceArtifactsList = new ArrayList<>();
214 serviceArtifactsList.add(serviceArtifacts);
215 Mockito.when(notificationData.getServiceArtifacts()).thenReturn(serviceArtifactsList);
217 CsarHandler csarHandler = new CsarHandler(notificationData, "", "");
219 .setFilePath(Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME_CDS).
221 Assert.assertEquals(csarHandler.getPolicyModelYaml(), Optional
222 .ofNullable(ResourceFileUtils.getResourceAsString("example/sdc/expected-result/policy-data.yaml")));
227 public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException,
228 CsarHandlerException, IOException, InterruptedException, BlueprintParserException {
229 String generatedName = RandomStringUtils.randomAlphanumeric(5);
230 CsarHandler csarHandler = buildFakeCsarHandler(generatedName, CSAR_ARTIFACT_NAME_CDS);
231 assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isFalse();
232 csarInstaller.installTheCsar(csarHandler);
233 assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isTrue();
238 public void testWithoutCdsTca() throws SdcArtifactInstallerException, SdcToscaParserException,
239 CsarHandlerException, IOException, InterruptedException, BlueprintParserException {
240 String generatedName = RandomStringUtils.randomAlphanumeric(5);
241 CsarHandler csarHandler = buildFakeCsarHandler(generatedName, CSAR_ARTIFACT_NAME_NO_CDS);
243 assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isFalse();
244 csarInstaller.installTheCsar(csarHandler);
245 assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isTrue();
248 @Test(expected = SdcArtifactInstallerException.class)
250 public void testInstallTheBadCsarTca()
251 throws IOException, SdcToscaParserException, InterruptedException, BlueprintParserException,
252 SdcArtifactInstallerException {
253 // This test validates that the blueprint is well rejected because the blueprint contains a link
254 // to a policy that does not exist on the policy engine emulator.
255 String generatedName = RandomStringUtils.randomAlphanumeric(5);
256 csarInstaller.installTheCsar(buildBadFakeCsarHandler(generatedName, CSAR_ARTIFACT_NAME_NO_CDS));
262 public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException,
263 CsarHandlerException, IOException, JSONException, InterruptedException, BlueprintParserException {
264 String generatedName = RandomStringUtils.randomAlphanumeric(5);
265 csarInstaller.installTheCsar(buildFakeCsarHandler(generatedName, CSAR_ARTIFACT_NAME_CDS));
267 assertThat(serviceRepository.existsById("63cac700-ab9a-4115-a74f-7eac85e3fce0")).isTrue();
268 // We should have CDS info
269 assertThat(serviceRepository.findById("63cac700-ab9a-4115-a74f-7eac85e3fce0").get().getResourceByType("VF")
270 .getAsJsonObject("vLoadBalancerMS 0").getAsJsonObject(
271 CdsDataInstaller.CONTROLLER_PROPERTIES)).isNotNull();
272 assertThat(loopTemplatesRepo.existsById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0",
273 RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml"))).isTrue();
274 assertThat(loopTemplatesRepo.existsById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0",
275 RESOURCE_INSTANCE_NAME_RESOURCE1, "tca_3.yaml"))).isTrue();
276 assertThat(loopTemplatesRepo.existsById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0",
277 RESOURCE_INSTANCE_NAME_RESOURCE2, "tca_2.yaml"))).isTrue();
278 // Verify now that policy and json representation, global properties are well
280 LoopTemplate loopTemplate = loopTemplatesRepo.findById(LoopTemplate.generateLoopTemplateName(generatedName,
281 "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get();
282 assertThat(loopTemplate.getLoopElementModelsUsed()).hasSize(1);
283 assertThat(loopTemplate.getModelService().getServiceUuid()).isEqualTo("63cac700-ab9a-4115-a74f-7eac85e3fce0");
284 JSONAssert.assertEquals(ResourceFileUtils.getResourceAsString("tosca/model-properties.json"),
285 JsonUtils.GSON_JPA_MODEL.toJson(loopTemplate.getModelService()), true);
286 JSONAssert.assertEquals(ResourceFileUtils.getResourceAsString("tosca/service-details.json"),
287 JsonUtils.GSON_JPA_MODEL.toJson(loopTemplate.getModelService().getServiceDetails()), true);
288 JSONAssert.assertEquals(ResourceFileUtils.getResourceAsString("tosca/resource-details.json"),
289 JsonUtils.GSON_JPA_MODEL.toJson(loopTemplate.getModelService().getResourceDetails()), true);
290 assertThat(((LoopTemplateLoopElementModel) (loopTemplate.getLoopElementModelsUsed().toArray()[0]))
291 .getLoopElementModel().getName()).isNotEmpty();
293 loopTemplate = loopTemplatesRepo.findById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0",
294 RESOURCE_INSTANCE_NAME_RESOURCE1, "tca_3.yaml")).get();
295 assertThat(((LoopTemplateLoopElementModel) (loopTemplate.getLoopElementModelsUsed().toArray()[0]))
296 .getLoopElementModel().getName()).isNotEmpty();
297 assertThat(((LoopTemplateLoopElementModel) (loopTemplate.getLoopElementModelsUsed().toArray()[0]))
298 .getLoopElementModel().getName()).isNotEmpty();
299 assertThat(loopTemplate.getMaximumInstancesAllowed()).isEqualByComparingTo(Integer.valueOf(0));
300 loopTemplate = loopTemplatesRepo.findById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0",
301 RESOURCE_INSTANCE_NAME_RESOURCE2, "tca_2.yaml")).get();
302 assertThat(((LoopTemplateLoopElementModel) (loopTemplate.getLoopElementModelsUsed().toArray()[0]))
303 .getLoopElementModel().getName()).isNotEmpty();
305 assertThat(policyModelsRepository.findAll().size()).isGreaterThanOrEqualTo(1);
306 assertThat(policyModelsRepository
307 .existsById(new PolicyModelId("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0"))).isTrue();
308 assertThat(policyModelsRepository
309 .getOne((new PolicyModelId("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0")))
310 .getPolicyModelTosca()).isNotBlank();