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;
30 import static org.mockito.Matchers.any;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
37 import java.util.Optional;
39 import javax.transaction.Transactional;
41 import org.apache.commons.lang3.RandomStringUtils;
42 import org.json.JSONException;
43 import org.junit.Assert;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mockito;
47 import org.onap.clamp.clds.Application;
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.sdc.controller.installer.BlueprintArtifact;
52 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
53 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
54 import org.onap.clamp.clds.util.ResourceFileUtil;
55 import org.onap.sdc.api.notification.IArtifactInfo;
56 import org.onap.sdc.api.notification.INotificationData;
57 import org.onap.sdc.api.notification.IResourceInstance;
58 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
59 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
60 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
61 import org.onap.sdc.toscaparser.api.elements.Metadata;
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.context.ActiveProfiles;
66 import org.springframework.test.context.junit4.SpringRunner;
68 @RunWith(SpringRunner.class)
69 @SpringBootTest(classes = Application.class)
70 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller-new")
71 public class CsarInstallerItCase {
73 private static final String CSAR_ARTIFACT_NAME = "example/sdc/service-Vloadbalancerms-csar.csar";
74 private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92";
75 private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec";
76 private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad";
77 private static final String RESOURCE_INSTANCE_NAME_RESOURCE1 = "ResourceInstanceName1";
78 private static final String RESOURCE_INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2";
81 private LoopsRepository loopsRepo;
84 @Qualifier("loopInstaller")
85 private CsarInstaller csarInstaller;
87 private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
88 String blueprintFilePath, String artifactName, String invariantServiceUuid) throws IOException {
89 IResourceInstance resource = Mockito.mock(IResourceInstance.class);
90 Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
91 Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
92 BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
93 Mockito.when(blueprintArtifact.getDcaeBlueprint())
94 .thenReturn(ResourceFileUtil.getResourceAsString(blueprintFilePath));
95 Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(artifactName);
96 Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
97 Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
98 return blueprintArtifact;
101 private CsarHandler buildFakeCsarHandler(String generatedName) throws IOException, SdcToscaParserException {
102 // Create fake notification
103 INotificationData notificationData = Mockito.mock(INotificationData.class);
104 Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
105 // Create fake resource in notification
106 CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
107 List<IResourceInstance> listResources = new ArrayList<>();
108 Mockito.when(notificationData.getResources()).thenReturn(listResources);
109 Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
110 Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
111 // Create fake blueprint artifact 1 on resource1
112 BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1,
113 INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", "tca.yaml", INVARIANT_SERVICE_UUID);
114 listResources.add(blueprintArtifact.getResourceAttached());
115 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
116 // Create fake blueprint artifact 2 on resource2
117 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
118 "example/sdc/blueprint-dcae/tca_2.yaml", "tca_2.yaml", INVARIANT_SERVICE_UUID);
119 listResources.add(blueprintArtifact.getResourceAttached());
120 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
122 // Create fake blueprint artifact 3 on resource 1 so that it's possible to
123 // test multiple CL deployment per Service/vnf
124 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1, INVARIANT_RESOURCE1_UUID,
125 "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID);
126 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
128 // Build fake csarhandler
129 Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
130 // Build fake csar Helper
131 ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
132 Metadata data = Mockito.mock(Metadata.class);
133 Mockito.when(data.getValue("name")).thenReturn(generatedName);
134 Mockito.when(notificationData.getServiceName()).thenReturn(generatedName);
135 Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
137 // Create helper based on real csar to test policy yaml and global properties
139 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
140 String path = Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile();
141 ISdcCsarHelper sdcHelper = factory
142 .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, "", "");
162 csarHandler.setFilePath(Thread.currentThread().getContextClassLoader()
163 .getResource(CSAR_ARTIFACT_NAME).getFile());
164 Optional<String> testyaml = csarHandler.getPolicyModelYaml();
165 Assert.assertEquals(testyaml, Optional.ofNullable(ResourceFileUtil
166 .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 public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException,
183 CsarHandlerException, IOException, JSONException, InterruptedException, PolicyModelException {
184 String generatedName = RandomStringUtils.randomAlphanumeric(5);
185 CsarHandler csar = buildFakeCsarHandler(generatedName);
186 csarInstaller.installTheCsar(csar);
188 .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")))
191 .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca_3.yaml")))
194 .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE2, "tca_2.yaml")))
196 // Verify now that policy and json representation, global properties are well
198 Loop loop = loopsRepo
199 .findById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get();
200 assertThat(loop.getSvgRepresentation()).startsWith("<svg ");
201 assertThat(loop.getGlobalPropertiesJson().get("dcaeDeployParameters")).isNotNull();
202 assertThat(loop.getMicroServicePolicies()).hasSize(1);
203 assertThat(loop.getOperationalPolicies()).hasSize(1);
204 assertThat(loop.getModelPropertiesJson().get("serviceDetails")).isNotNull();
205 assertThat(loop.getModelPropertiesJson().get("resourceDetails")).isNotNull();
208 @Test(expected = SdcArtifactInstallerException.class)
210 public void shouldThrowSdcArtifactInstallerException() throws SdcArtifactInstallerException, SdcToscaParserException, IOException, InterruptedException, PolicyModelException {
211 String generatedName = RandomStringUtils.randomAlphanumeric(5);
212 CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
213 Mockito.when(csarHandler.getMapOfBlueprints()).thenThrow(IOException.class);
214 csarInstaller.installTheCsar(csarHandler);