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