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 private INotificationData sdcNotification;
71 public CsarHandler(INotificationData iNotif, String controller, String clampCsarPath) throws CsarHandlerException {
72 this.sdcNotification = iNotif;
73 this.controllerName = controller;
74 this.artifactElement = searchForUniqueCsar(iNotif);
75 this.csarFilePath = buildFilePathForCsar(artifactElement, clampCsarPath);
78 private String buildFilePathForCsar(IArtifactInfo artifactElement, String clampCsarPath) {
79 return clampCsarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
82 private IArtifactInfo searchForUniqueCsar(INotificationData iNotif) throws CsarHandlerException {
83 List<IArtifactInfo> serviceArtifacts = iNotif.getServiceArtifacts();
84 for (IArtifactInfo artifact : serviceArtifacts) {
85 if (artifact.getArtifactType().equals(CSAR_TYPE)) {
89 throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
92 public synchronized void save(IDistributionClientDownloadResult resultArtifact)
93 throws SdcArtifactInstallerException, SdcToscaParserException {
95 logger.info("Writing CSAR file : " + artifactElement.getArtifactURL() + " UUID "
96 + artifactElement.getArtifactUUID() + ")");
97 Path path = Paths.get(csarFilePath);
98 Files.createDirectories(path.getParent());
99 Files.createFile(path);
100 try (FileOutputStream outFile = new FileOutputStream(csarFilePath)) {
101 outFile.write(resultArtifact.getArtifactPayload(), 0, resultArtifact.getArtifactPayload().length);
103 sdcCsarHelper = factory.getSdcCsarHelper(csarFilePath);
104 this.loadDcaeBlueprint();
105 this.loadBlueprintArtifactDetails();
106 } catch (IOException e) {
107 throw new SdcArtifactInstallerException(
108 "Exception caught when trying to write the CSAR on the file system to " + csarFilePath, e);
112 private void loadBlueprintArtifactDetails() {
113 blueprintInvariantServiceUuid = this.getSdcNotification().getServiceInvariantUUID();
114 for (IResourceInstance resource : this.getSdcNotification().getResources()) {
115 if ("VF".equals(resource.getResourceType())) {
116 for (IArtifactInfo artifact : resource.getArtifacts()) {
117 if ("DCAE_INVENTORY_BLUEPRINT".equals(artifact.getArtifactType())) {
118 blueprintArtifactName = artifact.getArtifactName();
119 blueprintInvariantResourceUuid = resource.getResourceInvariantUUID();
126 private void loadDcaeBlueprint() throws IOException, SdcArtifactInstallerException {
127 List<ZipEntry> listEntries = new ArrayList<>();
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("DCAE_INVENTORY_BLUEPRINT")) {
133 listEntries.add(entry);
136 if (listEntries.size() > 1) {
137 throw new SdcArtifactInstallerException("There are multiple entries in the DCAE inventory");
139 try (InputStream stream = zipFile.getInputStream(listEntries.get(0))) {
140 this.dcaeBlueprint = IOUtils.toString(stream);
145 public IArtifactInfo getArtifactElement() {
146 return artifactElement;
149 public String getFilePath() {
153 public synchronized ISdcCsarHelper getSdcCsarHelper() {
154 return sdcCsarHelper;
157 public synchronized String getDcaeBlueprint() {
158 return dcaeBlueprint;
161 public INotificationData getSdcNotification() {
162 return sdcNotification;
165 public String getBlueprintArtifactName() {
166 return blueprintArtifactName;
169 public String getBlueprintInvariantResourceUuid() {
170 return blueprintInvariantResourceUuid;
173 public String getBlueprintInvariantServiceUuid() {
174 return blueprintInvariantServiceUuid;