Eliminate BindingToNormalizedNodeCodecFactory.getOrCreateInstance()
[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.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
14 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
15 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
16 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
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      * Creates a new BindingToNormalizedNodeCodec instance.
30      *
31      * @param classLoadingStrategy
32      * @return the BindingToNormalizedNodeCodec instance
33      */
34     public static BindingToNormalizedNodeCodec newInstance(final ClassLoadingStrategy classLoadingStrategy) {
35         final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(
36                 StreamWriterGenerator.create(SingletonHolder.JAVASSIST));
37         return new BindingToNormalizedNodeCodec(classLoadingStrategy, codecRegistry, true);
38     }
39
40     /**
41      * Registers the given instance with the SchemaService as a SchemaContextListener.
42      *
43      * @param instance the BindingToNormalizedNodeCodec instance
44      * @param schemaService the SchemaService.
45      * @return the ListenerRegistration
46      */
47     public static ListenerRegistration<SchemaContextListener> registerInstance(final BindingToNormalizedNodeCodec instance,
48             final DOMSchemaService schemaService) {
49         return schemaService.registerSchemaContextListener(instance);
50     }
51
52     /**
53      * This method is called via blueprint to register a BindingToNormalizedNodeCodec instance with the OSGI
54      * service registry. This is done in code instead of directly via blueprint because the BindingToNormalizedNodeCodec
55      * instance must be advertised with the actual class for backwards compatibility with CSS modules and blueprint
56      * will try to create a proxy wrapper which is problematic with BindingToNormalizedNodeCodec because it's final
57      * and has final methods which can't be proxied.
58      *
59      * @param instance the BindingToNormalizedNodeCodec instance
60      * @param bundleContext the BundleContext
61      * @return ServiceRegistration instance
62      */
63     public static ServiceRegistration<BindingToNormalizedNodeCodec> registerOSGiService(final BindingToNormalizedNodeCodec instance,
64             final BundleContext bundleContext) {
65         final Dictionary<String, String> props = new Hashtable<>();
66
67         // Set the appropriate service properties so the corresponding CSS module is restarted if this
68         // blueprint container is restarted
69         props.put("config-module-namespace", "urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding:impl");
70         props.put("config-module-name", "runtime-generated-mapping");
71         props.put("config-instance-name", "runtime-mapping-singleton");
72         return bundleContext.registerService(BindingToNormalizedNodeCodec.class, instance, props );
73     }
74 }