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 final GroupingDefinition groupingDefinition = (GroupingDefinition) effectiveStatement;
49 groupingsInit.add(groupingDefinition);
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);
57 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
61 this.groupings = ImmutableSet.copyOf(groupingsInit);
62 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
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;
73 public final ContainerSchemaNode getInput() {
78 public final ContainerSchemaNode getOutput() {
83 public final Set<TypeDefinition<?>> getTypeDefinitions() {
84 return typeDefinitions;
88 public final Set<GroupingDefinition> getGroupings() {
93 public final int hashCode() {
94 return Objects.hash(getQName(), getPath());
98 public final boolean equals(final Object obj) {
102 if (obj == null || getClass() != obj.getClass()) {
105 final AbstractEffectiveOperationDefinition<?> other =
106 (AbstractEffectiveOperationDefinition<?>) obj;
107 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
111 public final String toString() {
112 return MoreObjects.toStringHelper(this).add("qname", getQName()).add("path", getPath()).add("input", input)
113 .add("output", output).toString();