2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.test.tools;
19 import static com.google.common.base.Preconditions.checkState;
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;
38 * Common logic to initialize serializers/deserializers/etc while working with yang based data
40 interface YangContextProducer {
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",
48 final Object possibleContext = suitableMethods[0].invoke(test);
49 checkState(possibleContext instanceof ModuleInfoBackedContext, "%s is not ModuleInfoBackedContext",
51 return ModuleInfoBackedContext.class.cast(possibleContext);
55 * Get a codec for instance identifiers.
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
62 final JSONCodecFactory jsonCodecFactory = JSONCodecFactory.create(ctx.getSchemaContext());
63 final Constructor<?> cstr =
64 Class.forName("org.opendaylight.yangtools.yang.data.codec.gson.JSONStringInstanceIdentifierCodec")
65 .getDeclaredConstructor(SchemaContext.class, JSONCodecFactory.class);
66 cstr.setAccessible(true);
67 return (AbstractModuleStringInstanceIdentifierCodec) cstr.newInstance(ctx.getSchemaContext(), jsonCodecFactory);
70 default BindingToNormalizedNodeCodec createSerializer(final ModuleInfoBackedContext moduleInfoBackedContext) {
72 final BindingNormalizedNodeCodecRegistry codecRegistry =
73 new BindingNormalizedNodeCodecRegistry(
74 StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault())));
76 .onBindingRuntimeContextUpdated(
77 BindingRuntimeContext.create(moduleInfoBackedContext, moduleInfoBackedContext.getSchemaContext()));
78 return new BindingToNormalizedNodeCodec(moduleInfoBackedContext, codecRegistry);