0b62cf316040a655d706ba5eb99868f0a0dd76ff
[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         @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<AugmentationSchema> 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 DataSchemaNode getDataChildByName(QName name) {
168             switch (name.getLocalName()) {
169                 case "input":
170                     return rpcDefinition.getInput();
171                 case "output":
172                     return rpcDefinition.getOutput();
173                 default:
174                     return null;
175             }
176         }
177
178         @Override
179         public boolean isAddedByUses() {
180             return false;
181         }
182     }
183
184     private static final class NotificationContainerSchemaNode extends AbstractContainerSchemaNode {
185
186         private final NotificationDefinition notification;
187         private final Map<QName, DataSchemaNode> mapNodes;
188
189         private NotificationContainerSchemaNode(final NotificationDefinition notification) {
190             super(notification);
191             this.notification = notification;
192             mapNodes = Maps.uniqueIndex(notification.getChildNodes(), DataSchemaNode::getQName);
193         }
194
195         @Override
196         public Set<AugmentationSchema> getAvailableAugmentations() {
197             return notification.getAvailableAugmentations();
198         }
199
200         @Override
201         public Set<TypeDefinition<?>> getTypeDefinitions() {
202             return notification.getTypeDefinitions();
203         }
204
205         @Override
206         public Collection<DataSchemaNode> getChildNodes() {
207             return notification.getChildNodes();
208         }
209
210         @Override
211         public Set<GroupingDefinition> getGroupings() {
212             return notification.getGroupings();
213         }
214
215         @Override
216         public DataSchemaNode getDataChildByName(QName name) {
217             return mapNodes.get(name);
218         }
219
220         @Override
221         public boolean isAddedByUses() {
222             //FIXME: reference to https://bugs.opendaylight.org/show_bug.cgi?id=6897
223             return false;
224         }
225     }
226 }