summaryrefslogtreecommitdiffstats
path: root/appc-asdc-listener/appc-yang-generator/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'appc-asdc-listener/appc-yang-generator/src/main/java')
-rw-r--r--appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/YANGGenerator.java46
-rw-r--r--appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/exception/YANGGenerationException.java48
-rw-r--r--appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorFactory.java49
-rw-r--r--appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorImpl.java173
-rw-r--r--appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/objects/Leaf.java69
-rw-r--r--appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/type/YangTypes.java105
6 files changed, 490 insertions, 0 deletions
diff --git a/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/YANGGenerator.java b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/YANGGenerator.java
new file mode 100644
index 000000000..112790d86
--- /dev/null
+++ b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/YANGGenerator.java
@@ -0,0 +1,46 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : APP-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.appc.yang;
+
+import org.openecomp.appc.yang.exception.YANGGenerationException;
+
+import java.io.OutputStream;
+
+/**
+ * The Interface YANGGenerator - provides method to generate YANG file from TOSCA.
+ */
+public interface YANGGenerator {
+
+ /**
+ * Generate YANG from TOSCA.
+ * if any exceptional Type is coming in the input tosca as a part of configuration parameter property, YANGGenerationException will be thrown.
+ * This API is not supporting below mentioned built-in Types:
+ * bits, decimal64, enumeration, identityref, leafref, union
+ *
+ * @param uniqueID - Set as module name in the yang, mandatory, cannot be null or empty
+ * @param tosca - TOSCA String from which the YANG is to be generated, mandatory, cannot be null or empty
+ * @param stream - The outputStream to which the generated yang is written, mandatory, cannot be null
+ * @throws YANGGenerationException - Thrown when any error occurred during method execution, the origin can be found from ex.getCause() or ex.getMessage()
+ */
+
+ void generateYANG(String uniqueID, String tosca, OutputStream stream) throws YANGGenerationException;
+}
diff --git a/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/exception/YANGGenerationException.java b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/exception/YANGGenerationException.java
new file mode 100644
index 000000000..b049e6f21
--- /dev/null
+++ b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/exception/YANGGenerationException.java
@@ -0,0 +1,48 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : APP-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.appc.yang.exception;
+
+/**
+ * The Class YANGGenerationException.
+ */
+public class YANGGenerationException extends Exception {
+
+
+ /**
+ * Instantiates a new YANG generation exception.
+ *
+ * @param message - the appropriate message
+ * @param cause -the appropriate cause
+ */
+ public YANGGenerationException(String message,Throwable cause){
+ super(message,cause);
+ }
+
+
+ /**
+ * Instantiates new YANG generation exception
+ * @param message
+ */
+ public YANGGenerationException(String message){
+ super(message);
+ }
+}
diff --git a/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorFactory.java b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorFactory.java
new file mode 100644
index 000000000..ca5c0ae7d
--- /dev/null
+++ b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorFactory.java
@@ -0,0 +1,49 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : APP-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.appc.yang.impl;
+
+import org.openecomp.appc.yang.YANGGenerator;
+
+/**
+ * A factory for creating YANGGenerator objects.
+ */
+public class YANGGeneratorFactory {
+
+ private YANGGeneratorFactory(){}
+
+ private static class InstanceHolder
+ {
+ private static YANGGeneratorImpl instance = new YANGGeneratorImpl();
+ private InstanceHolder(){}
+ }
+
+ /**
+ * Gets the YANG generator.
+ *
+ * @return the YANG generator
+ */
+ public static YANGGenerator getYANGGenerator()
+ {
+ return InstanceHolder.instance;
+ }
+
+}
diff --git a/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorImpl.java b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorImpl.java
new file mode 100644
index 000000000..0ca7878a9
--- /dev/null
+++ b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorImpl.java
@@ -0,0 +1,173 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : APP-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.appc.yang.impl;
+
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+import org.apache.commons.lang.StringUtils;
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.exception.ParseErrorException;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.velocity.runtime.RuntimeConstants;
+import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
+import org.openecomp.appc.yang.YANGGenerator;
+import org.openecomp.appc.yang.exception.YANGGenerationException;
+import org.openecomp.appc.yang.objects.Leaf;
+import org.openecomp.appc.yang.type.YangTypes;
+import org.openecomp.sdc.tosca.datatypes.model.NodeType;
+import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
+import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
+import org.openecomp.sdc.tosca.services.yamlutil.ToscaExtensionYamlUtil;
+
+import java.io.*;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+@SuppressWarnings("CheckStyle")
+public class YANGGeneratorImpl implements YANGGenerator {
+
+ private static final EELFLogger Log = EELFManager.getInstance().getLogger(YANGGeneratorImpl.class);
+ private static final String MODULE_TYPE = "moduleType";
+ private static final String LEAVES = "leaves";
+
+
+ /* (non-Javadoc)
+ * @see org.openecomp.appc.yang.YANGGenerator#generateYANG(java.lang.String, java.lang.String, java.io.OutputStream)
+ */
+ @Override
+ public void generateYANG(String uniqueID, String tosca, OutputStream stream)
+ throws YANGGenerationException {
+ Log.info("Entered into generateYANG.");
+ Log.debug("Received Tosca:\n" + tosca +"\n Received uniqueID: "+uniqueID);
+
+ validateInput(uniqueID, tosca, stream);
+ Map<String,Object> parsedToscaMap = parseTosca(tosca);
+ String moduleType =parsedToscaMap.get(MODULE_TYPE).toString();
+ List<Leaf> leaves = (List<Leaf>) parsedToscaMap.get(LEAVES);
+ VelocityEngine ve = new VelocityEngine();
+ ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
+ ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
+ ve.init();
+ Template template;
+
+ try {
+ template = ve.getTemplate("templates/YangTemplate.vm");
+ } catch ( ResourceNotFoundException | ParseErrorException ex) {
+ Log.error("Error while retrieving YANG Template", ex);
+ throw new YANGGenerationException("Error while retrieving YANG Template",ex);
+ }
+
+ VelocityContext vc = new VelocityContext();
+
+ vc.put("moduleName", uniqueID);
+ vc.put(MODULE_TYPE, moduleType);
+ vc.put(LEAVES, leaves);
+
+ StringWriter sw = new StringWriter();
+ template.merge(vc,sw);
+ Log.debug("generated YANG \n "+sw.toString());
+ try {
+ String yang = sw.toString();
+ validateYang(yang);
+ stream.write(yang.getBytes());
+ stream.flush();
+ } catch (IOException e) {
+ Log.error("Error while writing to outputstream", e);
+ throw new YANGGenerationException("Error writing to outputstream",e);
+ } finally {
+ try {
+ stream.close();
+ } catch (IOException e) {
+ Log.error("Error while closing outputstream", e);
+ }
+ }
+ Log.info("exiting generateYANG method.");
+ }
+
+ private void validateYang(String yang) throws YANGGenerationException {
+
+ try(InputStream inputYangStream = new ByteArrayInputStream(yang.getBytes())){
+ YangStatementSourceImpl statementSource = new YangStatementSourceImpl(inputYangStream);
+ if(statementSource.getYangAST()==null){
+ throw new YANGGenerationException("Syntax Error in Generated YANG = " + yang);
+ }
+ }
+ catch(IOException e){
+ Log.error("Error validating yang file "+ yang,e);
+ throw new YANGGenerationException("Invalid YANG generated",e);
+ }
+ }
+
+ private Map<String,Object> parseTosca(String tosca) throws YANGGenerationException {
+ Log.info("Entered into parseTosca.");
+ ServiceTemplate serviceTemplate = new ToscaExtensionYamlUtil().yamlToObject(tosca, ServiceTemplate.class);
+ Map<String, NodeType> nodeTypeMap = serviceTemplate.getNode_types();
+ String kind = nodeTypeMap.keySet().toArray(new String[0])[0];
+ NodeType nodeType = nodeTypeMap.get(kind);
+ Map<String,Object> returnMap= new HashMap<>();
+ Map<String, PropertyDefinition> propertyDefinitionFromTOSCA = nodeType.getProperties();
+ returnMap.put(MODULE_TYPE, kind);
+ List<Leaf> leaves = new LinkedList<>();
+
+ for(Map.Entry<String, PropertyDefinition> entry: propertyDefinitionFromTOSCA.entrySet()){
+ Leaf leaf = new Leaf();
+ leaf.setName(entry.getKey());
+ PropertyDefinition pd = entry.getValue();
+ Map<String,String> typeMap=YangTypes.getYangTypeMap();
+ if (typeMap.containsKey(pd.getType())) {
+ String paramType = typeMap.get(pd.getType());
+ leaf.setType(paramType);
+ leaf.setDescription(!StringUtils.isEmpty(pd.getDescription()) ? pd.getDescription() : "");
+ leaf.setMandatory((pd.getRequired() != null) ? Boolean.toString(pd.getRequired()) : Boolean.toString(false));
+ leaf.setDefaultValue((pd.get_default() != null) ? pd.get_default().toString(): "");
+ leaves.add(leaf);
+ } else {
+ YANGGenerationException yangGenerationException = new YANGGenerationException(pd.getType() + " Type is not supported ", null);
+ Log.error(pd.getType() + " Type is not supported ", yangGenerationException);
+ throw yangGenerationException;
+ }
+ }
+ returnMap.put(LEAVES, leaves);
+ Log.info("exiting parseTosca method with return MAP "+returnMap);
+ return returnMap;
+ }
+
+ private void validateInput(String uniqueID, String tosca, OutputStream stream) throws YANGGenerationException {
+ Log.info("Entered into validateInput.");
+ if(StringUtils.isEmpty(uniqueID)) {
+ throw new YANGGenerationException("uniqueID is mandatory, cannot be null or empty.",null);
+ }
+ if(StringUtils.isEmpty(tosca)) {
+ throw new YANGGenerationException("tosca is mandatory, cannot be null or empty.",null);
+ }
+ if(stream == null){
+ throw new YANGGenerationException("stream is mandatory, cannot be null.",null);
+ }
+ Log.info("exiting validateInput method.");
+ }
+
+}
diff --git a/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/objects/Leaf.java b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/objects/Leaf.java
new file mode 100644
index 000000000..d7f328db1
--- /dev/null
+++ b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/objects/Leaf.java
@@ -0,0 +1,69 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : APP-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.appc.yang.objects;
+
+public class Leaf {
+ private String name;
+ private String type;
+ private String description;
+ private String mandatory;
+ private String defaultValue;
+
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ public String getType() {
+ return type;
+ }
+ public void setType(String type) {
+ this.type = type;
+ }
+ public String getDescription() {
+ return description;
+ }
+ public void setDescription(String description) {
+ this.description = description;
+ }
+ public String getMandatory() {
+ return mandatory;
+ }
+ public void setMandatory(String mandatory) {
+ this.mandatory = mandatory;
+ }
+ public String getDefaultValue() {
+ return defaultValue;
+ }
+ public void setDefaultValue(String defaultValue) {
+ this.defaultValue = defaultValue;
+ }
+ @Override
+ public String toString() {
+ return "Leaf [name=" + name + ", type=" + type + ", description=" + description + ", mandatory=" + mandatory
+ + ", defaultValue=" + defaultValue + "]";
+ }
+
+
+
+}
diff --git a/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/type/YangTypes.java b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/type/YangTypes.java
new file mode 100644
index 000000000..3e818623d
--- /dev/null
+++ b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/type/YangTypes.java
@@ -0,0 +1,105 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : APP-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.appc.yang.type;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public class YangTypes {
+
+ private static final Map<String, String> yangTypeMap;
+ private YangTypes(){}
+ static {
+ Map<String, String> typeMap = new HashMap<>();
+
+ /* standard Types */
+ /* typeMap.put("bits","bits");
+ typeMap.put("leafref","leafref");
+ typeMap.put("decimal64","decimal64");
+ typeMap.put("enumeration","enumeration");
+ typeMap.put("identityref","identityref");
+ typeMap.put("union","union");*/
+ typeMap.put("binary","binary");
+ typeMap.put("boolean","boolean");
+ typeMap.put("empty","empty");
+ typeMap.put("instance-identifier","instance-identifier");
+ typeMap.put("int8","int8");
+ typeMap.put("int16","int16");
+ typeMap.put("int32","int32");
+ typeMap.put("int64","int64");
+ typeMap.put("string","string");
+ typeMap.put("uint8","uint8");
+ typeMap.put("uint16","uint16");
+ typeMap.put("uint32","uint32");
+ typeMap.put("uint64","uint64");
+
+
+ /* ietf-yang-types */
+
+ typeMap.put("counter32","yang:counter32");
+ typeMap.put("zero-based-counter32","yang:zero-based-counter32");
+ typeMap.put("counter64","yang:counter64");
+ typeMap.put("zero-based-counter64","yang:zero-based-counter64");
+ typeMap.put("gauge32","yang:gauge32");
+ typeMap.put("gauge64","yang:gauge64");
+ typeMap.put("object-identifier","yang:object-identifier");
+ typeMap.put("object-identifier-128","yang:object-identifier-128");
+ typeMap.put("yang-identifier","yang:yang-identifier");
+ typeMap.put("date-and-time","yang:date-and-time");
+ typeMap.put("timeticks","yang:timeticks");
+ typeMap.put("timestamp","yang:timestamp");
+ typeMap.put("phys-address","yang:phys-address");
+ typeMap.put("mac-address","yang:mac-address");
+ typeMap.put("xpath1.0","yang:xpath1.0");
+ typeMap.put("hex-string","yang:hex-string");
+ typeMap.put("uuid","yang:uuid");
+ typeMap.put("dotted-quad","yang:dotted-quad");
+
+ /* ietf-inet-types */
+
+ typeMap.put("ip-version","inet:ip-version");
+ typeMap.put("dscp","inet:dscp");
+ typeMap.put("ipv6-flow-label","inet:ipv6-flow-label");
+ typeMap.put("port-number","inet:port-number");
+ typeMap.put("as-number","inet:as-number");
+ typeMap.put("ip-address","inet:ip-address");
+ typeMap.put("ipv4-address","inet:ipv4-address");
+ typeMap.put("ipv6-address","inet:ipv6-address");
+ typeMap.put("ip-address-no-zone","inet:ip-address-no-zone");
+ typeMap.put("ipv4-address-no-zone","inet:ipv4-address-no-zone");
+ typeMap.put("ipv6-address-no-zone","inet:ipv6-address-no-zone");
+ typeMap.put("ip-prefix","inet:ip-prefix");
+ typeMap.put("ipv4-prefix","inet:ipv4-prefix");
+ typeMap.put("ipv6-prefix","inet:ipv6-prefix");
+ typeMap.put("domain-name","inet:domain-name");
+ typeMap.put("host","inet:host");
+ typeMap.put("uri","inet:uri");
+
+ yangTypeMap = Collections.unmodifiableMap(typeMap);
+ }
+
+ public static Map<String, String> getYangTypeMap(){
+ return yangTypeMap;
+ }
+
+}