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 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.clamp.clds.sdc.controller.installer;
26 import com.att.aft.dme2.internal.apache.commons.io.IOUtils;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.ArrayList;
37 import java.util.Enumeration;
38 import java.util.List;
39 import java.util.zip.ZipEntry;
40 import java.util.zip.ZipFile;
42 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
43 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
44 import org.openecomp.sdc.api.notification.IArtifactInfo;
45 import org.openecomp.sdc.api.notification.INotificationData;
46 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
47 import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
48 import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
49 import org.openecomp.sdc.tosca.parser.impl.SdcToscaParserFactory;
52 * CsarDescriptor that will be used to deploy file in CLAMP file system. Some
53 * methods can also be used to get some data from it.
55 public class CsarHandler {
57 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarHandler.class);
58 private IArtifactInfo artifactElement;
59 private String csarFilePath;
60 private String controllerName;
61 private SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
62 private ISdcCsarHelper sdcCsarHelper;
63 private String dcaeBlueprint;
64 public static final String CSAR_TYPE = "TOSCA_CSAR";
66 public CsarHandler(INotificationData iNotif, String controller, String clampCsarPath) throws CsarHandlerException {
67 this.controllerName = controller;
68 this.artifactElement = searchForUniqueCsar(iNotif);
69 this.csarFilePath = buildFilePathForCsar(artifactElement, clampCsarPath);
72 private String buildFilePathForCsar(IArtifactInfo artifactElement, String clampCsarPath) {
73 return clampCsarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
76 private IArtifactInfo searchForUniqueCsar(INotificationData iNotif) throws CsarHandlerException {
77 List<IArtifactInfo> serviceArtifacts = iNotif.getServiceArtifacts();
78 for (IArtifactInfo artifact : serviceArtifacts) {
79 if (artifact.getArtifactType().equals(CSAR_TYPE)) {
83 throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
86 public synchronized void save(IDistributionClientDownloadResult resultArtifact)
87 throws SdcArtifactInstallerException, SdcToscaParserException {
89 logger.info("Writing CSAR file : " + artifactElement.getArtifactURL() + " UUID "
90 + artifactElement.getArtifactUUID() + ")");
91 Path path = Paths.get(csarFilePath);
92 Files.createDirectories(path.getParent());
93 Files.createFile(path);
94 try (FileOutputStream outFile = new FileOutputStream(csarFilePath)) {
95 outFile.write(resultArtifact.getArtifactPayload(), 0, resultArtifact.getArtifactPayload().length);
97 sdcCsarHelper = factory.getSdcCsarHelper(csarFilePath);
98 this.loadDcaeBlueprint();
99 } catch (IOException e) {
100 throw new SdcArtifactInstallerException(
101 "Exception caught when trying to write the CSAR on the file system to " + csarFilePath, e);
105 private void loadDcaeBlueprint() throws IOException, SdcArtifactInstallerException {
106 List<ZipEntry> listEntries = new ArrayList<>();
107 try (ZipFile zipFile = new ZipFile(csarFilePath)) {
108 Enumeration<? extends ZipEntry> entries = zipFile.entries();
109 while (entries.hasMoreElements()) {
110 ZipEntry entry = entries.nextElement();
111 if (entry.getName().contains("DCAE_INVENTORY_BLUEPRINT")) {
112 listEntries.add(entry);
115 if (listEntries.size() > 1) {
116 throw new SdcArtifactInstallerException("There are multiple entries in the DCAE inventory");
118 try (InputStream stream = zipFile.getInputStream(listEntries.get(0))) {
119 this.dcaeBlueprint = IOUtils.toString(stream);
124 public IArtifactInfo getArtifactElement() {
125 return artifactElement;
128 public String getFilePath() {
132 public synchronized ISdcCsarHelper getSdcCsarHelper() {
133 return sdcCsarHelper;
136 public synchronized String getDcaeBlueprint() {
137 return dcaeBlueprint;