Merge "Allow no payload for RPCs with no input"
[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 com.google.common.base.Throwables;
12 import java.util.Collection;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.atomic.AtomicInteger;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
18 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
21 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.MonitoringModule;
22 import org.opendaylight.restconf.nb.rfc8040.utils.mapping.RestconfMappingNodeUtil;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Implementation of {@link SchemaContextHandler}.
38  *
39  */
40 @SuppressWarnings("checkstyle:FinalClass")
41 public class SchemaContextHandler implements SchemaContextListenerHandler, AutoCloseable {
42
43     private static final Logger LOG = LoggerFactory.getLogger(SchemaContextHandler.class);
44
45     private final AtomicInteger moduleSetId = new AtomicInteger(0);
46
47     private final TransactionChainHandler transactionChainHandler;
48     private final DOMSchemaService domSchemaService;
49     private ListenerRegistration<SchemaContextListener> listenerRegistration;
50
51     private volatile SchemaContext schemaContext;
52
53     /**
54      * Constructor.
55      *
56      * @param transactionChainHandler Transaction chain handler
57      */
58     private SchemaContextHandler(final TransactionChainHandler transactionChainHandler,
59             final DOMSchemaService domSchemaService) {
60         this.transactionChainHandler = transactionChainHandler;
61         this.domSchemaService = domSchemaService;
62     }
63
64     public static SchemaContextHandler newInstance(final TransactionChainHandler transactionChainHandler,
65             final DOMSchemaService domSchemaService) {
66         return new SchemaContextHandler(transactionChainHandler, domSchemaService);
67     }
68
69     public void init() {
70         listenerRegistration = domSchemaService.registerSchemaContextListener(this);
71     }
72
73     @Override
74     public void close() {
75         if (listenerRegistration != null) {
76             listenerRegistration.close();
77         }
78     }
79
80     @Override
81     @SuppressWarnings("checkstyle:hiddenField")
82     public void onGlobalContextUpdated(final SchemaContext context) {
83         Preconditions.checkNotNull(context);
84         schemaContext = context;
85
86         final Module ietfYangLibraryModule =
87                 context.findModule(IetfYangLibrary.MODULE_QNAME).orElse(null);
88         if (ietfYangLibraryModule != null) {
89             NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
90                     RestconfMappingNodeUtil.mapModulesByIetfYangLibraryYang(context.getModules(), ietfYangLibraryModule,
91                             context, String.valueOf(this.moduleSetId.incrementAndGet()));
92             putData(normNode);
93         }
94
95         final Module monitoringModule =
96                 schemaContext.findModule(MonitoringModule.MODULE_QNAME).orElse(null);
97         if (monitoringModule != null) {
98             NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
99                     RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
100             putData(normNode);
101         }
102     }
103
104     @Override
105     public SchemaContext get() {
106         return schemaContext;
107     }
108
109     private void putData(
110             final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode) {
111         final DOMDataTreeWriteTransaction wTx = this.transactionChainHandler.get().newWriteOnlyTransaction();
112         wTx.put(LogicalDatastoreType.OPERATIONAL,
113                 YangInstanceIdentifier.create(NodeIdentifier.create(normNode.getNodeType())), normNode);
114         try {
115             wTx.commit().get();
116         } catch (InterruptedException e) {
117             throw new RestconfDocumentedException("Problem occurred while putting data to DS.", e);
118         } catch (ExecutionException e) {
119             final TransactionCommitFailedException failure = Throwables.getCauseAs(e,
120                 TransactionCommitFailedException.class);
121             if (failure.getCause() instanceof ConflictingModificationAppliedException) {
122                 /*
123                  * Ignore error when another cluster node is already putting the same data to DS.
124                  * We expect that cluster is homogeneous and that node was going to write the same data
125                  * (that means no retry is needed). Transaction chain reset must be invoked to be able
126                  * to continue writing data with another transaction after failed transaction.
127                  * This is workaround for bug https://bugs.opendaylight.org/show_bug.cgi?id=7728
128                  */
129                 LOG.warn("Ignoring that another cluster node is already putting the same data to DS.", e);
130                 this.transactionChainHandler.reset();
131             } else {
132                 throw new RestconfDocumentedException("Problem occurred while putting data to DS.", failure);
133             }
134         }
135     }
136 }