6a3b02ec5780ae98d0e23506157514d8af35bdb4
[netconf.git] / netconf / yanglib / src / main / java / org / opendaylight / controller / config / yang / yanglib / impl / YanglibModule.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.controller.config.yang.yanglib.impl;
10
11 import java.io.File;
12 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
13 import org.opendaylight.yanglib.impl.YangLibProvider;
14 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
15 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
16 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class YanglibModule extends org.opendaylight.controller.config.yang.yanglib.impl.AbstractYanglibModule {
21     private static final Logger LOG = LoggerFactory.getLogger(YanglibModule.class);
22
23     public YanglibModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
24                          org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
25         super(identifier, dependencyResolver);
26     }
27
28     public YanglibModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier,
29                          org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
30                          org.opendaylight.controller.config.yang.yanglib.impl.YanglibModule oldModule,
31                          java.lang.AutoCloseable oldInstance) {
32         super(identifier, dependencyResolver, oldModule, oldInstance);
33     }
34
35     @Override
36     public void customValidation() {
37         JmxAttributeValidationException.checkNotNull(getCacheFolder(), cacheFolderJmxAttribute);
38         final File file = new File(getCacheFolder());
39         JmxAttributeValidationException
40                 .checkCondition(file.exists(), "Non existing cache file", cacheFolderJmxAttribute);
41         JmxAttributeValidationException
42                 .checkCondition(file.isDirectory(), "Non directory cache file", cacheFolderJmxAttribute);
43     }
44
45     @Override
46     public java.lang.AutoCloseable createInstance() {
47         // Start cache and Text to AST transformer
48         final SharedSchemaRepository repository = new SharedSchemaRepository("yang-library");
49         YangLibProvider provider = new YangLibProvider(repository, getBindingAddr(), getBindingPort());
50
51         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache =
52                 new FilesystemSchemaSourceCache<>(repository, YangTextSchemaSource.class, new File(getCacheFolder()));
53         repository.registerSchemaSourceListener(cache);
54         LOG.info("Starting yang library with sources from {}", getCacheFolder());
55         getBrokerDependency().registerProvider(provider);
56         return provider;
57     }
58 }