HONEYCOMB-418: Bump ODL dependencies from Nitrogen SR1 to Nitrogen SR2
[honeycomb.git] / infra / test-utils / test-tools / src / main / java / io / fd / honeycomb / test / tools / YangContextProducer.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.test.tools;
18
19 import static com.google.common.base.Preconditions.checkState;
20
21 import io.fd.honeycomb.test.tools.annotations.SchemaContextProvider;
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import javassist.ClassPool;
26 import org.apache.commons.lang3.reflect.MethodUtils;
27 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
28 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
29 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
30 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
31 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
32 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
33 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
34 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36
37 /**
38  * Common logic to initialize serializers/deserializers/etc while working with yang based data
39  */
40 interface YangContextProducer {
41
42     default ModuleInfoBackedContext getCheckedModuleInfoContext(final Object test)
43             throws IllegalAccessException, InvocationTargetException {
44         final Method[] suitableMethods =
45                 MethodUtils.getMethodsWithAnnotation(test.getClass(), SchemaContextProvider.class);
46         checkState(suitableMethods.length == 1, "Only single method should be @SchemaContextProvider, actual : %s",
47                 suitableMethods);
48         final Object possibleContext = suitableMethods[0].invoke(test);
49         checkState(possibleContext instanceof ModuleInfoBackedContext, "%s is not ModuleInfoBackedContext",
50                 possibleContext);
51         return ModuleInfoBackedContext.class.cast(possibleContext);
52     }
53
54     /**
55      * Get a codec for instance identifiers.
56      */
57     default AbstractModuleStringInstanceIdentifierCodec getIIDCodec(final ModuleInfoBackedContext ctx)
58             throws NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException,
59             java.lang.reflect.InvocationTargetException {
60         // Reusing codec for JSON ... not public so here goes reflection
61
62         final JSONCodecFactory jsonCodecFactory = JSONCodecFactory.create(ctx.getSchemaContext());
63         final Constructor<?> cstr =
64                 Class.forName("org.opendaylight.yangtools.yang.data.codec.gson.JSONInstanceIdentifierCodec")
65                         .getDeclaredConstructor(SchemaContext.class, JSONCodecFactory.class);
66         cstr.setAccessible(true);
67         return (AbstractModuleStringInstanceIdentifierCodec) cstr.newInstance(ctx.getSchemaContext(), jsonCodecFactory);
68     }
69
70     default BindingToNormalizedNodeCodec createSerializer(final ModuleInfoBackedContext moduleInfoBackedContext) {
71
72         final BindingNormalizedNodeCodecRegistry codecRegistry =
73                 new BindingNormalizedNodeCodecRegistry(
74                         StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault())));
75         codecRegistry
76                 .onBindingRuntimeContextUpdated(
77                     BindingRuntimeContext.create(moduleInfoBackedContext, moduleInfoBackedContext.getSchemaContext()));
78         return new BindingToNormalizedNodeCodec(moduleInfoBackedContext, codecRegistry);
79     }
80 }