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;
38 import javax.transaction.Transactional;
40 import org.apache.commons.lang3.RandomStringUtils;
41 import org.json.JSONException;
42 import org.junit.Assert;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mockito;
46 import org.onap.clamp.clds.Application;
47 import org.onap.clamp.clds.exception.sdc.controller.BlueprintParserException;
48 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
49 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
50 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
51 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
52 import org.onap.clamp.clds.util.JsonUtils;
53 import org.onap.clamp.clds.util.ResourceFileUtil;
54 import org.onap.clamp.loop.service.ServiceRepository;
55 import org.onap.clamp.loop.template.LoopTemplate;
56 import org.onap.clamp.loop.template.LoopTemplateLoopElementModel;
57 import org.onap.clamp.loop.template.LoopTemplatesRepository;
58 import org.onap.clamp.loop.template.PolicyModelId;
59 import org.onap.clamp.loop.template.PolicyModelsRepository;
60 import org.onap.sdc.api.notification.IArtifactInfo;
61 import org.onap.sdc.api.notification.INotificationData;
62 import org.onap.sdc.api.notification.IResourceInstance;
63 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
64 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
65 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
66 import org.onap.sdc.toscaparser.api.elements.Metadata;
67 import org.skyscreamer.jsonassert.JSONAssert;
68 import org.springframework.beans.factory.annotation.Autowired;
69 import org.springframework.beans.factory.annotation.Qualifier;
70 import org.springframework.boot.test.context.SpringBootTest;
71 import org.springframework.test.annotation.Commit;
72 import org.springframework.test.context.ActiveProfiles;
73 import org.springframework.test.context.junit4.SpringRunner;
75 @RunWith(SpringRunner.class)
76 @SpringBootTest(classes = Application.class)
77 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller")
78 public class CsarInstallerItCase {
80 private static final String CSAR_ARTIFACT_NAME = "example/sdc/service_Vloadbalancerms_csar.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 ServiceRepository serviceRepository;
94 PolicyModelsRepository policyModelsRepository;
97 @Qualifier("csarInstaller")
98 private CsarInstaller csarInstaller;
100 private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
101 String blueprintFilePath, String artifactName, String invariantServiceUuid) throws IOException {
102 IResourceInstance resource = Mockito.mock(IResourceInstance.class);
103 Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
104 Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
105 BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
106 Mockito.when(blueprintArtifact.getDcaeBlueprint())
107 .thenReturn(ResourceFileUtil.getResourceAsString(blueprintFilePath));
108 Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(artifactName);
109 Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
110 Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
111 return blueprintArtifact;
114 private CsarHandler buildFakeCsarHandler(String generatedName) throws IOException, SdcToscaParserException {
115 // Create fake notification
116 INotificationData notificationData = Mockito.mock(INotificationData.class);
117 Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
118 // Create fake resource in notification
119 CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
120 List<IResourceInstance> listResources = new ArrayList<>();
121 Mockito.when(notificationData.getResources()).thenReturn(listResources);
122 Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
123 Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
124 // Create fake blueprint artifact 1 on resource1
125 BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1,
126 INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", "tca.yaml", INVARIANT_SERVICE_UUID);
127 listResources.add(blueprintArtifact.getResourceAttached());
128 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
129 // Create fake blueprint artifact 2 on resource2
130 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
131 "example/sdc/blueprint-dcae/tca_2.yaml", "tca_2.yaml", INVARIANT_SERVICE_UUID);
132 listResources.add(blueprintArtifact.getResourceAttached());
133 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
135 // Create fake blueprint artifact 3 on resource 1 so that it's possible to
136 // test multiple CL deployment per Service/vnf
137 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1, INVARIANT_RESOURCE1_UUID,
138 "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID);
139 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
141 // Build fake csarhandler
142 Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
143 // Build fake csar Helper
144 ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
145 Metadata data = Mockito.mock(Metadata.class);
146 Mockito.when(data.getValue("name")).thenReturn(generatedName);
147 Mockito.when(notificationData.getServiceName()).thenReturn(generatedName);
148 Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
150 // Create helper based on real csar to test policy yaml and global properties
152 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
153 String path = Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile();
154 ISdcCsarHelper sdcHelper = factory.getSdcCsarHelper(path);
155 Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcHelper);
157 // Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
158 Mockito.when(csarHandler.getPolicyModelYaml())
159 .thenReturn(Optional.ofNullable(ResourceFileUtil.getResourceAsString("tosca/tosca_example.yaml")));
165 public void testGetPolicyModelYaml() throws IOException, SdcToscaParserException, CsarHandlerException {
166 INotificationData notificationData = Mockito.mock(INotificationData.class);
167 IArtifactInfo serviceArtifacts = Mockito.mock(IArtifactInfo.class);
168 Mockito.when(serviceArtifacts.getArtifactType()).thenReturn("TOSCA_CSAR");
169 List<IArtifactInfo> serviceArtifactsList = new ArrayList<>();
170 serviceArtifactsList.add(serviceArtifacts);
171 Mockito.when(notificationData.getServiceArtifacts()).thenReturn(serviceArtifactsList);
173 CsarHandler csarHandler = new CsarHandler(notificationData, "", "");
175 .setFilePath(Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile());
176 Optional<String> testyaml = csarHandler.getPolicyModelYaml();
177 Assert.assertEquals(testyaml, Optional
178 .ofNullable(ResourceFileUtil.getResourceAsString("example/sdc/expected-result/policy-data.yaml")));
183 public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException,
184 CsarHandlerException, IOException, InterruptedException, BlueprintParserException {
185 String generatedName = RandomStringUtils.randomAlphanumeric(5);
186 CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
187 assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isFalse();
188 csarInstaller.installTheCsar(csarHandler);
189 assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isTrue();
195 public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException,
196 CsarHandlerException, IOException, JSONException, InterruptedException, BlueprintParserException {
197 String generatedName = RandomStringUtils.randomAlphanumeric(5);
198 CsarHandler csar = buildFakeCsarHandler(generatedName);
199 csarInstaller.installTheCsar(csar);
200 assertThat(serviceRepository.existsById("63cac700-ab9a-4115-a74f-7eac85e3fce0")).isTrue();
201 assertThat(loopTemplatesRepo.existsById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0",
202 RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml"))).isTrue();
203 assertThat(loopTemplatesRepo.existsById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0",
204 RESOURCE_INSTANCE_NAME_RESOURCE1, "tca_3.yaml"))).isTrue();
205 assertThat(loopTemplatesRepo.existsById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0",
206 RESOURCE_INSTANCE_NAME_RESOURCE2, "tca_2.yaml"))).isTrue();
207 // Verify now that policy and json representation, global properties are well
209 LoopTemplate loopTemplate = loopTemplatesRepo.findById(LoopTemplate.generateLoopTemplateName(generatedName,
210 "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get();
211 assertThat(loopTemplate.getSvgRepresentation()).startsWith("<svg ");
212 assertThat(loopTemplate.getLoopElementModelsUsed()).hasSize(1);
213 assertThat(loopTemplate.getModelService().getServiceUuid()).isEqualTo("63cac700-ab9a-4115-a74f-7eac85e3fce0");
214 JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/model-properties.json"),
215 JsonUtils.GSON_JPA_MODEL.toJson(loopTemplate.getModelService()), true);
216 JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/service-details.json"),
217 JsonUtils.GSON_JPA_MODEL.toJson(loopTemplate.getModelService().getServiceDetails()), true);
218 JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/resource-details.json"),
219 JsonUtils.GSON_JPA_MODEL.toJson(loopTemplate.getModelService().getResourceDetails()), true);
220 assertThat(((LoopTemplateLoopElementModel) (loopTemplate.getLoopElementModelsUsed().toArray()[0]))
221 .getLoopElementModel().getName()).isNotEmpty();
223 loopTemplate = loopTemplatesRepo.findById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0",
224 RESOURCE_INSTANCE_NAME_RESOURCE1, "tca_3.yaml")).get();
225 assertThat(((LoopTemplateLoopElementModel) (loopTemplate.getLoopElementModelsUsed().toArray()[0]))
226 .getLoopElementModel().getName()).isNotEmpty();
227 assertThat(((LoopTemplateLoopElementModel) (loopTemplate.getLoopElementModelsUsed().toArray()[0]))
228 .getLoopElementModel().getName()).isNotEmpty();
229 assertThat(loopTemplate.getMaximumInstancesAllowed()).isEqualByComparingTo(Integer.valueOf(0));
230 loopTemplate = loopTemplatesRepo.findById(LoopTemplate.generateLoopTemplateName(generatedName, "1.0",
231 RESOURCE_INSTANCE_NAME_RESOURCE2, "tca_2.yaml")).get();
232 assertThat(((LoopTemplateLoopElementModel) (loopTemplate.getLoopElementModelsUsed().toArray()[0]))
233 .getLoopElementModel().getName()).isNotEmpty();
235 assertThat(policyModelsRepository.findAll().size()).isGreaterThanOrEqualTo(1);
236 assertThat(policyModelsRepository
237 .existsById(new PolicyModelId("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0"))).isTrue();
238 assertThat(policyModelsRepository
239 .getOne((new PolicyModelId("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0")))
240 .getPolicyModelTosca()).isNotBlank();