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.handlers;
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
15 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
16 import org.opendaylight.restconf.RestConnectorProvider;
17 import org.opendaylight.restconf.Rfc8040.IetfYangLibrary;
18 import org.opendaylight.restconf.Rfc8040.MonitoringModule;
19 import org.opendaylight.restconf.utils.mapping.RestconfMappingNodeUtil;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Implementation of {@link SchemaContextHandler}
35 public class SchemaContextHandler implements SchemaContextListenerHandler {
37 private static final Logger LOG = LoggerFactory.getLogger(SchemaContextHandler.class);
39 private final TransactionChainHandler transactionChainHandler;
40 private SchemaContext context;
42 private int moduleSetId;
45 * Set module-set-id on initial value - 0
47 * @param transactionChainHandler
49 public SchemaContextHandler(final TransactionChainHandler transactionChainHandler) {
50 this.transactionChainHandler = transactionChainHandler;
55 public void onGlobalContextUpdated(final SchemaContext context) {
56 Preconditions.checkNotNull(context);
58 this.context = context;
60 final Module ietfYangLibraryModule =
61 context.findModuleByNamespaceAndRevision(IetfYangLibrary.URI_MODULE, IetfYangLibrary.DATE);
62 NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
63 RestconfMappingNodeUtil.mapModulesByIetfYangLibraryYang(context.getModules(), ietfYangLibraryModule,
64 context, String.valueOf(this.moduleSetId));
67 final Module monitoringModule =
68 this.context.findModuleByNamespaceAndRevision(MonitoringModule.URI_MODULE, MonitoringModule.DATE);
69 normNode = RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
74 public SchemaContext get() {
79 final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode) {
80 final DOMDataWriteTransaction wTx = this.transactionChainHandler.get().newWriteOnlyTransaction();
81 wTx.put(LogicalDatastoreType.OPERATIONAL,
82 YangInstanceIdentifier.create(NodeIdentifier.create(normNode.getNodeType())), normNode);
84 wTx.submit().checkedGet();
85 } catch (final TransactionCommitFailedException e) {
86 if (!(e.getCause() instanceof ConflictingModificationAppliedException)) {
87 throw new RestconfDocumentedException("Problem occurred while putting data to DS.", e);
91 Ignore error when another cluster node is already putting the same data to DS.
92 We expect that cluster is homogeneous and that node was going to write the same data
93 (that means no retry is needed). Transaction chain reset must be invoked to be able
94 to continue writing data with another transaction after failed transaction.
95 This is workaround for bug:
96 https://bugs.opendaylight.org/show_bug.cgi?id=7728
98 LOG.warn("Ignoring that another cluster node is already putting the same data to DS.", e);
99 RestConnectorProvider.resetTransactionChainForAdapaters(this.transactionChainHandler.get());