Merge "SDC proxy do not depend on CLAMP"
[clamp.git] / src / main / java / org / onap / clamp / clds / sdc / controller / installer / CsarHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
22  */
23
24 package org.onap.clamp.clds.sdc.controller.installer;
25
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;
29
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;
41
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;
50
51 /**
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.
54  */
55 public class CsarHandler {
56
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";
65
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);
70     }
71
72     private String buildFilePathForCsar(IArtifactInfo artifactElement, String clampCsarPath) {
73         return clampCsarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
74     }
75
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)) {
80                 return artifact;
81             }
82         }
83         throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
84     }
85
86     public synchronized void save(IDistributionClientDownloadResult resultArtifact)
87             throws SdcArtifactInstallerException, SdcToscaParserException {
88         try {
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);
96             }
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);
102         }
103     }
104
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);
113                 }
114             }
115             if (listEntries.size() > 1) {
116                 throw new SdcArtifactInstallerException("There are multiple entries in the DCAE inventory");
117             }
118             try (InputStream stream = zipFile.getInputStream(listEntries.get(0))) {
119                 this.dcaeBlueprint = IOUtils.toString(stream);
120             }
121         }
122     }
123
124     public IArtifactInfo getArtifactElement() {
125         return artifactElement;
126     }
127
128     public String getFilePath() {
129         return csarFilePath;
130     }
131
132     public synchronized ISdcCsarHelper getSdcCsarHelper() {
133         return sdcCsarHelper;
134     }
135
136     public synchronized String getDcaeBlueprint() {
137         return dcaeBlueprint;
138     }
139 }