04d5e5e449e1f00305a8be995b3861d7ef24e47b
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / NetconfDeviceDatastoreAdapter.java
1 /*
2  * Copyright (c) 2014 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.controller.sal.connect.netconf.sal;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.FluentIterable;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.util.Set;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.inventory.rev140108.NetconfNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.inventory.rev140108.NetconfNodeBuilder;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Asynchronous (Binding-aware) adapter over datastore subtree for netconf device.
38  *
39  * All data changes are submitted to an ExecutorService to avoid Thread blocking while sal is waiting for schema.
40  */
41 final class NetconfDeviceDatastoreAdapter implements AutoCloseable {
42
43     private static final Logger logger  = LoggerFactory.getLogger(NetconfDeviceDatastoreAdapter.class);
44
45     private final RemoteDeviceId id;
46     private final DataBroker dataService;
47
48     NetconfDeviceDatastoreAdapter(final RemoteDeviceId deviceId, final DataBroker dataService) {
49         this.id = Preconditions.checkNotNull(deviceId);
50         this.dataService = Preconditions.checkNotNull(dataService);
51
52         initDeviceData();
53     }
54
55     public void updateDeviceState(final boolean up, final Set<QName> capabilities) {
56         final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node data = buildDataForDeviceState(
57                 up, capabilities, id);
58
59         final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
60         logger.trace("{}: Update device state transaction {} merging operational data started.", id, transaction.getIdentifier());
61         transaction.merge(LogicalDatastoreType.OPERATIONAL, id.getBindingPath(), data);
62         logger.trace("{}: Update device state transaction {} merging operational data ended.", id, transaction.getIdentifier());
63
64         commitTransaction(transaction, "update");
65     }
66
67     private void removeDeviceConfigAndState() {
68         final WriteTransaction transaction = dataService.newWriteOnlyTransaction();
69         logger.trace("{}: Close device state transaction {} removing all data started.", id, transaction.getIdentifier());
70         transaction.delete(LogicalDatastoreType.CONFIGURATION, id.getBindingPath());
71         transaction.delete(LogicalDatastoreType.OPERATIONAL, id.getBindingPath());
72         logger.trace("{}: Close device state transaction {} removing all data ended.", id, transaction.getIdentifier());
73
74         commitTransaction(transaction, "close");
75     }
76
77     private void initDeviceData() {
78         final WriteTransaction transaction = dataService.newWriteOnlyTransaction();
79
80         createNodesListIfNotPresent(transaction);
81
82         final InstanceIdentifier<Node> path = id.getBindingPath();
83         final Node nodeWithId = getNodeWithId(id);
84
85         logger.trace("{}: Init device state transaction {} putting if absent operational data started.", id, transaction.getIdentifier());
86         transaction.merge(LogicalDatastoreType.OPERATIONAL, path, nodeWithId);
87         logger.trace("{}: Init device state transaction {} putting operational data ended.", id, transaction.getIdentifier());
88
89         logger.trace("{}: Init device state transaction {} putting if absent config data started.", id, transaction.getIdentifier());
90         transaction.merge(LogicalDatastoreType.CONFIGURATION, path, nodeWithId);
91         logger.trace("{}: Init device state transaction {} putting config data ended.", id, transaction.getIdentifier());
92
93         commitTransaction(transaction, "init");
94     }
95
96     private void createNodesListIfNotPresent(final WriteTransaction writeTx) {
97         final Nodes nodes = new NodesBuilder().build();
98         final InstanceIdentifier<Nodes> path = InstanceIdentifier.builder(Nodes.class).build();
99         logger.trace("{}: Merging {} container to ensure its presence", id, Nodes.QNAME, writeTx.getIdentifier());
100         writeTx.merge(LogicalDatastoreType.CONFIGURATION, path, nodes);
101         writeTx.merge(LogicalDatastoreType.OPERATIONAL, path, nodes);
102     }
103
104     private void commitTransaction(final WriteTransaction transaction, final String txType) {
105         logger.trace("{}: Committing Transaction {}:{}", id, txType, transaction.getIdentifier());
106         final CheckedFuture<Void, TransactionCommitFailedException> result = transaction.submit();
107
108         Futures.addCallback(result, new FutureCallback<Void>() {
109             @Override
110             public void onSuccess(final Void result) {
111                 logger.trace("{}: Transaction({}) {} SUCCESSFUL", id, txType, transaction.getIdentifier());
112             }
113
114             @Override
115             public void onFailure(final Throwable t) {
116                 logger.error("{}: Transaction({}) {} FAILED!", id, txType, transaction.getIdentifier(), t);
117                 throw new IllegalStateException(id + "  Transaction(" + txType + ") not committed correctly", t);
118             }
119         });
120
121     }
122
123     @Override
124     public void close() throws Exception {
125         removeDeviceConfigAndState();
126     }
127
128     public static org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node buildDataForDeviceState(
129             final boolean up, final Set<QName> capabilities, final RemoteDeviceId id) {
130
131         final NodeBuilder nodeBuilder = getNodeWithIdBuilder(id);
132         final NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
133         netconfNodeBuilder.setConnected(up);
134         netconfNodeBuilder.setInitialCapability(FluentIterable.from(capabilities)
135                 .transform(new Function<QName, String>() {
136                     @Override
137                     public String apply(final QName input) {
138                         return input.toString();
139                     }
140                 }).toList());
141         nodeBuilder.addAugmentation(NetconfNode.class, netconfNodeBuilder.build());
142
143         return nodeBuilder.build();
144     }
145
146     private static ListenableFuture<Optional<Node>> readNodeData(
147             final LogicalDatastoreType store,
148             final ReadWriteTransaction transaction,
149             final InstanceIdentifier<Node> path) {
150         return transaction.read(store, path);
151     }
152
153     private static Node getNodeWithId(final RemoteDeviceId id) {
154         final NodeBuilder nodeBuilder = getNodeWithIdBuilder(id);
155         return nodeBuilder.build();
156     }
157
158     private static NodeBuilder getNodeWithIdBuilder(final RemoteDeviceId id) {
159         final NodeBuilder nodeBuilder = new NodeBuilder();
160         nodeBuilder.setKey(id.getBindingKey());
161         nodeBuilder.setId(id.getBindingKey().getId());
162         return nodeBuilder;
163     }
164 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.