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 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.ConstraintDefinition;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
29 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
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.SchemaPath;
33 import org.opendaylight.yangtools.yang.model.api.Status;
34 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.UsesNode;
40 * org.opendaylight.yangtools.yang.data.util
41 * Utility class for taking notification or rpc as ContainerSchemaNode.
42 * @author <a href="mailto:geng.xingyuan@zte.com.cn">Geng Xingyuan</a>
44 public final class ContainerSchemaNodes {
46 private ContainerSchemaNodes() {
50 public static ContainerSchemaNode forNotification(final NotificationDefinition notification) {
51 return new NotificationContainerSchemaNode(notification);
55 public static ContainerSchemaNode forRPC(final RpcDefinition rpc) {
56 return new RpcContainerSchemaNode(rpc);
59 private abstract static class AbstractContainerSchemaNode implements ContainerSchemaNode {
61 private final SchemaNode schemaNode;
63 private AbstractContainerSchemaNode(final SchemaNode schemaNode) {
64 this.schemaNode = schemaNode;
68 public boolean isPresenceContainer() {
73 public Set<UsesNode> getUses() {
74 return ImmutableSet.of();
78 public boolean isAugmenting() {
83 public boolean isConfiguration() {
88 public ConstraintDefinition getConstraints() {
89 return EmptyConstraintDefinition.create(false);
94 public QName getQName() {
95 return schemaNode.getQName();
100 public SchemaPath getPath() {
101 return schemaNode.getPath();
105 public Optional<String> getDescription() {
106 return schemaNode.getDescription();
110 public Optional<String> getReference() {
111 return schemaNode.getReference();
116 public Status getStatus() {
117 return schemaNode.getStatus();
122 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
123 return ImmutableList.of();
127 private static final class RpcContainerSchemaNode extends AbstractContainerSchemaNode {
129 private final RpcDefinition rpcDefinition;
131 private RpcContainerSchemaNode(final RpcDefinition rpcDefinition) {
132 super(rpcDefinition);
133 this.rpcDefinition = rpcDefinition;
137 public Set<GroupingDefinition> getGroupings() {
138 return rpcDefinition.getGroupings();
142 public Set<TypeDefinition<?>> getTypeDefinitions() {
143 return rpcDefinition.getTypeDefinitions();
147 public Set<AugmentationSchemaNode> getAvailableAugmentations() {
148 return ImmutableSet.of();
152 public Collection<DataSchemaNode> getChildNodes() {
153 final ContainerSchemaNode input = rpcDefinition.getInput();
154 final ContainerSchemaNode output = rpcDefinition.getOutput();
155 if (input == null && output == null) {
156 return ImmutableList.of();
157 } else if (input != null && output != null) {
158 return ImmutableList.of(input,output);
159 } else if (input != null) {
160 return ImmutableList.of(input);
162 return ImmutableList.of(output);
167 public Optional<DataSchemaNode> findDataChildByName(final QName name) {
168 switch (name.getLocalName()) {
170 return Optional.of(rpcDefinition.getInput());
172 return Optional.of(rpcDefinition.getOutput());
174 return Optional.empty();
179 public boolean isAddedByUses() {
184 public Set<ActionDefinition> getActions() {
185 return ImmutableSet.of();
189 public Set<NotificationDefinition> getNotifications() {
190 return ImmutableSet.of();
194 private static final class NotificationContainerSchemaNode extends AbstractContainerSchemaNode {
196 private final NotificationDefinition notification;
197 private final Map<QName, DataSchemaNode> mapNodes;
199 private NotificationContainerSchemaNode(final NotificationDefinition notification) {
201 this.notification = notification;
202 mapNodes = Maps.uniqueIndex(notification.getChildNodes(), DataSchemaNode::getQName);
206 public Set<NotificationDefinition> getNotifications() {
207 return ImmutableSet.of(notification);
211 public Set<AugmentationSchemaNode> getAvailableAugmentations() {
212 return notification.getAvailableAugmentations();
216 public Set<TypeDefinition<?>> getTypeDefinitions() {
217 return notification.getTypeDefinitions();
221 public Collection<DataSchemaNode> getChildNodes() {
222 return notification.getChildNodes();
226 public Set<GroupingDefinition> getGroupings() {
227 return notification.getGroupings();
231 public Optional<DataSchemaNode> findDataChildByName(final QName name) {
232 return Optional.ofNullable(mapNodes.get(requireNonNull(name)));
236 public boolean isAddedByUses() {
237 //FIXME: reference to https://bugs.opendaylight.org/show_bug.cgi?id=6897
242 public Set<ActionDefinition> getActions() {
243 return ImmutableSet.of();