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.math.BigInteger;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.concurrent.TimeUnit;
25 import javax.annotation.CheckForNull;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
31 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
33 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
34 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
35 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
36 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
37 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
38 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
39 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
40 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
41 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
42 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
43 import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
44 import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
45 import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
46 import org.opendaylight.openflowplugin.impl.rpc.RequestContextImpl;
47 import org.opendaylight.openflowplugin.impl.services.OFJResult2RequestCtxFuture;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCase;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCase;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortDescCase;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.MultipartReplyPortDesc;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.multipart.reply.port.desc.Ports;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
82 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
83 import org.opendaylight.yangtools.yang.common.RpcError;
84 import org.opendaylight.yangtools.yang.common.RpcResult;
85 import org.slf4j.Logger;
86 import org.slf4j.LoggerFactory;
91 public class DeviceManagerImpl implements DeviceManager, AutoCloseable {
93 private static final Logger LOG = LoggerFactory.getLogger(DeviceManagerImpl.class);
95 private static final long TICK_DURATION = 500; // 0.5 sec.
97 private final DataBroker dataBroker;
98 private final HashedWheelTimer hashedWheelTimer;
99 private RequestContextStack dummyRequestContextStack;
100 private TranslatorLibrary translatorLibrary;
101 private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
102 private NotificationProviderService notificationService;
103 private final List<DeviceContext> synchronizedDeviceContextsList = Collections
104 .<DeviceContext>synchronizedList(new ArrayList<DeviceContext>());
106 public DeviceManagerImpl(@Nonnull final DataBroker dataBroker) {
107 this.dataBroker = Preconditions.checkNotNull(dataBroker);
108 hashedWheelTimer = new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, 10);
109 /* merge empty nodes to oper DS to predict any problems with missing parent for Node */
110 final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
111 tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), new NodesBuilder().build());
114 dummyRequestContextStack = new RequestContextStack() {
116 public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
121 public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> data) {
122 return data.getFuture();
126 public <T> RequestContext<T> createRequestContext() {
127 return new RequestContextImpl<>(this);
133 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
134 deviceInitPhaseHandler = handler;
138 public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
139 // final phase - we have to add new Device to MD-SAL DataStore
140 Preconditions.checkNotNull(deviceContext != null);
141 ((DeviceContextImpl) deviceContext).submitTransaction();
142 new BarrierTaskBuilder(deviceContext).buildAndFireBarrierTask();
146 public void deviceConnected(@CheckForNull final ConnectionContext connectionContext) {
147 Preconditions.checkArgument(connectionContext != null);
149 final DeviceState deviceState = new DeviceStateImpl(connectionContext.getFeatures(), connectionContext.getNodeId());
151 final DeviceContextImpl deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker, hashedWheelTimer);
153 deviceContext.setNotificationService(notificationService);
154 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, deviceState.getNodeInstanceIdentifier(), new NodeBuilder().setId(deviceState.getNodeId()).build());
156 deviceContext.setTranslatorLibrary(translatorLibrary);
158 final OpenflowProtocolListenerFullImpl messageListener = new OpenflowProtocolListenerFullImpl(
159 connectionContext.getConnectionAdapter(), deviceContext);
161 deviceContext.attachOpenflowMessageListener(messageListener);
163 final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo(messageListener,
164 MultipartType.OFPMPDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
166 final ListenableFuture<RpcResult<List<MultipartReply>>> replyMeterFeature = getNodeStaticInfo(messageListener,
167 MultipartType.OFPMPMETERFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
169 final ListenableFuture<RpcResult<List<MultipartReply>>> replyGroupFeatures = getNodeStaticInfo(messageListener,
170 MultipartType.OFPMPGROUPFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
172 final ListenableFuture<RpcResult<List<MultipartReply>>> replyTableFeatures = getNodeStaticInfo(messageListener,
173 MultipartType.OFPMPTABLEFEATURES, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
175 final ListenableFuture<RpcResult<List<MultipartReply>>> replyPortDescription = getNodeStaticInfo(messageListener,
176 MultipartType.OFPMPPORTDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
178 final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture =
179 Futures.allAsList(Arrays.asList(replyDesc, replyMeterFeature, replyGroupFeatures, replyTableFeatures, replyPortDescription));
181 Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
183 public void onSuccess(final List<RpcResult<List<MultipartReply>>> result) {
184 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
188 public void onFailure(final Throwable t) {
189 // FIXME : remove session
195 public TranslatorLibrary oook() {
196 return translatorLibrary;
200 public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
201 this.translatorLibrary = translatorLibrary;
204 private ListenableFuture<RpcResult<List<MultipartReply>>> getNodeStaticInfo(final MultiMsgCollector multiMsgCollector, final MultipartType type, final DeviceContext deviceContext,
205 final InstanceIdentifier<Node> nodeII, final short version) {
207 final Xid xid = deviceContext.getNextXid();
208 final RequestContext<List<MultipartReply>> requestContext = dummyRequestContextStack.createRequestContext();
209 requestContext.setXid(xid);
210 multiMsgCollector.registerMultipartXid(xid.getValue());
211 Futures.addCallback(requestContext.getFuture(), new FutureCallback<RpcResult<List<MultipartReply>>>() {
213 public void onSuccess(final RpcResult<List<MultipartReply>> rpcResult) {
214 final List<MultipartReply> result = rpcResult.getResult();
215 if (result != null) {
216 translateAndWriteReply(type, deviceContext, nodeII, result);
218 final Iterator<RpcError> rpcErrorIterator = rpcResult.getErrors().iterator();
219 while (rpcErrorIterator.hasNext()) {
220 final RpcError rpcError = rpcErrorIterator.next();
221 LOG.info("Failed to retrieve static node {} info: {}", type, rpcError.getMessage());
222 if (null != rpcError.getCause()) {
223 LOG.trace("Detailed error:", rpcError.getCause());
226 if (MultipartType.OFPMPTABLEFEATURES.equals(type)) {
227 makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext().getFeatures().getTables());
233 public void onFailure(final Throwable t) {
234 LOG.info("Failed to retrieve static node {} info: {}", type, t.getMessage());
238 final ListenableFuture<RpcResult<Void>> rpcFuture = JdkFutureAdapters.listenInPoolThread(deviceContext.getPrimaryConnectionContext().getConnectionAdapter()
239 .multipartRequest(MultipartRequestInputFactory.makeMultipartRequestInput(xid.getValue(), version, type)));
240 final OFJResult2RequestCtxFuture OFJResult2RequestCtxFuture = new OFJResult2RequestCtxFuture(requestContext, deviceContext);
241 OFJResult2RequestCtxFuture.processResultFromOfJava(rpcFuture);
243 return requestContext.getFuture();
246 // FIXME : remove after ovs tableFeatures fix
247 private static void makeEmptyTables(final DeviceContext dContext, final InstanceIdentifier<Node> nodeII, final Short nrOfTables) {
248 for (int i = 0; i < nrOfTables; i++) {
249 final short tId = (short) i;
250 final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tId));
251 final TableBuilder tableBuilder = new TableBuilder().setId(tId).addAugmentation(FlowTableStatisticsData.class, new FlowTableStatisticsDataBuilder().build());
252 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
256 private static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext,
257 final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result) {
258 for (final MultipartReply reply : result) {
259 final MultipartReplyBody body = reply.getMultipartReplyBody();
262 Preconditions.checkArgument(body instanceof MultipartReplyDescCase);
263 final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc();
264 final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc);
265 final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
266 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
269 case OFPMPTABLEFEATURES:
270 Preconditions.checkArgument(body instanceof MultipartReplyTableFeaturesCase);
271 final MultipartReplyTableFeatures tableFeatures = ((MultipartReplyTableFeaturesCase) body).getMultipartReplyTableFeatures();
272 final List<TableFeatures> tables = NodeStaticReplyTranslatorUtil.nodeTableFeatureTranslator(tableFeatures);
273 for (final TableFeatures table : tables) {
274 final Short tableId = table.getTableId();
275 final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId));
276 final TableBuilder tableBuilder = new TableBuilder().setId(tableId).setTableFeatures(Collections.singletonList(table));
277 tableBuilder.addAugmentation(FlowTableStatisticsData.class, new FlowTableStatisticsDataBuilder().build());
278 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
282 case OFPMPMETERFEATURES:
283 Preconditions.checkArgument(body instanceof MultipartReplyMeterFeaturesCase);
284 final MultipartReplyMeterFeatures meterFeatures = ((MultipartReplyMeterFeaturesCase) body).getMultipartReplyMeterFeatures();
285 final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil.nodeMeterFeatureTranslator(meterFeatures);
286 final InstanceIdentifier<NodeMeterFeatures> mFeatureII = nodeII.augmentation(NodeMeterFeatures.class);
287 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature);
288 if (0L < mFeature.getMeterFeatures().getMaxMeter().getValue()) {
289 dContext.getDeviceState().setMeterAvailable(true);
293 case OFPMPGROUPFEATURES:
294 Preconditions.checkArgument(body instanceof MultipartReplyGroupFeaturesCase);
295 final MultipartReplyGroupFeatures groupFeatures = ((MultipartReplyGroupFeaturesCase) body).getMultipartReplyGroupFeatures();
296 final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil.nodeGroupFeatureTranslator(groupFeatures);
297 final InstanceIdentifier<NodeGroupFeatures> gFeatureII = nodeII.augmentation(NodeGroupFeatures.class);
298 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature);
299 dContext.getDeviceState().setGroupAvailable(true);
303 Preconditions.checkArgument(body instanceof MultipartReplyPortDescCase);
304 final MultipartReplyPortDesc portDesc = ((MultipartReplyPortDescCase) body).getMultipartReplyPortDesc();
305 for (final Ports port : portDesc.getPorts()) {
306 final short ofVersion = dContext.getDeviceState().getVersion();
307 final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName());
308 final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = dContext.oook().lookupTranslator(translatorKey);
309 final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, dContext, null);
311 final BigInteger dataPathId = dContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
312 final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(dataPathId.toString(), port.getPortNo(), ofVersion);
313 final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId);
314 ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector);
315 ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build());
316 final NodeConnector connector = ncBuilder.build();
318 final InstanceIdentifier<NodeConnector> connectorII = nodeII.child(NodeConnector.class, connector.getKey());
319 dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector);
320 //FlowCapableNodeConnectorBuilder
325 throw new IllegalArgumentException("Unnexpected MultipartType " + type);
331 public void setNotificationService(final NotificationProviderService notificationServiceParam) {
332 notificationService = notificationServiceParam;
336 public void close() throws Exception {
337 for (DeviceContext deviceContext : synchronizedDeviceContextsList) {
338 deviceContext.close();