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.restconf.nb.rfc8040.handlers;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Throwables;
13 import java.util.Collection;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.atomic.AtomicInteger;
16 import javax.annotation.PostConstruct;
17 import javax.annotation.PreDestroy;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.apache.aries.blueprint.annotation.service.Reference;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
24 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
25 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
26 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
27 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
28 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.MonitoringModule;
29 import org.opendaylight.restconf.nb.rfc8040.utils.mapping.RestconfMappingNodeUtil;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
37 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Implementation of {@link SchemaContextHandler}.
46 public class SchemaContextHandler implements SchemaContextListenerHandler, AutoCloseable {
47 private static final Logger LOG = LoggerFactory.getLogger(SchemaContextHandler.class);
49 private final AtomicInteger moduleSetId = new AtomicInteger(0);
51 private final TransactionChainHandler transactionChainHandler;
52 private final DOMSchemaService domSchemaService;
53 private ListenerRegistration<?> listenerRegistration;
55 private volatile EffectiveModelContext schemaContext;
60 * @param transactionChainHandler Transaction chain handler
63 public SchemaContextHandler(final TransactionChainHandler transactionChainHandler,
64 final @Reference DOMSchemaService domSchemaService) {
65 this.transactionChainHandler = transactionChainHandler;
66 this.domSchemaService = domSchemaService;
71 listenerRegistration = domSchemaService.registerSchemaContextListener(this);
77 if (listenerRegistration != null) {
78 listenerRegistration.close();
83 @SuppressWarnings("checkstyle:hiddenField")
84 public void onModelContextUpdated(final EffectiveModelContext context) {
85 schemaContext = requireNonNull(context);
87 final Module ietfYangLibraryModule =
88 context.findModule(IetfYangLibrary.MODULE_QNAME).orElse(null);
89 if (ietfYangLibraryModule != null) {
90 NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
91 RestconfMappingNodeUtil.mapModulesByIetfYangLibraryYang(context.getModules(), ietfYangLibraryModule,
92 context, String.valueOf(this.moduleSetId.incrementAndGet()));
96 final Module monitoringModule =
97 schemaContext.findModule(MonitoringModule.MODULE_QNAME).orElse(null);
98 if (monitoringModule != null) {
99 NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
100 RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
106 public EffectiveModelContext get() {
107 return schemaContext;
110 private void putData(
111 final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode) {
112 final DOMTransactionChain transactionChain = this.transactionChainHandler.get();
113 final DOMDataTreeWriteTransaction wTx = transactionChain.newWriteOnlyTransaction();
114 wTx.put(LogicalDatastoreType.OPERATIONAL,
115 YangInstanceIdentifier.create(NodeIdentifier.create(normNode.getNodeType())), normNode);
118 } catch (InterruptedException e) {
119 throw new RestconfDocumentedException("Problem occurred while putting data to DS.", e);
120 } catch (ExecutionException e) {
121 final TransactionCommitFailedException failure = Throwables.getCauseAs(e,
122 TransactionCommitFailedException.class);
123 if (failure.getCause() instanceof ConflictingModificationAppliedException) {
125 * Ignore error when another cluster node is already putting the same data to DS.
126 * We expect that cluster is homogeneous and that node was going to write the same data
127 * (that means no retry is needed). Transaction chain reset must be invoked to be able
128 * to continue writing data with another transaction after failed transaction.
129 * This is workaround for bug https://bugs.opendaylight.org/show_bug.cgi?id=7728
131 LOG.warn("Ignoring that another cluster node is already putting the same data to DS.", e);
133 throw new RestconfDocumentedException("Problem occurred while putting data to DS.", failure);
136 transactionChain.close();