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:";
79 * Constructor for CsarHandler taking sdc notification in input.
81 public CsarHandler(INotificationData data, String controller, String clampCsarPath) throws CsarHandlerException {
82 this.sdcNotification = data;
83 this.controllerName = controller;
84 this.artifactElement = searchForUniqueCsar(data);
85 this.csarFilePath = buildFilePathForCsar(artifactElement, clampCsarPath);
88 private String buildFilePathForCsar(IArtifactInfo artifactElement, String clampCsarPath) {
89 return clampCsarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
92 private IArtifactInfo searchForUniqueCsar(INotificationData notificationData) throws CsarHandlerException {
93 List<IArtifactInfo> serviceArtifacts = notificationData.getServiceArtifacts();
94 for (IArtifactInfo artifact : serviceArtifacts) {
95 if (artifact.getArtifactType().equals(CSAR_TYPE)) {
99 throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
103 * This saves the notification to disk and database.
105 * @param resultArtifact The artifact to install
106 * @throws SdcArtifactInstallerException In case of issues with the installation
107 * @throws SdcToscaParserException In case of issues with the parsing of
110 public synchronized void save(IDistributionClientDownloadResult resultArtifact)
111 throws SdcArtifactInstallerException, SdcToscaParserException {
113 logger.info("Writing CSAR file to: " + csarFilePath + " UUID " + artifactElement.getArtifactUUID() + ")");
114 Path path = Paths.get(csarFilePath);
115 Files.createDirectories(path.getParent());
116 // Create or replace the file
117 try (OutputStream out = Files.newOutputStream(path)) {
118 out.write(resultArtifact.getArtifactPayload(), 0, resultArtifact.getArtifactPayload().length);
120 sdcCsarHelper = factory.getSdcCsarHelper(csarFilePath);
121 this.loadDcaeBlueprint();
122 } catch (IOException e) {
123 throw new SdcArtifactInstallerException(
124 "Exception caught when trying to write the CSAR on the file system to " + csarFilePath, e);
128 private IResourceInstance searchForResourceByInstanceName(String blueprintResourceInstanceName)
129 throws SdcArtifactInstallerException {
130 for (IResourceInstance resource : this.sdcNotification.getResources()) {
131 String filteredString = resource.getResourceInstanceName().replaceAll("-", "");
132 filteredString = filteredString.replaceAll(" ", "");
133 if (filteredString.equalsIgnoreCase(blueprintResourceInstanceName)) {
137 throw new SdcArtifactInstallerException("Error when searching for " + blueprintResourceInstanceName
138 + " as ResourceInstanceName in Sdc notification and did not find it");
141 private void loadDcaeBlueprint() throws IOException, SdcArtifactInstallerException {
142 try (ZipFile zipFile = new ZipFile(csarFilePath)) {
143 Enumeration<? extends ZipEntry> entries = zipFile.entries();
144 while (entries.hasMoreElements()) {
145 ZipEntry entry = entries.nextElement();
146 if (!entry.isDirectory() && entry.getName().contains(BLUEPRINT_TYPE)) {
147 BlueprintArtifact blueprintArtifact = new BlueprintArtifact();
148 blueprintArtifact.setBlueprintArtifactName(
149 entry.getName().substring(entry.getName().lastIndexOf('/') + 1, entry.getName().length()));
151 .setBlueprintInvariantServiceUuid(this.getSdcNotification().getServiceInvariantUUID());
152 try (InputStream stream = zipFile.getInputStream(entry)) {
153 blueprintArtifact.setDcaeBlueprint(IOUtils.toString(stream, StandardCharsets.UTF_8));
155 blueprintArtifact.setResourceAttached(searchForResourceByInstanceName(entry.getName().substring(
156 entry.getName().indexOf(RESOURCE_INSTANCE_NAME_PREFIX)
157 + RESOURCE_INSTANCE_NAME_PREFIX.length(),
158 entry.getName().indexOf(RESOURCE_INSTANCE_NAME_SUFFIX))));
159 this.mapOfBlueprints.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
160 logger.info("Found a blueprint entry in the CSAR " + blueprintArtifact.getBlueprintArtifactName()
161 + " for resource instance Name "
162 + blueprintArtifact.getResourceAttached().getResourceInstanceName());
165 logger.info(this.mapOfBlueprints.size() + " blueprint(s) will be converted to closed loop");
169 public IArtifactInfo getArtifactElement() {
170 return artifactElement;
173 public String getFilePath() {
177 public String setFilePath(String newPath) {
178 return csarFilePath = newPath;
181 public synchronized ISdcCsarHelper getSdcCsarHelper() {
182 return sdcCsarHelper;
185 public INotificationData getSdcNotification() {
186 return sdcNotification;
189 public Map<String, BlueprintArtifact> getMapOfBlueprints() {
190 return mapOfBlueprints;
194 * Get the whole policy model Yaml. It combines the content of policies.yaml and
197 * @return The whole policy model yaml
198 * @throws IOException The IO Exception
200 public Optional<String> getPolicyModelYaml() throws IOException {
201 String result = null;
202 try (ZipFile zipFile = new ZipFile(csarFilePath)) {
203 ZipEntry entry = zipFile.getEntry(POLICY_DEFINITION_NAME_SUFFIX);
205 ZipEntry data = zipFile.getEntry(DATA_DEFINITION_NAME_SUFFIX);
207 String dataStr = IOUtils.toString(zipFile.getInputStream(data), StandardCharsets.UTF_8);
208 String dataStrWithoutHeader = dataStr.substring(dataStr.indexOf(DATA_DEFINITION_KEY));
209 String policyStr = IOUtils.toString(zipFile.getInputStream(entry), StandardCharsets.UTF_8);
210 StringUtils.chomp(policyStr);
211 result = policyStr.concat(dataStrWithoutHeader);
213 result = IOUtils.toString(zipFile.getInputStream(entry), StandardCharsets.UTF_8);
216 logger.info("Policy model not found inside the CSAR file: " + csarFilePath);
218 return Optional.ofNullable(result);