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