BUG-770: NumberFormatException for input string on switch OFPT_HELLO
[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.SchemaServiceListener;
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(ClassLoadingStrategy classLoadingStrategy, BundleContext context) {
31         service = new RuntimeGeneratedMappingServiceImpl(classLoadingStrategy);
32         service.setPool(CLASS_POOL);
33         service.init();
34         registration = OsgiRegistrationUtil.registerService(context, service,
35                 SchemaServiceListener.class, BindingIndependentMappingService.class);
36     }
37
38     public CodecRegistry getCodecRegistry() {
39         return service.getCodecRegistry();
40     }
41
42     @Override
43     public void close() throws Exception {
44         registration.close();
45     }
46 }