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.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
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;
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;
47 * CsarDescriptor that will be used to deploy in CLAMP.
49 public class CsarHandler {
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;
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);
67 private String buildFilePathForCsar(IArtifactInfo artifactElement) {
68 return csarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
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)) {
78 throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
81 public void save(IDistributionClientDownloadResult resultArtifact)
82 throws SdcArtifactInstallerException, SdcToscaParserException {
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);
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);
99 public IArtifactInfo getArtifactElement() {
100 return artifactElement;
103 public String getFilePath() {
107 public ISdcCsarHelper getSdcCsarHelper() {
108 return sdcCsarHelper;