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 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.restconf.common.errors.RestconfDocumentedException;
16 import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
17 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
18 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.MonitoringModule;
19 import org.opendaylight.restconf.nb.rfc8040.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;
41 private static SchemaContext actualSchemaContext;
43 private int moduleSetId;
46 * Set module-set-id on initial value - 0.
48 * @param transactionChainHandler Transaction chain handler
50 public SchemaContextHandler(final TransactionChainHandler transactionChainHandler) {
51 this.transactionChainHandler = transactionChainHandler;
53 actualSchemaContext = null;
57 public void onGlobalContextUpdated(final SchemaContext context) {
58 Preconditions.checkNotNull(context);
60 this.context = context;
62 actualSchemaContext = context;
65 final Module ietfYangLibraryModule =
66 context.findModuleByNamespaceAndRevision(IetfYangLibrary.URI_MODULE, IetfYangLibrary.DATE);
67 NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
68 RestconfMappingNodeUtil.mapModulesByIetfYangLibraryYang(context.getModules(), ietfYangLibraryModule,
69 context, String.valueOf(this.moduleSetId));
72 final Module monitoringModule =
73 this.context.findModuleByNamespaceAndRevision(MonitoringModule.URI_MODULE, MonitoringModule.DATE);
74 normNode = RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
79 public SchemaContext get() {
83 public static SchemaContext getActualSchemaContext() {
84 return actualSchemaContext;
87 public static void setActualSchemaContext(final SchemaContext schemaContext) {
88 actualSchemaContext = schemaContext;
92 final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode) {
93 final DOMDataWriteTransaction wTx = this.transactionChainHandler.get().newWriteOnlyTransaction();
94 wTx.put(LogicalDatastoreType.OPERATIONAL,
95 YangInstanceIdentifier.create(NodeIdentifier.create(normNode.getNodeType())), normNode);
97 wTx.submit().checkedGet();
98 } catch (final TransactionCommitFailedException e) {
99 if (!(e.getCause() instanceof ConflictingModificationAppliedException)) {
100 throw new RestconfDocumentedException("Problem occurred while putting data to DS.", e);
104 Ignore error when another cluster node is already putting the same data to DS.
105 We expect that cluster is homogeneous and that node was going to write the same data
106 (that means no retry is needed). Transaction chain reset must be invoked to be able
107 to continue writing data with another transaction after failed transaction.
108 This is workaround for bug:
109 https://bugs.opendaylight.org/show_bug.cgi?id=7728
111 LOG.warn("Ignoring that another cluster node is already putting the same data to DS.", e);
112 RestConnectorProvider.resetTransactionChainForAdapaters(this.transactionChainHandler.get());