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