2 * Copyright (c) 2016 ZTE, 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.data.util;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.collect.Maps;
17 import java.util.Collection;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
26 import org.opendaylight.yangtools.yang.model.api.InputSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
28 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
29 import org.opendaylight.yangtools.yang.model.api.OutputSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
31 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Status;
33 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.UsesNode;
35 import org.opendaylight.yangtools.yang.xpath.api.YangXPathExpression.QualifiedBound;
39 * org.opendaylight.yangtools.yang.data.util
40 * Utility class for taking notification or rpc as ContainerSchemaNode.
41 * @author <a href="mailto:geng.xingyuan@zte.com.cn">Geng Xingyuan</a>
43 public final class ContainerSchemaNodes {
45 private ContainerSchemaNodes() {
49 public static ContainerLike forNotification(final NotificationDefinition notification) {
50 return new NotificationContainerSchemaNode(notification);
54 public static ContainerLike forRPC(final RpcDefinition rpc) {
55 return new RpcContainerSchemaNode(rpc);
58 private abstract static class AbstractContainerSchemaNode implements ContainerLike {
59 private final SchemaNode schemaNode;
61 private AbstractContainerSchemaNode(final SchemaNode schemaNode) {
62 this.schemaNode = schemaNode;
66 public Collection<? extends UsesNode> getUses() {
67 return ImmutableSet.of();
72 public boolean isAugmenting() {
77 public Optional<Boolean> effectiveConfig() {
78 return Optional.empty();
82 public QName getQName() {
83 return schemaNode.getQName();
87 public Optional<String> getDescription() {
88 return schemaNode.getDescription();
92 public Optional<String> getReference() {
93 return schemaNode.getReference();
97 public Status getStatus() {
98 return schemaNode.getStatus();
102 public Collection<? extends @NonNull MustDefinition> getMustConstraints() {
103 return ImmutableList.of();
107 public Optional<? extends QualifiedBound> getWhenCondition() {
108 return Optional.empty();
112 private static final class RpcContainerSchemaNode extends AbstractContainerSchemaNode {
114 private final RpcDefinition rpcDefinition;
116 private RpcContainerSchemaNode(final RpcDefinition rpcDefinition) {
117 super(rpcDefinition);
118 this.rpcDefinition = rpcDefinition;
122 public Collection<? extends GroupingDefinition> getGroupings() {
123 return rpcDefinition.getGroupings();
127 public Collection<? extends TypeDefinition<?>> getTypeDefinitions() {
128 return rpcDefinition.getTypeDefinitions();
132 public Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
133 return ImmutableSet.of();
137 public Collection<? extends DataSchemaNode> getChildNodes() {
138 final InputSchemaNode input = rpcDefinition.getInput();
139 final OutputSchemaNode output = rpcDefinition.getOutput();
140 if (input == null && output == null) {
141 return ImmutableList.of();
142 } else if (input != null && output != null) {
143 return ImmutableList.of(input,output);
144 } else if (input != null) {
145 return ImmutableList.of(input);
147 return ImmutableList.of(output);
152 public DataSchemaNode dataChildByName(final QName name) {
153 switch (name.getLocalName()) {
155 return rpcDefinition.getInput();
157 return rpcDefinition.getOutput();
164 @Deprecated(forRemoval = true)
165 public boolean isAddedByUses() {
170 public Collection<? extends ActionDefinition> getActions() {
171 return ImmutableSet.of();
175 public Collection<? extends NotificationDefinition> getNotifications() {
176 return ImmutableSet.of();
180 private static final class NotificationContainerSchemaNode extends AbstractContainerSchemaNode {
182 private final NotificationDefinition notification;
183 private final ImmutableMap<QName, ? extends DataSchemaNode> mapNodes;
185 private NotificationContainerSchemaNode(final NotificationDefinition notification) {
187 this.notification = notification;
188 mapNodes = Maps.uniqueIndex(notification.getChildNodes(), DataSchemaNode::getQName);
192 public Collection<? extends NotificationDefinition> getNotifications() {
193 return ImmutableSet.of(notification);
197 public Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
198 return notification.getAvailableAugmentations();
202 public Collection<? extends TypeDefinition<?>> getTypeDefinitions() {
203 return notification.getTypeDefinitions();
207 public Collection<? extends DataSchemaNode> getChildNodes() {
208 return notification.getChildNodes();
212 public Collection<? extends GroupingDefinition> getGroupings() {
213 return notification.getGroupings();
217 public DataSchemaNode dataChildByName(final QName name) {
218 return mapNodes.get(requireNonNull(name));
222 @Deprecated(forRemoval = true)
223 public boolean isAddedByUses() {
224 //FIXME: reference to https://bugs.opendaylight.org/show_bug.cgi?id=6897
229 public Collection<? extends ActionDefinition> getActions() {
230 return ImmutableSet.of();