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