2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.loop;
26 import static org.assertj.core.api.Assertions.assertThat;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.Optional;
35 import javax.transaction.Transactional;
37 import org.apache.commons.lang3.RandomStringUtils;
38 import org.json.JSONException;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mockito;
42 import org.onap.clamp.clds.Application;
43 import org.onap.clamp.clds.exception.policy.PolicyModelException;
44 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
45 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
46 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
47 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
48 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
49 import org.onap.clamp.clds.util.ResourceFileUtil;
50 import org.onap.sdc.api.notification.INotificationData;
51 import org.onap.sdc.api.notification.IResourceInstance;
52 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
53 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
54 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
55 import org.onap.sdc.toscaparser.api.elements.Metadata;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.boot.test.context.SpringBootTest;
58 import org.springframework.test.context.ActiveProfiles;
59 import org.springframework.test.context.junit4.SpringRunner;
61 @RunWith(SpringRunner.class)
62 @SpringBootTest(classes = Application.class)
63 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller-new")
64 public class CsarInstallerItCase {
66 private static final String CSAR_ARTIFACT_NAME = "example/sdc/service-Simsfoimap0112.csar";
67 private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92";
68 private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec";
69 private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad";
70 private static final String RESOURCE_INSTANCE_NAME_RESOURCE1 = "ResourceInstanceName1";
71 private static final String RESOURCE_INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2";
74 private LoopsRepository loopsRepo;
77 private CsarInstaller csarInstaller;
79 private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
80 String blueprintFilePath, String artifactName, String invariantServiceUuid) throws IOException {
81 IResourceInstance resource = Mockito.mock(IResourceInstance.class);
82 Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
83 Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
84 BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
85 Mockito.when(blueprintArtifact.getDcaeBlueprint())
86 .thenReturn(ResourceFileUtil.getResourceAsString(blueprintFilePath));
87 Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(artifactName);
88 Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
89 Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
90 return blueprintArtifact;
93 private CsarHandler buildFakeCsarHandler(String generatedName) throws IOException, SdcToscaParserException {
94 // Create fake notification
95 INotificationData notificationData = Mockito.mock(INotificationData.class);
96 Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
97 // Create fake resource in notification
98 CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
99 List<IResourceInstance> listResources = new ArrayList<>();
100 Mockito.when(notificationData.getResources()).thenReturn(listResources);
101 Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
102 Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
103 // Create fake blueprint artifact 1 on resource1
104 BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1,
105 INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", "tca.yaml", INVARIANT_SERVICE_UUID);
106 listResources.add(blueprintArtifact.getResourceAttached());
107 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
108 // Create fake blueprint artifact 2 on resource2
109 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
110 "example/sdc/blueprint-dcae/tca_2.yaml", "tca_2.yaml", INVARIANT_SERVICE_UUID);
111 listResources.add(blueprintArtifact.getResourceAttached());
112 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
114 // Create fake blueprint artifact 3 on resource 1 so that it's possible to
115 // test multiple CL deployment per Service/vnf
116 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1, INVARIANT_RESOURCE1_UUID,
117 "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID);
118 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
120 // Build fake csarhandler
121 Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
122 // Build fake csar Helper
123 ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
124 Metadata data = Mockito.mock(Metadata.class);
125 Mockito.when(data.getValue("name")).thenReturn(generatedName);
126 Mockito.when(notificationData.getServiceName()).thenReturn(generatedName);
127 Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
129 // Create helper based on real csar to test policy yaml and global properties
131 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
132 ISdcCsarHelper sdcHelper = factory
133 .getSdcCsarHelper(Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile());
134 Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcHelper);
136 // Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
137 Mockito.when(csarHandler.getPolicyModelYaml())
138 .thenReturn(Optional.ofNullable(ResourceFileUtil.getResourceAsString("tosca/tosca_example.yaml")));
144 public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException,
145 CsarHandlerException, IOException, InterruptedException, PolicyModelException {
146 String generatedName = RandomStringUtils.randomAlphanumeric(5);
147 CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
148 assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isFalse();
149 csarInstaller.installTheCsar(csarHandler);
150 assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isTrue();
155 public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException,
156 CsarHandlerException, IOException, JSONException, InterruptedException, PolicyModelException {
157 String generatedName = RandomStringUtils.randomAlphanumeric(5);
158 CsarHandler csar = buildFakeCsarHandler(generatedName);
159 csarInstaller.installTheCsar(csar);
161 .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")))
164 .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca_3.yaml")))
167 .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE2, "tca_2.yaml")))
169 // Verify now that policy and json representation, global properties are well
171 Loop loop = loopsRepo
172 .findById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get();
173 assertThat(loop.getSvgRepresentation()).startsWith("<svg ");
174 assertThat(loop.getGlobalPropertiesJson().get("dcaeDeployParameters")).isNotNull();
175 assertThat(loop.getMicroServicePolicies()).hasSize(1);
176 assertThat(loop.getOperationalPolicies()).hasSize(1);
177 assertThat(loop.getModelPropertiesJson().get("serviceDetails")).isNotNull();
178 assertThat(loop.getModelPropertiesJson().get("resourceDetails")).isNotNull();