2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yanglib.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Predicate;
12 import com.google.common.base.Strings;
13 import com.google.common.collect.Iterables;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.MoreExecutors;
18 import java.util.ArrayList;
19 import java.util.List;
20 import javax.annotation.Nullable;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesState;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesStateBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.OptionalRevision;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.RevisionIdentifier;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.Module;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.ModuleBuilder;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.ModuleKey;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfig;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.common.Revision;
36 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
37 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
38 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
39 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
40 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
41 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
42 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
43 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
44 import org.osgi.framework.BundleContext;
45 import org.osgi.framework.FrameworkUtil;
46 import org.osgi.framework.ServiceReference;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * Listens on new schema sources registered event. For each new source
52 * registered generates URL representing its schema source and write this URL
53 * along with source identifier to
54 * ietf-netconf-yang-library/modules-state/module list.
56 public class YangLibProvider implements AutoCloseable, SchemaSourceListener {
57 private static final Logger LOG = LoggerFactory.getLogger(YangLibProvider.class);
59 private static final OptionalRevision NO_REVISION = new OptionalRevision("");
60 private static final Predicate<PotentialSchemaSource<?>> YANG_SCHEMA_SOURCE =
61 input -> YangTextSchemaSource.class.isAssignableFrom(input.getRepresentation());
63 private final DataBroker dataBroker;
64 private final YangLibServiceImpl yangLibService;
65 private final YanglibConfig yanglibConfig;
66 private SchemaListenerRegistration schemaListenerRegistration;
67 private SharedSchemaRepository schemaRepository;
69 public YangLibProvider(final YanglibConfig yanglibConfig, final DataBroker dataBroker,
70 final YangLibServiceImpl yangLibService) {
71 this.yanglibConfig = Preconditions.checkNotNull(yanglibConfig);
72 this.dataBroker = Preconditions.checkNotNull(dataBroker);
73 this.yangLibService = Preconditions.checkNotNull(yangLibService);
78 yangLibService.setSchemaRepository(null);
79 if (schemaListenerRegistration != null) {
80 schemaListenerRegistration.close();
85 if (Strings.isNullOrEmpty(yanglibConfig.getCacheFolder())) {
86 LOG.info("No cache-folder set in yanglib-config - yang library services will not be available");
90 final File cacheFolderFile = new File(yanglibConfig.getCacheFolder());
91 Preconditions.checkArgument(cacheFolderFile.exists(), "cache-folder %s does not exist", cacheFolderFile);
92 Preconditions.checkArgument(cacheFolderFile.isDirectory(), "cache-folder %s is not a directory",
95 schemaRepository = new SharedSchemaRepository("yang-library");
96 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache =
97 new FilesystemSchemaSourceCache<>(schemaRepository, YangTextSchemaSource.class, cacheFolderFile);
98 schemaRepository.registerSchemaSourceListener(cache);
100 schemaListenerRegistration = schemaRepository.registerSchemaSourceListener(this);
101 yangLibService.setSchemaRepository(schemaRepository);
103 LOG.info("Started yang library with sources from {}", cacheFolderFile);
107 public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
112 public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
113 final List<Module> newModules = new ArrayList<>();
115 for (PotentialSchemaSource<?> potentialYangSource : Iterables.filter(sources, YANG_SCHEMA_SOURCE)) {
116 final YangIdentifier moduleName = new YangIdentifier(potentialYangSource.getSourceIdentifier().getName());
118 final OptionalRevision moduleRevision = getRevisionForModule(potentialYangSource.getSourceIdentifier());
120 final Module newModule = new ModuleBuilder()
122 .setRevision(moduleRevision)
123 .setSchema(getUrlForModule(potentialYangSource.getSourceIdentifier()))
126 newModules.add(newModule);
129 if (newModules.isEmpty()) {
130 // If no new yang modules then do nothing
134 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
135 tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(ModulesState.class),
136 new ModulesStateBuilder().setModule(newModules).build());
138 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
140 public void onSuccess(@Nullable final Void result) {
141 LOG.debug("Modules state successfully populated with new modules");
145 public void onFailure(final Throwable throwable) {
146 LOG.warn("Unable to update modules state", throwable);
148 }, MoreExecutors.directExecutor());
152 public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
153 if (!YANG_SCHEMA_SOURCE.apply(source)) {
154 // if representation of potential schema source is not yang text schema source do nothing
155 // we do not want to delete this module entry from module list
159 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
160 tx.delete(LogicalDatastoreType.OPERATIONAL,
161 InstanceIdentifier.create(ModulesState.class)
164 new YangIdentifier(source.getSourceIdentifier().getName()),
165 getRevisionForModule(source.getSourceIdentifier()))));
167 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
169 public void onSuccess(@Nullable final Void result) {
170 LOG.debug("Modules state successfully updated.");
174 public void onFailure(final Throwable throwable) {
175 LOG.warn("Unable to update modules state", throwable);
177 }, MoreExecutors.directExecutor());
182 private Uri getUrlForModule(final SourceIdentifier sourceIdentifier) {
183 return new Uri("http://" + yanglibConfig.getBindingAddr() + ':' + yanglibConfig.getBindingPort()
184 + "/yanglib/schemas/" + sourceIdentifier.getName() + '/' + revString(sourceIdentifier));
187 private static String revString(final SourceIdentifier id) {
188 return id.getRevision().map(Revision::toString).orElse("");
191 private static OptionalRevision getRevisionForModule(final SourceIdentifier sourceIdentifier) {
192 return sourceIdentifier.getRevision().map(rev -> new OptionalRevision(new RevisionIdentifier(rev.toString())))
193 .orElse(NO_REVISION);
196 private <T> T getObjectFromBundleContext(final Class<T> type, final String serviceRefName) {
197 final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
198 final ServiceReference<?> serviceReference = bundleContext.getServiceReference(serviceRefName);
199 return (T) bundleContext.getService(serviceReference);