326d5a44b693a8442faf5e9d2bcc900ddb977799
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / EnumSpecificationEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 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.yangtools.yang.parser.stmt.rfc6020.effective.type;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableList;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Objects;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.YangConstants;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.model.api.Status;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
25 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase;
30
31 public class EnumSpecificationEffectiveStatementImpl extends
32         EffectiveStatementBase<String, EnumSpecification> implements EnumTypeDefinition, TypeEffectiveStatement<EnumSpecification> {
33
34     private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, "enumeration");
35
36     private static final String DESCRIPTION = "The enumeration built-in type represents values from a set of assigned names.";
37     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.6";
38     private static final String UNITS = "";
39
40     private final SchemaPath path;
41     private final EnumPair defaultEnum;
42     private final List<EnumPair> enums;
43     private EnumerationType enumerationTypeInstance = null;
44
45     public EnumSpecificationEffectiveStatementImpl(final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
46         super(ctx);
47
48         List<EnumPair> enumsInit = new ArrayList<>();
49
50         path = Utils.getSchemaPath(ctx.getParentContext()).createChild(QNAME);
51
52         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
53             if (effectiveStatement instanceof EnumPair) {
54                 enumsInit.add((EnumPair) effectiveStatement);
55             }
56         }
57
58         // FIXME: get from parentContext
59         defaultEnum = null;
60         enums = ImmutableList.copyOf(enumsInit);
61     }
62
63     @Override
64     public List<EnumPair> getValues() {
65         return enums;
66     }
67
68     @Override
69     public EnumTypeDefinition getBaseType() {
70         return null;
71     }
72
73     @Override
74     public String getUnits() {
75         return UNITS;
76     }
77
78     @Override
79     public Object getDefaultValue() {
80         return defaultEnum;
81     }
82
83     @Override
84     public QName getQName() {
85         return QNAME;
86     }
87
88     @Override
89     public SchemaPath getPath() {
90         return path;
91     }
92
93     @Override
94     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
95         return Collections.emptyList();
96     }
97
98     @Override
99     public String getDescription() {
100         return DESCRIPTION;
101     }
102
103     @Override
104     public String getReference() {
105         return REFERENCE;
106     }
107
108     @Override
109     public Status getStatus() {
110         return Status.CURRENT;
111     }
112
113     @Override
114     public int hashCode() {
115         final int prime = 31;
116         int result = 1;
117         result = prime * result + Objects.hashCode(defaultEnum);
118         result = prime * result + Objects.hashCode(enums);
119         result = prime * result + QNAME.hashCode();
120         result = prime * result + Objects.hashCode(path);
121         return result;
122     }
123
124     @Override
125     public boolean equals(final Object obj) {
126         if (this == obj) {
127             return true;
128         }
129         if (obj == null) {
130             return false;
131         }
132         if (getClass() != obj.getClass()) {
133             return false;
134         }
135         EnumSpecificationEffectiveStatementImpl other = (EnumSpecificationEffectiveStatementImpl) obj;
136         if (!Objects.equals(defaultEnum, other.defaultEnum)) {
137             return false;
138         }
139         if (!Objects.equals(enums, other.enums)) {
140             return false;
141         }
142         if (!Objects.equals(path, other.path)) {
143             return false;
144         }
145         return true;
146     }
147
148     @Override
149     public String toString() {
150         StringBuilder builder = new StringBuilder();
151         builder.append(EnumSpecificationEffectiveStatementImpl.class.getSimpleName());
152         builder.append(" [name=");
153         builder.append(QNAME);
154         builder.append(", path=");
155         builder.append(path);
156         builder.append(", description=");
157         builder.append(DESCRIPTION);
158         builder.append(", reference=");
159         builder.append(REFERENCE);
160         builder.append(", defaultEnum=");
161         builder.append(defaultEnum);
162         builder.append(", enums=");
163         builder.append(enums);
164         builder.append(", units=");
165         builder.append(UNITS);
166         builder.append("]");
167         return builder.toString();
168     }
169
170     @Override
171     public TypeDefinition<?> getTypeDefinition() {
172
173         if (enumerationTypeInstance !=null) {
174             return enumerationTypeInstance;
175         }
176
177         // FIXME: set defaultValue as parameter
178         enumerationTypeInstance = EnumerationType.create(path, enums, Optional.<EnumPair>absent());
179
180         return enumerationTypeInstance;
181     }
182 }