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.ImmutableSet;
15 import com.google.common.collect.Maps;
16 import java.util.Collection;
17 import java.util.List;
19 import java.util.Optional;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
30 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
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() {
89 public ConstraintDefinition getConstraints() {
90 return EmptyConstraintDefinition.create(false);
95 public QName getQName() {
96 return schemaNode.getQName();
101 public SchemaPath getPath() {
102 return schemaNode.getPath();
107 public String getDescription() {
108 return schemaNode.getDescription();
113 public String getReference() {
114 return schemaNode.getReference();
119 public Status getStatus() {
120 return schemaNode.getStatus();
125 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
126 return ImmutableList.of();
130 private static final class RpcContainerSchemaNode extends AbstractContainerSchemaNode {
132 private final RpcDefinition rpcDefinition;
134 private RpcContainerSchemaNode(final RpcDefinition rpcDefinition) {
135 super(rpcDefinition);
136 this.rpcDefinition = rpcDefinition;
140 public Set<GroupingDefinition> getGroupings() {
141 return rpcDefinition.getGroupings();
145 public Set<TypeDefinition<?>> getTypeDefinitions() {
146 return rpcDefinition.getTypeDefinitions();
150 public Set<AugmentationSchemaNode> getAvailableAugmentations() {
151 return ImmutableSet.of();
155 public Collection<DataSchemaNode> getChildNodes() {
156 final ContainerSchemaNode input = rpcDefinition.getInput();
157 final ContainerSchemaNode output = rpcDefinition.getOutput();
158 if (input == null && output == null) {
159 return ImmutableList.of();
160 } else if (input != null && output != null) {
161 return ImmutableList.of(input,output);
162 } else if (input != null) {
163 return ImmutableList.of(input);
165 return ImmutableList.of(output);
170 public Optional<DataSchemaNode> findDataChildByName(final QName name) {
171 switch (name.getLocalName()) {
173 return Optional.of(rpcDefinition.getInput());
175 return Optional.of(rpcDefinition.getOutput());
177 return Optional.empty();
182 public boolean isAddedByUses() {
187 public Set<ActionDefinition> getActions() {
188 return ImmutableSet.of();
192 private static final class NotificationContainerSchemaNode extends AbstractContainerSchemaNode {
194 private final NotificationDefinition notification;
195 private final Map<QName, DataSchemaNode> mapNodes;
197 private NotificationContainerSchemaNode(final NotificationDefinition notification) {
199 this.notification = notification;
200 mapNodes = Maps.uniqueIndex(notification.getChildNodes(), DataSchemaNode::getQName);
204 public Set<NotificationDefinition> getNotifications() {
205 return ImmutableSet.of(notification);
209 public Set<AugmentationSchemaNode> getAvailableAugmentations() {
210 return notification.getAvailableAugmentations();
214 public Set<TypeDefinition<?>> getTypeDefinitions() {
215 return notification.getTypeDefinitions();
219 public Collection<DataSchemaNode> getChildNodes() {
220 return notification.getChildNodes();
224 public Set<GroupingDefinition> getGroupings() {
225 return notification.getGroupings();
229 public Optional<DataSchemaNode> findDataChildByName(final QName name) {
230 return Optional.ofNullable(mapNodes.get(requireNonNull(name)));
234 public boolean isAddedByUses() {
235 //FIXME: reference to https://bugs.opendaylight.org/show_bug.cgi?id=6897
240 public Set<ActionDefinition> getActions() {
241 return ImmutableSet.of();