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.netconf.mdsal.yang.library;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import javax.annotation.PreDestroy;
19 import javax.inject.Inject;
20 import javax.inject.Singleton;
21 import org.checkerframework.checker.lock.qual.GuardedBy;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.WriteTransaction;
24 import org.opendaylight.mdsal.common.api.CommitInfo;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.datastores.rev180214.Operational;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryBuilder;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafsRevisionBuilder;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module.ConformanceType;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.ImportOnlyModuleRevisionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.set.parameters.module.SubmoduleBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.yang.library.parameters.DatastoreBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.yang.library.parameters.ModuleSetBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.yang.library.parameters.SchemaBuilder;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
41 import org.opendaylight.yangtools.concepts.ListenerRegistration;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
44 import org.opendaylight.yangtools.yang.common.Revision;
45 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
46 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
47 import org.opendaylight.yangtools.yang.model.api.Module;
48 import org.osgi.service.component.annotations.Activate;
49 import org.osgi.service.component.annotations.Component;
50 import org.osgi.service.component.annotations.Deactivate;
51 import org.osgi.service.component.annotations.Reference;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * Listens for updates on global schema context, transforms context to ietf-yang-library/yang-library and writes this
57 * state to operational data store.
60 @Component(immediate = true, service = {})
61 public class YangLibraryWriter implements EffectiveModelContextListener, AutoCloseable {
62 private static final Logger LOG = LoggerFactory.getLogger(YangLibraryWriter.class);
63 private static final String MODULE_SET_NAME = "state-modules";
64 private static final String SCHEMA_NAME = "state-schema";
66 private static final InstanceIdentifier<YangLibrary> YANG_LIBRARY_INSTANCE_IDENTIFIER =
67 InstanceIdentifier.create(YangLibrary.class);
69 private static final InstanceIdentifier<ModulesState> MODULES_STATE_INSTANCE_IDENTIFIER =
70 InstanceIdentifier.create(ModulesState.class);
72 private final DataBroker dataBroker;
74 private long moduleSetId;
76 private ListenerRegistration<?> reg;
80 public YangLibraryWriter(final @Reference DOMSchemaService schemaService,
81 final @Reference DataBroker dataBroker) {
82 this.dataBroker = requireNonNull(dataBroker);
83 reg = schemaService.registerSchemaContextListener(this);
89 public synchronized void close() throws InterruptedException, ExecutionException {
98 // FIXME: we should be using a transaction chain for this, but, really, this should be a dynamically-populated
99 // shard (i.e. no storage whatsoever)!
100 final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
101 tx.delete(LogicalDatastoreType.OPERATIONAL, YANG_LIBRARY_INSTANCE_IDENTIFIER);
102 tx.delete(LogicalDatastoreType.OPERATIONAL, MODULES_STATE_INSTANCE_IDENTIFIER);
104 final FluentFuture<? extends CommitInfo> future = tx.commit();
105 future.addCallback(new FutureCallback<CommitInfo>() {
107 public void onSuccess(final CommitInfo info) {
108 LOG.debug("YANG library cleared successfully");
112 public void onFailure(final Throwable throwable) {
113 LOG.warn("Unable to clear YANG library", throwable);
115 }, MoreExecutors.directExecutor());
117 // We need to synchronize here, otherwise we'd end up trampling over ourselves
122 public void onModelContextUpdated(final EffectiveModelContext context) {
123 final Module ietfYangLibraryModule = context.findModule(YangLibrary.QNAME.getModule()).orElse(null);
124 if (ietfYangLibraryModule != null) {
125 updateYangLibrary(context);
127 LOG.debug("ietf-yang-library not present in context, skipping update");
131 private synchronized void updateYangLibrary(final EffectiveModelContext context) {
133 // Already shut down, do not do anything
137 final long currentSetId = moduleSetId++;
139 final YangLibrary newYangLibrary = createYangLibraryFromContext(context.getModules(), currentSetId);
140 final ModulesState newModuleState = createModuleStateFromModules(context.getModules(), currentSetId);
141 LOG.debug("Trying to write new yang-library: {}", newYangLibrary);
142 LOG.debug("Trying to write new module-state: {}", newModuleState);
144 final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
145 tx.put(LogicalDatastoreType.OPERATIONAL, YANG_LIBRARY_INSTANCE_IDENTIFIER, newYangLibrary);
146 tx.put(LogicalDatastoreType.OPERATIONAL, MODULES_STATE_INSTANCE_IDENTIFIER, newModuleState);
147 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
149 public void onSuccess(final CommitInfo result) {
150 LOG.debug("Yang library updated successfully");
154 public void onFailure(final Throwable throwable) {
155 LOG.warn("Failed to update yang library", throwable);
157 }, MoreExecutors.directExecutor());
160 private static YangLibrary createYangLibraryFromContext(final Collection<? extends Module> modules,
161 final long moduleSetId) {
162 final var moduleMap = modules.stream()
164 final var submoduleMap = module.getSubmodules().stream()
165 .map(subModule -> new SubmoduleBuilder()
166 .setName(new YangIdentifier(subModule.getName()))
167 .setRevision(ImportOnlyModuleRevisionBuilder.fromYangCommon(subModule.getQNameModule()
168 .getRevision()).getRevisionIdentifier())
170 .collect(BindingMap.toMap());
172 return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library
173 .rev190104.module.set.parameters.ModuleBuilder()
174 .setName(new YangIdentifier(module.getName() + "_"
175 // FIXME: 'orElse' seems to be wrong here
176 + module.getRevision().map(Revision::toString).orElse(null)))
177 .setRevision(ImportOnlyModuleRevisionBuilder.fromYangCommon(module.getQNameModule().getRevision())
178 .getRevisionIdentifier())
179 .setNamespace(new Uri(module.getNamespace().toString()))
180 // FIXME: inline this once it's disambiguated
181 .setSubmodule(submoduleMap)
184 .collect(BindingMap.toMap());
186 return new YangLibraryBuilder()
187 .setModuleSet(BindingMap.of(new ModuleSetBuilder()
188 .setName(MODULE_SET_NAME)
189 // FIXME: inline this once it's disambiguated
190 .setModule(moduleMap)
192 .setSchema(BindingMap.of(new SchemaBuilder()
193 .setName(SCHEMA_NAME)
194 .setModuleSet(List.of(MODULE_SET_NAME))
196 .setDatastore(BindingMap.of(new DatastoreBuilder()
197 .setName(Operational.class)
198 .setSchema(SCHEMA_NAME)
200 .setContentId(String.valueOf(moduleSetId))
205 private static ModulesState createModuleStateFromModules(final Collection<? extends Module> modules,
206 final long moduleSetId) {
207 final var moduleMap = modules.stream()
209 final var submoduleMap = module.getSubmodules().stream()
210 .map(subModule -> new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library
211 .rev190104.module.list.module.SubmoduleBuilder()
212 .setName(new YangIdentifier(subModule.getName()))
213 .setRevision(CommonLeafsRevisionBuilder.fromYangCommon(subModule.getQNameModule()
216 .collect(BindingMap.toMap());
218 return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library
219 .rev190104.module.list.ModuleBuilder()
220 .setName(new YangIdentifier(module.getName()))
221 .setRevision(CommonLeafsRevisionBuilder.fromYangCommon(module.getQNameModule().getRevision()))
222 .setNamespace(new Uri(module.getNamespace().toString()))
223 // FIXME: Conformance type is always set to Implement value, but it should it really be like this?
224 .setConformanceType(ConformanceType.Implement)
225 // FIXME: inline this once it's disambiguated
226 .setSubmodule(submoduleMap)
227 // FIXME: Add also deviations and features lists to module entries
230 .collect(BindingMap.toMap());
232 return new ModulesStateBuilder()
233 // FIXME: inline this once it's disambiguated
234 .setModule(moduleMap)
235 .setModuleSetId(String.valueOf(moduleSetId))