Added generation of Transfer Objects from Type Definitions.
[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.NL;
11 import static org.opendaylight.controller.sal.java.api.generator.Constants.RCB;
12 import static org.opendaylight.controller.sal.java.api.generator.Constants.TAB;
13
14 import java.io.IOException;
15 import java.io.StringWriter;
16 import java.io.Writer;
17 import java.util.List;
18
19 import org.opendaylight.controller.sal.binding.model.api.CodeGenerator;
20 import org.opendaylight.controller.sal.binding.model.api.Constant;
21 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
22 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
23 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
24 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
25 import org.opendaylight.controller.sal.binding.model.api.Type;
26
27 public class InterfaceGenerator implements CodeGenerator {
28
29     public Writer generate(Type type) throws IOException {
30         Writer writer = new StringWriter();
31         if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
32             GeneratedType genType = (GeneratedType) type;
33
34             final List<Constant> constants = genType.getConstantDefinitions();
35             final List<MethodSignature> methods = genType
36                     .getMethodDefinitions();
37             final List<Enumeration> enums = genType.getEnumDefintions();
38
39             writer.write(GeneratorUtil.createIfcDeclarationWithPkgName(genType,
40                     ""));
41             writer.write(NL);
42
43             if (constants != null) {
44                 for (Constant c : constants) {
45                     writer.write(GeneratorUtil.createConstant(c, TAB) + NL);
46                 }
47                 writer.write(NL);
48             }
49
50             if (methods != null) {
51                 for (MethodSignature m : methods) {
52                     writer.write(GeneratorUtil.createMethodDeclaration(m, TAB)
53                             + NL);
54                 }
55                 writer.write(NL);
56             }
57
58             if (enums != null) {
59                 for (Enumeration e : enums) {
60                     writer.write(GeneratorUtil.createEnum(e, TAB) + NL);
61                 }
62                 writer.write(NL);
63             }
64
65             writer.write(RCB);
66         }
67         return writer;
68     }
69
70 }

©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.