c9f849bc4eb007ba2184454cb6c9fbdef21f3e91
[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.RestconfDocumentedException;
16 import org.opendaylight.restconf.Draft18.IetfYangLibrary;
17 import org.opendaylight.restconf.Draft18.MonitoringModule;
18 import org.opendaylight.restconf.utils.mapping.RestconfMappingNodeUtil;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26
27 /**
28  * Implementation of {@link SchemaContextHandler}
29  *
30  */
31 public class SchemaContextHandler implements SchemaContextListenerHandler {
32
33     private SchemaContext context;
34
35     private int moduleSetId;
36     private final TransactionChainHandler transactionChainHandler;
37
38     /**
39      * Set module-set-id on initial value - 0
40      *
41      * @param transactionChainHandler
42      */
43     public SchemaContextHandler(final TransactionChainHandler transactionChainHandler) {
44         this.transactionChainHandler = transactionChainHandler;
45         this.moduleSetId = 0;
46     }
47
48     @Override
49     public void onGlobalContextUpdated(final SchemaContext context) {
50         Preconditions.checkNotNull(context);
51         this.context = null;
52         this.context = context;
53         this.moduleSetId++;
54         final Module ietfYangLibraryModule =
55                 context.findModuleByNamespaceAndRevision(IetfYangLibrary.URI_MODULE, IetfYangLibrary.DATE);
56         NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode =
57                 RestconfMappingNodeUtil.mapModulesByIetfYangLibraryYang(context.getModules(), ietfYangLibraryModule,
58                         context, String.valueOf(this.moduleSetId));
59         putData(normNode);
60
61         final Module monitoringModule =
62                 this.context.findModuleByNamespaceAndRevision(MonitoringModule.URI_MODULE, MonitoringModule.DATE);
63         normNode = RestconfMappingNodeUtil.mapCapabilites(monitoringModule);
64         putData(normNode);
65     }
66
67     @Override
68     public SchemaContext get() {
69         return this.context;
70     }
71
72     private void putData(
73             final NormalizedNode<NodeIdentifier, Collection<DataContainerChild<? extends PathArgument, ?>>> normNode) {
74         final DOMDataWriteTransaction wTx = this.transactionChainHandler.get().newWriteOnlyTransaction();
75         wTx.put(LogicalDatastoreType.OPERATIONAL,
76                 YangInstanceIdentifier.create(NodeIdentifier.create(normNode.getNodeType())), normNode);
77         try {
78             wTx.submit().checkedGet();
79         } catch (final TransactionCommitFailedException e) {
80             throw new RestconfDocumentedException("Problem occured while putting data to DS.", e);
81         }
82     }
83 }