Update SDC library
[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.onap.sdc.api.notification.IArtifactInfo;
45 import org.onap.sdc.api.notification.INotificationData;
46 import org.onap.sdc.api.notification.IResourceInstance;
47 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
48 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
49 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
50 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
51
52 /**
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.
55  */
56 public class CsarHandler {
57
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     public static final String BLUEPRINT_TYPE = "DCAE_INVENTORY_BLUEPRINT";
70     private INotificationData sdcNotification;
71
72     public CsarHandler(INotificationData iNotif, String controller, String clampCsarPath) throws CsarHandlerException {
73         this.sdcNotification = iNotif;
74         this.controllerName = controller;
75         this.artifactElement = searchForUniqueCsar(iNotif);
76         this.csarFilePath = buildFilePathForCsar(artifactElement, clampCsarPath);
77     }
78
79     private String buildFilePathForCsar(IArtifactInfo artifactElement, String clampCsarPath) {
80         return clampCsarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
81     }
82
83     private IArtifactInfo searchForUniqueCsar(INotificationData iNotif) throws CsarHandlerException {
84         List<IArtifactInfo> serviceArtifacts = iNotif.getServiceArtifacts();
85         for (IArtifactInfo artifact : serviceArtifacts) {
86             if (artifact.getArtifactType().equals(CSAR_TYPE)) {
87                 return artifact;
88             }
89         }
90         throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
91     }
92
93     public synchronized void save(IDistributionClientDownloadResult resultArtifact)
94             throws SdcArtifactInstallerException, SdcToscaParserException {
95         try {
96             logger.info("Writing CSAR file : " + artifactElement.getArtifactURL() + " UUID "
97                     + artifactElement.getArtifactUUID() + ")");
98             Path path = Paths.get(csarFilePath);
99             Files.createDirectories(path.getParent());
100             Files.createFile(path);
101             try (FileOutputStream outFile = new FileOutputStream(csarFilePath)) {
102                 outFile.write(resultArtifact.getArtifactPayload(), 0, resultArtifact.getArtifactPayload().length);
103             }
104             sdcCsarHelper = factory.getSdcCsarHelper(csarFilePath);
105             this.loadDcaeBlueprint();
106             this.loadBlueprintArtifactDetails();
107         } catch (IOException e) {
108             throw new SdcArtifactInstallerException(
109                     "Exception caught when trying to write the CSAR on the file system to " + csarFilePath, e);
110         }
111     }
112
113     private void loadBlueprintArtifactDetails() {
114         blueprintInvariantServiceUuid = this.getSdcNotification().getServiceInvariantUUID();
115         for (IResourceInstance resource : this.getSdcNotification().getResources()) {
116             if ("VF".equals(resource.getResourceType())) {
117                 for (IArtifactInfo artifact : resource.getArtifacts()) {
118                     if (BLUEPRINT_TYPE.equals(artifact.getArtifactType())) {
119                         blueprintArtifactName = artifact.getArtifactName();
120                         blueprintInvariantResourceUuid = resource.getResourceInvariantUUID();
121                     }
122                 }
123             }
124         }
125     }
126
127     private void loadDcaeBlueprint() throws IOException, SdcArtifactInstallerException {
128         List<ZipEntry> listEntries = new ArrayList<>();
129         try (ZipFile zipFile = new ZipFile(csarFilePath)) {
130             Enumeration<? extends ZipEntry> entries = zipFile.entries();
131             while (entries.hasMoreElements()) {
132                 ZipEntry entry = entries.nextElement();
133                 if (entry.getName().contains(BLUEPRINT_TYPE)) {
134                     listEntries.add(entry);
135                 }
136             }
137             if (listEntries.size() > 1) {
138                 throw new SdcArtifactInstallerException("There are multiple entries in the DCAE inventory");
139             }
140             try (InputStream stream = zipFile.getInputStream(listEntries.get(0))) {
141                 this.dcaeBlueprint = IOUtils.toString(stream);
142             }
143         }
144     }
145
146     public IArtifactInfo getArtifactElement() {
147         return artifactElement;
148     }
149
150     public String getFilePath() {
151         return csarFilePath;
152     }
153
154     public synchronized ISdcCsarHelper getSdcCsarHelper() {
155         return sdcCsarHelper;
156     }
157
158     public synchronized String getDcaeBlueprint() {
159         return dcaeBlueprint;
160     }
161
162     public INotificationData getSdcNotification() {
163         return sdcNotification;
164     }
165
166     public String getBlueprintArtifactName() {
167         return blueprintArtifactName;
168     }
169
170     public String getBlueprintInvariantResourceUuid() {
171         return blueprintInvariantResourceUuid;
172     }
173
174     public String getBlueprintInvariantServiceUuid() {
175         return blueprintInvariantServiceUuid;
176     }
177 }