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.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Listens on new schema sources registered event. For each new source
49 * registered generates URL representing its schema source and write this URL
50 * along with source identifier to
51 * ietf-netconf-yang-library/modules-state/module list.
53 public class YangLibProvider implements AutoCloseable, SchemaSourceListener {
54 private static final Logger LOG = LoggerFactory.getLogger(YangLibProvider.class);
56 private static final OptionalRevision NO_REVISION = new OptionalRevision("");
57 private static final Predicate<PotentialSchemaSource<?>> YANG_SCHEMA_SOURCE =
58 input -> YangTextSchemaSource.class.isAssignableFrom(input.getRepresentation());
60 private final DataBroker dataBroker;
61 private final YangLibServiceImpl yangLibService;
62 private final YanglibConfig yanglibConfig;
63 private SchemaListenerRegistration schemaListenerRegistration;
64 private SharedSchemaRepository schemaRepository;
66 public YangLibProvider(final YanglibConfig yanglibConfig, final DataBroker dataBroker,
67 final YangLibServiceImpl yangLibService) {
68 this.yanglibConfig = Preconditions.checkNotNull(yanglibConfig);
69 this.dataBroker = Preconditions.checkNotNull(dataBroker);
70 this.yangLibService = Preconditions.checkNotNull(yangLibService);
75 yangLibService.setSchemaRepository(null);
76 if (schemaListenerRegistration != null) {
77 schemaListenerRegistration.close();
82 if (Strings.isNullOrEmpty(yanglibConfig.getCacheFolder())) {
83 LOG.info("No cache-folder set in yanglib-config - yang library services will not be available");
87 final File cacheFolderFile = new File(yanglibConfig.getCacheFolder());
88 Preconditions.checkArgument(cacheFolderFile.exists(), "cache-folder %s does not exist", cacheFolderFile);
89 Preconditions.checkArgument(cacheFolderFile.isDirectory(), "cache-folder %s is not a directory",
92 schemaRepository = new SharedSchemaRepository("yang-library");
93 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache =
94 new FilesystemSchemaSourceCache<>(schemaRepository, YangTextSchemaSource.class, cacheFolderFile);
95 schemaRepository.registerSchemaSourceListener(cache);
97 schemaListenerRegistration = schemaRepository.registerSchemaSourceListener(this);
98 yangLibService.setSchemaRepository(schemaRepository);
100 LOG.info("Started yang library with sources from {}", cacheFolderFile);
104 public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
109 public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
110 final List<Module> newModules = new ArrayList<>();
112 for (PotentialSchemaSource<?> potentialYangSource : Iterables.filter(sources, YANG_SCHEMA_SOURCE)) {
113 final YangIdentifier moduleName = new YangIdentifier(potentialYangSource.getSourceIdentifier().getName());
115 final OptionalRevision moduleRevision = getRevisionForModule(potentialYangSource.getSourceIdentifier());
117 final Module newModule = new ModuleBuilder()
119 .setRevision(moduleRevision)
120 .setSchema(getUrlForModule(potentialYangSource.getSourceIdentifier()))
123 newModules.add(newModule);
126 if (newModules.isEmpty()) {
127 // If no new yang modules then do nothing
131 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
132 tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(ModulesState.class),
133 new ModulesStateBuilder().setModule(newModules).build());
135 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
137 public void onSuccess(@Nullable final Void result) {
138 LOG.debug("Modules state successfully populated with new modules");
142 public void onFailure(final Throwable throwable) {
143 LOG.warn("Unable to update modules state", throwable);
145 }, MoreExecutors.directExecutor());
149 public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
150 if (!YANG_SCHEMA_SOURCE.apply(source)) {
151 // if representation of potential schema source is not yang text schema source do nothing
152 // we do not want to delete this module entry from module list
156 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
157 tx.delete(LogicalDatastoreType.OPERATIONAL,
158 InstanceIdentifier.create(ModulesState.class)
161 new YangIdentifier(source.getSourceIdentifier().getName()),
162 getRevisionForModule(source.getSourceIdentifier()))));
164 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
166 public void onSuccess(@Nullable final Void result) {
167 LOG.debug("Modules state successfully updated.");
171 public void onFailure(final Throwable throwable) {
172 LOG.warn("Unable to update modules state", throwable);
174 }, MoreExecutors.directExecutor());
179 private Uri getUrlForModule(final SourceIdentifier sourceIdentifier) {
180 return new Uri("http://" + yanglibConfig.getBindingAddr() + ':' + yanglibConfig.getBindingPort()
181 + "/yanglib/schemas/" + sourceIdentifier.getName() + '/' + revString(sourceIdentifier));
184 private static String revString(final SourceIdentifier id) {
185 return id.getRevision().map(Revision::toString).orElse("");
188 private static OptionalRevision getRevisionForModule(final SourceIdentifier sourceIdentifier) {
189 return sourceIdentifier.getRevision().map(rev -> new OptionalRevision(new RevisionIdentifier(rev.toString())))
190 .orElse(NO_REVISION);