52ac098b0d6cfa8b3e9d33cd058c1b0b6d1d5c42
[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 static java.util.Objects.requireNonNull;
11
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.Optional;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
26 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
27 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
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.UsesNode;
34
35 /**
36  * yang-data-util
37  * org.opendaylight.yangtools.yang.data.util
38  * Utility class for taking notification or rpc as ContainerSchemaNode.
39  * @author <a href="mailto:geng.xingyuan@zte.com.cn">Geng Xingyuan</a>
40  */
41 public final class ContainerSchemaNodes {
42
43     private ContainerSchemaNodes() {
44     }
45
46     @Beta
47     public static ContainerSchemaNode forNotification(final NotificationDefinition notification) {
48         return new NotificationContainerSchemaNode(notification);
49     }
50
51     @Beta
52     public static ContainerSchemaNode forRPC(final RpcDefinition rpc) {
53         return new RpcContainerSchemaNode(rpc);
54     }
55
56     private abstract static class AbstractContainerSchemaNode implements ContainerSchemaNode {
57
58         private final SchemaNode schemaNode;
59
60         private AbstractContainerSchemaNode(final SchemaNode schemaNode) {
61             this.schemaNode = schemaNode;
62         }
63
64         @Override
65         public boolean isPresenceContainer() {
66             return false;
67         }
68
69         @Override
70         public Collection<? extends UsesNode> getUses() {
71             return ImmutableSet.of();
72         }
73
74         @Override
75         public boolean isAugmenting() {
76             return false;
77         }
78
79         @Override
80         public boolean isConfiguration() {
81             return false;
82         }
83
84         @Override
85         public QName getQName() {
86             return schemaNode.getQName();
87         }
88
89         @Override
90         @Deprecated
91         public SchemaPath getPath() {
92             return schemaNode.getPath();
93         }
94
95         @Override
96         public Optional<String> getDescription() {
97             return schemaNode.getDescription();
98         }
99
100         @Override
101         public Optional<String> getReference() {
102             return schemaNode.getReference();
103         }
104
105         @Override
106         public Status getStatus() {
107             return schemaNode.getStatus();
108         }
109
110         @Override
111         public Collection<? extends MustDefinition> getMustConstraints() {
112             return ImmutableList.of();
113         }
114
115         @Override
116         public Optional<RevisionAwareXPath> getWhenCondition() {
117             return Optional.empty();
118         }
119     }
120
121     private static final class RpcContainerSchemaNode extends AbstractContainerSchemaNode {
122
123         private final RpcDefinition rpcDefinition;
124
125         private RpcContainerSchemaNode(final RpcDefinition rpcDefinition) {
126             super(rpcDefinition);
127             this.rpcDefinition = rpcDefinition;
128         }
129
130         @Override
131         public Collection<? extends GroupingDefinition> getGroupings() {
132             return rpcDefinition.getGroupings();
133         }
134
135         @Override
136         public Collection<? extends TypeDefinition<?>> getTypeDefinitions() {
137             return rpcDefinition.getTypeDefinitions();
138         }
139
140         @Override
141         public Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
142             return ImmutableSet.of();
143         }
144
145         @Override
146         public Collection<? extends DataSchemaNode> getChildNodes() {
147             final ContainerSchemaNode input = rpcDefinition.getInput();
148             final ContainerSchemaNode output = rpcDefinition.getOutput();
149             if (input == null && output == null) {
150                 return ImmutableList.of();
151             } else if (input != null && output != null) {
152                 return ImmutableList.of(input,output);
153             } else if (input != null) {
154                 return ImmutableList.of(input);
155             } else {
156                 return ImmutableList.of(output);
157             }
158         }
159
160         @Override
161         public Optional<DataSchemaNode> findDataChildByName(final QName name) {
162             switch (name.getLocalName()) {
163                 case "input":
164                     return Optional.of(rpcDefinition.getInput());
165                 case "output":
166                     return Optional.of(rpcDefinition.getOutput());
167                 default:
168                     return Optional.empty();
169             }
170         }
171
172         @Deprecated
173         @Override
174         public boolean isAddedByUses() {
175             return false;
176         }
177
178         @Override
179         public Collection<? extends ActionDefinition> getActions() {
180             return ImmutableSet.of();
181         }
182
183         @Override
184         public Collection<? extends NotificationDefinition> getNotifications() {
185             return ImmutableSet.of();
186         }
187     }
188
189     private static final class NotificationContainerSchemaNode extends AbstractContainerSchemaNode {
190
191         private final NotificationDefinition notification;
192         private final ImmutableMap<QName, ? extends DataSchemaNode> mapNodes;
193
194         private NotificationContainerSchemaNode(final NotificationDefinition notification) {
195             super(notification);
196             this.notification = notification;
197             mapNodes = Maps.uniqueIndex(notification.getChildNodes(), DataSchemaNode::getQName);
198         }
199
200         @Override
201         public Collection<? extends NotificationDefinition> getNotifications() {
202             return ImmutableSet.of(notification);
203         }
204
205         @Override
206         public Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
207             return notification.getAvailableAugmentations();
208         }
209
210         @Override
211         public Collection<? extends TypeDefinition<?>> getTypeDefinitions() {
212             return notification.getTypeDefinitions();
213         }
214
215         @Override
216         public Collection<? extends DataSchemaNode> getChildNodes() {
217             return notification.getChildNodes();
218         }
219
220         @Override
221         public Collection<? extends GroupingDefinition> getGroupings() {
222             return notification.getGroupings();
223         }
224
225         @Override
226         public Optional<DataSchemaNode> findDataChildByName(final QName name) {
227             return Optional.ofNullable(mapNodes.get(requireNonNull(name)));
228         }
229
230         @Override
231         @Deprecated
232         public boolean isAddedByUses() {
233             //FIXME: reference to https://bugs.opendaylight.org/show_bug.cgi?id=6897
234             return false;
235         }
236
237         @Override
238         public Collection<? extends ActionDefinition> getActions() {
239             return ImmutableSet.of();
240         }
241     }
242 }