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