2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.yang.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.velocity.Template;
31 import org.apache.velocity.VelocityContext;
32 import org.apache.velocity.app.VelocityEngine;
33 import org.apache.velocity.exception.ParseErrorException;
34 import org.apache.velocity.exception.ResourceNotFoundException;
35 import org.apache.velocity.runtime.RuntimeConstants;
36 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
37 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
38 import org.onap.appc.yang.YANGGenerator;
39 import org.onap.appc.yang.exception.YANGGenerationException;
40 import org.onap.appc.yang.objects.Leaf;
41 import org.onap.appc.yang.type.YangTypes;
42 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
43 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
44 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
45 import org.openecomp.sdc.tosca.services.YamlUtil;
48 import java.util.HashMap;
49 import java.util.LinkedList;
50 import java.util.List;
53 @SuppressWarnings("CheckStyle")
54 public class YANGGeneratorImpl implements YANGGenerator {
56 private static final EELFLogger Log = EELFManager.getInstance().getLogger(YANGGeneratorImpl.class);
57 private static final String MODULE_TYPE = "moduleType";
58 private static final String LEAVES = "leaves";
62 * @see org.onap.appc.yang.YANGGenerator#generateYANG(java.lang.String, java.lang.String, java.io.OutputStream)
65 public void generateYANG(String uniqueID, String tosca, OutputStream stream)
66 throws YANGGenerationException {
67 Log.info("Entered into generateYANG.");
68 Log.debug("Received Tosca:\n" + tosca +"\n Received uniqueID: "+uniqueID);
70 validateInput(uniqueID, tosca, stream);
71 Map<String,Object> parsedToscaMap = parseTosca(tosca);
72 String moduleType =parsedToscaMap.get(MODULE_TYPE).toString();
73 List<Leaf> leaves = (List<Leaf>) parsedToscaMap.get(LEAVES);
74 VelocityEngine ve = new VelocityEngine();
75 ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
76 ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
81 template = ve.getTemplate("templates/YangTemplate.vm");
82 } catch ( ResourceNotFoundException | ParseErrorException ex) {
83 Log.error("Error while retrieving YANG Template", ex);
84 throw new YANGGenerationException("Error while retrieving YANG Template",ex);
87 VelocityContext vc = new VelocityContext();
89 vc.put("moduleName", uniqueID);
90 vc.put(MODULE_TYPE, moduleType);
91 vc.put(LEAVES, leaves);
93 StringWriter sw = new StringWriter();
94 template.merge(vc,sw);
95 Log.debug("generated YANG \n "+sw.toString());
97 String yang = sw.toString();
99 stream.write(yang.getBytes());
101 } catch (IOException e) {
102 Log.error("Error while writing to outputstream", e);
103 throw new YANGGenerationException("Error writing to outputstream",e);
107 } catch (IOException e) {
108 Log.error("Error while closing outputstream", e);
111 Log.info("exiting generateYANG method.");
114 private void validateYang(String yang) throws YANGGenerationException {
116 try(InputStream inputYangStream = new ByteArrayInputStream(yang.getBytes())){
117 YangStatementSourceImpl statementSource = new YangStatementSourceImpl(inputYangStream);
118 if(statementSource.getYangAST()==null){
119 throw new YANGGenerationException("Syntax Error in Generated YANG = " + yang);
122 catch(IOException e){
123 Log.error("Error validating yang file "+ yang,e);
124 throw new YANGGenerationException("Invalid YANG generated",e);
128 private Map<String,Object> parseTosca(String tosca) throws YANGGenerationException {
129 Log.info("Entered into parseTosca.");
130 ServiceTemplate serviceTemplate = new YamlUtil().yamlToObject(tosca, ServiceTemplate.class);
131 Map<String, NodeType> nodeTypeMap = serviceTemplate.getNode_types();
132 String kind = nodeTypeMap.keySet().toArray(new String[0])[0];
133 NodeType nodeType = nodeTypeMap.get(kind);
134 Map<String,Object> returnMap= new HashMap<>();
135 Map<String, PropertyDefinition> propertyDefinitionFromTOSCA = nodeType.getProperties();
136 returnMap.put(MODULE_TYPE, kind);
137 List<Leaf> leaves = new LinkedList<>();
139 for(Map.Entry<String, PropertyDefinition> entry: propertyDefinitionFromTOSCA.entrySet()){
140 Leaf leaf = new Leaf();
141 leaf.setName(entry.getKey());
142 PropertyDefinition pd = entry.getValue();
143 Map<String,String> typeMap=YangTypes.getYangTypeMap();
144 if (typeMap.containsKey(pd.getType())) {
145 String paramType = typeMap.get(pd.getType());
146 leaf.setType(paramType);
147 leaf.setDescription(!StringUtils.isEmpty(pd.getDescription()) ? pd.getDescription() : "");
148 leaf.setMandatory((pd.getRequired() != null) ? Boolean.toString(pd.getRequired()) : Boolean.toString(false));
149 leaf.setDefaultValue((pd.get_default() != null) ? pd.get_default().toString(): "");
152 YANGGenerationException yangGenerationException = new YANGGenerationException(pd.getType() + " Type is not supported ", null);
153 Log.error(pd.getType() + " Type is not supported ", yangGenerationException);
154 throw yangGenerationException;
157 returnMap.put(LEAVES, leaves);
158 Log.info("exiting parseTosca method with return MAP "+returnMap);
162 private void validateInput(String uniqueID, String tosca, OutputStream stream) throws YANGGenerationException {
163 Log.info("Entered into validateInput.");
164 if(StringUtils.isEmpty(uniqueID)) {
165 throw new YANGGenerationException("uniqueID is mandatory, cannot be null or empty.",null);
167 if(StringUtils.isEmpty(tosca)) {
168 throw new YANGGenerationException("tosca is mandatory, cannot be null or empty.",null);
171 throw new YANGGenerationException("stream is mandatory, cannot be null.",null);
173 Log.info("exiting validateInput method.");