2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
26 package org.onap.clamp.loop;
28 import static org.assertj.core.api.Assertions.assertThat;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.Optional;
37 import javax.transaction.Transactional;
39 import org.apache.commons.lang3.RandomStringUtils;
40 import org.json.JSONException;
41 import org.junit.Assert;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mockito;
45 import org.onap.clamp.clds.Application;
46 import org.onap.clamp.clds.exception.policy.PolicyModelException;
47 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
48 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
49 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
50 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
51 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
52 import org.onap.clamp.clds.util.JsonUtils;
53 import org.onap.clamp.clds.util.ResourceFileUtil;
54 import org.onap.sdc.api.notification.IArtifactInfo;
55 import org.onap.sdc.api.notification.INotificationData;
56 import org.onap.sdc.api.notification.IResourceInstance;
57 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
58 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
59 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
60 import org.onap.sdc.toscaparser.api.elements.Metadata;
61 import org.skyscreamer.jsonassert.JSONAssert;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.beans.factory.annotation.Qualifier;
64 import org.springframework.boot.test.context.SpringBootTest;
65 import org.springframework.test.annotation.Rollback;
66 import org.springframework.test.context.ActiveProfiles;
67 import org.springframework.test.context.junit4.SpringRunner;
69 @RunWith(SpringRunner.class)
70 @SpringBootTest(classes = Application.class)
71 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller-new")
72 public class CsarInstallerItCase {
74 private static final String CSAR_ARTIFACT_NAME = "example/sdc/service_Vloadbalancerms_csar.csar";
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";
82 private LoopsRepository loopsRepo;
85 @Qualifier("loopInstaller")
86 private CsarInstaller csarInstaller;
88 private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
89 String blueprintFilePath, String artifactName, String invariantServiceUuid) throws IOException {
90 IResourceInstance resource = Mockito.mock(IResourceInstance.class);
91 Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
92 Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
93 BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
94 Mockito.when(blueprintArtifact.getDcaeBlueprint())
95 .thenReturn(ResourceFileUtil.getResourceAsString(blueprintFilePath));
96 Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(artifactName);
97 Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
98 Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
99 return blueprintArtifact;
102 private CsarHandler buildFakeCsarHandler(String generatedName) throws IOException, SdcToscaParserException {
103 // Create fake notification
104 INotificationData notificationData = Mockito.mock(INotificationData.class);
105 Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
106 // Create fake resource in notification
107 CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
108 List<IResourceInstance> listResources = new ArrayList<>();
109 Mockito.when(notificationData.getResources()).thenReturn(listResources);
110 Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
111 Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
112 // Create fake blueprint artifact 1 on resource1
113 BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1,
114 INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", "tca.yaml", INVARIANT_SERVICE_UUID);
115 listResources.add(blueprintArtifact.getResourceAttached());
116 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
117 // Create fake blueprint artifact 2 on resource2
118 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
119 "example/sdc/blueprint-dcae/tca_2.yaml", "tca_2.yaml", INVARIANT_SERVICE_UUID);
120 listResources.add(blueprintArtifact.getResourceAttached());
121 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
123 // Create fake blueprint artifact 3 on resource 1 so that it's possible to
124 // test multiple CL deployment per Service/vnf
125 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1, INVARIANT_RESOURCE1_UUID,
126 "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID);
127 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
129 // Build fake csarhandler
130 Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
131 // Build fake csar Helper
132 ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
133 Metadata data = Mockito.mock(Metadata.class);
134 Mockito.when(data.getValue("name")).thenReturn(generatedName);
135 Mockito.when(notificationData.getServiceName()).thenReturn(generatedName);
136 Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
138 // Create helper based on real csar to test policy yaml and global properties
140 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
141 String path = Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile();
142 ISdcCsarHelper sdcHelper = factory.getSdcCsarHelper(path);
143 Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcHelper);
145 // Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
146 Mockito.when(csarHandler.getPolicyModelYaml())
147 .thenReturn(Optional.ofNullable(ResourceFileUtil.getResourceAsString("tosca/tosca_example.yaml")));
153 public void testGetPolicyModelYaml() throws IOException, SdcToscaParserException, CsarHandlerException {
154 INotificationData notificationData = Mockito.mock(INotificationData.class);
155 IArtifactInfo serviceArtifacts = Mockito.mock(IArtifactInfo.class);
156 Mockito.when(serviceArtifacts.getArtifactType()).thenReturn("TOSCA_CSAR");
157 List<IArtifactInfo> serviceArtifactsList = new ArrayList<>();
158 serviceArtifactsList.add(serviceArtifacts);
159 Mockito.when(notificationData.getServiceArtifacts()).thenReturn(serviceArtifactsList);
161 CsarHandler csarHandler = new CsarHandler(notificationData, "", "");
163 .setFilePath(Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile());
164 Optional<String> testyaml = csarHandler.getPolicyModelYaml();
165 Assert.assertEquals(testyaml,
166 Optional.ofNullable(ResourceFileUtil.getResourceAsString("example/sdc/expected-result/policy-data.yaml")));
171 public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException,
172 CsarHandlerException, IOException, InterruptedException, PolicyModelException {
173 String generatedName = RandomStringUtils.randomAlphanumeric(5);
174 CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
175 assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isFalse();
176 csarInstaller.installTheCsar(csarHandler);
177 assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isTrue();
182 @Rollback(value = false)
183 public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException,
184 CsarHandlerException, IOException, JSONException, InterruptedException, PolicyModelException {
185 String generatedName = RandomStringUtils.randomAlphanumeric(5);
186 CsarHandler csar = buildFakeCsarHandler(generatedName);
187 csarInstaller.installTheCsar(csar);
189 .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")))
192 .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca_3.yaml")))
195 .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE2, "tca_2.yaml")))
197 // Verify now that policy and json representation, global properties are well
199 Loop loop = loopsRepo
200 .findById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get();
201 assertThat(loop.getSvgRepresentation()).startsWith("<svg ");
202 assertThat(loop.getGlobalPropertiesJson().get("dcaeDeployParameters")).isNotNull();
203 assertThat(loop.getMicroServicePolicies()).hasSize(1);
204 assertThat(loop.getOperationalPolicies()).hasSize(1);
205 assertThat(loop.getModelPropertiesJson().get("serviceDetails")).isNotNull();
206 assertThat(loop.getModelPropertiesJson().get("resourceDetails")).isNotNull();
207 JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/model-properties.json"),
208 JsonUtils.GSON.toJson(loop.getModelPropertiesJson()), true);