Merge "Add config classifier to netconf-mdsal-config jar"
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / mapping / CodecRegistryProvider.java
1 /*
2  * Copyright (c) 2013 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.controller.config.manager.impl.osgi.mapping;
9
10 import javassist.ClassPool;
11 import org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil;
12 import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
13 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
14 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
15 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
17 import org.osgi.framework.BundleContext;
18
19 /**
20  * Creates and initializes {@link RuntimeGeneratedMappingServiceImpl}, which is used to get {@link CodecRegistry}.
21  * Also maintains service registrations of {@link RuntimeGeneratedMappingServiceImpl}.
22  */
23 // TODO move to yang runtime
24 public class CodecRegistryProvider implements AutoCloseable {
25     private static final ClassPool CLASS_POOL = ClassPool.getDefault();
26
27     private final RuntimeGeneratedMappingServiceImpl service;
28     private final AutoCloseable registration;
29
30     public CodecRegistryProvider(final ClassLoadingStrategy classLoadingStrategy, final BundleContext context) {
31         service = new RuntimeGeneratedMappingServiceImpl(CLASS_POOL, classLoadingStrategy);
32         registration = OsgiRegistrationUtil.registerService(context, service,
33                 SchemaContextListener.class, BindingIndependentMappingService.class);
34     }
35
36     public CodecRegistry getCodecRegistry() {
37         return service.getCodecRegistry();
38     }
39
40     @Override
41     public void close() throws Exception {
42         registration.close();
43     }
44 }