Merge "Added topologyNorthbound integration test removed /r Replaced some Assert...
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / controller / sal / java / api / generator / ClassCodeGenerator.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.binding.generator.util.TypeConstants;
19 import org.opendaylight.controller.sal.binding.model.api.*;
20
21 public final class ClassCodeGenerator implements CodeGenerator {
22
23     private Map<String, String> imports;
24
25     @Override
26     public Writer generate(Type type) throws IOException {
27         final Writer writer = new StringWriter();
28
29         if (type instanceof GeneratedTransferObject) {
30             GeneratedTransferObject genTO = (GeneratedTransferObject) type;
31             imports = GeneratorUtil.createImports(genTO);
32
33             final String currentPkg = genTO.getPackageName();
34             final List<GeneratedProperty> fields = genTO.getProperties();
35             final List<Enumeration> enums = genTO.getEnumerations();
36             final List<Constant> consts = genTO.getConstantDefinitions();
37
38             writer.write(GeneratorUtil.createPackageDeclaration(currentPkg));
39             writer.write(NL);
40
41             List<String> importLines = GeneratorUtil.createImportLines(imports);
42             for (String line : importLines) {
43                 writer.write(line + NL);
44             }
45             writer.write(NL);
46
47             writer.write(GeneratorUtil.createClassDeclaration(genTO, "", imports, genTO.isAbstract()));
48             writer.write(NL);
49             writer.write(NL);
50
51             if (consts != null) {
52                 for (Constant con : consts) {
53                     writer.write(GeneratorUtil.createConstant(con, TAB, imports, currentPkg));
54                     writer.write(NL);
55                 }
56             }
57
58             if (enums != null) {
59                 EnumGenerator enumGenerator = new EnumGenerator();
60                 for (Enumeration e : enums) {
61                     writer.write(enumGenerator.generateInnerEnumeration(e, TAB).toString());
62                     writer.write(NL);
63                 }
64             }
65
66             boolean memberPatternListCodeRequired = false;
67             memberPatternListCodeRequired = (GeneratorUtil.isConstantInTO(TypeConstants.PATTERN_CONSTANT_NAME, genTO));
68             if (fields != null || memberPatternListCodeRequired) {
69                 if (fields != null) {
70                     for (GeneratedProperty field : fields) {
71                         writer.write(GeneratorUtil.createField(field, TAB, imports, currentPkg) + NL);
72                     }
73                 }
74                 if (memberPatternListCodeRequired) {
75                     writer.write(TAB + PRIVATE + GAP + "List<Pattern>" + GAP + MEMBER_PATTERN_LIST + GAP + ASSIGN + GAP
76                             + "new ArrayList<Pattern>()" + GAP + SC + NL);
77
78                 }
79                 writer.write(NL);
80                 writer.write(GeneratorUtil.createConstructor(genTO, TAB, imports, genTO.isAbstract()) + NL);
81                 writer.write(NL);
82                 if (fields != null) {
83                     for (GeneratedProperty field : fields) {
84                         writer.write(GeneratorUtil.createGetter(field, TAB, imports, currentPkg) + NL);
85                         if (!field.isReadOnly()) {
86                             writer.write(GeneratorUtil.createSetter(field, TAB, imports, currentPkg) + NL);
87                         }
88                     }
89                     writer.write(NL);
90
91                     if (!genTO.getHashCodeIdentifiers().isEmpty()) {
92                         writer.write(GeneratorUtil.createHashCode(genTO.getHashCodeIdentifiers(), TAB) + NL);
93                     }
94
95                     if (!genTO.getEqualsIdentifiers().isEmpty()) {
96                         writer.write(GeneratorUtil.createEquals(genTO, genTO.getEqualsIdentifiers(), TAB) + NL);
97                     }
98
99                     if (!genTO.getToStringIdentifiers().isEmpty()) {
100                         writer.write(GeneratorUtil.createToString(genTO, genTO.getToStringIdentifiers(), TAB) + NL);
101                     }
102
103                     writer.write(RCB);
104                 }
105             }
106         }
107         return writer;
108     }
109 }