Fix the CsarHandler
[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.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.List;
35
36 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
37 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
38 import org.openecomp.sdc.api.notification.IArtifactInfo;
39 import org.openecomp.sdc.api.notification.INotificationData;
40 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
41 import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
42 import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
43 import org.openecomp.sdc.tosca.parser.impl.SdcToscaParserFactory;
44
45 /**
46  * CsarDescriptor that will be used to deploy in CLAMP.
47  */
48 public class CsarHandler {
49
50     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarHandler.class);
51     private IArtifactInfo artifactElement;
52     private String filePath;
53     private String controllerName;
54     private SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
55     private ISdcCsarHelper sdcCsarHelper;
56     public static final String CSAR_TYPE = "TOSCA_CSAR";
57     private String csarPath;
58
59     public CsarHandler(INotificationData iNotif, String controller, String sdcCsarPath) throws CsarHandlerException {
60         this.csarPath = sdcCsarPath;
61         this.controllerName = controller;
62         this.artifactElement = searchForUniqueCsar(iNotif);
63         this.filePath = buildFilePathForCsar(artifactElement);
64     }
65
66     private String buildFilePathForCsar(IArtifactInfo artifactElement) {
67         return csarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
68     }
69
70     private IArtifactInfo searchForUniqueCsar(INotificationData iNotif) throws CsarHandlerException {
71         List<IArtifactInfo> serviceArtifacts = iNotif.getServiceArtifacts();
72         for (IArtifactInfo artifact : serviceArtifacts) {
73             if (artifact.getArtifactType().equals(CSAR_TYPE)) {
74                 return artifact;
75             }
76         }
77         throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
78     }
79
80     public void save(IDistributionClientDownloadResult resultArtifact)
81             throws SdcArtifactInstallerException, SdcToscaParserException {
82         try {
83             logger.info("Writing CSAR file : " + artifactElement.getArtifactURL() + " UUID "
84                     + artifactElement.getArtifactUUID() + ")");
85             Path path = Paths.get(filePath);
86             Files.createDirectories(path.getParent());
87             Files.createFile(path);
88             try (FileOutputStream outFile = new FileOutputStream(filePath)) {
89                 outFile.write(resultArtifact.getArtifactPayload(), 0, resultArtifact.getArtifactPayload().length);
90             }
91             sdcCsarHelper = factory.getSdcCsarHelper(filePath);
92         } catch (IOException e) {
93             throw new SdcArtifactInstallerException(
94                     "Exception caught when trying to write the CSAR on the file system to " + filePath, e);
95         }
96     }
97
98     public IArtifactInfo getArtifactElement() {
99         return artifactElement;
100     }
101
102     public String getFilePath() {
103         return filePath;
104     }
105
106     public ISdcCsarHelper getSdcCsarHelper() {
107         return sdcCsarHelper;
108     }
109 }