Add "code moved" warning to old CLAMP repo readme
[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  *
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.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 import java.nio.charset.StandardCharsets;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.Enumeration;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Optional;
41 import java.util.zip.ZipEntry;
42 import java.util.zip.ZipFile;
43
44 import org.apache.commons.io.IOUtils;
45 import org.codehaus.plexus.util.StringUtils;
46 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
47 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
48 import org.onap.sdc.api.notification.IArtifactInfo;
49 import org.onap.sdc.api.notification.INotificationData;
50 import org.onap.sdc.api.notification.IResourceInstance;
51 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
52 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
53 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
54 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
55
56 /**
57  * CsarDescriptor that will be used to deploy file in CLAMP file system. Some
58  * methods can also be used to get some data from it.
59  */
60 public class CsarHandler {
61
62     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarHandler.class);
63     private IArtifactInfo artifactElement;
64     private String csarFilePath;
65     private String controllerName;
66     private SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
67     private ISdcCsarHelper sdcCsarHelper;
68     private Map<String, BlueprintArtifact> mapOfBlueprints = new HashMap<>();
69     public static final String CSAR_TYPE = "TOSCA_CSAR";
70     public static final String BLUEPRINT_TYPE = "DCAE_INVENTORY_BLUEPRINT";
71     private INotificationData sdcNotification;
72     public static final String RESOURCE_INSTANCE_NAME_PREFIX = "/Artifacts/Resources/";
73     public static final String RESOURCE_INSTANCE_NAME_SUFFIX = "/Deployment/";
74     public static final String POLICY_DEFINITION_NAME_SUFFIX = "Definitions/policies.yml";
75     public static final String DATA_DEFINITION_NAME_SUFFIX = "Definitions/data.yml";
76     public static final String DATA_DEFINITION_KEY = "data_types:";
77
78     /**
79      * Constructor for CsarHandler taking sdc notification in input.
80      */
81     public CsarHandler(INotificationData data, String controller, String clampCsarPath) throws CsarHandlerException {
82         this.sdcNotification = data;
83         this.controllerName = controller;
84         this.artifactElement = searchForUniqueCsar(data);
85         this.csarFilePath = buildFilePathForCsar(artifactElement, clampCsarPath);
86     }
87
88     private String buildFilePathForCsar(IArtifactInfo artifactElement, String clampCsarPath) {
89         return clampCsarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
90     }
91
92     private IArtifactInfo searchForUniqueCsar(INotificationData notificationData) throws CsarHandlerException {
93         List<IArtifactInfo> serviceArtifacts = notificationData.getServiceArtifacts();
94         for (IArtifactInfo artifact : serviceArtifacts) {
95             if (artifact.getArtifactType().equals(CSAR_TYPE)) {
96                 return artifact;
97             }
98         }
99         throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
100     }
101
102     /**
103      * This saves the notification to disk and database.
104      * 
105      * @param resultArtifact The artifact to install
106      * @throws SdcArtifactInstallerException In case of issues with the installation
107      * @throws SdcToscaParserException       In case of issues with the parsing of
108      *                                       the CSAR
109      */
110     public synchronized void save(IDistributionClientDownloadResult resultArtifact)
111             throws SdcArtifactInstallerException, SdcToscaParserException {
112         try {
113             logger.info("Writing CSAR file to: " + csarFilePath + " UUID " + artifactElement.getArtifactUUID() + ")");
114             Path path = Paths.get(csarFilePath);
115             Files.createDirectories(path.getParent());
116             // Create or replace the file
117             try (OutputStream out = Files.newOutputStream(path)) {
118                 out.write(resultArtifact.getArtifactPayload(), 0, resultArtifact.getArtifactPayload().length);
119             }
120             sdcCsarHelper = factory.getSdcCsarHelper(csarFilePath);
121             this.loadDcaeBlueprint();
122         } catch (IOException e) {
123             throw new SdcArtifactInstallerException(
124                     "Exception caught when trying to write the CSAR on the file system to " + csarFilePath, e);
125         }
126     }
127
128     private IResourceInstance searchForResourceByInstanceName(String blueprintResourceInstanceName)
129             throws SdcArtifactInstallerException {
130         for (IResourceInstance resource : this.sdcNotification.getResources()) {
131             String filteredString = resource.getResourceInstanceName().replaceAll("-", "");
132             filteredString = filteredString.replaceAll(" ", "");
133             if (filteredString.equalsIgnoreCase(blueprintResourceInstanceName)) {
134                 return resource;
135             }
136         }
137         throw new SdcArtifactInstallerException("Error when searching for " + blueprintResourceInstanceName
138                 + " as ResourceInstanceName in Sdc notification and did not find it");
139     }
140
141     private void loadDcaeBlueprint() throws IOException, SdcArtifactInstallerException {
142         try (ZipFile zipFile = new ZipFile(csarFilePath)) {
143             Enumeration<? extends ZipEntry> entries = zipFile.entries();
144             while (entries.hasMoreElements()) {
145                 ZipEntry entry = entries.nextElement();
146                 if (!entry.isDirectory() && entry.getName().contains(BLUEPRINT_TYPE)) {
147                     BlueprintArtifact blueprintArtifact = new BlueprintArtifact();
148                     blueprintArtifact.setBlueprintArtifactName(
149                             entry.getName().substring(entry.getName().lastIndexOf('/') + 1, entry.getName().length()));
150                     blueprintArtifact
151                             .setBlueprintInvariantServiceUuid(this.getSdcNotification().getServiceInvariantUUID());
152                     try (InputStream stream = zipFile.getInputStream(entry)) {
153                         blueprintArtifact.setDcaeBlueprint(IOUtils.toString(stream, StandardCharsets.UTF_8));
154                     }
155                     blueprintArtifact.setResourceAttached(searchForResourceByInstanceName(entry.getName().substring(
156                             entry.getName().indexOf(RESOURCE_INSTANCE_NAME_PREFIX)
157                                     + RESOURCE_INSTANCE_NAME_PREFIX.length(),
158                             entry.getName().indexOf(RESOURCE_INSTANCE_NAME_SUFFIX))));
159                     this.mapOfBlueprints.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
160                     logger.info("Found a blueprint entry in the CSAR " + blueprintArtifact.getBlueprintArtifactName()
161                             + " for resource instance Name "
162                             + blueprintArtifact.getResourceAttached().getResourceInstanceName());
163                 }
164             }
165             logger.info(this.mapOfBlueprints.size() + " blueprint(s) will be converted to closed loop");
166         }
167     }
168
169     public IArtifactInfo getArtifactElement() {
170         return artifactElement;
171     }
172
173     public String getFilePath() {
174         return csarFilePath;
175     }
176
177     public String setFilePath(String newPath) {
178         return csarFilePath = newPath;
179     }
180
181     public synchronized ISdcCsarHelper getSdcCsarHelper() {
182         return sdcCsarHelper;
183     }
184
185     public INotificationData getSdcNotification() {
186         return sdcNotification;
187     }
188
189     public Map<String, BlueprintArtifact> getMapOfBlueprints() {
190         return mapOfBlueprints;
191     }
192
193     /**
194      * Get the whole policy model Yaml. It combines the content of policies.yaml and
195      * data.yaml.
196      * 
197      * @return The whole policy model yaml
198      * @throws IOException The IO Exception
199      */
200     public Optional<String> getPolicyModelYaml() throws IOException {
201         String result = null;
202         try (ZipFile zipFile = new ZipFile(csarFilePath)) {
203             ZipEntry entry = zipFile.getEntry(POLICY_DEFINITION_NAME_SUFFIX);
204             if (entry != null) {
205                 ZipEntry data = zipFile.getEntry(DATA_DEFINITION_NAME_SUFFIX);
206                 if (data != null) {
207                     String dataStr = IOUtils.toString(zipFile.getInputStream(data), StandardCharsets.UTF_8);
208                     String dataStrWithoutHeader = dataStr.substring(dataStr.indexOf(DATA_DEFINITION_KEY));
209                     String policyStr = IOUtils.toString(zipFile.getInputStream(entry), StandardCharsets.UTF_8);
210                     StringUtils.chomp(policyStr);
211                     result = policyStr.concat(dataStrWithoutHeader);
212                 } else {
213                     result = IOUtils.toString(zipFile.getInputStream(entry), StandardCharsets.UTF_8);
214                 }
215             } else {
216                 logger.info("Policy model not found inside the CSAR file: " + csarFilePath);
217             }
218             return Optional.ofNullable(result);
219         }
220     }
221 }