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 * ===================================================================
24 package org.onap.clamp.clds.sdc.controller.installer;
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;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
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;
40 import java.util.zip.ZipEntry;
41 import java.util.zip.ZipFile;
43 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
44 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
45 import org.onap.sdc.api.notification.IArtifactInfo;
46 import org.onap.sdc.api.notification.INotificationData;
47 import org.onap.sdc.api.notification.IResourceInstance;
48 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
49 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
50 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
51 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
54 * CsarDescriptor that will be used to deploy file in CLAMP file system. Some
55 * methods can also be used to get some data from it.
57 public class CsarHandler {
59 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarHandler.class);
60 private IArtifactInfo artifactElement;
61 private String csarFilePath;
62 private String controllerName;
63 private SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
64 private ISdcCsarHelper sdcCsarHelper;
65 private Map<String, BlueprintArtifact> mapOfBlueprints = new HashMap<>();
66 public static final String CSAR_TYPE = "TOSCA_CSAR";
67 public static final String BLUEPRINT_TYPE = "DCAE_INVENTORY_BLUEPRINT";
68 private INotificationData sdcNotification;
69 public static final String RESOURCE_INSTANCE_NAME_PREFIX = "/Artifacts/Resources/";
70 public static final String RESOURCE_INSTANCE_NAME_SUFFIX = "/Deployment/";
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);
79 private String buildFilePathForCsar(IArtifactInfo artifactElement, String clampCsarPath) {
80 return clampCsarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
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)) {
90 throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
93 public synchronized void save(IDistributionClientDownloadResult resultArtifact)
94 throws SdcArtifactInstallerException, SdcToscaParserException {
96 logger.info("Writing CSAR file to: " + csarFilePath + " UUID " + artifactElement.getArtifactUUID() + ")");
97 Path path = Paths.get(csarFilePath);
98 Files.createDirectories(path.getParent());
99 // Create or replace the file
100 try (OutputStream out = Files.newOutputStream(path)) {
101 out.write(resultArtifact.getArtifactPayload(), 0, resultArtifact.getArtifactPayload().length);
103 sdcCsarHelper = factory.getSdcCsarHelper(csarFilePath);
104 this.loadDcaeBlueprint();
105 } catch (IOException e) {
106 throw new SdcArtifactInstallerException(
107 "Exception caught when trying to write the CSAR on the file system to " + csarFilePath, e);
111 private IResourceInstance searchForResourceByInstanceName(String blueprintResourceInstanceName)
112 throws SdcArtifactInstallerException {
113 for (IResourceInstance resource : this.sdcNotification.getResources()) {
114 String filteredString = resource.getResourceInstanceName().replaceAll("-", "");
115 filteredString = filteredString.replaceAll(" ", "");
116 if (filteredString.equalsIgnoreCase(blueprintResourceInstanceName)) {
120 throw new SdcArtifactInstallerException("Error when searching for " + blueprintResourceInstanceName
121 + " as ResourceInstanceName in Sdc notification and did not find it");
124 private void loadDcaeBlueprint() throws IOException, SdcArtifactInstallerException {
125 try (ZipFile zipFile = new ZipFile(csarFilePath)) {
126 Enumeration<? extends ZipEntry> entries = zipFile.entries();
127 while (entries.hasMoreElements()) {
128 ZipEntry entry = entries.nextElement();
129 if (entry.getName().contains(BLUEPRINT_TYPE)) {
130 BlueprintArtifact blueprintArtifact = new BlueprintArtifact();
131 blueprintArtifact.setBlueprintArtifactName(
132 entry.getName().substring(entry.getName().lastIndexOf('/') + 1, entry.getName().length()));
134 .setBlueprintInvariantServiceUuid(this.getSdcNotification().getServiceInvariantUUID());
135 try (InputStream stream = zipFile.getInputStream(entry)) {
136 blueprintArtifact.setDcaeBlueprint(IOUtils.toString(stream));
138 blueprintArtifact.setResourceAttached(searchForResourceByInstanceName(entry.getName().substring(
139 entry.getName().indexOf(RESOURCE_INSTANCE_NAME_PREFIX)
140 + RESOURCE_INSTANCE_NAME_PREFIX.length(),
141 entry.getName().indexOf(RESOURCE_INSTANCE_NAME_SUFFIX))));
142 this.mapOfBlueprints.put(blueprintArtifact.getResourceAttached().getResourceInstanceName(),
144 logger.info("Found a blueprint entry in the CSAR " + blueprintArtifact.getBlueprintArtifactName()
145 + " for resource instance Name "
146 + blueprintArtifact.getResourceAttached().getResourceInstanceName());
149 logger.info(this.mapOfBlueprints.size() + " blueprint(s) will be converted to closed loop");
153 public IArtifactInfo getArtifactElement() {
154 return artifactElement;
157 public String getFilePath() {
161 public synchronized ISdcCsarHelper getSdcCsarHelper() {
162 return sdcCsarHelper;
165 public INotificationData getSdcNotification() {
166 return sdcNotification;
169 public Map<String, BlueprintArtifact> getMapOfBlueprints() {
170 return mapOfBlueprints;