Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / AbstractEffectiveOperationDefinition.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.rfc7950.stmt;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.HashSet;
15 import java.util.LinkedHashSet;
16 import java.util.Objects;
17 import java.util.Set;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.OperationDefinition;
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29
30 @Beta
31 public abstract class AbstractEffectiveOperationDefinition<D extends DeclaredStatement<QName>>
32         extends AbstractEffectiveSchemaNode<D> implements OperationDefinition {
33     private final ImmutableSet<TypeDefinition<?>> typeDefinitions;
34     private final ImmutableSet<GroupingDefinition> groupings;
35     private final ContainerSchemaNode input;
36     private final ContainerSchemaNode output;
37
38     protected AbstractEffectiveOperationDefinition(final StmtContext<QName, D, ?> ctx) {
39         super(ctx);
40         input = findAsContainer(this, InputEffectiveStatement.class);
41         output = findAsContainer(this, OutputEffectiveStatement.class);
42
43         // initSubstatements
44         final Set<GroupingDefinition> groupingsInit = new HashSet<>();
45         final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
46         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
47             if (effectiveStatement instanceof GroupingDefinition) {
48                 final GroupingDefinition groupingDefinition = (GroupingDefinition) effectiveStatement;
49                 groupingsInit.add(groupingDefinition);
50             }
51             if (effectiveStatement instanceof TypedefEffectiveStatement) {
52                 final TypedefEffectiveStatement typeDef = (TypedefEffectiveStatement) effectiveStatement;
53                 final TypeDefinition<?> type = typeDef.getTypeDefinition();
54                 if (!mutableTypeDefinitions.contains(type)) {
55                     mutableTypeDefinitions.add(type);
56                 } else {
57                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
58                 }
59             }
60         }
61         this.groupings = ImmutableSet.copyOf(groupingsInit);
62         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
63     }
64
65     private static ContainerSchemaNode findAsContainer(final EffectiveStatement<?, ?> parent,
66             final Class<? extends EffectiveStatement<QName, ?>> statementType) {
67         final EffectiveStatement<?, ?> statement = parent.findFirstEffectiveSubstatement(statementType).get();
68         Verify.verify(statement instanceof ContainerSchemaNode, "Child statement %s is not a ContainerSchemaNode");
69         return (ContainerSchemaNode) statement;
70     }
71
72     @Override
73     public final ContainerSchemaNode getInput() {
74         return input;
75     }
76
77     @Override
78     public final ContainerSchemaNode getOutput() {
79         return output;
80     }
81
82     @Override
83     public final Set<TypeDefinition<?>> getTypeDefinitions() {
84         return typeDefinitions;
85     }
86
87     @Override
88     public final Set<GroupingDefinition> getGroupings() {
89         return groupings;
90     }
91
92     @Override
93     public final int hashCode() {
94         return Objects.hash(getQName(), getPath());
95     }
96
97     @Override
98     public final boolean equals(final Object obj) {
99         if (this == obj) {
100             return true;
101         }
102         if (obj == null || getClass() != obj.getClass()) {
103             return false;
104         }
105         final AbstractEffectiveOperationDefinition<?> other =
106                 (AbstractEffectiveOperationDefinition<?>) obj;
107         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
108     }
109
110     @Override
111     public final String toString() {
112         return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).add("input", input)
113                 .add("output", output).toString();
114     }
115 }