Fix bugs from first run
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceManagerImpl.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 package org.opendaylight.openflowplugin.impl.device;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import io.netty.util.HashedWheelTimer;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.TimeUnit;
22 import javax.annotation.CheckForNull;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
30 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
31 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceContextReadyHandler;
32 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
33 import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
34 import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCase;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCase;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.common.RpcResult;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 /**
60  *
61  */
62 public class DeviceManagerImpl implements DeviceManager {
63
64     private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerImpl.class);
65
66     private static final long TICK_DURATION = 500; // 0.5 sec.
67
68     private final RpcManager rpcManager;
69     private final DataBroker dataBroker;
70     private final HashedWheelTimer hashedWheelTimer;
71
72
73     public DeviceManagerImpl (@Nonnull final RpcManager rpcManager, @Nonnull final DataBroker dataBroker) {
74         this.rpcManager = Preconditions.checkNotNull(rpcManager);
75         this.dataBroker = Preconditions.checkNotNull(dataBroker);
76         hashedWheelTimer = new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, 10);
77     }
78
79     @Override
80     public void deviceConnected(@CheckForNull final ConnectionContext connectionContext) {
81         Preconditions.checkArgument(connectionContext != null);
82         final DeviceState deviceState = new DeviceStateImpl(connectionContext.getFeatures(), connectionContext.getNodeId());
83         final DeviceContext deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker, hashedWheelTimer);
84
85         final Xid nodeDescXid = deviceContext.getNextXid();
86         final ListenableFuture<Collection<MultipartReply>> replyDesc = getNodeStaticInfo(nodeDescXid, connectionContext,
87                 MultipartType.OFPMPDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
88
89         final Xid nodeMeterXid = deviceContext.getNextXid();
90         final ListenableFuture<Collection<MultipartReply>> replyMeterFeature = getNodeStaticInfo(nodeMeterXid, connectionContext,
91                 MultipartType.OFPMPMETERFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
92
93         final Xid nodeGroupXid = deviceContext.getNextXid();
94         final ListenableFuture<Collection<MultipartReply>> replyGroupFeatures = getNodeStaticInfo(nodeGroupXid, connectionContext,
95                 MultipartType.OFPMPGROUPFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
96
97         final Xid nodeTableXid = deviceContext.getNextXid();
98         final ListenableFuture<Collection<MultipartReply>> replyTableFeatures = getNodeStaticInfo(nodeTableXid, connectionContext,
99                 MultipartType.OFPMPTABLEFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
100
101         final ListenableFuture<List<Collection<MultipartReply>>> deviceFeaturesFuture =
102                 Futures.allAsList(Arrays.asList(replyDesc, replyMeterFeature, replyGroupFeatures, replyTableFeatures));
103         Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<Collection<MultipartReply>>>() {
104             @Override
105             public void onSuccess(final List<Collection<MultipartReply>> result) {
106                 // FIXME : add statistics
107                 rpcManager.deviceConnected(deviceContext);
108                 ((DeviceContextImpl) deviceContext).submitTransaction();
109             }
110
111             @Override
112             public void onFailure(final Throwable t) {
113                 // FIXME : remove session
114             }
115         });
116     }
117
118     @Override
119     public void addRequestContextReadyHandler(final DeviceContextReadyHandler deviceContextReadyHandler) {
120         // TODO Auto-generated method stub
121     }
122
123     private static ListenableFuture<Collection<MultipartReply>> getNodeStaticInfo(final Xid xid, final ConnectionContext cContext,
124             final MultipartType type, final DeviceContext dContext, final InstanceIdentifier<Node> nodeII, final short version) {
125         final ListenableFuture<Collection<MultipartReply>> future = cContext.registerMultipartMsg(xid.getValue());
126         Futures.addCallback(future, new FutureCallback<Collection<MultipartReply>>() {
127             @Override
128             public void onSuccess(final Collection<MultipartReply> result) {
129                 Preconditions.checkArgument(result != null);
130                 translateAndWriteReply(type, dContext, nodeII, result);
131             }
132             @Override
133             public void onFailure(final Throwable t) {
134                 // TODO : ovs TableFeatures are broken for yet so we have to add workaround
135                 if (MultipartType.OFPMPTABLE.equals(type)) {
136                     makeEmptyTables(dContext, nodeII, cContext.getFeatures().getTables());
137                 }
138                 LOG.info("Failed to retrieve static node {} info: {}", type, t.getMessage());
139             }
140         });
141         final Future<RpcResult<Void>> rpcFuture = cContext.getConnectionAdapter()
142                 .multipartRequest(MultipartRequestInputFactory.makeMultipartRequestInput(xid.getValue(), version, type));
143         Futures.addCallback(JdkFutureAdapters.listenInPoolThread(rpcFuture), new FutureCallback<RpcResult<Void>>() {
144             @Override
145             public void onSuccess(final RpcResult<Void> result) {
146                 // NOOP
147             }
148             @Override
149             public void onFailure(final Throwable t) {
150                 future.cancel(true);
151             }
152         });
153         return future;
154     }
155
156     // FIXME : remove after ovs tableFeatures fix
157     private static void makeEmptyTables(final DeviceContext dContext, final InstanceIdentifier<Node> nodeII, final Short nrOfTables) {
158         for (int i = 0; i < nrOfTables; i++) {
159             final short tId = (short) i;
160             final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tId));
161             dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, new TableBuilder().setId(tId).build());
162         }
163     }
164
165     private static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext,
166             final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result) {
167         for (final MultipartReply reply : result) {
168             final MultipartReplyBody body = reply.getMultipartReplyBody();
169             switch (type) {
170             case OFPMPDESC:
171                 Preconditions.checkArgument(body instanceof MultipartReplyDescCase);
172                 final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc();
173                 final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc);
174                 final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
175                 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
176                 break;
177
178             case OFPMPTABLEFEATURES:
179                 Preconditions.checkArgument(body instanceof MultipartReplyTableFeaturesCase);
180                 final MultipartReplyTableFeatures tableFeatures = ((MultipartReplyTableFeaturesCase) body).getMultipartReplyTableFeatures();
181                 final List<TableFeatures> tables = NodeStaticReplyTranslatorUtil.nodeTableFeatureTranslator(tableFeatures);
182                 for (final TableFeatures table : tables) {
183                     final Short tableId = table.getTableId();
184                     final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId));
185                     dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, new TableBuilder().setId(tableId).setTableFeatures(Collections.singletonList(table)).build());
186                 }
187                 break;
188
189             case OFPMPMETERFEATURES:
190                 Preconditions.checkArgument(body instanceof MultipartReplyMeterFeaturesCase);
191                 final MultipartReplyMeterFeatures meterFeatures = ((MultipartReplyMeterFeaturesCase) body).getMultipartReplyMeterFeatures();
192                 final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil.nodeMeterFeatureTranslator(meterFeatures);
193                 final InstanceIdentifier<NodeMeterFeatures> mFeatureII = nodeII.augmentation(NodeMeterFeatures.class);
194                 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature);
195                 break;
196
197             case OFPMPGROUPFEATURES:
198                 Preconditions.checkArgument(body instanceof MultipartReplyGroupFeaturesCase);
199                 final MultipartReplyGroupFeatures groupFeatures = ((MultipartReplyGroupFeaturesCase) body).getMultipartReplyGroupFeatures();
200                 final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil.nodeGroupFeatureTranslator(groupFeatures);
201                 final InstanceIdentifier<NodeGroupFeatures> gFeatureII = nodeII.augmentation(NodeGroupFeatures.class);
202                 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature);
203                 break;
204
205             default:
206                 throw new IllegalArgumentException("Unnexpected MultipartType " + type);
207             }
208         }
209     }
210 }