2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type;
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.effective.EffectiveStatementBase;
30 public class EnumSpecificationEffectiveStatementImpl extends
31 EffectiveStatementBase<String, EnumSpecification> implements EnumTypeDefinition, TypeEffectiveStatement<EnumSpecification> {
33 private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, "enumeration");
35 private static final String DESCRIPTION = "The enumeration built-in type represents values from a set of assigned names.";
36 private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.6";
37 private static final String UNITS = "";
39 private final SchemaPath path;
40 private final EnumPair defaultEnum;
41 private final List<EnumPair> enums;
42 private EnumerationType enumerationTypeInstance = null;
44 public EnumSpecificationEffectiveStatementImpl(final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
47 List<EnumPair> enumsInit = new ArrayList<>();
49 path = ctx.getParentContext().getSchemaPath().get().createChild(QNAME);
51 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
52 if (effectiveStatement instanceof EnumPair) {
53 enumsInit.add((EnumPair) effectiveStatement);
57 // FIXME: get from parentContext
59 enums = ImmutableList.copyOf(enumsInit);
63 public List<EnumPair> getValues() {
68 public EnumTypeDefinition getBaseType() {
73 public String getUnits() {
78 public Object getDefaultValue() {
83 public QName getQName() {
88 public SchemaPath getPath() {
93 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
94 return Collections.emptyList();
98 public String getDescription() {
103 public String getReference() {
108 public Status getStatus() {
109 return Status.CURRENT;
113 public int hashCode() {
114 final int prime = 31;
116 result = prime * result + Objects.hashCode(defaultEnum);
117 result = prime * result + Objects.hashCode(enums);
118 result = prime * result + QNAME.hashCode();
119 result = prime * result + Objects.hashCode(path);
124 public boolean equals(final Object obj) {
131 if (getClass() != obj.getClass()) {
134 EnumSpecificationEffectiveStatementImpl other = (EnumSpecificationEffectiveStatementImpl) obj;
135 if (!Objects.equals(defaultEnum, other.defaultEnum)) {
138 if (!Objects.equals(enums, other.enums)) {
141 if (!Objects.equals(path, other.path)) {
148 public String toString() {
149 StringBuilder builder = new StringBuilder();
150 builder.append(EnumSpecificationEffectiveStatementImpl.class.getSimpleName());
151 builder.append(" [name=");
152 builder.append(QNAME);
153 builder.append(", path=");
154 builder.append(path);
155 builder.append(", description=");
156 builder.append(DESCRIPTION);
157 builder.append(", reference=");
158 builder.append(REFERENCE);
159 builder.append(", defaultEnum=");
160 builder.append(defaultEnum);
161 builder.append(", enums=");
162 builder.append(enums);
163 builder.append(", units=");
164 builder.append(UNITS);
166 return builder.toString();
170 public TypeDefinition<?> getTypeDefinition() {
172 if (enumerationTypeInstance !=null) {
173 return enumerationTypeInstance;
176 // FIXME: set defaultValue as parameter
177 enumerationTypeInstance = EnumerationType.create(path, enums, Optional.<EnumPair>absent());
179 return enumerationTypeInstance;