From 781b1a6df324419c846c84ea983c18fc8362bfd3 Mon Sep 17 00:00:00 2001 From: Patrick Brady Date: Wed, 13 Dec 2017 11:19:06 -0800 Subject: Third part of onap rename This part of the commit changes the folder structure on all other folders of appc. Change-Id: I8acfa11cdfcdcd36be0e137245d1dd7324f1abd3 Signed-off-by: Patrick Brady Issue-ID: APPC-13 --- .../java/org/onap/appc/yang/YANGGenerator.java | 49 +++++ .../yang/exception/YANGGenerationException.java | 51 ++++++ .../onap/appc/yang/impl/YANGGeneratorFactory.java | 52 ++++++ .../org/onap/appc/yang/impl/YANGGeneratorImpl.java | 176 ++++++++++++++++++ .../main/java/org/onap/appc/yang/objects/Leaf.java | 72 ++++++++ .../java/org/onap/appc/yang/type/YangTypes.java | 108 +++++++++++ .../org/openecomp/appc/yang/YANGGenerator.java | 49 ----- .../yang/exception/YANGGenerationException.java | 51 ------ .../appc/yang/impl/YANGGeneratorFactory.java | 52 ------ .../appc/yang/impl/YANGGeneratorImpl.java | 176 ------------------ .../java/org/openecomp/appc/yang/objects/Leaf.java | 72 -------- .../org/openecomp/appc/yang/type/YangTypes.java | 108 ----------- .../test/java/org/onap/appc/TestYANGGenerator.java | 197 +++++++++++++++++++++ .../java/org/openecomp/appc/TestYANGGenerator.java | 197 --------------------- 14 files changed, 705 insertions(+), 705 deletions(-) create mode 100644 appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/YANGGenerator.java create mode 100644 appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/exception/YANGGenerationException.java create mode 100644 appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/impl/YANGGeneratorFactory.java create mode 100644 appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/impl/YANGGeneratorImpl.java create mode 100644 appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/objects/Leaf.java create mode 100644 appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/type/YangTypes.java delete mode 100644 appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/YANGGenerator.java delete mode 100644 appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/exception/YANGGenerationException.java delete mode 100644 appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorFactory.java delete mode 100644 appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorImpl.java delete mode 100644 appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/objects/Leaf.java delete mode 100644 appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/type/YangTypes.java create mode 100644 appc-sdc-listener/appc-yang-generator/src/test/java/org/onap/appc/TestYANGGenerator.java delete mode 100644 appc-sdc-listener/appc-yang-generator/src/test/java/org/openecomp/appc/TestYANGGenerator.java (limited to 'appc-sdc-listener/appc-yang-generator/src') diff --git a/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/YANGGenerator.java b/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/YANGGenerator.java new file mode 100644 index 000000000..e0ad22a54 --- /dev/null +++ b/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/YANGGenerator.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.yang; + +import org.onap.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-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/exception/YANGGenerationException.java b/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/exception/YANGGenerationException.java new file mode 100644 index 000000000..690a2fda6 --- /dev/null +++ b/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/exception/YANGGenerationException.java @@ -0,0 +1,51 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.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-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/impl/YANGGeneratorFactory.java b/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/impl/YANGGeneratorFactory.java new file mode 100644 index 000000000..fe278d412 --- /dev/null +++ b/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/impl/YANGGeneratorFactory.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.yang.impl; + +import org.onap.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-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/impl/YANGGeneratorImpl.java b/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/impl/YANGGeneratorImpl.java new file mode 100644 index 000000000..55ff41cbd --- /dev/null +++ b/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/impl/YANGGeneratorImpl.java @@ -0,0 +1,176 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.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.onap.appc.yang.YANGGenerator; +import org.onap.appc.yang.exception.YANGGenerationException; +import org.onap.appc.yang.objects.Leaf; +import org.onap.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; + +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.onap.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 parsedToscaMap = parseTosca(tosca); + String moduleType =parsedToscaMap.get(MODULE_TYPE).toString(); + List leaves = (List) 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 parseTosca(String tosca) throws YANGGenerationException { + Log.info("Entered into parseTosca."); + ServiceTemplate serviceTemplate = new YamlUtil().yamlToObject(tosca, ServiceTemplate.class); + Map nodeTypeMap = serviceTemplate.getNode_types(); + String kind = nodeTypeMap.keySet().toArray(new String[0])[0]; + NodeType nodeType = nodeTypeMap.get(kind); + Map returnMap= new HashMap<>(); + Map propertyDefinitionFromTOSCA = nodeType.getProperties(); + returnMap.put(MODULE_TYPE, kind); + List leaves = new LinkedList<>(); + + for(Map.Entry entry: propertyDefinitionFromTOSCA.entrySet()){ + Leaf leaf = new Leaf(); + leaf.setName(entry.getKey()); + PropertyDefinition pd = entry.getValue(); + Map 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-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/objects/Leaf.java b/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/objects/Leaf.java new file mode 100644 index 000000000..2ec66b680 --- /dev/null +++ b/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/objects/Leaf.java @@ -0,0 +1,72 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.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-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/type/YangTypes.java b/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/type/YangTypes.java new file mode 100644 index 000000000..97fb9973d --- /dev/null +++ b/appc-sdc-listener/appc-yang-generator/src/main/java/org/onap/appc/yang/type/YangTypes.java @@ -0,0 +1,108 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.yang.type; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class YangTypes { + + private static final Map yangTypeMap; + private YangTypes(){} + static { + Map 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 getYangTypeMap(){ + return yangTypeMap; + } + +} diff --git a/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/YANGGenerator.java b/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/YANGGenerator.java deleted file mode 100644 index e0ad22a54..000000000 --- a/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/YANGGenerator.java +++ /dev/null @@ -1,49 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.yang; - -import org.onap.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-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/exception/YANGGenerationException.java b/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/exception/YANGGenerationException.java deleted file mode 100644 index 690a2fda6..000000000 --- a/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/exception/YANGGenerationException.java +++ /dev/null @@ -1,51 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.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-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorFactory.java b/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorFactory.java deleted file mode 100644 index fe278d412..000000000 --- a/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorFactory.java +++ /dev/null @@ -1,52 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.yang.impl; - -import org.onap.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-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorImpl.java b/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorImpl.java deleted file mode 100644 index 55ff41cbd..000000000 --- a/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorImpl.java +++ /dev/null @@ -1,176 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.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.onap.appc.yang.YANGGenerator; -import org.onap.appc.yang.exception.YANGGenerationException; -import org.onap.appc.yang.objects.Leaf; -import org.onap.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; - -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.onap.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 parsedToscaMap = parseTosca(tosca); - String moduleType =parsedToscaMap.get(MODULE_TYPE).toString(); - List leaves = (List) 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 parseTosca(String tosca) throws YANGGenerationException { - Log.info("Entered into parseTosca."); - ServiceTemplate serviceTemplate = new YamlUtil().yamlToObject(tosca, ServiceTemplate.class); - Map nodeTypeMap = serviceTemplate.getNode_types(); - String kind = nodeTypeMap.keySet().toArray(new String[0])[0]; - NodeType nodeType = nodeTypeMap.get(kind); - Map returnMap= new HashMap<>(); - Map propertyDefinitionFromTOSCA = nodeType.getProperties(); - returnMap.put(MODULE_TYPE, kind); - List leaves = new LinkedList<>(); - - for(Map.Entry entry: propertyDefinitionFromTOSCA.entrySet()){ - Leaf leaf = new Leaf(); - leaf.setName(entry.getKey()); - PropertyDefinition pd = entry.getValue(); - Map 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-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/objects/Leaf.java b/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/objects/Leaf.java deleted file mode 100644 index 2ec66b680..000000000 --- a/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/objects/Leaf.java +++ /dev/null @@ -1,72 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.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-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/type/YangTypes.java b/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/type/YangTypes.java deleted file mode 100644 index 97fb9973d..000000000 --- a/appc-sdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/type/YangTypes.java +++ /dev/null @@ -1,108 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc.yang.type; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class YangTypes { - - private static final Map yangTypeMap; - private YangTypes(){} - static { - Map 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 getYangTypeMap(){ - return yangTypeMap; - } - -} diff --git a/appc-sdc-listener/appc-yang-generator/src/test/java/org/onap/appc/TestYANGGenerator.java b/appc-sdc-listener/appc-yang-generator/src/test/java/org/onap/appc/TestYANGGenerator.java new file mode 100644 index 000000000..21130ee25 --- /dev/null +++ b/appc-sdc-listener/appc-yang-generator/src/test/java/org/onap/appc/TestYANGGenerator.java @@ -0,0 +1,197 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc; + +import org.junit.*; +import org.junit.rules.TemporaryFolder; +import org.onap.appc.yang.YANGGenerator; +import org.onap.appc.yang.exception.YANGGenerationException; +import org.onap.appc.yang.impl.YANGGeneratorFactory; + +import java.io.*; + +/** + * The Class TestYANGGenerator - Junit Test Class for all related test cases. + */ +@Ignore +public class TestYANGGenerator { + + private YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator(); + private static String tosca; + private static String toscaWithSyntaxError; + private static String expectedYang; + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + /** + * Run before test method. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void runBeforeTestMethod() throws IOException { + tosca= getFileContent("tosca/toscaFile.yml"); + toscaWithSyntaxError = getFileContent("tosca/toscaFileWithSyntaxError.yml"); + expectedYang = getFileContent("yang/expectedYang.yang"); + } + + /** + * Test YANG generator for success. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws YANGGenerationException the YANG generation exception + */ + @Test + public void TestYANGGeneratorForSuccess() throws IOException, YANGGenerationException { + File tempFile = temporaryFolder.newFile("generatedYang.yang"); + OutputStream out = new FileOutputStream(tempFile); + Assert.assertNotNull(tosca); + Assert.assertFalse("tosca file is emply or blank", tosca.equals("")); + yangGenerator.generateYANG("ATD456", tosca, out); + out.flush(); + out.close(); + String generatedYang = getFileContent(tempFile); + Assert.assertEquals(expectedYang,generatedYang); + } + + @Test(expected = YANGGenerationException.class) + public void testYangGenerationForSyntaxError() throws IOException, YANGGenerationException { + File tempFile = temporaryFolder.newFile("generatedYang.yang"); + OutputStream out = new FileOutputStream(tempFile); + yangGenerator.generateYANG("ATD456",toscaWithSyntaxError,out); + } + + + /** + * Test for Yang Generator which generates YANG that is not matching with expected YANG. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws YANGGenerationException - the YANG generation exception + */ + @Test + public void unmatchedYangGenerationTest() throws IOException, YANGGenerationException { + File tempFile = temporaryFolder.newFile("generatedYang.yang"); + OutputStream out = new FileOutputStream(tempFile); + yangGenerator.generateYANG("112476", tosca, out); + out.flush(); + out.close(); + String generatedYang = getFileContent(tempFile); + Assert.assertNotSame(expectedYang, generatedYang); + + } + + /** + * Yang generation test for empty tosca input. + * + * @throws YANGGenerationException the YANG generation exception + */ + @Test(expected = YANGGenerationException.class) + public void YangGenerationTestForEmptyUniqueIDInput() throws IOException, YANGGenerationException { +// OutputStream out = new FileOutputStream(classLoader.getResource("yang/generatedYang.yang").getFile()); + File tempFile = temporaryFolder.newFile("generatedYang.yang"); + OutputStream out = new FileOutputStream(tempFile); + yangGenerator.generateYANG("", tosca, out); + } + + /** + * Yang generation test for empty tosca input. + * + * @throws YANGGenerationException the YANG generation exception + */ + @Test(expected = YANGGenerationException.class) + public void YangGenerationTestForUnSupportedType() throws IOException, YANGGenerationException { + tosca= getFileContent("tosca/toscaFileWithUnsupportedTypes.yml"); + File tempFile = temporaryFolder.newFile("generatedYang.yang"); + OutputStream out = new FileOutputStream(tempFile); + yangGenerator.generateYANG("", tosca, out); + } + + /** + * Yang generation test for empty tosca input. + * + * @throws YANGGenerationException the YANG generation exception + */ + @Test(expected = YANGGenerationException.class) + public void YangGenerationTestForEmptyToscaInput() throws IOException, YANGGenerationException { + File tempFile = temporaryFolder.newFile("generatedYang.yang"); + OutputStream out = new FileOutputStream(tempFile); + yangGenerator.generateYANG("1111", "", out); + } + + /** + * YANG generation test with invalid method arguments. + * + * @throws YANGGenerationException the YANG generation exception + */ + @Test(expected = YANGGenerationException.class) + public void YANGGenerationTestWithInvalidMethodArguments() throws YANGGenerationException { + yangGenerator.generateYANG("112476", "ToscaSAMPLE", null); + } + + @Test(expected = YANGGenerationException.class) + public void YANGGenerationTestWithIOException() throws IOException, YANGGenerationException { + File tempFile = temporaryFolder.newFile("generatedYang.yang"); + OutputStream out = new FileOutputStream(tempFile); + out.flush(); + out.close(); + yangGenerator.generateYANG("1111", tosca, out); + } + + + private String getFileContent(String fileName) throws IOException + { + ClassLoader classLoader = new TestYANGGenerator().getClass().getClassLoader(); + InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile()); + BufferedReader buf = new BufferedReader(new InputStreamReader(is)); + String line = buf.readLine(); + StringBuilder sb = new StringBuilder(); + + while (line != null) { + sb.append(line).append("\n"); + line = buf.readLine(); + } + String fileString = sb.toString(); + is.close(); + return fileString; + } + + private String getFileContent(File file) throws IOException + { + InputStream is = new FileInputStream(file); + BufferedReader buf = new BufferedReader(new InputStreamReader(is)); + String line = buf.readLine(); + StringBuilder sb = new StringBuilder(); + + while (line != null) { + sb.append(line).append("\n"); + line = buf.readLine(); + } + String fileString = sb.toString(); + is.close(); + return fileString; + } + +} diff --git a/appc-sdc-listener/appc-yang-generator/src/test/java/org/openecomp/appc/TestYANGGenerator.java b/appc-sdc-listener/appc-yang-generator/src/test/java/org/openecomp/appc/TestYANGGenerator.java deleted file mode 100644 index 21130ee25..000000000 --- a/appc-sdc-listener/appc-yang-generator/src/test/java/org/openecomp/appc/TestYANGGenerator.java +++ /dev/null @@ -1,197 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.onap.appc; - -import org.junit.*; -import org.junit.rules.TemporaryFolder; -import org.onap.appc.yang.YANGGenerator; -import org.onap.appc.yang.exception.YANGGenerationException; -import org.onap.appc.yang.impl.YANGGeneratorFactory; - -import java.io.*; - -/** - * The Class TestYANGGenerator - Junit Test Class for all related test cases. - */ -@Ignore -public class TestYANGGenerator { - - private YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator(); - private static String tosca; - private static String toscaWithSyntaxError; - private static String expectedYang; - - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - - /** - * Run before test method. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void runBeforeTestMethod() throws IOException { - tosca= getFileContent("tosca/toscaFile.yml"); - toscaWithSyntaxError = getFileContent("tosca/toscaFileWithSyntaxError.yml"); - expectedYang = getFileContent("yang/expectedYang.yang"); - } - - /** - * Test YANG generator for success. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws YANGGenerationException the YANG generation exception - */ - @Test - public void TestYANGGeneratorForSuccess() throws IOException, YANGGenerationException { - File tempFile = temporaryFolder.newFile("generatedYang.yang"); - OutputStream out = new FileOutputStream(tempFile); - Assert.assertNotNull(tosca); - Assert.assertFalse("tosca file is emply or blank", tosca.equals("")); - yangGenerator.generateYANG("ATD456", tosca, out); - out.flush(); - out.close(); - String generatedYang = getFileContent(tempFile); - Assert.assertEquals(expectedYang,generatedYang); - } - - @Test(expected = YANGGenerationException.class) - public void testYangGenerationForSyntaxError() throws IOException, YANGGenerationException { - File tempFile = temporaryFolder.newFile("generatedYang.yang"); - OutputStream out = new FileOutputStream(tempFile); - yangGenerator.generateYANG("ATD456",toscaWithSyntaxError,out); - } - - - /** - * Test for Yang Generator which generates YANG that is not matching with expected YANG. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws YANGGenerationException - the YANG generation exception - */ - @Test - public void unmatchedYangGenerationTest() throws IOException, YANGGenerationException { - File tempFile = temporaryFolder.newFile("generatedYang.yang"); - OutputStream out = new FileOutputStream(tempFile); - yangGenerator.generateYANG("112476", tosca, out); - out.flush(); - out.close(); - String generatedYang = getFileContent(tempFile); - Assert.assertNotSame(expectedYang, generatedYang); - - } - - /** - * Yang generation test for empty tosca input. - * - * @throws YANGGenerationException the YANG generation exception - */ - @Test(expected = YANGGenerationException.class) - public void YangGenerationTestForEmptyUniqueIDInput() throws IOException, YANGGenerationException { -// OutputStream out = new FileOutputStream(classLoader.getResource("yang/generatedYang.yang").getFile()); - File tempFile = temporaryFolder.newFile("generatedYang.yang"); - OutputStream out = new FileOutputStream(tempFile); - yangGenerator.generateYANG("", tosca, out); - } - - /** - * Yang generation test for empty tosca input. - * - * @throws YANGGenerationException the YANG generation exception - */ - @Test(expected = YANGGenerationException.class) - public void YangGenerationTestForUnSupportedType() throws IOException, YANGGenerationException { - tosca= getFileContent("tosca/toscaFileWithUnsupportedTypes.yml"); - File tempFile = temporaryFolder.newFile("generatedYang.yang"); - OutputStream out = new FileOutputStream(tempFile); - yangGenerator.generateYANG("", tosca, out); - } - - /** - * Yang generation test for empty tosca input. - * - * @throws YANGGenerationException the YANG generation exception - */ - @Test(expected = YANGGenerationException.class) - public void YangGenerationTestForEmptyToscaInput() throws IOException, YANGGenerationException { - File tempFile = temporaryFolder.newFile("generatedYang.yang"); - OutputStream out = new FileOutputStream(tempFile); - yangGenerator.generateYANG("1111", "", out); - } - - /** - * YANG generation test with invalid method arguments. - * - * @throws YANGGenerationException the YANG generation exception - */ - @Test(expected = YANGGenerationException.class) - public void YANGGenerationTestWithInvalidMethodArguments() throws YANGGenerationException { - yangGenerator.generateYANG("112476", "ToscaSAMPLE", null); - } - - @Test(expected = YANGGenerationException.class) - public void YANGGenerationTestWithIOException() throws IOException, YANGGenerationException { - File tempFile = temporaryFolder.newFile("generatedYang.yang"); - OutputStream out = new FileOutputStream(tempFile); - out.flush(); - out.close(); - yangGenerator.generateYANG("1111", tosca, out); - } - - - private String getFileContent(String fileName) throws IOException - { - ClassLoader classLoader = new TestYANGGenerator().getClass().getClassLoader(); - InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile()); - BufferedReader buf = new BufferedReader(new InputStreamReader(is)); - String line = buf.readLine(); - StringBuilder sb = new StringBuilder(); - - while (line != null) { - sb.append(line).append("\n"); - line = buf.readLine(); - } - String fileString = sb.toString(); - is.close(); - return fileString; - } - - private String getFileContent(File file) throws IOException - { - InputStream is = new FileInputStream(file); - BufferedReader buf = new BufferedReader(new InputStreamReader(is)); - String line = buf.readLine(); - StringBuilder sb = new StringBuilder(); - - while (line != null) { - sb.append(line).append("\n"); - line = buf.readLine(); - } - String fileString = sb.toString(); - is.close(); - return fileString; - } - -} -- cgit 1.2.3-korg