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.codehaus.plexus.util.StringUtils;
46 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
47 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
48 import org.onap.sdc.api.notification.IArtifactInfo;
49 import org.onap.sdc.api.notification.INotificationData;
50 import org.onap.sdc.api.notification.IResourceInstance;
51 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
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;
57 * CsarDescriptor that will be used to deploy file in CLAMP file system. Some
58 * methods can also be used to get some data from it.
60 public class CsarHandler {
62 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarHandler.class);
63 private IArtifactInfo artifactElement;
64 private String csarFilePath;
65 private String controllerName;
66 private SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
67 private ISdcCsarHelper sdcCsarHelper;
68 private Map<String, BlueprintArtifact> mapOfBlueprints = new HashMap<>();
69 public static final String CSAR_TYPE = "TOSCA_CSAR";
70 public static final String BLUEPRINT_TYPE = "DCAE_INVENTORY_BLUEPRINT";
71 private INotificationData sdcNotification;
72 public static final String RESOURCE_INSTANCE_NAME_PREFIX = "/Artifacts/Resources/";
73 public static final String RESOURCE_INSTANCE_NAME_SUFFIX = "/Deployment/";
74 public static final String POLICY_DEFINITION_NAME_SUFFIX = "Definitions/policies.yml";
75 public static final String DATA_DEFINITION_NAME_SUFFIX = "Definitions/data.yml";
76 public static final String DATA_DEFINITION_KEY = "data_types:";
78 public CsarHandler(INotificationData iNotif, String controller, String clampCsarPath) throws CsarHandlerException {
79 this.sdcNotification = iNotif;
80 this.controllerName = controller;
81 this.artifactElement = searchForUniqueCsar(iNotif);
82 this.csarFilePath = buildFilePathForCsar(artifactElement, clampCsarPath);
85 private String buildFilePathForCsar(IArtifactInfo artifactElement, String clampCsarPath) {
86 return clampCsarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
89 private IArtifactInfo searchForUniqueCsar(INotificationData iNotif) throws CsarHandlerException {
90 List<IArtifactInfo> serviceArtifacts = iNotif.getServiceArtifacts();
91 for (IArtifactInfo artifact : serviceArtifacts) {
92 if (artifact.getArtifactType().equals(CSAR_TYPE)) {
96 throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
99 public synchronized void save(IDistributionClientDownloadResult resultArtifact)
100 throws SdcArtifactInstallerException, SdcToscaParserException {
102 logger.info("Writing CSAR file to: " + csarFilePath + " UUID " + artifactElement.getArtifactUUID() + ")");
103 Path path = Paths.get(csarFilePath);
104 Files.createDirectories(path.getParent());
105 // Create or replace the file
106 try (OutputStream out = Files.newOutputStream(path)) {
107 out.write(resultArtifact.getArtifactPayload(), 0, resultArtifact.getArtifactPayload().length);
109 sdcCsarHelper = factory.getSdcCsarHelper(csarFilePath);
110 this.loadDcaeBlueprint();
111 } catch (IOException e) {
112 throw new SdcArtifactInstallerException(
113 "Exception caught when trying to write the CSAR on the file system to " + csarFilePath, e);
117 private IResourceInstance searchForResourceByInstanceName(String blueprintResourceInstanceName)
118 throws SdcArtifactInstallerException {
119 for (IResourceInstance resource : this.sdcNotification.getResources()) {
120 String filteredString = resource.getResourceInstanceName().replaceAll("-", "");
121 filteredString = filteredString.replaceAll(" ", "");
122 if (filteredString.equalsIgnoreCase(blueprintResourceInstanceName)) {
126 throw new SdcArtifactInstallerException("Error when searching for " + blueprintResourceInstanceName
127 + " as ResourceInstanceName in Sdc notification and did not find it");
130 private void loadDcaeBlueprint() throws IOException, SdcArtifactInstallerException {
131 try (ZipFile zipFile = new ZipFile(csarFilePath)) {
132 Enumeration<? extends ZipEntry> entries = zipFile.entries();
133 while (entries.hasMoreElements()) {
134 ZipEntry entry = entries.nextElement();
135 if (entry.getName().contains(BLUEPRINT_TYPE)) {
136 BlueprintArtifact blueprintArtifact = new BlueprintArtifact();
137 blueprintArtifact.setBlueprintArtifactName(
138 entry.getName().substring(entry.getName().lastIndexOf('/') + 1, entry.getName().length()));
140 .setBlueprintInvariantServiceUuid(this.getSdcNotification().getServiceInvariantUUID());
141 try (InputStream stream = zipFile.getInputStream(entry)) {
142 blueprintArtifact.setDcaeBlueprint(IOUtils.toString(stream, StandardCharsets.UTF_8));
144 blueprintArtifact.setResourceAttached(searchForResourceByInstanceName(entry.getName().substring(
145 entry.getName().indexOf(RESOURCE_INSTANCE_NAME_PREFIX) + RESOURCE_INSTANCE_NAME_PREFIX.length(),
146 entry.getName().indexOf(RESOURCE_INSTANCE_NAME_SUFFIX))));
147 this.mapOfBlueprints.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
148 logger.info("Found a blueprint entry in the CSAR " + blueprintArtifact.getBlueprintArtifactName()
149 + " for resource instance Name "
150 + blueprintArtifact.getResourceAttached().getResourceInstanceName());
153 logger.info(this.mapOfBlueprints.size() + " blueprint(s) will be converted to closed loop");
157 public IArtifactInfo getArtifactElement() {
158 return artifactElement;
161 public String getFilePath() {
165 public String setFilePath(String newPath) {
166 return csarFilePath = newPath;
169 public synchronized ISdcCsarHelper getSdcCsarHelper() {
170 return sdcCsarHelper;
173 public INotificationData getSdcNotification() {
174 return sdcNotification;
177 public Map<String, BlueprintArtifact> getMapOfBlueprints() {
178 return mapOfBlueprints;
182 * Get the whole policy model Yaml. It combines the content of policies.yaml and data.yaml.
183 * @return The whole policy model yaml
184 * @throws IOException The IO Exception
186 public Optional<String> getPolicyModelYaml() throws IOException {
187 String result = null;
188 try (ZipFile zipFile = new ZipFile(csarFilePath)) {
189 ZipEntry entry = zipFile.getEntry(POLICY_DEFINITION_NAME_SUFFIX);
191 ZipEntry data = zipFile.getEntry(DATA_DEFINITION_NAME_SUFFIX);
193 String dataStr = IOUtils.toString(zipFile.getInputStream(data), StandardCharsets.UTF_8);
194 String dataStrWithoutHeader = dataStr.substring(dataStr.indexOf(DATA_DEFINITION_KEY));
195 String policyStr = IOUtils.toString(zipFile.getInputStream(entry), StandardCharsets.UTF_8);
196 StringUtils.chomp(policyStr);
197 result = policyStr.concat(dataStrWithoutHeader);
199 result = IOUtils.toString(zipFile.getInputStream(entry), StandardCharsets.UTF_8);
202 logger.info("Policy model not found inside the CSAR file: " + csarFilePath);
204 return Optional.ofNullable(result);