this.genTransferObjects = genTransferObjects;
}
- public boolean generateToFile() {
+ public List<File> generateToFile() throws IOException {
return generateToFile(null);
}
- public boolean generateToFile(String path) {
- try {
- for (GeneratedType type : types) {
- String parentPath = generateParentPath(path,
- type.getPackageName());
+ public List<File> generateToFile(String path) throws IOException {
+ final List<File> result = new ArrayList<File>();
- File file = new File(parentPath, type.getName() + ".java");
- File parent = file.getParentFile();
- if (!parent.exists()) {
- parent.mkdirs();
- }
+ for (GeneratedType type : types) {
+ String parentPath = generateParentPath(path,
+ type.getPackageName());
- if (!file.exists()) {
- FileWriter fw = null;
- BufferedWriter bw = null;
-
- file.createNewFile();
- fw = new FileWriter(file);
- bw = new BufferedWriter(fw);
- Writer writer = interfaceGenerator.generate(type);
- bw.write(writer.toString());
-
- if (bw != null) {
- try {
- bw.close();
- } catch (IOException e) {
- // TODO: log?
- }
- }
- }
+ File file = new File(parentPath, type.getName() + ".java");
+ File parent = file.getParentFile();
+ if (!parent.exists()) {
+ parent.mkdirs();
}
- for (GeneratedTransferObject transferObject : genTransferObjects) {
- String parentPath = generateParentPath(path,
- transferObject.getPackageName());
-
- File file = new File(parentPath, transferObject.getName() + ".java");
- File parent = file.getParentFile();
- if (!parent.exists()) {
- parent.mkdirs();
+
+ if (!file.exists()) {
+ FileWriter fw = null;
+ BufferedWriter bw = null;
+
+ file.createNewFile();
+ fw = new FileWriter(file);
+ bw = new BufferedWriter(fw);
+ Writer writer = interfaceGenerator.generate(type);
+ bw.write(writer.toString());
+
+ if (bw != null) {
+ bw.close();
}
+ result.add(file);
+ }
+ }
- if (!file.exists()) {
- FileWriter fw = null;
- BufferedWriter bw = null;
-
- file.createNewFile();
- fw = new FileWriter(file);
- bw = new BufferedWriter(fw);
- Writer writer = classGenerator.generate(transferObject);
- bw.write(writer.toString());
-
- if (bw != null) {
- try {
- bw.close();
- } catch (IOException e) {
- // TODO: log?
- }
- }
+ for (GeneratedTransferObject transferObject : genTransferObjects) {
+ String parentPath = generateParentPath(path,
+ transferObject.getPackageName());
+
+ File file = new File(parentPath, transferObject.getName() + ".java");
+ File parent = file.getParentFile();
+ if (!parent.exists()) {
+ parent.mkdirs();
+ }
+
+ if (!file.exists()) {
+ FileWriter fw = null;
+ BufferedWriter bw = null;
+
+ file.createNewFile();
+ fw = new FileWriter(file);
+ bw = new BufferedWriter(fw);
+ Writer writer = classGenerator.generate(transferObject);
+ bw.write(writer.toString());
+
+ if (bw != null) {
+ bw.close();
}
+ result.add(file);
}
- return true;
- } catch (IOException e) {
- // TODO: log?
- return false;
}
+
+ return result;
}
private String generateParentPath(String path, String pkg) {
import static org.junit.Assert.assertTrue;
import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
}
@Test
- public void test() {
+ public void test() throws IOException {
final Set<GeneratedType> types = new HashSet<GeneratedType>();
GeneratedType t1 = createGeneratedType(
"org.opendaylight.controller.gen", "Type1");
new InterfaceGenerator(), types);
generator.generateToFile(PATH);
- // path: test-dir/com/cisco/yang
String[] files = new File(PATH + FS + "org" + FS + "opendaylight" + FS + "controller" + FS + "gen").list();
List<String> filesList = Arrays.asList(files);
package org.opendaylight.controller.maven.sal.api.gen.plugin;
import java.io.File;
-import java.util.Arrays;
+import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
@Override
public Collection<File> generateSources(SchemaContext context,
- File outputBaseDir) {
+ File outputBaseDir) throws IOException {
final BindingGenerator bindingGenerator = new BindingGeneratorImpl();
final List<Type> types = bindingGenerator.generateTypes(context);
typesToGenerate.add((GeneratedType) type);
}
-
+
}
final GeneratorJavaFile generator = new GeneratorJavaFile(typesToGenerate, tosToGenerate);
- generator.generateToFile(outputBaseDir.getAbsolutePath());
-
- return Arrays.asList(outputBaseDir.listFiles());
+ return generator.generateToFile(outputBaseDir.getAbsolutePath());
}
}
package org.opendaylight.controller.yang2sources.plugin;
import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
*/
private void generateSourcesWithOneGenerator(SchemaContext context,
CodeGeneratorArg codeGeneratorCfg) throws ClassNotFoundException,
- InstantiationException, IllegalAccessException {
+ InstantiationException, IllegalAccessException, IOException {
codeGeneratorCfg.check();
package org.opendaylight.controller.yang2sources.spi;
import java.io.File;
+import java.io.IOException;
import java.util.Collection;
import org.opendaylight.controller.yang.model.api.SchemaContext;
/**
* Generate sources from provided {@link SchemaContext}
- *
+ *
* @param context
* parsed from yang files
* @param outputBaseDir
* user
* @return collection of files that were generated from schema context
*/
- Collection<File> generateSources(SchemaContext context, File outputBaseDir);
+ Collection<File> generateSources(SchemaContext context, File outputBaseDir) throws IOException;
}
private final Map<String, DeviationBuilder> addedDeviations = new HashMap<String, DeviationBuilder>();
private final Map<List<String>, TypeDefinitionBuilder> addedTypedefs = new HashMap<List<String>, TypeDefinitionBuilder>();
private final List<ExtensionBuilder> addedExtensions = new ArrayList<ExtensionBuilder>();
+ private final Set<UnknownSchemaNodeBuilder> addedUnknownNodes = new HashSet<UnknownSchemaNodeBuilder>();
private final Map<List<String>, TypeAwareBuilder> dirtyNodes = new HashMap<List<String>, TypeAwareBuilder>();
instance.setNotifications(notifications);
// AUGMENTATIONS
- // instance.setAugmentations(augmentations);
final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
for (AugmentationSchemaBuilder builder : addedAugments) {
augmentations.add(builder.build());
return addedUsesNodes;
}
+ public Set<UnknownSchemaNodeBuilder> getAddedUnknownNodes() {
+ return addedUnknownNodes;
+ }
+
public Set<TypeDefinitionBuilder> getModuleTypedefs() {
Set<TypeDefinitionBuilder> typedefs = new HashSet<TypeDefinitionBuilder>();
for (Map.Entry<List<String>, TypeDefinitionBuilder> entry : addedTypedefs
List<String> pathToCase = new ArrayList<String>(parentPath);
ChoiceCaseBuilder builder = new ChoiceCaseBuilder(caseName);
- final ChoiceBuilder parent = (ChoiceBuilder) moduleNodes
+ final ChildNodeBuilder parent = (ChildNodeBuilder) moduleNodes
.get(pathToCase);
if (parent != null) {
if (parent instanceof AugmentationSchemaBuilder) {
}
pathToCase.add(caseName.getLocalName());
- addedChilds.put(pathToCase, builder);
moduleNodes.put(pathToCase, builder);
return builder;
public void addIdentityrefType(String baseString, List<String> parentPath,
SchemaPath schemaPath) {
+ List<String> pathToIdentityref = new ArrayList<String>(parentPath);
TypeAwareBuilder parent = (TypeAwareBuilder) moduleNodes
- .get(parentPath);
+ .get(pathToIdentityref);
IdentityrefTypeBuilder identityref = new IdentityrefTypeBuilder(
baseString, schemaPath);
parent.setType(identityref);
- dirtyNodes.put(parentPath, parent);
+ dirtyNodes.put(pathToIdentityref, parent);
}
public DeviationBuilder addDeviation(String targetPath,
} else if (parent instanceof SchemaNodeBuilder) {
((SchemaNodeBuilder) parent).addUnknownSchemaNode(builder);
}
+ addedUnknownNodes.add(builder);
return builder;
}
import org.opendaylight.controller.yang.model.parser.builder.api.SchemaNodeBuilder;
public class UnknownSchemaNodeBuilder implements SchemaNodeBuilder {
-
+ private final UnknownSchemaNodeImpl instance;
private final QName qname;
private SchemaPath schemaPath;
- private final UnknownSchemaNodeImpl instance;
private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
+ private QName nodeType;
+ private String nodeParameter;
UnknownSchemaNodeBuilder(final QName qname) {
this.qname = qname;
@Override
public UnknownSchemaNode build() {
instance.setPath(schemaPath);
+ instance.setNodeType(nodeType);
+ instance.setNodeParameter(nodeParameter);
// UNKNOWN NODES
final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
addedUnknownNodes.add(unknownNode);
}
+ public QName getNodeType() {
+ return nodeType;
+ }
+
+ public void setNodeType(final QName nodeType) {
+ this.nodeType = nodeType;
+ }
+
+ public String getNodeParameter() {
+ return nodeParameter;
+ }
+
+ public void setNodeParameter(final String nodeParameter) {
+ this.nodeParameter = nodeParameter;
+ }
+
private static class UnknownSchemaNodeImpl implements UnknownSchemaNode {
private final QName qname;
private SchemaPath path;
private String reference;
private Status status = Status.CURRENT;
private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
+ private QName nodeType;
+ private String nodeParameter;
private UnknownSchemaNodeImpl(final QName qname) {
this.qname = qname;
this.unknownNodes = unknownNodes;
}
}
+
+ @Override
+ public QName getNodeType() {
+ return nodeType;
+ }
+
+ private void setNodeType(final QName nodeType) {
+ this.nodeType = nodeType;
+ }
+
+ @Override
+ public String getNodeParameter() {
+ return nodeParameter;
+ }
+
+ private void setNodeParameter(final String nodeParameter) {
+ this.nodeParameter = nodeParameter;
+ }
}
}
resolveDirtyNodes(modules, builder);
resolveIdentities(modules, builder);
resolveUses(modules, builder);
+ resolveUnknownNodes(modules, builder);
}
/**
return result;
}
+ private void resolveUnknownNodes(
+ final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+ final ModuleBuilder module) {
+ for (UnknownSchemaNodeBuilder usnb : module.getAddedUnknownNodes()) {
+ QName nodeType = usnb.getNodeType();
+ if (nodeType.getNamespace() == null
+ || nodeType.getRevision() == null) {
+ try {
+ ModuleBuilder dependentModule = findDependentModule(
+ modules, module, nodeType.getPrefix());
+ QName newNodeType = new QName(
+ dependentModule.getNamespace(),
+ dependentModule.getRevision(),
+ nodeType.getPrefix(), nodeType.getLocalName());
+ usnb.setNodeType(newNodeType);
+ } catch (YangParseException e) {
+ logger.debug("Failed to find unknown node type: "
+ + nodeType);
+ }
+ }
+ }
+ }
+
/**
* Find dependent module based on given prefix
*
// Unknown types
@Override
public void enterIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
- final String name = stringFromNode(ctx);
+ final String nodeParameter = stringFromNode(ctx);
+ QName nodeType = null;
+
+ final String nodeTypeStr = ctx.getChild(0).getText();
+ final String[] splittedElement = nodeTypeStr.split(":");
+ if (splittedElement.length == 1) {
+ nodeType = new QName(null, null, null, splittedElement[0]);
+ } else {
+ nodeType = new QName(null, null, splittedElement[0],
+ splittedElement[1]);
+ }
QName qname;
- if (name != null) {
- String[] splittedName = name.split(":");
+ if (nodeParameter != null) {
+ String[] splittedName = nodeParameter.split(":");
if (splittedName.length == 2) {
qname = new QName(null, null, splittedName[0], splittedName[1]);
} else {
splittedName[0]);
}
} else {
- qname = new QName(namespace, revision, yangModelPrefix, name);
+ qname = new QName(namespace, revision, yangModelPrefix, nodeParameter);
}
UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(
qname, actualPath);
- updatePath(name);
+ builder.setNodeType(nodeType);
+ builder.setNodeParameter(nodeParameter);
+ updatePath(nodeParameter);
builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
yangModelPrefix));
parseSchemaNodeArgs(ctx, builder);
ContainerSchemaNode network = (ContainerSchemaNode)testModule.getDataChildByName("network");
List<UnknownSchemaNode> unknownNodes = network.getUnknownSchemaNodes();
assertEquals(1, unknownNodes.size());
+ UnknownSchemaNode unknownNode = unknownNodes.get(0);
+ assertNotNull(unknownNode.getNodeType());
+ assertEquals("point", unknownNode.getNodeParameter());
}
@Test
+++ /dev/null
-module abstract-topology {
- yang-version 1;
- namespace "";
- prefix "tp";
-
- import ietf-inet-types {
- prefix "inet";
- revision-date 2010-09-24;
- }
-
- organization "OPEN DAYLIGHT";
- contact "http://www.opendaylight.org/";
-
- description
- "This module contains the definitions of elements that creates network
- topology i.e. definition of network nodes and links. This module is not designed to be used solely for network representation. This module SHOULD be used as base module in defining the network topology.";
-
- revision "2013-02-08" {
- reference "~~~ WILL BE DEFINED LATER";
- }
-
- typedef node-id-ref {
- type leafref {
- path "/tp:topology/tp:network-nodes/tp:network-node/tp:node-id";
- }
- description "This type is used for leafs that reference network node instance.";
- }
-
- typedef link-id-ref {
- type leafref {
- path "/tp:topology/tp:network-links/tp:network-link/tp:link-id";
- }
- description "This type is used for leafs that reference network link instance.";
- }
-
- container topology {
- description "This is the model of abstract topology which contains only Network Nodes and Network Links. Each topology MUST be identified by unique topology-id for reason that the store could contain many topologies.";
-
- leaf topology-id {
- type inet:uri;
- description "It is presumed that datastore will contain many topologies. To distinguish between topologies it is vital to have
- UNIQUE topology identifier.";
- }
-
- container network-nodes {
- list network-node {
- key "node-id";
-
- leaf node-id {
- type inet:uri;
- description "The Topology identifier of network-node.";
- }
-
- container attributes {
- description "Aditional attributes that can Network Node contains.";
- }
- description "The list of network nodes defined for topology.";
- }
- }
-
- container network-links {
- list network-link {
- key "link-id";
-
- leaf link-id {
- type inet:uri;
- description "";
- }
-
- container source-node {
- leaf id {
- type node-id-ref;
- description "Source node identifier.";
- }
- }
-
- container destination-node {
- leaf id {
- type node-id-ref;
- description "Destination node identifier.";
- }
- }
-
- container attributes {
- description "Aditional attributes that can Network Link contains.";
- }
- description "The Network Link which is defined by Local (Source) and Remote (Destination) Network Nodes. Every link MUST be defined either by identifier and
- his local and remote Network Nodes (In real applications it is common that many links are originated from one node and end up in same remote node). To ensure that we would always know to distinguish between links, every link SHOULD have identifier.";
- }
- }
- }
-}
\ No newline at end of file
+++ /dev/null
-module test-model {
- yang-version 1;
- namespace "urn:cisco:params:xml:ns:yang:controller:network";
- prefix "topos";
-
- import ietf-inet-types { prefix "inet"; }
- import iana-if-type {prefix "if-type";}
- import mount {prefix "mnt";}
-
- organization "OPEN DAYLIGHT";
- contact "http://www.opendaylight.org/";
-
- description "module description";
- reference "module reference";
-
- typedef topology-id {
- type inet:uri;
- }
-
- typedef node-id {
- type inet:uri;
- }
-
- typedef link-id {
- type inet:uri;
- }
-
- typedef tp-id {
- type inet:uri;
- description "identifier for termination points on a port";
- }
-
- typedef tp-ref {
- type leafref {
- path "/network/topologies/topology/nodes/node/termination-points/termination-point/tp-id";
- }
- }
- typedef topology-ref {
- type leafref {
- path "/network/topologies/topology/topology-id";
- }
- description "This type is used for leafs that reference topology identifier instance.";
- // currently not used
- }
-
- typedef node-ref {
- type leafref {
- path "/network/topologies/topology/nodes/node/node-id";
- }
- description "This type is used for leafs that reference a node instance.";
- }
-
- typedef link-ref {
- type leafref {
- path "/network/topologies/topology/links/link/link-id";
- }
- description "This type is used for leafs that reference a link instance.";
- // currently not used
- }
-
- typedef network-element-ref {
- type leafref {
- path "/network/network-elements/network-element/element-id";
- }
- }
-
-
- typedef element-id {
- type string;
- }
-
-
- container network {
- description "network-description";
- reference "network-reference";
- status obsolete;
- config true;
- presence "some presence text";
-
- mnt:mountpoint point {
- mnt:target-ref target;
- }
-
- leaf-list locked-node {
- type instance-identifier;
- min-elements 1;
- when "a/b/c";
- description "List of locked nodes in the running datastore";
- }
-
- leaf locked-test {
- mandatory true;
- }
-
- container topologies {
- list topology {
- description "Test description of list 'topology'.";
- key "topology-id";
- leaf topology-id {
- type topology-id;
- description "Test description of leaf 'topology-id'";
- }
-
- container nodes {
- list node {
- description "The list of network nodes defined for topology.";
-
- key "node-id";
- leaf node-id {
- type node-id;
- description "The Topology identifier of network-node.";
- }
-
- leaf supporting-ne {
- type network-element-ref;
- }
-
- container termination-points {
- list termination-point {
- key "tp-id";
- leaf tp-id {
- type tp-id;
- }
- }
- }
- }
- }
-
- container links {
- list link {
- description "Test description of list 'link'.";
- key "link-id";
- leaf link-id {
- type link-id;
- description "";
- }
-
- container source {
- leaf source-node {
- type node-ref;
- description "Source node identifier.";
- }
- leaf source-tp {
- type tp-ref;
- }
- }
-
- container destination {
- leaf dest-node {
- type node-ref;
- description "Destination node identifier.";
- }
- leaf dest-tp {
- type tp-ref;
- }
- }
- }
- }
- }
- }
- }
-
-}
*/
package org.opendaylight.controller.yang.model.api;
+import org.opendaylight.controller.yang.common.QName;
+
public interface UnknownSchemaNode extends SchemaNode {
+ QName getNodeType();
+ String getNodeParameter();
+
}