2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.binding.data.codec.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import org.opendaylight.yangtools.util.ClassLoaderUtils;
16 import org.opendaylight.yangtools.yang.binding.BindingMapping;
17 import org.opendaylight.yangtools.yang.binding.ChildOf;
18 import org.opendaylight.yangtools.yang.binding.DataContainer;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.DataRoot;
21 import org.opendaylight.yangtools.yang.binding.Notification;
22 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
32 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
36 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
38 final class SchemaRootCodecContext<D extends DataObject> extends DataContainerCodecContext<D,SchemaContext> {
40 private final LoadingCache<Class<?>, DataContainerCodecContext<?,?>> childrenByClass = CacheBuilder.newBuilder()
41 .build(new CacheLoader<Class<?>, DataContainerCodecContext<?,?>>() {
43 public DataContainerCodecContext<?,?> load(final Class<?> key) {
44 return createDataTreeChildContext(key);
49 private final LoadingCache<Class<?>, ContainerNodeCodecContext<?>> rpcDataByClass = CacheBuilder.newBuilder().build(
50 new CacheLoader<Class<?>, ContainerNodeCodecContext<?>>() {
52 public ContainerNodeCodecContext<?> load(final Class<?> key) {
53 return createRpcDataContext(key);
57 private final LoadingCache<Class<?>, NotificationCodecContext<?>> notificationsByClass = CacheBuilder.newBuilder()
58 .build(new CacheLoader<Class<?>, NotificationCodecContext<?>>() {
60 public NotificationCodecContext<?> load(final Class<?> key) {
61 return createNotificationDataContext(key);
65 private final LoadingCache<QName, DataContainerCodecContext<?,?>> childrenByQName = CacheBuilder.newBuilder().build(
66 new CacheLoader<QName, DataContainerCodecContext<?,?>>() {
67 @SuppressWarnings("unchecked")
69 public DataContainerCodecContext<?,?> load(final QName qname) {
70 final DataSchemaNode childSchema = schema().getDataChildByName(qname);
71 Preconditions.checkArgument(childSchema != null, "Argument %s is not valid child of %s", qname,
74 if (childSchema instanceof DataNodeContainer || childSchema instanceof ChoiceSchemaNode) {
75 @SuppressWarnings("rawtypes")
76 final Class childCls = factory().getRuntimeContext().getClassForSchema(childSchema);
77 return streamChild(childCls);
79 throw new UnsupportedOperationException("Unsupported child type " + childSchema.getClass());
84 private final LoadingCache<SchemaPath, ContainerNodeCodecContext<?>> rpcDataByPath = CacheBuilder.newBuilder().build(
85 new CacheLoader<SchemaPath, ContainerNodeCodecContext<?>>() {
87 @SuppressWarnings({ "rawtypes", "unchecked" })
89 public ContainerNodeCodecContext load(final SchemaPath key) {
90 final ContainerSchemaNode schema = SchemaContextUtil.getRpcDataSchema(schema(), key);
91 final Class cls = factory().getRuntimeContext().getClassForSchema(schema);
96 private final LoadingCache<SchemaPath, NotificationCodecContext<?>> notificationsByPath = CacheBuilder.newBuilder()
97 .build(new CacheLoader<SchemaPath, NotificationCodecContext<?>>() {
99 @SuppressWarnings({ "rawtypes", "unchecked" })
101 public NotificationCodecContext load(final SchemaPath key) throws Exception {
102 final NotificationDefinition schema = SchemaContextUtil.getNotificationSchema(schema(), key);
103 final Class clz = factory().getRuntimeContext().getClassForSchema(schema);
104 return getNotification(clz);
108 private SchemaRootCodecContext(final DataContainerCodecPrototype<SchemaContext> dataPrototype) {
109 super(dataPrototype);
113 * Creates RootNode from supplied CodecContextFactory.
116 * CodecContextFactory
119 static SchemaRootCodecContext<?> create(final CodecContextFactory factory) {
120 final DataContainerCodecPrototype<SchemaContext> prototype = DataContainerCodecPrototype.rootPrototype(factory);
121 return new SchemaRootCodecContext<>(prototype);
125 @SuppressWarnings("unchecked")
127 public <DV extends DataObject> DataContainerCodecContext<DV, ?> streamChild(Class<DV> childClass)
128 throws IllegalArgumentException {
129 /* FIXME: This is still not solved for RPCs
130 * TODO: Probably performance wise RPC, Data and Notification loading cache
131 * should be merge for performance resons. Needs microbenchmark to
132 * determine which is faster (keeping them separate or in same cache).
134 if (Notification.class.isAssignableFrom(childClass)) {
135 return (DataContainerCodecContext<DV, ?>) getNotification((Class<? extends Notification>)childClass);
137 return (DataContainerCodecContext<DV, ?>) childrenByClass.getUnchecked(childClass);
141 public <E extends DataObject> Optional<DataContainerCodecContext<E,?>> possibleStreamChild(final Class<E> childClass) {
142 throw new UnsupportedOperationException("Not supported");
146 public DataContainerCodecContext<?,?> yangPathArgumentChild(final PathArgument arg) {
147 return childrenByQName.getUnchecked(arg.getNodeType());
151 public D deserialize(final NormalizedNode<?, ?> normalizedNode) {
152 throw new UnsupportedOperationException(
153 "Could not create Binding data representation for root");
157 ContainerNodeCodecContext<?> getRpc(final Class<? extends DataContainer> rpcInputOrOutput) {
158 return rpcDataByClass.getUnchecked(rpcInputOrOutput);
161 NotificationCodecContext<?> getNotification(final Class<? extends Notification> notification) {
162 return notificationsByClass.getUnchecked(notification);
165 NotificationCodecContext<?> getNotification(final SchemaPath notification) {
166 return notificationsByPath.getUnchecked(notification);
169 ContainerNodeCodecContext<?> getRpc(final SchemaPath notification) {
170 return rpcDataByPath.getUnchecked(notification);
173 private DataContainerCodecContext<?,?> createDataTreeChildContext(final Class<?> key) {
174 final Class<Object> parent = ClassLoaderUtils.findFirstGenericArgument(key, ChildOf.class);
175 Preconditions.checkArgument(DataRoot.class.isAssignableFrom(parent));
176 final QName qname = BindingReflections.findQName(key);
177 final DataSchemaNode childSchema = schema().getDataChildByName(qname);
178 return DataContainerCodecPrototype.from(key, childSchema, factory()).get();
181 private ContainerNodeCodecContext<?> createRpcDataContext(final Class<?> key) {
182 Preconditions.checkArgument(DataContainer.class.isAssignableFrom(key));
183 final QName qname = BindingReflections.findQName(key);
184 final QNameModule module = qname.getModule();
185 RpcDefinition rpc = null;
186 for (final RpcDefinition potential : schema().getOperations()) {
187 final QName potentialQName = potential.getQName();
190 * Check if rpc and class represents data from same module and then
191 * checks if rpc local name produces same class name as class name
192 * appended with Input/Output based on QName associated with bidning
195 * FIXME: Rework this to have more precise logic regarding Binding
198 if (module.equals(potentialQName.getModule())
199 && key.getSimpleName().equals(
200 BindingMapping.getClassName(potentialQName) + BindingMapping.getClassName(qname))) {
205 Preconditions.checkArgument(rpc != null, "Supplied class %s is not valid RPC class.", key);
206 final ContainerSchemaNode schema = SchemaNodeUtils.getRpcDataSchema(rpc, qname);
207 Preconditions.checkArgument(schema != null, "Schema for %s does not define input / output.", rpc.getQName());
208 return (ContainerNodeCodecContext<?>) DataContainerCodecPrototype.from(key, schema, factory()).get();
211 private NotificationCodecContext<?> createNotificationDataContext(final Class<?> notificationType) {
212 Preconditions.checkArgument(Notification.class.isAssignableFrom(notificationType));
213 Preconditions.checkArgument(notificationType.isInterface(), "Supplied class must be interface.");
214 final QName qname = BindingReflections.findQName(notificationType);
216 * FIXME: After Lithium cleanup of yang-model-api, use direct call on schema context
217 * to retrieve notification via index.
219 final NotificationDefinition schema = SchemaContextUtil.getNotificationSchema(schema(),
220 SchemaPath.create(true, qname));
221 Preconditions.checkArgument(schema != null, "Supplied %s is not valid notification", notificationType);
223 return new NotificationCodecContext<>(notificationType, schema, factory());
227 protected Object deserializeObject(NormalizedNode<?, ?> normalizedNode) {
228 throw new UnsupportedOperationException("Unable to deserialize root");