1d9e7c91700fb8ea682aa08ab726e42e708c2cd7
[netconf.git] / opendaylight / netconf / netconf-topology / src / main / java / org / opendaylight / netconf / topology / impl / NetconfNodeOperationalDataAggregator.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.netconf.topology.impl;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.netconf.topology.StateAggregator;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.ClusteredConnectionStatusBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.clustered.connection.status.NodeStatus;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class NetconfNodeOperationalDataAggregator implements StateAggregator{
29
30     private static final Logger LOG = LoggerFactory.getLogger(NetconfNodeOperationalDataAggregator.class);
31
32     @Override
33     public ListenableFuture<Node> combineCreateAttempts(final List<ListenableFuture<Node>> stateFutures) {
34         final SettableFuture<Node> future = SettableFuture.create();
35         final ListenableFuture<List<Node>> allAsList = Futures.allAsList(stateFutures);
36         Futures.addCallback(allAsList, new FutureCallback<List<Node>>() {
37             @Override
38             public void onSuccess(final List<Node> result) {
39                 Node base = null;
40                 NetconfNode baseAugmentation = null;
41                 final ArrayList<NodeStatus> statusList = new ArrayList<>();
42                 for (final Node node : result) {
43                     final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
44                     if (base == null && netconfNode.getConnectionStatus().equals(ConnectionStatus.Connected)) {
45                         base = node;
46                         baseAugmentation = netconfNode;
47                     }
48                     LOG.debug(netconfNode.toString());
49                     statusList.addAll(netconfNode.getClusteredConnectionStatus().getNodeStatus());
50                 }
51
52                 if (base == null) {
53                     base = result.get(0);
54                     baseAugmentation = result.get(0).getAugmentation(NetconfNode.class);
55                     LOG.warn("All results {}", result.toString());
56                 }
57
58                 LOG.warn("Base node: {}", base);
59
60                 final Node aggregatedNode =
61                         new NodeBuilder(base)
62                                 .addAugmentation(NetconfNode.class,
63                                         new NetconfNodeBuilder(baseAugmentation)
64                                                 .setClusteredConnectionStatus(
65                                                         new ClusteredConnectionStatusBuilder()
66                                                                 .setNodeStatus(statusList)
67                                                                 .build())
68                                                 .build())
69                                 .build();
70                 future.set(aggregatedNode);
71             }
72
73             @Override
74             public void onFailure(final Throwable t) {
75                 LOG.error("One of the combined create attempts failed {}", t);
76                 future.setException(t);
77             }
78         });
79         return future;
80     }
81
82     @Override
83     public ListenableFuture<Node> combineUpdateAttempts(final List<ListenableFuture<Node>> stateFutures) {
84         final SettableFuture<Node> future = SettableFuture.create();
85         final ListenableFuture<List<Node>> allAsList = Futures.allAsList(stateFutures);
86         Futures.addCallback(allAsList, new FutureCallback<List<Node>>() {
87             @Override
88             public void onSuccess(final List<Node> result) {
89                 Node base = null;
90                 NetconfNode baseAugmentation = null;
91                 final ArrayList<NodeStatus> statusList = new ArrayList<>();
92                 for (final Node node : result) {
93                     final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
94                     if (base == null && netconfNode.getConnectionStatus().equals(ConnectionStatus.Connected)) {
95                         base = node;
96                         baseAugmentation = netconfNode;
97                     }
98                     LOG.debug(netconfNode.toString());
99                     statusList.addAll(netconfNode.getClusteredConnectionStatus().getNodeStatus());
100                 }
101
102                 if (base == null) {
103                     base = result.get(0);
104                     baseAugmentation = result.get(0).getAugmentation(NetconfNode.class);
105                     LOG.warn("All results {}", result.toString());
106                 }
107
108                 final Node aggregatedNode =
109                         new NodeBuilder(base)
110                                 .addAugmentation(NetconfNode.class,
111                                         new NetconfNodeBuilder(baseAugmentation)
112                                                 .setClusteredConnectionStatus(
113                                                         new ClusteredConnectionStatusBuilder()
114                                                                 .setNodeStatus(statusList)
115                                                                 .build())
116                                                 .build())
117                                 .build();
118                 future.set(aggregatedNode);
119             }
120
121             @Override
122             public void onFailure(final Throwable t) {
123                 LOG.error("One of the combined update attempts failed {}", t);
124                 future.setException(t);
125             }
126         });
127         return future;
128     }
129
130     @Override
131     public ListenableFuture<Void> combineDeleteAttempts(final List<ListenableFuture<Void>> stateFutures) {
132         final SettableFuture<Void> future = SettableFuture.create();
133         final ListenableFuture<List<Void>> allAsList = Futures.allAsList(stateFutures);
134         Futures.addCallback(allAsList, new FutureCallback<List<Void>>() {
135             @Override
136             public void onSuccess(final List<Void> result) {
137                 future.set(null);
138             }
139
140             @Override
141             public void onFailure(final Throwable t) {
142                 LOG.error("One of the combined delete attempts failed {}", t);
143                 future.setException(t);
144             }
145         });
146         return future;
147     }
148 }