2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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.clds.sdc.controller.installer;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 import java.nio.charset.StandardCharsets;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.Enumeration;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.Optional;
41 import java.util.zip.ZipEntry;
42 import java.util.zip.ZipFile;
44 import org.apache.commons.io.IOUtils;
45 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
46 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
47 import org.onap.sdc.api.notification.IArtifactInfo;
48 import org.onap.sdc.api.notification.INotificationData;
49 import org.onap.sdc.api.notification.IResourceInstance;
50 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
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;
56 * CsarDescriptor that will be used to deploy file in CLAMP file system. Some
57 * methods can also be used to get some data from it.
59 public class CsarHandler {
61 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarHandler.class);
62 private IArtifactInfo artifactElement;
63 private String csarFilePath;
64 private String controllerName;
65 private SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
66 private ISdcCsarHelper sdcCsarHelper;
67 private Map<String, BlueprintArtifact> mapOfBlueprints = new HashMap<>();
68 public static final String CSAR_TYPE = "TOSCA_CSAR";
69 public static final String BLUEPRINT_TYPE = "DCAE_INVENTORY_BLUEPRINT";
70 private INotificationData sdcNotification;
71 public static final String RESOURCE_INSTANCE_NAME_PREFIX = "/Artifacts/Resources/";
72 public static final String RESOURCE_INSTANCE_NAME_SUFFIX = "/Deployment/";
73 public static final String POLICY_DEFINITION_NAME_SUFFIX = "Definitions/policies.yml";
75 public CsarHandler(INotificationData iNotif, String controller, String clampCsarPath) throws CsarHandlerException {
76 this.sdcNotification = iNotif;
77 this.controllerName = controller;
78 this.artifactElement = searchForUniqueCsar(iNotif);
79 this.csarFilePath = buildFilePathForCsar(artifactElement, clampCsarPath);
82 private String buildFilePathForCsar(IArtifactInfo artifactElement, String clampCsarPath) {
83 return clampCsarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
86 private IArtifactInfo searchForUniqueCsar(INotificationData iNotif) throws CsarHandlerException {
87 List<IArtifactInfo> serviceArtifacts = iNotif.getServiceArtifacts();
88 for (IArtifactInfo artifact : serviceArtifacts) {
89 if (artifact.getArtifactType().equals(CSAR_TYPE)) {
93 throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
96 public synchronized void save(IDistributionClientDownloadResult resultArtifact)
97 throws SdcArtifactInstallerException, SdcToscaParserException {
99 logger.info("Writing CSAR file to: " + csarFilePath + " UUID " + artifactElement.getArtifactUUID() + ")");
100 Path path = Paths.get(csarFilePath);
101 Files.createDirectories(path.getParent());
102 // Create or replace the file
103 try (OutputStream out = Files.newOutputStream(path)) {
104 out.write(resultArtifact.getArtifactPayload(), 0, resultArtifact.getArtifactPayload().length);
106 sdcCsarHelper = factory.getSdcCsarHelper(csarFilePath);
107 this.loadDcaeBlueprint();
108 } catch (IOException e) {
109 throw new SdcArtifactInstallerException(
110 "Exception caught when trying to write the CSAR on the file system to " + csarFilePath, e);
114 private IResourceInstance searchForResourceByInstanceName(String blueprintResourceInstanceName)
115 throws SdcArtifactInstallerException {
116 for (IResourceInstance resource : this.sdcNotification.getResources()) {
117 String filteredString = resource.getResourceInstanceName().replaceAll("-", "");
118 filteredString = filteredString.replaceAll(" ", "");
119 if (filteredString.equalsIgnoreCase(blueprintResourceInstanceName)) {
123 throw new SdcArtifactInstallerException("Error when searching for " + blueprintResourceInstanceName
124 + " as ResourceInstanceName in Sdc notification and did not find it");
127 private void loadDcaeBlueprint() throws IOException, SdcArtifactInstallerException {
128 try (ZipFile zipFile = new ZipFile(csarFilePath)) {
129 Enumeration<? extends ZipEntry> entries = zipFile.entries();
130 while (entries.hasMoreElements()) {
131 ZipEntry entry = entries.nextElement();
132 if (entry.getName().contains(BLUEPRINT_TYPE)) {
133 BlueprintArtifact blueprintArtifact = new BlueprintArtifact();
134 blueprintArtifact.setBlueprintArtifactName(
135 entry.getName().substring(entry.getName().lastIndexOf('/') + 1, entry.getName().length()));
137 .setBlueprintInvariantServiceUuid(this.getSdcNotification().getServiceInvariantUUID());
138 try (InputStream stream = zipFile.getInputStream(entry)) {
139 blueprintArtifact.setDcaeBlueprint(IOUtils.toString(stream, StandardCharsets.UTF_8));
141 blueprintArtifact.setResourceAttached(searchForResourceByInstanceName(entry.getName().substring(
142 entry.getName().indexOf(RESOURCE_INSTANCE_NAME_PREFIX) + RESOURCE_INSTANCE_NAME_PREFIX.length(),
143 entry.getName().indexOf(RESOURCE_INSTANCE_NAME_SUFFIX))));
144 this.mapOfBlueprints.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
145 logger.info("Found a blueprint entry in the CSAR " + blueprintArtifact.getBlueprintArtifactName()
146 + " for resource instance Name "
147 + blueprintArtifact.getResourceAttached().getResourceInstanceName());
150 logger.info(this.mapOfBlueprints.size() + " blueprint(s) will be converted to closed loop");
154 public IArtifactInfo getArtifactElement() {
155 return artifactElement;
158 public String getFilePath() {
162 public synchronized ISdcCsarHelper getSdcCsarHelper() {
163 return sdcCsarHelper;
166 public INotificationData getSdcNotification() {
167 return sdcNotification;
170 public Map<String, BlueprintArtifact> getMapOfBlueprints() {
171 return mapOfBlueprints;
174 public Optional<String> getPolicyModelYaml() throws IOException {
175 String result = null;
176 try (ZipFile zipFile = new ZipFile(csarFilePath)) {
177 ZipEntry entry = zipFile.getEntry(POLICY_DEFINITION_NAME_SUFFIX);
179 result = IOUtils.toString(zipFile.getInputStream(entry), StandardCharsets.UTF_8);
181 logger.info("Policy model not found inside the CSAR file: " + csarFilePath);
183 return Optional.ofNullable(result);