dbce362ba7c27f1e8f493c58250d80cdf4b4c288
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / main / java / org / onap / appc / sdc / artifacts / impl / ToscaCsarArtifactProcessor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.appc.sdc.artifacts.impl;
24
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import com.fasterxml.jackson.databind.JsonNode;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
30 import org.apache.commons.lang.StringUtils;
31 import org.onap.appc.adapter.message.EventSender;
32 import org.onap.appc.exceptions.APPCException;
33 import org.onap.appc.sdc.artifacts.helper.DependencyModelGenerator;
34 import org.onap.appc.sdc.artifacts.object.Resource;
35 import org.onap.appc.sdc.artifacts.object.SDCArtifact;
36 import org.onap.appc.sdc.artifacts.object.SDCReference;
37 import org.onap.sdc.api.IDistributionClient;
38 import org.onap.sdc.api.notification.IArtifactInfo;
39 import org.onap.sdc.api.notification.INotificationData;
40 import org.onap.sdc.api.notification.IResourceInstance;
41 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
42
43 import java.io.BufferedReader;
44 import java.io.ByteArrayInputStream;
45 import java.io.IOException;
46 import java.io.InputStreamReader;
47 import java.net.URI;
48 import java.util.HashMap;
49 import java.util.Iterator;
50 import java.util.LinkedList;
51 import java.util.List;
52 import java.util.Map;
53 import java.net.URI;
54 import java.util.*;
55 import java.util.zip.ZipEntry;
56 import java.util.zip.ZipInputStream;
57
58 public class ToscaCsarArtifactProcessor extends AbstractArtifactProcessor{
59
60     private final EELFLogger logger = EELFManager.getInstance().getLogger(ToscaCsarArtifactProcessor.class);
61
62     private DependencyModelGenerator dependencyModelGenerator;
63
64     public ToscaCsarArtifactProcessor(IDistributionClient client,
65                                       EventSender eventSender,
66                                       INotificationData notification,
67                                       IResourceInstance resource,
68                                       IArtifactInfo artifact,
69                                       URI storeUri){
70         super(client,eventSender,notification,resource,artifact,storeUri);
71         dependencyModelGenerator = new DependencyModelGenerator();
72     }
73
74     @Override
75     public void processArtifact(IDistributionClientDownloadResult download) throws APPCException {
76         logger.debug("processing artifact " + super.artifact.getArtifactType());
77         byte[] byteArray = download.getArtifactPayload();
78         String serviceFileName = "";
79         String serviceTemplateContent = "";
80         List<Resource> resources = null;
81         Map<String,String> csarFiles = new HashMap<>();
82         try (ZipInputStream inputStream = new ZipInputStream(new ByteArrayInputStream(byteArray))) {
83             ZipEntry entry = inputStream.getNextEntry();
84             logger.debug("First Entry = " +entry);
85             while(entry!= null){
86                 String filename = entry.getName();
87                 logger.debug("Next Entry = "+ filename);
88
89                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
90                 String str = null;
91                 StringBuilder sb = new StringBuilder();
92                 while((str = bufferedReader.readLine()) != null){
93                     sb.append(new String(str)).append(System.getProperty("line.separator"));
94                 }
95                 csarFiles.put(filename,sb.toString());
96                 entry = inputStream.getNextEntry();
97             }
98
99         } catch (IOException e) {
100             logger.error("Error Reading TOSCA.meta from CSAR",e);
101             throw new APPCException(e);
102         }
103         serviceFileName = readServiceFileName(csarFiles.get("TOSCA-Metadata/TOSCA.meta"));
104         logger.debug("Service File Name = " + serviceFileName);
105         serviceTemplateContent = csarFiles.get(serviceFileName);
106
107         try {
108             resources = readResources (serviceTemplateContent);
109         } catch (Exception e) {
110             logger.error("Error reading resources from " + ", serviceFileName = " + serviceFileName
111                     + ", TOSCA Metadata = " + csarFiles.get("TOSCA-Metadata/TOSCA.meta"),e);
112             throw new APPCException(e);
113         }
114
115         for(Resource resource:resources){
116             String resourceTemplate = csarFiles.get("Definitions/resource-" + resource.getFileNameTag() + "-template.yml");
117             SDCArtifact artifact = this.getArtifactObject(resource,resourceTemplate);
118             processArtifact(artifact);
119         }
120     }
121
122     private String readServiceFileName(String toscaMetadata) {
123         toscaMetadata = toscaMetadata.substring(toscaMetadata.indexOf("Entry-Definitions"), toscaMetadata.indexOf(System.getProperty("line.separator"),toscaMetadata.indexOf("Entry-Definitions")));
124         toscaMetadata =toscaMetadata.split(":")[1].trim();
125         return toscaMetadata;
126     }
127
128     protected SDCArtifact getArtifactObject(Resource resource, String data){
129
130         SDCArtifact sdcArtifact = new SDCArtifact();
131
132         sdcArtifact.setArtifactUUID(this.artifact.getArtifactUUID());
133         sdcArtifact.setArtifactName(this.artifact.getArtifactName());
134         sdcArtifact.setArtifactType(this.artifact.getArtifactType());
135         sdcArtifact.setArtifactVersion(this.artifact.getArtifactVersion());
136         sdcArtifact.setArtifactDescription(this.artifact.getArtifactDescription());
137         sdcArtifact.setArtifactContent(data);
138         sdcArtifact.setCreationDate(super.getCurrentDateTime());
139
140         sdcArtifact.setDistributionId(this.notification.getDistributionID());
141         sdcArtifact.setServiceUUID(this.notification.getServiceUUID());
142         sdcArtifact.setServiceName(this.notification.getServiceName());
143         sdcArtifact.setServiceDescription(this.notification.getServiceDescription());
144
145         sdcArtifact.setResourceName(resource.getName());
146         sdcArtifact.setResourceType(resource.getType());
147         sdcArtifact.setResourceVersion(resource.getVersion());
148         sdcArtifact.setResourceUUID(resource.getUuid());
149         sdcArtifact.setResourceInstanceName(resource.getInstanceName());
150
151         return sdcArtifact;
152     }
153
154     private List<Resource> readResources(String serviceTemplateContent) throws IOException {
155         List<Resource> resources = new LinkedList<>();
156         ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
157         JsonNode root = mapper.readTree(serviceTemplateContent);
158         JsonNode topologyTemplate =  root.get("topology_template");
159         JsonNode nodeTemplates =  topologyTemplate.get("node_templates");
160         Iterator<Map.Entry<String, JsonNode>> itr = nodeTemplates.fields();
161         while(itr.hasNext()){
162             Map.Entry<String, JsonNode> entry = itr.next();
163             String instanceName = entry.getKey();
164             JsonNode nodeTemplate = entry.getValue();
165
166             String fileNameTag =  nodeTemplate.get("type").asText();
167             logger.debug("Resource type in Service Template = " + fileNameTag);
168             fileNameTag = fileNameTag.substring(fileNameTag.lastIndexOf(".")+1,fileNameTag.length());
169             String version = nodeTemplate.get("metadata").get("version").asText();
170             String uuid = nodeTemplate.get("metadata").get("UUID").asText();
171             String name = nodeTemplate.get("metadata").get("name").asText();
172             String type = nodeTemplate.get("metadata").get("type").asText();
173
174             if(!"VF".equalsIgnoreCase(type)){
175                 continue;
176             }
177
178             Resource resource = new Resource();
179             resource.setFileNameTag(fileNameTag);
180             resource.setVersion(version);
181             resource.setUuid(uuid);
182             resource.setInstanceName(instanceName);
183             resource.setName(name);
184             resource.setType(type);
185
186             resources.add(resource);
187         }
188         return resources;
189     }
190
191
192     @Override
193     protected void processArtifact(SDCArtifact artifact) throws APPCException {
194         String vnfType = artifact.getResourceName();
195         String version = artifact.getResourceVersion();
196         String packageArtifactID = artifact.getArtifactUUID();
197
198         if (StringUtils.isEmpty(vnfType) ||
199                 StringUtils.isEmpty(version) ||
200                 StringUtils.isEmpty(packageArtifactID)) {
201             String errStr = String.format("Missing information in SDC request. Details: resource_type='%s', resource_version='%s', artifactID='%s'", vnfType, version, packageArtifactID);
202             logger.error(errStr);
203             throw new APPCException(errStr);
204         }
205         try {
206             SDCReference reference = new SDCReference();
207             reference.setVnfType(vnfType);
208             reference.setFileCategory("tosca_model");
209             reference.setArtifactName(artifact.getArtifactName());
210             logger.debug("Storing TOSCA to SDC Artifact");
211             artifactStorageService.storeSDCArtifactWithReference(artifact,reference);
212
213             SDCArtifact dependencyArtifact = getDependencyArtifact(artifact);
214             SDCReference dependencyReference = new SDCReference();
215             dependencyReference.setVnfType(vnfType);
216             dependencyReference.setFileCategory("tosca_dependency_model");
217             dependencyReference.setArtifactName(dependencyArtifact.getArtifactName());
218             logger.debug("Storing Dependency to SDC Artifact");
219             artifactStorageService.storeSDCArtifactWithReference(dependencyArtifact,dependencyReference);
220         } catch (Exception e) {
221             logger.error("Error processing artifact : " + artifact.toString() );
222             throw new APPCException(e.getMessage(),e);
223         }
224     }
225
226     private SDCArtifact getDependencyArtifact(SDCArtifact toscaArtifact) throws APPCException {
227         SDCArtifact artifact = new SDCArtifact();
228         artifact.setArtifactName("dependency_"+toscaArtifact.getArtifactName());
229         String dependencyModel = dependencyModelGenerator.getDependencyModel(toscaArtifact.getArtifactContent(),toscaArtifact.getResourceName());
230         artifact.setArtifactContent(dependencyModel);
231         artifact.setArtifactType("DEPENDENCY_MODEL");
232
233         artifact.setArtifactUUID(toscaArtifact.getArtifactUUID());
234         artifact.setArtifactVersion(toscaArtifact.getArtifactVersion());
235         artifact.setArtifactDescription(toscaArtifact.getArtifactDescription());
236         artifact.setCreationDate(super.getCurrentDateTime());
237         artifact.setDistributionId(toscaArtifact.getDistributionId());
238         artifact.setServiceUUID(toscaArtifact.getServiceUUID());
239         artifact.setServiceName(toscaArtifact.getServiceName());
240         artifact.setServiceDescription(toscaArtifact.getServiceDescription());
241         artifact.setResourceName(toscaArtifact.getResourceName());
242         artifact.setResourceType(toscaArtifact.getResourceType());
243         artifact.setResourceVersion(toscaArtifact.getResourceVersion());
244         artifact.setResourceUUID(toscaArtifact.getResourceUUID());
245         artifact.setResourceInstanceName(toscaArtifact.getResourceInstanceName());
246         return artifact;
247     }
248
249
250 }