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.ResourceFileUtil;
53 import org.onap.sdc.api.notification.IArtifactInfo;
54 import org.onap.sdc.api.notification.INotificationData;
55 import org.onap.sdc.api.notification.IResourceInstance;
56 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
57 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
58 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
59 import org.onap.sdc.toscaparser.api.elements.Metadata;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.beans.factory.annotation.Qualifier;
62 import org.springframework.boot.test.context.SpringBootTest;
63 import org.springframework.test.annotation.Rollback;
64 import org.springframework.test.context.ActiveProfiles;
65 import org.springframework.test.context.junit4.SpringRunner;
67 @RunWith(SpringRunner.class)
68 @SpringBootTest(classes = Application.class)
69 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller-new")
70 public class CsarInstallerItCase {
72 private static final String CSAR_ARTIFACT_NAME = "example/sdc/service-Vloadbalancerms-csar.csar";
73 private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92";
74 private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec";
75 private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad";
76 private static final String RESOURCE_INSTANCE_NAME_RESOURCE1 = "ResourceInstanceName1";
77 private static final String RESOURCE_INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2";
80 private LoopsRepository loopsRepo;
83 @Qualifier("loopInstaller")
84 private CsarInstaller csarInstaller;
86 private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
87 String blueprintFilePath, String artifactName, String invariantServiceUuid) throws IOException {
88 IResourceInstance resource = Mockito.mock(IResourceInstance.class);
89 Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
90 Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
91 BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
92 Mockito.when(blueprintArtifact.getDcaeBlueprint())
93 .thenReturn(ResourceFileUtil.getResourceAsString(blueprintFilePath));
94 Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(artifactName);
95 Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
96 Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
97 return blueprintArtifact;
100 private CsarHandler buildFakeCsarHandler(String generatedName) throws IOException, SdcToscaParserException {
101 // Create fake notification
102 INotificationData notificationData = Mockito.mock(INotificationData.class);
103 Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
104 // Create fake resource in notification
105 CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
106 List<IResourceInstance> listResources = new ArrayList<>();
107 Mockito.when(notificationData.getResources()).thenReturn(listResources);
108 Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
109 Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
110 // Create fake blueprint artifact 1 on resource1
111 BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1,
112 INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", "tca.yaml", INVARIANT_SERVICE_UUID);
113 listResources.add(blueprintArtifact.getResourceAttached());
114 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
115 // Create fake blueprint artifact 2 on resource2
116 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
117 "example/sdc/blueprint-dcae/tca_2.yaml", "tca_2.yaml", INVARIANT_SERVICE_UUID);
118 listResources.add(blueprintArtifact.getResourceAttached());
119 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
121 // Create fake blueprint artifact 3 on resource 1 so that it's possible to
122 // test multiple CL deployment per Service/vnf
123 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1, INVARIANT_RESOURCE1_UUID,
124 "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID);
125 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
127 // Build fake csarhandler
128 Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
129 // Build fake csar Helper
130 ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
131 Metadata data = Mockito.mock(Metadata.class);
132 Mockito.when(data.getValue("name")).thenReturn(generatedName);
133 Mockito.when(notificationData.getServiceName()).thenReturn(generatedName);
134 Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
136 // Create helper based on real csar to test policy yaml and global properties
138 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
139 String path = Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile();
140 ISdcCsarHelper sdcHelper = factory
141 .getSdcCsarHelper(path);
142 Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcHelper);
144 // Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
145 Mockito.when(csarHandler.getPolicyModelYaml())
146 .thenReturn(Optional.ofNullable(ResourceFileUtil.getResourceAsString("tosca/tosca_example.yaml")));
152 public void testGetPolicyModelYaml() throws IOException, SdcToscaParserException, CsarHandlerException {
153 INotificationData notificationData = Mockito.mock(INotificationData.class);
154 IArtifactInfo serviceArtifacts = Mockito.mock(IArtifactInfo.class);
155 Mockito.when(serviceArtifacts.getArtifactType()).thenReturn("TOSCA_CSAR");
156 List<IArtifactInfo> serviceArtifactsList = new ArrayList<>();
157 serviceArtifactsList.add(serviceArtifacts);
158 Mockito.when(notificationData.getServiceArtifacts()).thenReturn(serviceArtifactsList);
160 CsarHandler csarHandler = new CsarHandler(notificationData, "", "");
161 csarHandler.setFilePath(Thread.currentThread().getContextClassLoader()
162 .getResource(CSAR_ARTIFACT_NAME).getFile());
163 Optional<String> testyaml = csarHandler.getPolicyModelYaml();
164 Assert.assertEquals(testyaml, Optional.ofNullable(ResourceFileUtil
165 .getResourceAsString("example/sdc/expected-result/policy-data.yaml")));
170 public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException,
171 CsarHandlerException, IOException, InterruptedException, PolicyModelException {
172 String generatedName = RandomStringUtils.randomAlphanumeric(5);
173 CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
174 assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isFalse();
175 csarInstaller.installTheCsar(csarHandler);
176 assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isTrue();
181 public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException,
182 CsarHandlerException, IOException, JSONException, InterruptedException, PolicyModelException {
183 String generatedName = RandomStringUtils.randomAlphanumeric(5);
184 CsarHandler csar = buildFakeCsarHandler(generatedName);
185 csarInstaller.installTheCsar(csar);
187 .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")))
190 .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca_3.yaml")))
193 .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE2, "tca_2.yaml")))
195 // Verify now that policy and json representation, global properties are well
197 Loop loop = loopsRepo
198 .findById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get();
199 assertThat(loop.getSvgRepresentation()).startsWith("<svg ");
200 assertThat(loop.getGlobalPropertiesJson().get("dcaeDeployParameters")).isNotNull();
201 assertThat(loop.getMicroServicePolicies()).hasSize(1);
202 assertThat(loop.getOperationalPolicies()).hasSize(1);
203 assertThat(loop.getModelPropertiesJson().get("serviceDetails")).isNotNull();
204 assertThat(loop.getModelPropertiesJson().get("resourceDetails")).isNotNull();