c8241d5e753f0954e5f6a82da450b6cd10692ec5
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / ContainerSchemaNodes.java
1 /*
2  * Copyright (c) 2016 ZTE, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.util;
9
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;
14
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
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;
35
36
37 /**
38  * yang-data-util
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>
42  */
43 public final class ContainerSchemaNodes {
44
45     private ContainerSchemaNodes() {
46     }
47
48     @Beta
49     public static ContainerSchemaNode forNotification(final NotificationDefinition notification) {
50         return new NotificationContainerSchemaNode(notification);
51     }
52
53     @Beta
54     public static ContainerSchemaNode forRPC(final RpcDefinition rpc) {
55         return new RpcContainerSchemaNode(rpc);
56     }
57
58     private abstract static class AbstractContainerSchemaNode implements ContainerSchemaNode {
59
60         private final SchemaNode schemaNode;
61
62         private AbstractContainerSchemaNode(final SchemaNode schemaNode) {
63             this.schemaNode = schemaNode;
64         }
65
66         @Override
67         public boolean isPresenceContainer() {
68             return false;
69         }
70
71         @Override
72         public Set<UsesNode> getUses() {
73             return ImmutableSet.of();
74         }
75
76         @Override
77         public boolean isAugmenting() {
78             return false;
79         }
80
81         @Override
82         public boolean isConfiguration() {
83             return false;
84         }
85
86         @Override
87         public ConstraintDefinition getConstraints() {
88             return EmptyConstraintDefinition.create(false);
89         }
90
91         @Nonnull
92         @Override
93         public QName getQName() {
94             return schemaNode.getQName();
95         }
96
97         @Nonnull
98         @Override
99         public SchemaPath getPath() {
100             return schemaNode.getPath();
101         }
102
103         @Nullable
104         @Override
105         public String getDescription() {
106             return schemaNode.getDescription();
107         }
108
109         @Nullable
110         @Override
111         public String getReference() {
112             return schemaNode.getReference();
113         }
114
115         @Nonnull
116         @Override
117         public Status getStatus() {
118             return schemaNode.getStatus();
119         }
120
121         @Nonnull
122         @Override
123         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
124             return ImmutableList.of();
125         }
126     }
127
128     private static final class RpcContainerSchemaNode extends AbstractContainerSchemaNode {
129
130         private final RpcDefinition rpcDefinition;
131
132         private RpcContainerSchemaNode(final RpcDefinition rpcDefinition) {
133             super(rpcDefinition);
134             this.rpcDefinition = rpcDefinition;
135         }
136
137         @Override
138         public Set<GroupingDefinition> getGroupings() {
139             return rpcDefinition.getGroupings();
140         }
141
142         @Override
143         public Set<TypeDefinition<?>> getTypeDefinitions() {
144             return rpcDefinition.getTypeDefinitions();
145         }
146
147         @Override
148         public Set<AugmentationSchema> getAvailableAugmentations() {
149             return ImmutableSet.of();
150         }
151
152         @Override
153         public Collection<DataSchemaNode> getChildNodes() {
154             final ContainerSchemaNode input = rpcDefinition.getInput();
155             final ContainerSchemaNode output = rpcDefinition.getOutput();
156             if (input == null && output == null) {
157                 return ImmutableList.of();
158             } else if (input != null && output != null) {
159                 return ImmutableList.of(input,output);
160             } else if (input != null) {
161                 return ImmutableList.of(input);
162             } else {
163                 return ImmutableList.of(output);
164             }
165         }
166
167         @Override
168         public DataSchemaNode getDataChildByName(QName name) {
169             switch (name.getLocalName()) {
170                 case "input":
171                     return rpcDefinition.getInput();
172                 case "output":
173                     return rpcDefinition.getOutput();
174                 default:
175                     return null;
176             }
177         }
178
179         @Override
180         public boolean isAddedByUses() {
181             return false;
182         }
183     }
184
185     private static final class NotificationContainerSchemaNode extends AbstractContainerSchemaNode {
186
187         private final NotificationDefinition notification;
188         private final Map<QName, DataSchemaNode> mapNodes;
189
190         private NotificationContainerSchemaNode(final NotificationDefinition notification) {
191             super(notification);
192             this.notification = notification;
193             mapNodes = Maps.uniqueIndex(notification.getChildNodes(), DataSchemaNode::getQName);
194         }
195
196         @Override
197         public Set<AugmentationSchema> getAvailableAugmentations() {
198             return notification.getAvailableAugmentations();
199         }
200
201         @Override
202         public Set<TypeDefinition<?>> getTypeDefinitions() {
203             return notification.getTypeDefinitions();
204         }
205
206         @Override
207         public Collection<DataSchemaNode> getChildNodes() {
208             return notification.getChildNodes();
209         }
210
211         @Override
212         public Set<GroupingDefinition> getGroupings() {
213             return notification.getGroupings();
214         }
215
216         @Override
217         public DataSchemaNode getDataChildByName(QName name) {
218             return mapNodes.get(name);
219         }
220
221         @Override
222         public boolean isAddedByUses() {
223             //FIXME: reference to https://bugs.opendaylight.org/show_bug.cgi?id=6897
224             return false;
225         }
226     }
227 }