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.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.Optional;
36 import org.apache.commons.lang3.RandomStringUtils;
37 import org.json.JSONException;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.onap.clamp.clds.Application;
42 import org.onap.clamp.clds.exception.policy.PolicyModelException;
43 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
44 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
45 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
46 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
47 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
48 import org.onap.clamp.clds.util.ResourceFileUtil;
49 import org.onap.sdc.api.notification.INotificationData;
50 import org.onap.sdc.api.notification.IResourceInstance;
51 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
52 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
53 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
54 import org.onap.sdc.toscaparser.api.elements.Metadata;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.boot.test.context.SpringBootTest;
57 import org.springframework.test.context.ActiveProfiles;
58 import org.springframework.test.context.junit4.SpringRunner;
60 @RunWith(SpringRunner.class)
61 @SpringBootTest(classes = Application.class)
62 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller-new")
63 public class CsarInstallerItCase {
65 private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar";
66 private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92";
67 private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec";
68 private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad";
69 private static final String RESOURCE_INSTANCE_NAME_RESOURCE1 = "ResourceInstanceName1";
70 private static final String RESOURCE_INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2";
73 private CsarInstaller csarInstaller;
75 private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
76 String blueprintFilePath, String artifactName, String invariantServiceUuid) throws IOException {
77 IResourceInstance resource = Mockito.mock(IResourceInstance.class);
78 Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
79 Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
80 BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
81 Mockito.when(blueprintArtifact.getDcaeBlueprint())
82 .thenReturn(ResourceFileUtil.getResourceAsString(blueprintFilePath));
83 Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(artifactName);
84 Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
85 Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
86 return blueprintArtifact;
89 private CsarHandler buildFakeCsarHandler(String generatedName) throws IOException, SdcToscaParserException {
90 // Create fake notification
91 INotificationData notificationData = Mockito.mock(INotificationData.class);
92 Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
93 // Create fake resource in notification
94 CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
95 List<IResourceInstance> listResources = new ArrayList<>();
96 Mockito.when(notificationData.getResources()).thenReturn(listResources);
97 Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
98 Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
99 // Create fake blueprint artifact 1 on resource1
100 BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1,
101 INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", "tca.yaml", INVARIANT_SERVICE_UUID);
102 listResources.add(blueprintArtifact.getResourceAttached());
103 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
104 // Create fake blueprint artifact 2 on resource2
105 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
106 "example/sdc/blueprint-dcae/tca_2.yaml", "tca_2.yaml", INVARIANT_SERVICE_UUID);
107 listResources.add(blueprintArtifact.getResourceAttached());
108 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
110 // Create fake blueprint artifact 3 on resource 1 so that it's possible to
111 // test multiple CL deployment per Service/vnf
112 blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1, INVARIANT_RESOURCE1_UUID,
113 "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID);
114 blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
116 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
117 ISdcCsarHelper sdcHelper = factory.getSdcCsarHelper(Thread.currentThread().getContextClassLoader()
118 .getResource("example/sdc/service-Simsfoimap0112.csar").getFile());
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);
128 Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcHelper);
129 // Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
130 Mockito.when(csarHandler.getPolicyModelYaml()).thenReturn(Optional.ofNullable(""));
134 public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException,
135 CsarHandlerException, IOException, InterruptedException, PolicyModelException {
136 String generatedName = RandomStringUtils.randomAlphanumeric(5);
137 CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
138 assertFalse(csarInstaller.isCsarAlreadyDeployed(csarHandler));
139 csarInstaller.installTheCsar(csarHandler);
140 assertTrue(csarInstaller.isCsarAlreadyDeployed(csarHandler));
144 public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException,
145 CsarHandlerException, IOException, JSONException, InterruptedException, PolicyModelException {
146 String generatedName = RandomStringUtils.randomAlphanumeric(5);
147 CsarHandler csar = buildFakeCsarHandler(generatedName);
148 csarInstaller.installTheCsar(csar);