b6299697cc5f08d91dd6f9588f03e5af1a340e5e
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / config / yang / md / sal / connector / netconf / NetconfConnectorModuleFactory.java
1 /*
2  * Copyright (c) 2014 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.yang.md.sal.connector.netconf;
9
10 import java.io.File;
11
12 import org.opendaylight.controller.config.api.DependencyResolver;
13 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
14 import org.opendaylight.controller.config.spi.Module;
15 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
16 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
17 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
18 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
19 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
20 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
21 import org.osgi.framework.BundleContext;
22
23 /**
24 *
25 */
26 public class NetconfConnectorModuleFactory extends
27         org.opendaylight.controller.config.yang.md.sal.connector.netconf.AbstractNetconfConnectorModuleFactory {
28
29     // TODO this should be injected
30     // Netconf devices have separated schema registry + factory from controller
31     private final SharedSchemaRepository repository = new SharedSchemaRepository(NAME);
32     private final SchemaContextFactory schemaContextFactory
33             = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
34
35     public NetconfConnectorModuleFactory() {
36         // Start cache and Text to AST transformer
37         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(repository, YangTextSchemaSource.class, new File("cache/schema"));
38         repository.registerSchemaSourceListener(cache);
39         repository.registerSchemaSourceListener(TextToASTTransformer.create(repository, repository));
40     }
41
42     @Override
43     public Module createModule(final String instanceName, final DependencyResolver dependencyResolver,
44             final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
45         final NetconfConnectorModule module = (NetconfConnectorModule) super.createModule(instanceName, dependencyResolver,
46                 old, bundleContext);
47
48         module.setBundleContext(bundleContext);
49         module.setSchemaRegistry(repository);
50         module.setSchemaContextFactory(schemaContextFactory);
51         return module;
52     }
53
54     @Override
55     public Module createModule(final String instanceName, final DependencyResolver dependencyResolver, final BundleContext bundleContext) {
56         final NetconfConnectorModule module = (NetconfConnectorModule) super.createModule(instanceName, dependencyResolver,
57                 bundleContext);
58         module.setBundleContext(bundleContext);
59         module.setSchemaRegistry(repository);
60         module.setSchemaContextFactory(schemaContextFactory);
61         return module;
62     }
63 }