2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.rfc7950.stmt;
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;
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;
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;
38 protected AbstractEffectiveOperationDefinition(final StmtContext<QName, D, ?> ctx) {
40 input = findAsContainer(this, InputEffectiveStatement.class);
41 output = findAsContainer(this, OutputEffectiveStatement.class);
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 && !groupingsInit.add((GroupingDefinition) effectiveStatement)) {
49 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
51 if (effectiveStatement instanceof TypedefEffectiveStatement) {
52 final TypeDefinition<?> type = ((TypedefEffectiveStatement) effectiveStatement).getTypeDefinition();
53 if (!mutableTypeDefinitions.add(type)) {
54 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
58 this.groupings = ImmutableSet.copyOf(groupingsInit);
59 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
62 private static ContainerSchemaNode findAsContainer(final EffectiveStatement<?, ?> parent,
63 final Class<? extends EffectiveStatement<QName, ?>> statementType) {
64 final EffectiveStatement<?, ?> statement = parent.findFirstEffectiveSubstatement(statementType).get();
65 Verify.verify(statement instanceof ContainerSchemaNode, "Child statement %s is not a ContainerSchemaNode");
66 return (ContainerSchemaNode) statement;
70 public final ContainerSchemaNode getInput() {
75 public final ContainerSchemaNode getOutput() {
80 public final Set<TypeDefinition<?>> getTypeDefinitions() {
81 return typeDefinitions;
85 public final Set<GroupingDefinition> getGroupings() {
90 public final int hashCode() {
91 return Objects.hash(getQName(), getPath());
95 public final boolean equals(final Object obj) {
99 if (obj == null || getClass() != obj.getClass()) {
102 final AbstractEffectiveOperationDefinition<?> other =
103 (AbstractEffectiveOperationDefinition<?>) obj;
104 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
108 public final String toString() {
109 return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).add("input", input)
110 .add("output", output).toString();