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