4d35fd92c942a30f8c7f86d6b6f5e8abd32c17d8
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / restconf / handlers / SchemaContextHandler.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.handlers;
9
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;
30
31 /**
32  * Implementation of {@link SchemaContextHandler}.
33  *
34  */
35 public class SchemaContextHandler implements SchemaContextListenerHandler {
36
37     private static final Logger LOG = LoggerFactory.getLogger(SchemaContextHandler.class);
38
39     private final TransactionChainHandler transactionChainHandler;
40     private SchemaContext context;
41
42     private int moduleSetId;
43
44     /**
45      * Set module-set-id on initial value - 0.
46      *
47      * @param transactionChainHandler Transaction chain handler
48      */
49     public SchemaContextHandler(final TransactionChainHandler transactionChainHandler) {
50         this.transactionChainHandler = transactionChainHandler;
51         this.moduleSetId = 0;
52     }
53
54     @Override
55     public void onGlobalContextUpdated(final SchemaContext context) {
56         Preconditions.checkNotNull(context);
57         this.context = null;
58         this.context = context;
59         this.moduleSetId++;
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));
65         putData(normNode);
66
67         final Module monitoringModule =
68                 this.context.findModuleByNamespaceAndRevision(MonitoringModule.URI_MODULE, MonitoringModule.DATE);
69         normNode = RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
70         putData(normNode);
71     }
72
73     @Override
74     public SchemaContext get() {
75         return this.context;
76     }
77
78     private void putData(
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);
83         try {
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);
88             }
89
90             /*
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
97             */
98             LOG.warn("Ignoring that another cluster node is already putting the same data to DS.", e);
99             RestConnectorProvider.resetTransactionChainForAdapaters(this.transactionChainHandler.get());
100         }
101     }
102 }