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.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
26 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
27 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
28 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
29 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.opendaylight.yangtools.yang.model.api.Status;
32 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.UsesNode;
37 * org.opendaylight.yangtools.yang.data.util
38 * Utility class for taking notification or rpc as ContainerSchemaNode.
39 * @author <a href="mailto:geng.xingyuan@zte.com.cn">Geng Xingyuan</a>
41 public final class ContainerSchemaNodes {
43 private ContainerSchemaNodes() {
47 public static ContainerSchemaNode forNotification(final NotificationDefinition notification) {
48 return new NotificationContainerSchemaNode(notification);
52 public static ContainerSchemaNode forRPC(final RpcDefinition rpc) {
53 return new RpcContainerSchemaNode(rpc);
56 private abstract static class AbstractContainerSchemaNode implements ContainerSchemaNode {
58 private final SchemaNode schemaNode;
60 private AbstractContainerSchemaNode(final SchemaNode schemaNode) {
61 this.schemaNode = schemaNode;
65 public boolean isPresenceContainer() {
70 public Collection<? extends UsesNode> getUses() {
71 return ImmutableSet.of();
75 public boolean isAugmenting() {
80 public boolean isConfiguration() {
85 public QName getQName() {
86 return schemaNode.getQName();
91 public SchemaPath getPath() {
92 return schemaNode.getPath();
96 public Optional<String> getDescription() {
97 return schemaNode.getDescription();
101 public Optional<String> getReference() {
102 return schemaNode.getReference();
106 public Status getStatus() {
107 return schemaNode.getStatus();
111 public Collection<? extends MustDefinition> getMustConstraints() {
112 return ImmutableList.of();
116 public Optional<RevisionAwareXPath> getWhenCondition() {
117 return Optional.empty();
121 private static final class RpcContainerSchemaNode extends AbstractContainerSchemaNode {
123 private final RpcDefinition rpcDefinition;
125 private RpcContainerSchemaNode(final RpcDefinition rpcDefinition) {
126 super(rpcDefinition);
127 this.rpcDefinition = rpcDefinition;
131 public Collection<? extends GroupingDefinition> getGroupings() {
132 return rpcDefinition.getGroupings();
136 public Collection<? extends TypeDefinition<?>> getTypeDefinitions() {
137 return rpcDefinition.getTypeDefinitions();
141 public Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
142 return ImmutableSet.of();
146 public Collection<? extends DataSchemaNode> getChildNodes() {
147 final ContainerSchemaNode input = rpcDefinition.getInput();
148 final ContainerSchemaNode output = rpcDefinition.getOutput();
149 if (input == null && output == null) {
150 return ImmutableList.of();
151 } else if (input != null && output != null) {
152 return ImmutableList.of(input,output);
153 } else if (input != null) {
154 return ImmutableList.of(input);
156 return ImmutableList.of(output);
161 public Optional<DataSchemaNode> findDataChildByName(final QName name) {
162 switch (name.getLocalName()) {
164 return Optional.of(rpcDefinition.getInput());
166 return Optional.of(rpcDefinition.getOutput());
168 return Optional.empty();
174 public boolean isAddedByUses() {
179 public Collection<? extends ActionDefinition> getActions() {
180 return ImmutableSet.of();
184 public Collection<? extends NotificationDefinition> getNotifications() {
185 return ImmutableSet.of();
189 private static final class NotificationContainerSchemaNode extends AbstractContainerSchemaNode {
191 private final NotificationDefinition notification;
192 private final ImmutableMap<QName, ? extends DataSchemaNode> mapNodes;
194 private NotificationContainerSchemaNode(final NotificationDefinition notification) {
196 this.notification = notification;
197 mapNodes = Maps.uniqueIndex(notification.getChildNodes(), DataSchemaNode::getQName);
201 public Collection<? extends NotificationDefinition> getNotifications() {
202 return ImmutableSet.of(notification);
206 public Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
207 return notification.getAvailableAugmentations();
211 public Collection<? extends TypeDefinition<?>> getTypeDefinitions() {
212 return notification.getTypeDefinitions();
216 public Collection<? extends DataSchemaNode> getChildNodes() {
217 return notification.getChildNodes();
221 public Collection<? extends GroupingDefinition> getGroupings() {
222 return notification.getGroupings();
226 public Optional<DataSchemaNode> findDataChildByName(final QName name) {
227 return Optional.ofNullable(mapNodes.get(requireNonNull(name)));
232 public boolean isAddedByUses() {
233 //FIXME: reference to https://bugs.opendaylight.org/show_bug.cgi?id=6897
238 public Collection<? extends ActionDefinition> getActions() {
239 return ImmutableSet.of();