Fix FinalModifierException due to BindingToNormalizedNodeCodec final
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingToNormalizedNodeCodecFactory.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.controller.md.sal.binding.impl;
9
10 import java.util.Dictionary;
11 import java.util.Hashtable;
12 import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
13 import org.opendaylight.controller.sal.core.api.model.SchemaService;
14 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
15 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
16 import org.opendaylight.yangtools.concepts.ListenerRegistration;
17 import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.ServiceRegistration;
21
22 /**
23  * Factory class for creating and initializing the BindingToNormalizedNodeCodec instances.
24  *
25  * @author Thomas Pantelis
26  */
27 public class BindingToNormalizedNodeCodecFactory {
28     /**
29      * This method is deprecated in favor of newInstance/registerInstance.
30      *
31      * @param classLoadingStrategy
32      * @param schemaService
33      * @return BindingToNormalizedNodeCodec instance
34      */
35     @Deprecated
36     public static BindingToNormalizedNodeCodec getOrCreateInstance(ClassLoadingStrategy classLoadingStrategy,
37                             SchemaService schemaService) {
38         BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(
39                 StreamWriterGenerator.create(SingletonHolder.JAVASSIST));
40         BindingToNormalizedNodeCodec instance = new BindingToNormalizedNodeCodec(
41                                classLoadingStrategy, codecRegistry, true);
42         schemaService.registerSchemaContextListener(instance);
43         return instance;
44     }
45
46     /**
47      * Creates a new BindingToNormalizedNodeCodec instance.
48      *
49      * @param classLoadingStrategy
50      * @return the BindingToNormalizedNodeCodec instance
51      */
52     public static BindingToNormalizedNodeCodec newInstance(ClassLoadingStrategy classLoadingStrategy) {
53         BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(
54                 StreamWriterGenerator.create(SingletonHolder.JAVASSIST));
55         return new BindingToNormalizedNodeCodec(classLoadingStrategy, codecRegistry, true);
56     }
57
58     /**
59      * Registers the given instance with the SchemaService as a SchemaContextListener.
60      *
61      * @param instance the BindingToNormalizedNodeCodec instance
62      * @param schemaService the SchemaService.
63      * @return the ListenerRegistration
64      */
65     public static ListenerRegistration<SchemaContextListener> registerInstance(BindingToNormalizedNodeCodec instance,
66             SchemaService schemaService) {
67         return schemaService.registerSchemaContextListener(instance);
68     }
69
70     /**
71      * This method is called via blueprint to register a BindingToNormalizedNodeCodec instance with the OSGI
72      * service registry. This is done in code instead of directly via blueprint because the BindingToNormalizedNodeCodec
73      * instance must be advertised with the actual class for backwards compatibility with CSS modules and blueprint
74      * will try to create a proxy wrapper which is problematic with BindingToNormalizedNodeCodec because it's final
75      * and has final methods which can't be proxied.
76      *
77      * @param instance the BindingToNormalizedNodeCodec instance
78      * @param bundleContext the BundleContext
79      * @return ServiceRegistration instance
80      */
81     public static ServiceRegistration<BindingToNormalizedNodeCodec> registerOSGiService(BindingToNormalizedNodeCodec instance,
82             BundleContext bundleContext) {
83         Dictionary<String, String> props = new Hashtable<>();
84
85         // Set the appropriate service properties so the corresponding CSS module is restarted if this
86         // blueprint container is restarted
87         props.put("config-module-namespace", "urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding:impl");
88         props.put("config-module-name", "runtime-generated-mapping");
89         props.put("config-instance-name", "runtime-mapping-singleton");
90         return bundleContext.registerService(BindingToNormalizedNodeCodec.class, instance, props );
91     }
92 }