Changed to unmaintained
[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-2019 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.srvcomm.messaging.event.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.util.*;
54 import java.util.zip.ZipEntry;
55 import java.util.zip.ZipInputStream;
56
57 public class ToscaCsarArtifactProcessor extends AbstractArtifactProcessor{
58
59     private final EELFLogger logger = EELFManager.getInstance().getLogger(ToscaCsarArtifactProcessor.class);
60
61     private DependencyModelGenerator dependencyModelGenerator;
62
63     public ToscaCsarArtifactProcessor(IDistributionClient client,
64                                       EventSender eventSender,
65                                       INotificationData notification,
66                                       IResourceInstance resource,
67                                       IArtifactInfo artifact,
68                                       URI storeUri){
69         super(client,eventSender,notification,resource,artifact,storeUri);
70         dependencyModelGenerator = new DependencyModelGenerator();
71     }
72
73     @Override
74     public void processArtifact(IDistributionClientDownloadResult download) throws APPCException {
75         logger.debug("processing artifact " + super.artifact.getArtifactType());
76         byte[] byteArray = download.getArtifactPayload();
77         String serviceFileName = "";
78         String serviceTemplateContent = "";
79         List<Resource> resources = null;
80         Map<String,String> csarFiles = new HashMap<>();
81         try (ZipInputStream inputStream = new ZipInputStream(new ByteArrayInputStream(byteArray))) {
82             ZipEntry entry = inputStream.getNextEntry();
83             logger.debug("First Entry = " +entry);
84             while(entry!= null){
85                 String filename = entry.getName();
86                 logger.debug("Next Entry = "+ filename);
87
88                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
89                 String str = null;
90                 StringBuilder sb = new StringBuilder();
91                 while((str = bufferedReader.readLine()) != null){
92                     sb.append(new String(str)).append(System.getProperty("line.separator"));
93                 }
94                 csarFiles.put(filename,sb.toString());
95                 entry = inputStream.getNextEntry();
96             }
97
98         } catch (IOException e) {
99             logger.error("Error Reading TOSCA.meta from CSAR",e);
100             throw new APPCException(e);
101         }
102         serviceFileName = readServiceFileName(csarFiles.get("TOSCA-Metadata/TOSCA.meta"));
103         logger.debug("Service File Name = " + serviceFileName);
104         serviceTemplateContent = csarFiles.get(serviceFileName);
105
106         try {
107             resources = readResources (serviceTemplateContent);
108         } catch (Exception e) {
109             logger.error("Error reading resources from " + ", serviceFileName = " + serviceFileName
110                     + ", TOSCA Metadata = " + csarFiles.get("TOSCA-Metadata/TOSCA.meta"),e);
111             throw new APPCException(e);
112         }
113
114         for(Resource resource:resources){
115             String resourceTemplate = csarFiles.get("Definitions/resource-" + resource.getFileNameTag() + "-template.yml");
116             SDCArtifact artifact = this.getArtifactObject(resource,resourceTemplate);
117             processArtifact(artifact);
118         }
119     }
120
121     private String readServiceFileName(String toscaMetadata) {
122         toscaMetadata = toscaMetadata.substring(toscaMetadata.indexOf("Entry-Definitions"), toscaMetadata.indexOf(System.getProperty("line.separator"),toscaMetadata.indexOf("Entry-Definitions")));
123         toscaMetadata =toscaMetadata.split(":")[1].trim();
124         return toscaMetadata;
125     }
126
127     protected SDCArtifact getArtifactObject(Resource resource, String data){
128
129         SDCArtifact sdcArtifact = new SDCArtifact();
130
131         sdcArtifact.setArtifactUUID(this.artifact.getArtifactUUID());
132         sdcArtifact.setArtifactName(this.artifact.getArtifactName());
133         sdcArtifact.setArtifactType(this.artifact.getArtifactType());
134         sdcArtifact.setArtifactVersion(this.artifact.getArtifactVersion());
135         sdcArtifact.setArtifactDescription(this.artifact.getArtifactDescription());
136         sdcArtifact.setArtifactContent(data);
137         sdcArtifact.setCreationDate(super.getCurrentDateTime());
138
139         sdcArtifact.setDistributionId(this.notification.getDistributionID());
140         sdcArtifact.setServiceUUID(this.notification.getServiceUUID());
141         sdcArtifact.setServiceName(this.notification.getServiceName());
142         sdcArtifact.setServiceDescription(this.notification.getServiceDescription());
143
144         sdcArtifact.setResourceName(resource.getName());
145         sdcArtifact.setResourceType(resource.getType());
146         sdcArtifact.setResourceVersion(resource.getVersion());
147         sdcArtifact.setResourceUUID(resource.getUuid());
148         sdcArtifact.setResourceInstanceName(resource.getInstanceName());
149
150         return sdcArtifact;
151     }
152
153     private List<Resource> readResources(String serviceTemplateContent) throws IOException {
154         List<Resource> resources = new LinkedList<>();
155         ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
156         JsonNode root = mapper.readTree(serviceTemplateContent);
157         JsonNode topologyTemplate =  root.get("topology_template");
158         JsonNode nodeTemplates =  topologyTemplate.get("node_templates");
159         Iterator<Map.Entry<String, JsonNode>> itr = nodeTemplates.fields();
160         while(itr.hasNext()){
161             Map.Entry<String, JsonNode> entry = itr.next();
162             String instanceName = entry.getKey();
163             JsonNode nodeTemplate = entry.getValue();
164
165             String fileNameTag =  nodeTemplate.get("type").asText();
166             logger.debug("Resource type in Service Template = " + fileNameTag);
167             fileNameTag = fileNameTag.substring(fileNameTag.lastIndexOf(".")+1,fileNameTag.length());
168             String version = nodeTemplate.get("metadata").get("version").asText();
169             String uuid = nodeTemplate.get("metadata").get("UUID").asText();
170             String name = nodeTemplate.get("metadata").get("name").asText();
171             String type = nodeTemplate.get("metadata").get("type").asText();
172
173             if(!"VF".equalsIgnoreCase(type)){
174                 continue;
175             }
176
177             Resource resource = new Resource();
178             resource.setFileNameTag(fileNameTag);
179             resource.setVersion(version);
180             resource.setUuid(uuid);
181             resource.setInstanceName(instanceName);
182             resource.setName(name);
183             resource.setType(type);
184
185             resources.add(resource);
186         }
187         return resources;
188     }
189
190
191     @Override
192     protected void processArtifact(SDCArtifact artifact) throws APPCException {
193         String vnfType = artifact.getResourceName();
194         String version = artifact.getResourceVersion();
195         String packageArtifactID = artifact.getArtifactUUID();
196
197         if (StringUtils.isEmpty(vnfType) ||
198                 StringUtils.isEmpty(version) ||
199                 StringUtils.isEmpty(packageArtifactID)) {
200             String errStr = String.format("Missing information in SDC request. Details: resource_type='%s', resource_version='%s', artifactID='%s'", vnfType, version, packageArtifactID);
201             logger.error(errStr);
202             throw new APPCException(errStr);
203         }
204         try {
205             SDCReference reference = new SDCReference();
206             reference.setVnfType(vnfType);
207             reference.setFileCategory("tosca_model");
208             reference.setArtifactName(artifact.getArtifactName());
209             logger.debug("Storing TOSCA to SDC Artifact");
210             artifactStorageService.storeSDCArtifactWithReference(artifact,reference);
211
212             SDCArtifact dependencyArtifact = getDependencyArtifact(artifact);
213             SDCReference dependencyReference = new SDCReference();
214             dependencyReference.setVnfType(vnfType);
215             dependencyReference.setFileCategory("tosca_dependency_model");
216             dependencyReference.setArtifactName(dependencyArtifact.getArtifactName());
217             logger.debug("Storing Dependency to SDC Artifact");
218             artifactStorageService.storeSDCArtifactWithReference(dependencyArtifact,dependencyReference);
219         } catch (Exception e) {
220             logger.error("Error processing artifact : " + artifact.toString() );
221             throw new APPCException(e.getMessage(),e);
222         }
223     }
224
225     private SDCArtifact getDependencyArtifact(SDCArtifact toscaArtifact) throws APPCException {
226         SDCArtifact artifact = new SDCArtifact();
227         artifact.setArtifactName("dependency_"+toscaArtifact.getArtifactName());
228         String dependencyModel = dependencyModelGenerator.getDependencyModel(toscaArtifact.getArtifactContent(),toscaArtifact.getResourceName());
229         artifact.setArtifactContent(dependencyModel);
230         artifact.setArtifactType("DEPENDENCY_MODEL");
231
232         artifact.setArtifactUUID(toscaArtifact.getArtifactUUID());
233         artifact.setArtifactVersion(toscaArtifact.getArtifactVersion());
234         artifact.setArtifactDescription(toscaArtifact.getArtifactDescription());
235         artifact.setCreationDate(super.getCurrentDateTime());
236         artifact.setDistributionId(toscaArtifact.getDistributionId());
237         artifact.setServiceUUID(toscaArtifact.getServiceUUID());
238         artifact.setServiceName(toscaArtifact.getServiceName());
239         artifact.setServiceDescription(toscaArtifact.getServiceDescription());
240         artifact.setResourceName(toscaArtifact.getResourceName());
241         artifact.setResourceType(toscaArtifact.getResourceType());
242         artifact.setResourceVersion(toscaArtifact.getResourceVersion());
243         artifact.setResourceUUID(toscaArtifact.getResourceUUID());
244         artifact.setResourceInstanceName(toscaArtifact.getResourceInstanceName());
245         return artifact;
246     }
247
248
249 }