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 com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.Maps;
15 import java.util.Collection;
16 import java.util.List;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
23 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
27 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
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.UnknownSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.UsesNode;
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 ContainerSchemaNode forNotification(final NotificationDefinition notification) {
50 return new NotificationContainerSchemaNode(notification);
54 public static ContainerSchemaNode forRPC(final RpcDefinition rpc) {
55 return new RpcContainerSchemaNode(rpc);
58 private abstract static class AbstractContainerSchemaNode implements ContainerSchemaNode {
60 private final SchemaNode schemaNode;
62 private AbstractContainerSchemaNode(final SchemaNode schemaNode) {
63 this.schemaNode = schemaNode;
67 public boolean isPresenceContainer() {
72 public Set<UsesNode> getUses() {
73 return ImmutableSet.of();
77 public boolean isAugmenting() {
82 public boolean isConfiguration() {
87 public ConstraintDefinition getConstraints() {
88 return EmptyConstraintDefinition.create(false);
93 public QName getQName() {
94 return schemaNode.getQName();
99 public SchemaPath getPath() {
100 return schemaNode.getPath();
105 public String getDescription() {
106 return schemaNode.getDescription();
111 public String getReference() {
112 return schemaNode.getReference();
117 public Status getStatus() {
118 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<AugmentationSchema> 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 DataSchemaNode getDataChildByName(QName name) {
168 switch (name.getLocalName()) {
170 return rpcDefinition.getInput();
172 return rpcDefinition.getOutput();
179 public boolean isAddedByUses() {
184 private static final class NotificationContainerSchemaNode extends AbstractContainerSchemaNode {
186 private final NotificationDefinition notification;
187 private final Map<QName, DataSchemaNode> mapNodes;
189 private NotificationContainerSchemaNode(final NotificationDefinition notification) {
191 this.notification = notification;
192 mapNodes = Maps.uniqueIndex(notification.getChildNodes(), DataSchemaNode::getQName);
196 public Set<AugmentationSchema> getAvailableAugmentations() {
197 return notification.getAvailableAugmentations();
201 public Set<TypeDefinition<?>> getTypeDefinitions() {
202 return notification.getTypeDefinitions();
206 public Collection<DataSchemaNode> getChildNodes() {
207 return notification.getChildNodes();
211 public Set<GroupingDefinition> getGroupings() {
212 return notification.getGroupings();
216 public DataSchemaNode getDataChildByName(QName name) {
217 return mapNodes.get(name);
221 public boolean isAddedByUses() {
222 //FIXME: reference to https://bugs.opendaylight.org/show_bug.cgi?id=6897