Set schemas to global context
[netconf.git] / restconf / sal-rest-connector / 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.ControllerContext;
16 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
17 import org.opendaylight.restconf.RestConnectorProvider;
18 import org.opendaylight.restconf.Rfc8040.IetfYangLibrary;
19 import org.opendaylight.restconf.Rfc8040.MonitoringModule;
20 import org.opendaylight.restconf.utils.mapping.RestconfMappingNodeUtil;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
24 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Implementation of {@link SchemaContextHandler}.
34  *
35  */
36 public class SchemaContextHandler implements SchemaContextListenerHandler {
37
38     private static final Logger LOG = LoggerFactory.getLogger(SchemaContextHandler.class);
39
40     private final TransactionChainHandler transactionChainHandler;
41     private SchemaContext context;
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     }
54
55     @Override
56     public void onGlobalContextUpdated(final SchemaContext context) {
57         Preconditions.checkNotNull(context);
58         ControllerContext.getInstance().setSchemas(context);
59         this.context = null;
60         this.context = context;
61         this.moduleSetId++;
62         final Module ietfYangLibraryModule =
63                 context.findModuleByNamespaceAndRevision(IetfYangLibrary.URI_MODULE, IetfYangLibrary.DATE);
64         NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
65                 RestconfMappingNodeUtil.mapModulesByIetfYangLibraryYang(context.getModules(), ietfYangLibraryModule,
66                         context, String.valueOf(this.moduleSetId));
67         putData(normNode);
68
69         final Module monitoringModule =
70                 this.context.findModuleByNamespaceAndRevision(MonitoringModule.URI_MODULE, MonitoringModule.DATE);
71         normNode = RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
72         putData(normNode);
73     }
74
75     @Override
76     public SchemaContext get() {
77         return this.context;
78     }
79
80     private void putData(
81             final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode) {
82         final DOMDataWriteTransaction wTx = this.transactionChainHandler.get().newWriteOnlyTransaction();
83         wTx.put(LogicalDatastoreType.OPERATIONAL,
84                 YangInstanceIdentifier.create(NodeIdentifier.create(normNode.getNodeType())), normNode);
85         try {
86             wTx.submit().checkedGet();
87         } catch (final TransactionCommitFailedException e) {
88             if (!(e.getCause() instanceof ConflictingModificationAppliedException)) {
89                 throw new RestconfDocumentedException("Problem occurred while putting data to DS.", e);
90             }
91
92             /*
93               Ignore error when another cluster node is already putting the same data to DS.
94               We expect that cluster is homogeneous and that node was going to write the same data
95               (that means no retry is needed). Transaction chain reset must be invoked to be able
96               to continue writing data with another transaction after failed transaction.
97               This is workaround for bug:
98               https://bugs.opendaylight.org/show_bug.cgi?id=7728
99             */
100             LOG.warn("Ignoring that another cluster node is already putting the same data to DS.", e);
101             RestConnectorProvider.resetTransactionChainForAdapaters(this.transactionChainHandler.get());
102         }
103     }
104 }