Added generation of Transfer Objects from Type Definitions.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / code-generator-demo / src / main / java / org / opendaylight / controller / yang / Demo.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.yang;
9
10 import java.io.File;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
17 import org.opendaylight.controller.sal.binding.generator.impl.BindingGeneratorImpl;
18 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
19 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
20 import org.opendaylight.controller.sal.binding.model.api.Type;
21 import org.opendaylight.controller.sal.java.api.generator.GeneratorJavaFile;
22 import org.opendaylight.controller.yang.model.api.Module;
23 import org.opendaylight.controller.yang.model.api.SchemaContext;
24 import org.opendaylight.controller.yang.model.parser.impl.YangModelParserImpl;
25
26 public class Demo {
27     private static final String ERR_MSG = "2 parameters expected: 1. -f=<path-to-input-folder>, 2. -o=<output-folder>";
28
29     public static void main(String[] args) throws Exception {
30         if (args.length != 2) {
31             System.err.println(ERR_MSG);
32             return;
33         }
34
35         String inputFilesDir = null;
36         String outputFilesDir = null;
37         if (args[0].startsWith("-f=")) {
38             inputFilesDir = args[0].substring(3);
39         } else {
40             System.err.println("Missing input-folder declaration (-f=)");
41         }
42
43         if (args[1].startsWith("-o=")) {
44             outputFilesDir = args[1].substring(3);
45         } else {
46             System.err.println("Missing output-folder declaration (-o=)");
47         }
48
49         File resourceDir = new File(inputFilesDir);
50         if (!resourceDir.exists()) {
51             throw new IllegalArgumentException(
52                     "Specified input-folder does not exists: "
53                             + resourceDir.getAbsolutePath());
54         }
55
56         String[] dirList = resourceDir.list();
57         List<File> inputFiles = new ArrayList<File>();
58         for (String fileName : dirList) {
59             inputFiles.add(new File(resourceDir, fileName));
60         }
61
62         final YangModelParserImpl parser = new YangModelParserImpl();
63         final BindingGenerator bindingGenerator = new BindingGeneratorImpl();
64         final Set<Module> modulesToBuild = parser.parseYangModels(inputFiles);
65
66         final SchemaContext context = parser
67                 .resolveSchemaContext(modulesToBuild);
68         final List<Type> types = bindingGenerator.generateTypes(context);
69         final Set<GeneratedType> typesToGenerate = new HashSet<GeneratedType>();
70         final Set<GeneratedTransferObject> tosToGenerate = new HashSet<GeneratedTransferObject>();
71         for (Type type : types) {
72             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
73                 typesToGenerate.add((GeneratedType) type);
74             }
75
76             if (type instanceof GeneratedTransferObject) {
77                 tosToGenerate.add((GeneratedTransferObject) type);
78             }
79         }
80
81         final GeneratorJavaFile generator = new GeneratorJavaFile(typesToGenerate, tosToGenerate);
82         
83         generator.generateToFile(outputFilesDir);
84         System.out.println("Modules built: " + modulesToBuild.size());
85     }
86 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.