e4ebf373248ecca3095d87f6a3ca6c3569fda760
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Throwables;
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.mdsal.dom.api.DOMTransactionChain;
25 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
26 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
27 import org.opendaylight.restconf.nb.rfc8040.utils.mapping.RestconfMappingNodeUtil;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.RestconfState;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Implementation of {@link SchemaContextHandler}.
42  */
43 // FIXME: this really is a service which is maintaining ietf-yang-library contents inside the datastore. It really
44 //        should live in MD-SAL and be a dynamic store fragment. As a first step we should be turning this into a
45 //        completely standalone application.
46 @Singleton
47 public class SchemaContextHandler implements EffectiveModelContextListener, AutoCloseable {
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<?> listenerRegistration;
55
56     private volatile EffectiveModelContext 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     @PostConstruct
71     public void init() {
72         listenerRegistration = domSchemaService.registerSchemaContextListener(this);
73     }
74
75     @Override
76     @PreDestroy
77     public void close() {
78         if (listenerRegistration != null) {
79             listenerRegistration.close();
80         }
81     }
82
83     @Override
84     public void onModelContextUpdated(final EffectiveModelContext context) {
85         schemaContext = requireNonNull(context);
86
87         if (context.findModule(IetfYangLibrary.MODULE_QNAME).isPresent()) {
88             putData(RestconfMappingNodeUtil.mapModulesByIetfYangLibraryYang(context.getModules(), context,
89                 String.valueOf(this.moduleSetId.incrementAndGet())));
90         }
91
92         final Module monitoringModule = schemaContext.findModule(RestconfState.QNAME.getModule()).orElse(null);
93         if (monitoringModule != null) {
94             putData(RestconfMappingNodeUtil.mapCapabilites(monitoringModule));
95         }
96     }
97
98     public EffectiveModelContext get() {
99         return schemaContext;
100     }
101
102     private void putData(final ContainerNode normNode) {
103         final DOMTransactionChain transactionChain = this.transactionChainHandler.get();
104         final DOMDataTreeWriteTransaction wTx = transactionChain.newWriteOnlyTransaction();
105         wTx.put(LogicalDatastoreType.OPERATIONAL,
106                 YangInstanceIdentifier.create(NodeIdentifier.create(normNode.getIdentifier().getNodeType())), normNode);
107         try {
108             wTx.commit().get();
109         } catch (InterruptedException e) {
110             throw new RestconfDocumentedException("Problem occurred while putting data to DS.", e);
111         } catch (ExecutionException e) {
112             final TransactionCommitFailedException failure = Throwables.getCauseAs(e,
113                 TransactionCommitFailedException.class);
114             if (failure.getCause() instanceof ConflictingModificationAppliedException) {
115                 /*
116                  * Ignore error when another cluster node is already putting the same data to DS.
117                  * We expect that cluster is homogeneous and that node was going to write the same data
118                  * (that means no retry is needed). Transaction chain reset must be invoked to be able
119                  * to continue writing data with another transaction after failed transaction.
120                  * This is workaround for bug https://bugs.opendaylight.org/show_bug.cgi?id=7728
121                  */
122                 LOG.warn("Ignoring that another cluster node is already putting the same data to DS.", e);
123             } else {
124                 throw new RestconfDocumentedException("Problem occurred while putting data to DS.", failure);
125             }
126         } finally {
127             transactionChain.close();
128         }
129     }
130 }