Split Restconf implementations (draft02 and RFC) - providers tests
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / 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.nb.rfc8040.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.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;
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     private static SchemaContext actualSchemaContext;
42
43     private int moduleSetId;
44
45     /**
46      * Set module-set-id on initial value - 0.
47      *
48      * @param transactionChainHandler Transaction chain handler
49      */
50     public SchemaContextHandler(final TransactionChainHandler transactionChainHandler) {
51         this.transactionChainHandler = transactionChainHandler;
52         this.moduleSetId = 0;
53         actualSchemaContext = null;
54     }
55
56     @Override
57     public void onGlobalContextUpdated(final SchemaContext context) {
58         Preconditions.checkNotNull(context);
59         this.context = null;
60         this.context = context;
61
62         actualSchemaContext = context;
63
64         this.moduleSetId++;
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));
70         putData(normNode);
71
72         final Module monitoringModule =
73                 this.context.findModuleByNamespaceAndRevision(MonitoringModule.URI_MODULE, MonitoringModule.DATE);
74         normNode = RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
75         putData(normNode);
76     }
77
78     @Override
79     public SchemaContext get() {
80         return this.context;
81     }
82
83     public static SchemaContext getActualSchemaContext() {
84         return actualSchemaContext;
85     }
86
87     public static void setActualSchemaContext(final SchemaContext schemaContext) {
88         actualSchemaContext = schemaContext;
89     }
90
91     private void putData(
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);
96         try {
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);
101             }
102
103             /*
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
110             */
111             LOG.warn("Ignoring that another cluster node is already putting the same data to DS.", e);
112             RestConnectorProvider.resetTransactionChainForAdapaters(this.transactionChainHandler.get());
113         }
114     }
115 }