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 com.google.common.base.Preconditions;
11 import com.google.common.base.Throwables;
12 import java.util.Collection;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.atomic.AtomicInteger;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
18 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
21 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.MonitoringModule;
22 import org.opendaylight.restconf.nb.rfc8040.utils.mapping.RestconfMappingNodeUtil;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Implementation of {@link SchemaContextHandler}.
40 @SuppressWarnings("checkstyle:FinalClass")
41 public class SchemaContextHandler implements SchemaContextListenerHandler, AutoCloseable {
43 private static final Logger LOG = LoggerFactory.getLogger(SchemaContextHandler.class);
45 private final AtomicInteger moduleSetId = new AtomicInteger(0);
47 private final TransactionChainHandler transactionChainHandler;
48 private final DOMSchemaService domSchemaService;
49 private ListenerRegistration<SchemaContextListener> listenerRegistration;
51 private volatile SchemaContext schemaContext;
56 * @param transactionChainHandler Transaction chain handler
58 private SchemaContextHandler(final TransactionChainHandler transactionChainHandler,
59 final DOMSchemaService domSchemaService) {
60 this.transactionChainHandler = transactionChainHandler;
61 this.domSchemaService = domSchemaService;
64 public static SchemaContextHandler newInstance(final TransactionChainHandler transactionChainHandler,
65 final DOMSchemaService domSchemaService) {
66 return new SchemaContextHandler(transactionChainHandler, domSchemaService);
70 listenerRegistration = domSchemaService.registerSchemaContextListener(this);
75 if (listenerRegistration != null) {
76 listenerRegistration.close();
81 @SuppressWarnings("checkstyle:hiddenField")
82 public void onGlobalContextUpdated(final SchemaContext context) {
83 Preconditions.checkNotNull(context);
84 schemaContext = context;
86 final Module ietfYangLibraryModule =
87 context.findModule(IetfYangLibrary.MODULE_QNAME).orElse(null);
88 if (ietfYangLibraryModule != null) {
89 NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
90 RestconfMappingNodeUtil.mapModulesByIetfYangLibraryYang(context.getModules(), ietfYangLibraryModule,
91 context, String.valueOf(this.moduleSetId.incrementAndGet()));
95 final Module monitoringModule =
96 schemaContext.findModule(MonitoringModule.MODULE_QNAME).orElse(null);
97 if (monitoringModule != null) {
98 NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
99 RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
105 public SchemaContext get() {
106 return schemaContext;
109 private void putData(
110 final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode) {
111 final DOMDataTreeWriteTransaction wTx = this.transactionChainHandler.get().newWriteOnlyTransaction();
112 wTx.put(LogicalDatastoreType.OPERATIONAL,
113 YangInstanceIdentifier.create(NodeIdentifier.create(normNode.getNodeType())), normNode);
116 } catch (InterruptedException e) {
117 throw new RestconfDocumentedException("Problem occurred while putting data to DS.", e);
118 } catch (ExecutionException e) {
119 final TransactionCommitFailedException failure = Throwables.getCauseAs(e,
120 TransactionCommitFailedException.class);
121 if (failure.getCause() instanceof ConflictingModificationAppliedException) {
123 * Ignore error when another cluster node is already putting the same data to DS.
124 * We expect that cluster is homogeneous and that node was going to write the same data
125 * (that means no retry is needed). Transaction chain reset must be invoked to be able
126 * to continue writing data with another transaction after failed transaction.
127 * This is workaround for bug https://bugs.opendaylight.org/show_bug.cgi?id=7728
129 LOG.warn("Ignoring that another cluster node is already putting the same data to DS.", e);
130 this.transactionChainHandler.reset();
132 throw new RestconfDocumentedException("Problem occurred while putting data to DS.", failure);