Added support to generate interfaces from Choices and Cases.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / FeatureBuilder.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.parser.builder.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.controller.yang.common.QName;
15 import org.opendaylight.controller.yang.model.api.FeatureDefinition;
16 import org.opendaylight.controller.yang.model.api.SchemaPath;
17 import org.opendaylight.controller.yang.model.api.Status;
18 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.controller.yang.parser.builder.api.AbstractSchemaNodeBuilder;
20
21 public final class FeatureBuilder extends AbstractSchemaNodeBuilder {
22     private boolean isBuilt;
23     private final FeatureDefinitionImpl instance;
24
25     FeatureBuilder(final QName qname, final int line) {
26         super(qname, line);
27         instance = new FeatureDefinitionImpl(qname);
28     }
29
30     @Override
31     public FeatureDefinitionImpl build() {
32         if (!isBuilt) {
33             instance.setPath(path);
34             instance.setDescription(description);
35             instance.setReference(reference);
36             instance.setStatus(status);
37
38             // UNKNOWN NODES
39             final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
40             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
41                 unknownNodes.add(b.build());
42             }
43             instance.setUnknownSchemaNodes(unknownNodes);
44
45             isBuilt = true;
46         }
47         return instance;
48     }
49
50     private final class FeatureDefinitionImpl implements FeatureDefinition {
51         private final QName qname;
52         private SchemaPath path;
53         private String description;
54         private String reference;
55         private Status status = Status.CURRENT;
56         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
57
58         private FeatureDefinitionImpl(final QName qname) {
59             this.qname = qname;
60         }
61
62         @Override
63         public QName getQName() {
64             return qname;
65         }
66
67         @Override
68         public SchemaPath getPath() {
69             return path;
70         }
71
72         private void setPath(final SchemaPath path) {
73             this.path = path;
74         }
75
76         @Override
77         public String getDescription() {
78             return description;
79         }
80
81         private void setDescription(final String description) {
82             this.description = description;
83         }
84
85         @Override
86         public String getReference() {
87             return reference;
88         }
89
90         private void setReference(final String reference) {
91             this.reference = reference;
92         }
93
94         @Override
95         public Status getStatus() {
96             return status;
97         }
98
99         private void setStatus(Status status) {
100             if (status != null) {
101                 this.status = status;
102             }
103         }
104
105         @Override
106         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
107             return unknownNodes;
108         }
109
110         private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
111             if (unknownNodes != null) {
112                 this.unknownNodes = unknownNodes;
113             }
114         }
115
116         @Override
117         public int hashCode() {
118             final int prime = 31;
119             int result = 1;
120             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
121             result = prime * result + ((path == null) ? 0 : path.hashCode());
122             return result;
123         }
124
125         @Override
126         public boolean equals(Object obj) {
127             if (this == obj) {
128                 return true;
129             }
130             if (obj == null) {
131                 return false;
132             }
133             if (getClass() != obj.getClass()) {
134                 return false;
135             }
136             FeatureDefinitionImpl other = (FeatureDefinitionImpl) obj;
137             if (qname == null) {
138                 if (other.qname != null) {
139                     return false;
140                 }
141             } else if (!qname.equals(other.qname)) {
142                 return false;
143             }
144             if (path == null) {
145                 if (other.path != null) {
146                     return false;
147                 }
148             } else if (!path.equals(other.path)) {
149                 return false;
150             }
151             return true;
152         }
153
154         @Override
155         public String toString() {
156             StringBuilder sb = new StringBuilder(FeatureDefinitionImpl.class.getSimpleName());
157             sb.append("[name=" + qname + "]");
158             return sb.toString();
159         }
160     }
161
162 }