fe0b24f890711969f4a6c446597919101caa2581
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / controller / sal / java / api / generator / InterfaceGenerator.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.sal.java.api.generator;
9
10 import static org.opendaylight.controller.sal.java.api.generator.Constants.*;
11
12 import java.io.IOException;
13 import java.io.StringWriter;
14 import java.io.Writer;
15 import java.util.List;
16 import java.util.Map;
17
18 import org.opendaylight.controller.sal.binding.model.api.*;
19
20 public final class InterfaceGenerator implements CodeGenerator {
21
22     private Map<String, String> imports;
23
24     private String generateEnums(List<Enumeration> enums) {
25         String result = "";
26         if (enums != null) {
27             EnumGenerator enumGenerator = new EnumGenerator();
28             for (Enumeration en : enums) {
29                 try {
30                     result = result + (enumGenerator.generateInnerEnumeration(en, TAB).toString() + NL);
31                 } catch (IOException e) {
32                     e.printStackTrace();
33                 }
34             }
35         }
36         return result;
37     }
38
39     private String generateConstants(List<Constant> constants, String pkgName) {
40         String result = "";
41         if (constants != null) {
42             for (Constant c : constants) {
43                 result = result + GeneratorUtil.createConstant(c, TAB, imports, pkgName) + NL;
44             }
45             result.concat(NL);
46         }
47         return result;
48     }
49
50     public String generateMethods(List<MethodSignature> methods, String pkgName) {
51         String result = "";
52
53         if (methods != null) {
54             for (MethodSignature m : methods) {
55                 result = result + GeneratorUtil.createMethodDeclaration(m, TAB, imports, pkgName) + NL;
56             }
57             result = result + NL;
58         }
59         return result;
60     }
61
62     public Writer generate(Type type) throws IOException {
63         Writer writer = new StringWriter();
64         if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
65             final GeneratedType genType = (GeneratedType) type;
66             imports = GeneratorUtil.createImports(genType);
67
68             final String currentPkg = genType.getPackageName();
69             final List<Constant> constants = genType.getConstantDefinitions();
70             final List<MethodSignature> methods = genType.getMethodDefinitions();
71             final List<Enumeration> enums = genType.getEnumerations();
72
73             writer.write(GeneratorUtil.createPackageDeclaration(genType.getPackageName()));
74             writer.write(NL);
75
76             final List<String> importLines = GeneratorUtil.createImportLines(imports);
77             for (String line : importLines) {
78                 writer.write(line + NL);
79             }
80             writer.write(NL);
81             writer.write(GeneratorUtil.createIfcDeclaration(genType, "", imports));
82             writer.write(NL);
83
84             writer.write(generateEnums(enums));
85             writer.write(generateConstants(constants, currentPkg));
86             writer.write(generateMethods(methods, currentPkg));
87
88             writer.write(RCB);
89         }
90         return writer;
91     }
92 }