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