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.notification.IResourceInstance;
47 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
48 import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
49 import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
50 import org.openecomp.sdc.tosca.parser.impl.SdcToscaParserFactory;
53 * CsarDescriptor that will be used to deploy file in CLAMP file system. Some
54 * methods can also be used to get some data from it.
56 public class CsarHandler {
58 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarHandler.class);
59 private IArtifactInfo artifactElement;
60 private String csarFilePath;
61 private String controllerName;
62 private SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
63 private ISdcCsarHelper sdcCsarHelper;
64 private String dcaeBlueprint;
65 private String blueprintArtifactName;
66 private String blueprintInvariantResourceUuid;
67 private String blueprintInvariantServiceUuid;
68 public static final String CSAR_TYPE = "TOSCA_CSAR";
69 public static final String BLUEPRINT_TYPE = "DCAE_INVENTORY_BLUEPRINT";
70 private INotificationData sdcNotification;
72 public CsarHandler(INotificationData iNotif, String controller, String clampCsarPath) throws CsarHandlerException {
73 this.sdcNotification = iNotif;
74 this.controllerName = controller;
75 this.artifactElement = searchForUniqueCsar(iNotif);
76 this.csarFilePath = buildFilePathForCsar(artifactElement, clampCsarPath);
79 private String buildFilePathForCsar(IArtifactInfo artifactElement, String clampCsarPath) {
80 return clampCsarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
83 private IArtifactInfo searchForUniqueCsar(INotificationData iNotif) throws CsarHandlerException {
84 List<IArtifactInfo> serviceArtifacts = iNotif.getServiceArtifacts();
85 for (IArtifactInfo artifact : serviceArtifacts) {
86 if (artifact.getArtifactType().equals(CSAR_TYPE)) {
90 throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
93 public synchronized void save(IDistributionClientDownloadResult resultArtifact)
94 throws SdcArtifactInstallerException, SdcToscaParserException {
96 logger.info("Writing CSAR file : " + artifactElement.getArtifactURL() + " UUID "
97 + artifactElement.getArtifactUUID() + ")");
98 Path path = Paths.get(csarFilePath);
99 Files.createDirectories(path.getParent());
100 Files.createFile(path);
101 try (FileOutputStream outFile = new FileOutputStream(csarFilePath)) {
102 outFile.write(resultArtifact.getArtifactPayload(), 0, resultArtifact.getArtifactPayload().length);
104 sdcCsarHelper = factory.getSdcCsarHelper(csarFilePath);
105 this.loadDcaeBlueprint();
106 this.loadBlueprintArtifactDetails();
107 } catch (IOException e) {
108 throw new SdcArtifactInstallerException(
109 "Exception caught when trying to write the CSAR on the file system to " + csarFilePath, e);
113 private void loadBlueprintArtifactDetails() {
114 blueprintInvariantServiceUuid = this.getSdcNotification().getServiceInvariantUUID();
115 for (IResourceInstance resource : this.getSdcNotification().getResources()) {
116 if ("VF".equals(resource.getResourceType())) {
117 for (IArtifactInfo artifact : resource.getArtifacts()) {
118 if (BLUEPRINT_TYPE.equals(artifact.getArtifactType())) {
119 blueprintArtifactName = artifact.getArtifactName();
120 blueprintInvariantResourceUuid = resource.getResourceInvariantUUID();
127 private void loadDcaeBlueprint() throws IOException, SdcArtifactInstallerException {
128 List<ZipEntry> listEntries = new ArrayList<>();
129 try (ZipFile zipFile = new ZipFile(csarFilePath)) {
130 Enumeration<? extends ZipEntry> entries = zipFile.entries();
131 while (entries.hasMoreElements()) {
132 ZipEntry entry = entries.nextElement();
133 if (entry.getName().contains(BLUEPRINT_TYPE)) {
134 listEntries.add(entry);
137 if (listEntries.size() > 1) {
138 throw new SdcArtifactInstallerException("There are multiple entries in the DCAE inventory");
140 try (InputStream stream = zipFile.getInputStream(listEntries.get(0))) {
141 this.dcaeBlueprint = IOUtils.toString(stream);
146 public IArtifactInfo getArtifactElement() {
147 return artifactElement;
150 public String getFilePath() {
154 public synchronized ISdcCsarHelper getSdcCsarHelper() {
155 return sdcCsarHelper;
158 public synchronized String getDcaeBlueprint() {
159 return dcaeBlueprint;
162 public INotificationData getSdcNotification() {
163 return sdcNotification;
166 public String getBlueprintArtifactName() {
167 return blueprintArtifactName;
170 public String getBlueprintInvariantResourceUuid() {
171 return blueprintInvariantResourceUuid;
174 public String getBlueprintInvariantServiceUuid() {
175 return blueprintInvariantServiceUuid;