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.List;
19 import java.util.Optional;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
28 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
29 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
30 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34 import org.opendaylight.yangtools.yang.model.api.Status;
35 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.UsesNode;
41 * org.opendaylight.yangtools.yang.data.util
42 * Utility class for taking notification or rpc as ContainerSchemaNode.
43 * @author <a href="mailto:geng.xingyuan@zte.com.cn">Geng Xingyuan</a>
45 public final class ContainerSchemaNodes {
47 private ContainerSchemaNodes() {
51 public static ContainerSchemaNode forNotification(final NotificationDefinition notification) {
52 return new NotificationContainerSchemaNode(notification);
56 public static ContainerSchemaNode forRPC(final RpcDefinition rpc) {
57 return new RpcContainerSchemaNode(rpc);
60 private abstract static class AbstractContainerSchemaNode implements ContainerSchemaNode {
62 private final SchemaNode schemaNode;
64 private AbstractContainerSchemaNode(final SchemaNode schemaNode) {
65 this.schemaNode = schemaNode;
69 public boolean isPresenceContainer() {
74 public Set<UsesNode> getUses() {
75 return ImmutableSet.of();
79 public boolean isAugmenting() {
84 public boolean isConfiguration() {
90 public QName getQName() {
91 return schemaNode.getQName();
96 public SchemaPath getPath() {
97 return schemaNode.getPath();
101 public Optional<String> getDescription() {
102 return schemaNode.getDescription();
106 public Optional<String> getReference() {
107 return schemaNode.getReference();
112 public Status getStatus() {
113 return schemaNode.getStatus();
118 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
119 return ImmutableList.of();
123 public Collection<MustDefinition> getMustConstraints() {
124 return ImmutableList.of();
128 public Optional<RevisionAwareXPath> getWhenCondition() {
129 return Optional.empty();
133 private static final class RpcContainerSchemaNode extends AbstractContainerSchemaNode {
135 private final RpcDefinition rpcDefinition;
137 private RpcContainerSchemaNode(final RpcDefinition rpcDefinition) {
138 super(rpcDefinition);
139 this.rpcDefinition = rpcDefinition;
143 public Set<GroupingDefinition> getGroupings() {
144 return rpcDefinition.getGroupings();
148 public Set<TypeDefinition<?>> getTypeDefinitions() {
149 return rpcDefinition.getTypeDefinitions();
153 public Set<AugmentationSchemaNode> getAvailableAugmentations() {
154 return ImmutableSet.of();
158 public Collection<DataSchemaNode> getChildNodes() {
159 final ContainerSchemaNode input = rpcDefinition.getInput();
160 final ContainerSchemaNode output = rpcDefinition.getOutput();
161 if (input == null && output == null) {
162 return ImmutableList.of();
163 } else if (input != null && output != null) {
164 return ImmutableList.of(input,output);
165 } else if (input != null) {
166 return ImmutableList.of(input);
168 return ImmutableList.of(output);
173 public Optional<DataSchemaNode> findDataChildByName(final QName name) {
174 switch (name.getLocalName()) {
176 return Optional.of(rpcDefinition.getInput());
178 return Optional.of(rpcDefinition.getOutput());
180 return Optional.empty();
186 public boolean isAddedByUses() {
191 public Set<ActionDefinition> getActions() {
192 return ImmutableSet.of();
196 public Set<NotificationDefinition> getNotifications() {
197 return ImmutableSet.of();
201 private static final class NotificationContainerSchemaNode extends AbstractContainerSchemaNode {
203 private final NotificationDefinition notification;
204 private final ImmutableMap<QName, DataSchemaNode> mapNodes;
206 private NotificationContainerSchemaNode(final NotificationDefinition notification) {
208 this.notification = notification;
209 mapNodes = Maps.uniqueIndex(notification.getChildNodes(), DataSchemaNode::getQName);
213 public Set<NotificationDefinition> getNotifications() {
214 return ImmutableSet.of(notification);
218 public Set<AugmentationSchemaNode> getAvailableAugmentations() {
219 return notification.getAvailableAugmentations();
223 public Set<TypeDefinition<?>> getTypeDefinitions() {
224 return notification.getTypeDefinitions();
228 public Collection<DataSchemaNode> getChildNodes() {
229 return notification.getChildNodes();
233 public Set<GroupingDefinition> getGroupings() {
234 return notification.getGroupings();
238 public Optional<DataSchemaNode> findDataChildByName(final QName name) {
239 return Optional.ofNullable(mapNodes.get(requireNonNull(name)));
244 public boolean isAddedByUses() {
245 //FIXME: reference to https://bugs.opendaylight.org/show_bug.cgi?id=6897
250 public Set<ActionDefinition> getActions() {
251 return ImmutableSet.of();