14afc58e6c991b7893e48794084df1fee38e1aa7
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingToNormalizedNodeCodec.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, 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.mdsal.binding.dom.adapter;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.annotations.Beta;
15 import com.google.common.cache.CacheBuilder;
16 import com.google.common.cache.CacheLoader;
17 import com.google.common.cache.LoadingCache;
18 import com.google.common.collect.ImmutableBiMap;
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.collect.Iterators;
21 import java.lang.reflect.Method;
22 import java.time.Instant;
23 import java.util.AbstractMap.SimpleEntry;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.Map.Entry;
27 import java.util.Optional;
28 import java.util.Set;
29 import java.util.concurrent.TimeUnit;
30 import java.util.stream.Collectors;
31 import javax.annotation.PreDestroy;
32 import javax.inject.Inject;
33 import javax.inject.Singleton;
34 import org.eclipse.jdt.annotation.NonNull;
35 import org.opendaylight.binding.runtime.api.BindingRuntimeContext;
36 import org.opendaylight.binding.runtime.api.BindingRuntimeGenerator;
37 import org.opendaylight.binding.runtime.api.ClassLoadingStrategy;
38 import org.opendaylight.binding.runtime.api.DefaultBindingRuntimeContext;
39 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
40 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
41 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
42 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
43 import org.opendaylight.mdsal.binding.dom.codec.api.BindingLazyContainerNode;
44 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
45 import org.opendaylight.mdsal.binding.dom.codec.api.MissingSchemaException;
46 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
47 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
48 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
49 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
50 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
51 import org.opendaylight.yangtools.concepts.ListenerRegistration;
52 import org.opendaylight.yangtools.yang.binding.Action;
53 import org.opendaylight.yangtools.yang.binding.DataContainer;
54 import org.opendaylight.yangtools.yang.binding.DataObject;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
57 import org.opendaylight.yangtools.yang.binding.Notification;
58 import org.opendaylight.yangtools.yang.binding.RpcInput;
59 import org.opendaylight.yangtools.yang.binding.RpcOutput;
60 import org.opendaylight.yangtools.yang.binding.RpcService;
61 import org.opendaylight.yangtools.yang.common.QNameModule;
62 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
63 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
64 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
66 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
67 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
68 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
69 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
70 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
71 import org.opendaylight.yangtools.yang.model.api.Module;
72 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
73 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
74 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
75 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78
79 /**
80  * A combinations of {@link BindingCodecTreeFactory} and {@link BindingNormalizedNodeSerializer}, with internal
81  * caching of instance identifiers.
82  *
83  * <p>
84  * NOTE: this class is non-final to allow controller adapter migration without duplicated code.
85  */
86 @Singleton
87 public class BindingToNormalizedNodeCodec implements BindingNormalizedNodeSerializer, EffectiveModelContextListener,
88         AutoCloseable {
89
90     private static final long WAIT_DURATION_SEC = 5;
91     private static final Logger LOG = LoggerFactory.getLogger(BindingToNormalizedNodeCodec.class);
92
93     private final LoadingCache<InstanceIdentifier<?>, YangInstanceIdentifier> iiCache = CacheBuilder.newBuilder()
94             .softValues().build(new CacheLoader<InstanceIdentifier<?>, YangInstanceIdentifier>() {
95                 @Override
96                 public YangInstanceIdentifier load(final InstanceIdentifier<?> key) {
97                     return toYangInstanceIdentifierBlocking(key);
98                 }
99             });
100     private final BindingNormalizedNodeCodecRegistry codecRegistry;
101     private final ClassLoadingStrategy classLoadingStrategy;
102     private final BindingRuntimeGenerator generator;
103     private final FutureSchema futureSchema;
104
105     private ListenerRegistration<?> listenerRegistration;
106
107     @Inject
108     public BindingToNormalizedNodeCodec(final BindingRuntimeGenerator generator,
109             final ClassLoadingStrategy classLoadingStrategy, final BindingNormalizedNodeCodecRegistry codecRegistry) {
110         this(generator, classLoadingStrategy, codecRegistry, false);
111     }
112
113     @Beta
114     public BindingToNormalizedNodeCodec(final BindingRuntimeContext runtimeContext) {
115         generator = (final SchemaContext context) -> {
116             throw new UnsupportedOperationException("Static context assigned");
117         };
118         classLoadingStrategy = runtimeContext.getStrategy();
119         codecRegistry = new BindingNormalizedNodeCodecRegistry(runtimeContext);
120         // TODO: this should have a specialized constructor or not be needed
121         futureSchema = FutureSchema.create(0, TimeUnit.SECONDS, false);
122         futureSchema.onRuntimeContextUpdated(runtimeContext);
123     }
124
125     public BindingToNormalizedNodeCodec(final BindingRuntimeGenerator generator,
126             final ClassLoadingStrategy classLoadingStrategy, final BindingNormalizedNodeCodecRegistry codecRegistry,
127             final boolean waitForSchema) {
128         this.generator = requireNonNull(generator, "generator");
129         this.classLoadingStrategy = requireNonNull(classLoadingStrategy, "classLoadingStrategy");
130         this.codecRegistry = requireNonNull(codecRegistry, "codecRegistry");
131         this.futureSchema = FutureSchema.create(WAIT_DURATION_SEC, TimeUnit.SECONDS, waitForSchema);
132     }
133
134     public static BindingToNormalizedNodeCodec newInstance(final BindingRuntimeGenerator generator,
135             final ClassLoadingStrategy classLoadingStrategy, final DOMSchemaService schemaService) {
136         final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry();
137         BindingToNormalizedNodeCodec instance = new BindingToNormalizedNodeCodec(generator, classLoadingStrategy,
138             codecRegistry, true);
139         instance.listenerRegistration = schemaService.registerSchemaContextListener(instance);
140         return instance;
141     }
142
143     protected YangInstanceIdentifier toYangInstanceIdentifierBlocking(
144             final InstanceIdentifier<? extends DataObject> binding) {
145         try {
146             return codecRegistry.toYangInstanceIdentifier(binding);
147         } catch (final MissingSchemaException e) {
148             waitForSchema(decompose(binding), e);
149             return codecRegistry.toYangInstanceIdentifier(binding);
150         }
151     }
152
153     /**
154      * Translates supplied Binding Instance Identifier into NormalizedNode
155      * instance identifier.
156      *
157      * @param binding
158      *            Binding Instance Identifier
159      * @return DOM Instance Identifier
160      * @throws IllegalArgumentException
161      *             If supplied Instance Identifier is not valid.
162      */
163     public final YangInstanceIdentifier toNormalized(final InstanceIdentifier<? extends DataObject> binding) {
164         return codecRegistry.toYangInstanceIdentifier(binding);
165     }
166
167     @Override
168     public final YangInstanceIdentifier toYangInstanceIdentifier(final InstanceIdentifier<?> binding) {
169         return codecRegistry.toYangInstanceIdentifier(binding);
170     }
171
172     protected YangInstanceIdentifier toYangInstanceIdentifierCached(final InstanceIdentifier<?> binding) {
173         return iiCache.getUnchecked(binding);
174     }
175
176     @Override
177     public final <T extends DataObject> Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalizedNode(
178             final InstanceIdentifier<T> path, final T data) {
179         try {
180             return codecRegistry.toNormalizedNode(path, data);
181         } catch (final MissingSchemaException e) {
182             waitForSchema(decompose(path), e);
183             return codecRegistry.toNormalizedNode(path, data);
184         }
185     }
186
187     /**
188      * Converts Binding Map.Entry to DOM Map.Entry.
189      *
190      * <p>
191      * Same as {@link #toNormalizedNode(InstanceIdentifier, DataObject)}.
192      *
193      * @param binding Map Entry with InstanceIdentifier as key and DataObject as value.
194      * @return DOM Map Entry with {@link YangInstanceIdentifier} as key and {@link NormalizedNode}
195      *         as value.
196      */
197     @SuppressWarnings({"unchecked", "rawtypes"})
198     public final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> toNormalizedNode(
199             final Entry<InstanceIdentifier<? extends DataObject>, DataObject> binding) {
200         return toNormalizedNode((InstanceIdentifier) binding.getKey(), binding.getValue());
201     }
202
203     @Override
204     public final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode(final YangInstanceIdentifier path,
205             final NormalizedNode<?, ?> data) {
206         return codecRegistry.fromNormalizedNode(path, data);
207     }
208
209     @Override
210     public final Notification fromNormalizedNodeNotification(final SchemaPath path, final ContainerNode data) {
211         return codecRegistry.fromNormalizedNodeNotification(path, data);
212     }
213
214     @Override
215     public final Notification fromNormalizedNodeNotification(final SchemaPath path, final ContainerNode data,
216             final Instant eventInstant) {
217         return codecRegistry.fromNormalizedNodeNotification(path, data, eventInstant);
218     }
219
220     @Override
221     public final DataObject fromNormalizedNodeRpcData(final SchemaPath path, final ContainerNode data) {
222         return codecRegistry.fromNormalizedNodeRpcData(path, data);
223     }
224
225     @Override
226     public <T extends RpcInput> T fromNormalizedNodeActionInput(final Class<? extends Action<?, ?, ?>> action,
227             final ContainerNode input) {
228         return codecRegistry.fromNormalizedNodeActionInput(action, input);
229     }
230
231     @Override
232     public <T extends RpcOutput> T fromNormalizedNodeActionOutput(final Class<? extends Action<?, ?, ?>> action,
233             final ContainerNode output) {
234         return codecRegistry.fromNormalizedNodeActionOutput(action, output);
235     }
236
237     @Override
238     public final InstanceIdentifier<?> fromYangInstanceIdentifier(final YangInstanceIdentifier dom) {
239         return codecRegistry.fromYangInstanceIdentifier(dom);
240     }
241
242     @Override
243     public final ContainerNode toNormalizedNodeNotification(final Notification data) {
244         return codecRegistry.toNormalizedNodeNotification(data);
245     }
246
247     @Override
248     public final ContainerNode toNormalizedNodeRpcData(final DataContainer data) {
249         return codecRegistry.toNormalizedNodeRpcData(data);
250     }
251
252     @Override
253     public ContainerNode toNormalizedNodeActionInput(final Class<? extends Action<?, ?, ?>> action,
254             final RpcInput input) {
255         return codecRegistry.toNormalizedNodeActionInput(action, input);
256     }
257
258     @Override
259     public ContainerNode toNormalizedNodeActionOutput(final Class<? extends Action<?, ?, ?>> action,
260             final RpcOutput output) {
261         return codecRegistry.toNormalizedNodeActionOutput(action, output);
262     }
263
264     @Override
265     public BindingLazyContainerNode<RpcInput> toLazyNormalizedNodeActionInput(
266             final Class<? extends Action<?, ?, ?>> action, final NodeIdentifier identifier, final RpcInput input) {
267         return codecRegistry.toLazyNormalizedNodeActionInput(action, identifier, input);
268     }
269
270     @Override
271     public BindingLazyContainerNode<RpcOutput> toLazyNormalizedNodeActionOutput(
272             final Class<? extends Action<?, ?, ?>> action, final NodeIdentifier identifier, final RpcOutput output) {
273         return codecRegistry.toLazyNormalizedNodeActionOutput(action, identifier, output);
274     }
275
276     /**
277      * Returns a Binding-Aware instance identifier from normalized
278      * instance-identifier if it is possible to create representation.
279      *
280      * <p>
281      * Returns Optional.empty for cases where target is mixin node except
282      * augmentation.
283      */
284     public final Optional<InstanceIdentifier<? extends DataObject>> toBinding(final YangInstanceIdentifier normalized)
285                     throws DeserializationException {
286         try {
287             return Optional.ofNullable(codecRegistry.fromYangInstanceIdentifier(normalized));
288         } catch (final IllegalArgumentException e) {
289             return Optional.empty();
290         }
291     }
292
293     public final Optional<Entry<InstanceIdentifier<? extends DataObject>, DataObject>> toBinding(
294             final @NonNull Entry<YangInstanceIdentifier, ? extends NormalizedNode<?, ?>> normalized)
295                     throws DeserializationException {
296         try {
297             /*
298              * This cast is required, due to generics behaviour in openjdk / oracle javac.
299              *
300              * <p>
301              * InstanceIdentifier has definition InstanceIdentifier<T extends DataObject>,
302              * this means '?' is always  <? extends DataObject>. Eclipse compiler
303              * is able to determine this relationship and treats
304              * Entry<InstanceIdentifier<?>, DataObject> and Entry<InstanceIdentifier<? extends DataObject, DataObject>
305              * as assignable. However openjdk / oracle javac treats this two types
306              * as incompatible and issues a compile error.
307              *
308              * <p>
309              * It is safe to lose generic information and cast it to other generic signature.
310              */
311             @SuppressWarnings("unchecked")
312             final Entry<InstanceIdentifier<? extends DataObject>, DataObject> binding = Entry.class.cast(
313                     codecRegistry.fromNormalizedNode(normalized.getKey(), normalized.getValue()));
314             return Optional.ofNullable(binding);
315         } catch (final IllegalArgumentException e) {
316             return Optional.empty();
317         }
318     }
319
320     @Override
321     public void onModelContextUpdated(final EffectiveModelContext newModelContext) {
322         final BindingRuntimeContext runtimeContext = DefaultBindingRuntimeContext.create(
323             generator.generateTypeMapping(newModelContext), classLoadingStrategy);
324         codecRegistry.onBindingRuntimeContextUpdated(runtimeContext);
325         futureSchema.onRuntimeContextUpdated(runtimeContext);
326     }
327
328     public final BindingNormalizedNodeCodecRegistry getCodecRegistry() {
329         return codecRegistry;
330     }
331
332     @Override
333     @PreDestroy
334     public void close() {
335         if (listenerRegistration != null) {
336             listenerRegistration.close();
337         }
338     }
339
340     public final BindingNormalizedNodeCodecRegistry getCodecFactory() {
341         return codecRegistry;
342     }
343
344     // FIXME: This should be probably part of Binding Runtime context
345     public final ImmutableBiMap<Method, SchemaPath> getRpcMethodToSchemaPath(final Class<? extends RpcService> key) {
346         final Module module = getModuleBlocking(key);
347         final ImmutableBiMap.Builder<Method, SchemaPath> ret = ImmutableBiMap.builder();
348         try {
349             for (final RpcDefinition rpcDef : module.getRpcs()) {
350                 final Method method = findRpcMethod(key, rpcDef);
351                 ret.put(method, rpcDef.getPath());
352             }
353         } catch (final NoSuchMethodException e) {
354             throw new IllegalStateException("Rpc defined in model does not have representation in generated class.", e);
355         }
356         return ret.build();
357     }
358
359     protected ImmutableBiMap<Method, RpcDefinition> getRpcMethodToSchema(final Class<? extends RpcService> key) {
360         final Module module = getModuleBlocking(key);
361         final ImmutableBiMap.Builder<Method, RpcDefinition> ret = ImmutableBiMap.builder();
362         try {
363             for (final RpcDefinition rpcDef : module.getRpcs()) {
364                 final Method method = findRpcMethod(key, rpcDef);
365                 ret.put(method, rpcDef);
366             }
367         } catch (final NoSuchMethodException e) {
368             throw new IllegalStateException("Rpc defined in model does not have representation in generated class.", e);
369         }
370         return ret.build();
371     }
372
373     private Module getModuleBlocking(final Class<?> modeledClass) {
374         final QNameModule moduleName = BindingReflections.getQNameModule(modeledClass);
375         BindingRuntimeContext localRuntimeContext = runtimeContext();
376         Module module = localRuntimeContext == null ? null :
377             localRuntimeContext.getSchemaContext().findModule(moduleName).orElse(null);
378         if (module == null && futureSchema.waitForSchema(moduleName)) {
379             localRuntimeContext = runtimeContext();
380             checkState(localRuntimeContext != null, "BindingRuntimeContext is not available.");
381             module = localRuntimeContext.getSchemaContext().findModule(moduleName).orElse(null);
382         }
383         if (module != null) {
384             return module;
385         }
386
387         LOG.debug("Schema for {} is not available; expected module name: {}; BindingRuntimeContext: {}",
388                 modeledClass, moduleName, localRuntimeContext);
389         throw new IllegalStateException(String.format("Schema for %s is not available; expected module name: %s; "
390                 + "full BindingRuntimeContext available in debug log", modeledClass, moduleName));
391     }
392
393     private void waitForSchema(final Collection<Class<?>> binding, final MissingSchemaException exception) {
394         LOG.warn("Blocking thread to wait for schema convergence updates for {} {}", futureSchema.getDuration(),
395             futureSchema.getUnit());
396         if (!futureSchema.waitForSchema(binding)) {
397             throw exception;
398         }
399     }
400
401     private Method findRpcMethod(final Class<? extends RpcService> key, final RpcDefinition rpcDef)
402             throws NoSuchMethodException {
403         final String methodName = BindingMapping.getRpcMethodName(rpcDef.getQName());
404         final Class<?> inputClz = runtimeContext().getClassForSchema(rpcDef.getInput());
405         return key.getMethod(methodName, inputClz);
406     }
407
408     protected @NonNull Entry<InstanceIdentifier<?>, BindingDataObjectCodecTreeNode<?>> getSubtreeCodec(
409             final YangInstanceIdentifier domIdentifier) {
410
411         final BindingCodecTree currentCodecTree = codecRegistry.getCodecContext();
412         final InstanceIdentifier<?> bindingPath = codecRegistry.fromYangInstanceIdentifier(domIdentifier);
413         checkArgument(bindingPath != null);
414         /**
415          * If we are able to deserialize YANG instance identifier, getSubtreeCodec must
416          * return non-null value.
417          */
418         final BindingDataObjectCodecTreeNode<?> codecContext = currentCodecTree.getSubtreeCodec(bindingPath);
419         return new SimpleEntry<>(bindingPath, codecContext);
420     }
421
422     @SuppressWarnings("unchecked")
423     public final Set<Class<? extends Notification>> getNotificationClasses(final Set<SchemaPath> interested) {
424         final Set<Class<? extends Notification>> result = new HashSet<>();
425         final BindingRuntimeContext runtimeContext = runtimeContext();
426         for (final NotificationDefinition notification : runtimeContext.getSchemaContext().getNotifications()) {
427             if (interested.contains(notification.getPath())) {
428                 try {
429                     result.add((Class<? extends Notification>) runtimeContext.getClassForSchema(notification));
430                 } catch (final IllegalStateException e) {
431                     // Ignore
432                     LOG.warn("Class for {} is currently not known.", notification.getPath(), e);
433                 }
434             }
435         }
436         return result;
437     }
438
439     SchemaPath getActionPath(final Class<? extends Action<?, ?, ?>> type) {
440         final ActionDefinition schema = runtimeContext().getActionDefinition(type);
441         checkArgument(schema != null, "Failed to find schema for %s", type);
442         return schema.getPath();
443     }
444
445     private BindingRuntimeContext runtimeContext() {
446         return futureSchema.runtimeContext();
447     }
448
449     private static Collection<Class<?>> decompose(final InstanceIdentifier<?> path) {
450         return ImmutableSet.copyOf(Iterators.transform(path.getPathArguments().iterator(), PathArgument::getType));
451     }
452
453     protected NormalizedNode<?, ?> instanceIdentifierToNode(final YangInstanceIdentifier parentPath) {
454         return ImmutableNodes.fromInstanceId(runtimeContext().getSchemaContext(), parentPath);
455     }
456
457     protected Collection<DOMDataTreeIdentifier> toDOMDataTreeIdentifiers(
458             final Collection<DataTreeIdentifier<?>> subtrees) {
459         return subtrees.stream().map(this::toDOMDataTreeIdentifier).collect(Collectors.toSet());
460     }
461
462     protected DOMDataTreeIdentifier toDOMDataTreeIdentifier(final DataTreeIdentifier<?> path) {
463         final YangInstanceIdentifier domPath = toYangInstanceIdentifierBlocking(path.getRootIdentifier());
464         return new DOMDataTreeIdentifier(path.getDatastoreType(), domPath);
465     }
466 }