2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.handlers;
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;
27 * Implementation of {@link SchemaContextHandler}
30 public class SchemaContextHandler implements SchemaContextListenerHandler {
32 private SchemaContext context;
34 private int moduleSetId;
35 private final TransactionChainHandler transactionChainHandler;
38 * Set module-set-id on initial value - 0
40 * @param transactionChainHandler
42 public SchemaContextHandler(final TransactionChainHandler transactionChainHandler) {
43 this.transactionChainHandler = transactionChainHandler;
48 public void onGlobalContextUpdated(final SchemaContext context) {
49 Preconditions.checkNotNull(context);
51 this.context = context;
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);
62 wTx.submit().checkedGet();
63 } catch (final TransactionCommitFailedException e) {
64 throw new RestconfDocumentedException("Problem occured while putting data to DS.", e);
69 public SchemaContext get() {