2 * Copyright (c) 2015 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.openflowplugin.impl.device;
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 com.google.common.util.concurrent.SettableFuture;
16 import io.netty.util.HashedWheelTimer;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.concurrent.Future;
23 import java.util.concurrent.TimeUnit;
24 import javax.annotation.CheckForNull;
25 import javax.annotation.Nonnull;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
32 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
33 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
34 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
35 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
36 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
37 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
38 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
39 import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
40 import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
41 import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
42 import org.opendaylight.openflowplugin.impl.rpc.RequestContextImpl;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCase;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCase;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63 import org.opendaylight.yangtools.yang.common.RpcError;
64 import org.opendaylight.yangtools.yang.common.RpcResult;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
71 public class DeviceManagerImpl implements DeviceManager {
73 private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerImpl.class);
75 private static final long TICK_DURATION = 500; // 0.5 sec.
77 private final RpcManager rpcManager;
78 private final DataBroker dataBroker;
79 private final HashedWheelTimer hashedWheelTimer;
80 private RequestContextStack dummyRequestContextStack;
81 private TranslatorLibrary translatorLibrary;
82 private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
85 public DeviceManagerImpl(@Nonnull final RpcManager rpcManager, @Nonnull final DataBroker dataBroker) {
86 this.rpcManager = Preconditions.checkNotNull(rpcManager);
87 this.dataBroker = Preconditions.checkNotNull(dataBroker);
88 hashedWheelTimer = new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, 10);
90 dummyRequestContextStack = new RequestContextStack() {
92 public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
97 public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> data) {
98 return data.getFuture();
102 public <T> RequestContext<T> createRequestContext() {
103 return new RequestContextImpl<>(this);
109 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
110 deviceInitPhaseHandler = handler;
114 public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
115 // final phase - we have to add new Device to MD-SAL DataStore
116 Preconditions.checkNotNull(deviceContext != null);
117 ((DeviceContextImpl) deviceContext).submitTransaction();
121 public void deviceConnected(@CheckForNull final ConnectionContext connectionContext) {
122 Preconditions.checkArgument(connectionContext != null);
123 final DeviceState deviceState = new DeviceStateImpl(connectionContext.getFeatures(), connectionContext.getNodeId());
124 final DeviceContextImpl deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker, hashedWheelTimer);
125 deviceContext.setTranslatorLibrary(translatorLibrary);
126 final OpenflowProtocolListenerFullImpl messageListener = new OpenflowProtocolListenerFullImpl(
127 connectionContext.getConnectionAdapter(), deviceContext);
128 connectionContext.getConnectionAdapter().setMessageListener(messageListener);
130 final Xid nodeDescXid = deviceContext.getNextXid();
131 final ListenableFuture<Collection<MultipartReply>> replyDesc = getNodeStaticInfo(nodeDescXid, messageListener,
132 MultipartType.OFPMPDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
134 final Xid nodeMeterXid = deviceContext.getNextXid();
135 final ListenableFuture<Collection<MultipartReply>> replyMeterFeature = getNodeStaticInfo(nodeMeterXid, messageListener,
136 MultipartType.OFPMPMETERFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
138 final Xid nodeGroupXid = deviceContext.getNextXid();
139 final ListenableFuture<Collection<MultipartReply>> replyGroupFeatures = getNodeStaticInfo(nodeGroupXid, messageListener,
140 MultipartType.OFPMPGROUPFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
142 final Xid nodeTableXid = deviceContext.getNextXid();
143 final ListenableFuture<Collection<MultipartReply>> replyTableFeatures = getNodeStaticInfo(nodeTableXid, messageListener,
144 MultipartType.OFPMPTABLEFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
146 final ListenableFuture<List<Collection<MultipartReply>>> deviceFeaturesFuture =
147 Futures.allAsList(Arrays.asList(replyDesc, replyMeterFeature, replyGroupFeatures, replyTableFeatures));
148 Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<Collection<MultipartReply>>>() {
150 public void onSuccess(final List<Collection<MultipartReply>> result) {
151 // wake up statistics
152 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
156 public void onFailure(final Throwable t) {
157 // FIXME : remove session
163 public TranslatorLibrary oook() {
164 return translatorLibrary;
168 public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
169 this.translatorLibrary = translatorLibrary;
172 private ListenableFuture<Collection<MultipartReply>> getNodeStaticInfo(final Xid xid,
173 final MultiMsgCollector multiMsgCollector, final MultipartType type, final DeviceContext dContext,
174 final InstanceIdentifier<Node> nodeII, final short version) {
175 final ListenableFuture<Collection<MultipartReply>> future = multiMsgCollector.registerMultipartMsg(xid.getValue());
177 final RequestContext<List<MultipartReply>> requestContext = dummyRequestContextStack.createRequestContext();
178 dContext.hookRequestCtx(xid, requestContext);
179 Futures.addCallback(requestContext.getFuture(), new FutureCallback<RpcResult<List<MultipartReply>>>() {
181 public void onSuccess(final RpcResult<List<MultipartReply>> rpcResult) {
182 final List<MultipartReply> result = rpcResult.getResult();
183 if (result != null) {
184 translateAndWriteReply(type, dContext, nodeII, result);
186 final Iterator<RpcError> rpcErrorIterator = rpcResult.getErrors().iterator();
187 while (rpcErrorIterator.hasNext()) {
188 final Throwable t = rpcErrorIterator.next().getCause();
189 LOG.info("Failed to retrieve static node {} info: {}", type, t.getMessage());
190 LOG.trace("Detailed error:", t);
192 if (MultipartType.OFPMPTABLE.equals(type)) {
193 makeEmptyTables(dContext, nodeII, dContext.getPrimaryConnectionContext().getFeatures().getTables());
199 public void onFailure(final Throwable t) {
200 LOG.info("Failed to retrieve static node {} info: {}", type, t.getMessage());
204 final Future<RpcResult<Void>> rpcFuture = dContext.getPrimaryConnectionContext().getConnectionAdapter()
205 .multipartRequest(MultipartRequestInputFactory.makeMultipartRequestInput(xid.getValue(), version, type));
206 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(rpcFuture), new FutureCallback<RpcResult<Void>>() {
208 public void onSuccess(final RpcResult<Void> result) {
213 public void onFailure(final Throwable t) {
220 // FIXME : remove after ovs tableFeatures fix
221 private static void makeEmptyTables(final DeviceContext dContext, final InstanceIdentifier<Node> nodeII, final Short nrOfTables) {
222 for (int i = 0; i < nrOfTables; i++) {
223 final short tId = (short) i;
224 final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tId));
225 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, new TableBuilder().setId(tId).build());
229 private static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext,
230 final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result) {
231 for (final MultipartReply reply : result) {
232 final MultipartReplyBody body = reply.getMultipartReplyBody();
235 Preconditions.checkArgument(body instanceof MultipartReplyDescCase);
236 final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc();
237 final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc);
238 final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
239 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
242 case OFPMPTABLEFEATURES:
243 Preconditions.checkArgument(body instanceof MultipartReplyTableFeaturesCase);
244 final MultipartReplyTableFeatures tableFeatures = ((MultipartReplyTableFeaturesCase) body).getMultipartReplyTableFeatures();
245 final List<TableFeatures> tables = NodeStaticReplyTranslatorUtil.nodeTableFeatureTranslator(tableFeatures);
246 for (final TableFeatures table : tables) {
247 final Short tableId = table.getTableId();
248 final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId));
249 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, new TableBuilder().setId(tableId).setTableFeatures(Collections.singletonList(table)).build());
253 case OFPMPMETERFEATURES:
254 Preconditions.checkArgument(body instanceof MultipartReplyMeterFeaturesCase);
255 final MultipartReplyMeterFeatures meterFeatures = ((MultipartReplyMeterFeaturesCase) body).getMultipartReplyMeterFeatures();
256 final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil.nodeMeterFeatureTranslator(meterFeatures);
257 final InstanceIdentifier<NodeMeterFeatures> mFeatureII = nodeII.augmentation(NodeMeterFeatures.class);
258 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature);
261 case OFPMPGROUPFEATURES:
262 Preconditions.checkArgument(body instanceof MultipartReplyGroupFeaturesCase);
263 final MultipartReplyGroupFeatures groupFeatures = ((MultipartReplyGroupFeaturesCase) body).getMultipartReplyGroupFeatures();
264 final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil.nodeGroupFeatureTranslator(groupFeatures);
265 final InstanceIdentifier<NodeGroupFeatures> gFeatureII = nodeII.augmentation(NodeGroupFeatures.class);
266 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature);
270 throw new IllegalArgumentException("Unnexpected MultipartType " + type);