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.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.SettableFuture;
13 import io.netty.util.HashedWheelTimer;
14 import io.netty.util.Timeout;
15 import java.math.BigInteger;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.List;
21 import java.util.TreeMap;
22 import java.util.concurrent.atomic.AtomicLong;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
26 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
27 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
30 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
31 import org.opendaylight.openflowplugin.api.openflow.connection.ThrottledConnectionsHolder;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
33 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
34 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
35 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
36 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
37 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
38 import org.opendaylight.openflowplugin.api.openflow.device.exception.DeviceDataException;
39 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceContextClosedHandler;
40 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
41 import org.opendaylight.openflowplugin.api.openflow.device.listener.OpenflowMessageListenerFacade;
42 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
43 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
44 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
45 import org.opendaylight.openflowplugin.api.openflow.registry.group.DeviceGroupRegistry;
46 import org.opendaylight.openflowplugin.api.openflow.registry.meter.DeviceMeterRegistry;
47 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
48 import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
49 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
50 import org.opendaylight.openflowplugin.impl.registry.group.DeviceGroupRegistryImpl;
51 import org.opendaylight.openflowplugin.impl.registry.meter.DeviceMeterRegistryImpl;
52 import org.opendaylight.openflowplugin.impl.services.RequestContextUtil;
53 import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketIn;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
72 import org.opendaylight.yangtools.yang.binding.ChildOf;
73 import org.opendaylight.yangtools.yang.binding.DataObject;
74 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
75 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
76 import org.opendaylight.yangtools.yang.common.RpcError;
77 import org.opendaylight.yangtools.yang.common.RpcResult;
78 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
85 public class DeviceContextImpl implements DeviceContext {
87 private static final Logger LOG = LoggerFactory.getLogger(DeviceContextImpl.class);
88 public static final String DEVICE_DISCONNECTED = "Device disconnected.";
90 private final ConnectionContext primaryConnectionContext;
91 private final DeviceState deviceState;
92 private final DataBroker dataBroker;
93 private final XidGenerator xidGenerator;
94 private final HashedWheelTimer hashedWheelTimer;
95 private final Map<Long, RequestContext> requests = new TreeMap<>();
97 private final Map<SwitchConnectionDistinguisher, ConnectionContext> auxiliaryConnectionContexts;
98 private final TransactionChainManager txChainManager;
99 private TranslatorLibrary translatorLibrary;
100 private OpenflowMessageListenerFacade openflowMessageListenerFacade;
101 private final DeviceFlowRegistry deviceFlowRegistry;
102 private final DeviceGroupRegistry deviceGroupRegistry;
103 private final DeviceMeterRegistry deviceMeterRegistry;
104 private Timeout barrierTaskTimeout;
105 private NotificationService notificationService;
106 private final MessageSpy<Class> messageSpy;
107 private DeviceDisconnectedHandler deviceDisconnectedHandler;
108 private final List<DeviceContextClosedHandler> closeHandlers = new ArrayList<>();
109 private NotificationPublishService notificationPublishService;
110 private final ThrottledConnectionsHolder throttledConnectionsHolder;
114 DeviceContextImpl(@Nonnull final ConnectionContext primaryConnectionContext,
115 @Nonnull final DeviceState deviceState,
116 @Nonnull final DataBroker dataBroker,
117 @Nonnull final HashedWheelTimer hashedWheelTimer,
118 @Nonnull final MessageSpy _messageSpy,
119 @Nonnull final ThrottledConnectionsHolder throttledConnectionsHolder) {
120 this.primaryConnectionContext = Preconditions.checkNotNull(primaryConnectionContext);
121 this.deviceState = Preconditions.checkNotNull(deviceState);
122 this.dataBroker = Preconditions.checkNotNull(dataBroker);
123 this.hashedWheelTimer = Preconditions.checkNotNull(hashedWheelTimer);
124 xidGenerator = new XidGenerator();
125 txChainManager = new TransactionChainManager(dataBroker, hashedWheelTimer, 500L, 500L);
126 auxiliaryConnectionContexts = new HashMap<>();
127 deviceFlowRegistry = new DeviceFlowRegistryImpl();
128 deviceGroupRegistry = new DeviceGroupRegistryImpl();
129 deviceMeterRegistry = new DeviceMeterRegistryImpl();
130 messageSpy = _messageSpy;
131 this.throttledConnectionsHolder = throttledConnectionsHolder;
135 * This method is called from {@link DeviceManagerImpl} only. So we could say "posthandshake process finish"
136 * and we are able to set a scheduler for an automatic transaction submitting by time (0,5sec).
138 void submitTransaction() {
139 txChainManager.enableSubmit();
140 txChainManager.submitTransaction();
144 public <M extends ChildOf<DataObject>> void onMessage(final M message, final RequestContext requestContext) {
145 // TODO Auto-generated method stub
150 public void addAuxiliaryConenctionContext(final ConnectionContext connectionContext) {
151 final SwitchConnectionDistinguisher connectionDistinguisher = createConnectionDistinguisher(connectionContext);
152 auxiliaryConnectionContexts.put(connectionDistinguisher, connectionContext);
155 private SwitchConnectionDistinguisher createConnectionDistinguisher(final ConnectionContext connectionContext) {
156 return new SwitchConnectionCookieOFImpl(connectionContext.getFeatures().getAuxiliaryId());
160 public void removeAuxiliaryConenctionContext(final ConnectionContext connectionContext) {
161 // TODO Auto-generated method stub
165 public DeviceState getDeviceState() {
170 public ReadTransaction getReadTransaction() {
171 return dataBroker.newReadOnlyTransaction();
175 public <T extends DataObject> void writeToTransaction(final LogicalDatastoreType store,
176 final InstanceIdentifier<T> path, final T data) {
177 txChainManager.writeToTransaction(store, path, data);
181 public <T extends DataObject> void addDeleteToTxChain(final LogicalDatastoreType store, final InstanceIdentifier<T> path) {
182 txChainManager.addDeleteOperationTotTxChain(store, path);
186 public ConnectionContext getPrimaryConnectionContext() {
187 return primaryConnectionContext;
191 public ConnectionContext getAuxiliaryConnectiobContexts(final BigInteger cookie) {
192 return auxiliaryConnectionContexts.get(new SwitchConnectionCookieOFImpl(cookie.longValue()));
196 public Xid getNextXid() {
197 return xidGenerator.generate();
201 public RequestContext lookupRequest(final Xid xid) {
202 synchronized (requests) {
203 return requests.get(xid.getValue());
208 public int getNumberOfOutstandingRequests() {
209 synchronized (requests) {
210 return requests.size();
215 public void hookRequestCtx(final Xid xid, final RequestContext requestFutureContext) {
216 synchronized (requests) {
217 requests.put(xid.getValue(), requestFutureContext);
222 public RequestContext unhookRequestCtx(final Xid xid) {
223 synchronized (requests) {
224 return requests.remove(xid.getValue());
229 public void attachOpenflowMessageListener(final OpenflowMessageListenerFacade openflowMessageListenerFacade) {
230 this.openflowMessageListenerFacade = openflowMessageListenerFacade;
231 primaryConnectionContext.getConnectionAdapter().setMessageListener(openflowMessageListenerFacade);
235 public OpenflowMessageListenerFacade getOpenflowMessageListenerFacade() {
236 return openflowMessageListenerFacade;
240 public DeviceFlowRegistry getDeviceFlowRegistry() {
241 return deviceFlowRegistry;
245 public DeviceGroupRegistry getDeviceGroupRegistry() {
246 return deviceGroupRegistry;
250 public DeviceMeterRegistry getDeviceMeterRegistry() {
251 return deviceMeterRegistry;
255 public void processReply(final OfHeader ofHeader) {
256 final RequestContext requestContext = requests.remove(ofHeader.getXid());
257 if (null != requestContext) {
258 final SettableFuture replyFuture = requestContext.getFuture();
259 RpcResult<OfHeader> rpcResult;
260 if (ofHeader instanceof Error) {
261 //TODO : this is the point, where we can discover that add flow operation failed and where we should
262 //TODO : remove this flow from deviceFlowRegistry
263 final Error error = (Error) ofHeader;
264 final String message = "Operation on device failed with xid " + ofHeader.getXid() + ".";
265 rpcResult = RpcResultBuilder
267 .withError(RpcError.ErrorType.APPLICATION, message, new DeviceDataException(message, error))
269 messageSpy.spyMessage(ofHeader.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE);
271 rpcResult = RpcResultBuilder
273 .withResult(ofHeader)
275 messageSpy.spyMessage(ofHeader.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS);
278 replyFuture.set(rpcResult);
280 requestContext.close();
281 } catch (final Exception e) {
282 LOG.warn("Closing RequestContext failed: {}", e.getMessage());
283 LOG.debug("Closing RequestContext failed.. ", e);
286 LOG.warn("Can't find request context registered for xid : {}. Type of reply: {}. From address: {}", ofHeader.getXid(), ofHeader.getClass().getName(),
287 getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress());
292 public void processReply(final Xid xid, final List<MultipartReply> ofHeaderList) {
293 final RequestContext requestContext;
294 synchronized (requests) {
295 requestContext = requests.remove(xid.getValue());
297 if (null != requestContext) {
298 final SettableFuture replyFuture = requestContext.getFuture();
299 final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
300 .<List<MultipartReply>>success()
301 .withResult(ofHeaderList)
303 replyFuture.set(rpcResult);
304 for (final MultipartReply multipartReply : ofHeaderList) {
305 messageSpy.spyMessage(multipartReply.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE);
309 requestContext.close();
310 } catch (final Exception e) {
311 LOG.warn("Closing RequestContext failed: {}", e.getMessage());
312 LOG.debug("Closing RequestContext failed.. ", e);
315 LOG.warn("Can't find request context registered for xid : {}. Type of reply: MULTIPART. From address: {}", xid.getValue(),
316 getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress());
321 public void processException(final Xid xid, final DeviceDataException deviceDataException) {
323 LOG.trace("Processing exception for xid : {}", xid.getValue());
325 final RequestContext requestContext = requests.remove(xid.getValue());
327 if (null != requestContext) {
328 final SettableFuture replyFuture = requestContext.getFuture();
329 final RpcResult<List<OfHeader>> rpcResult = RpcResultBuilder
330 .<List<OfHeader>>failed()
331 .withError(RpcError.ErrorType.APPLICATION, String.format("Message processing failed : %s", deviceDataException.getError()), deviceDataException)
333 replyFuture.set(rpcResult);
334 messageSpy.spyMessage(deviceDataException.getClass(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE);
336 requestContext.close();
337 } catch (final Exception e) {
338 LOG.warn("Closing RequestContext failed: ", e);
339 LOG.debug("Closing RequestContext failed..", e);
342 LOG.warn("Can't find request context registered for xid : {}. Exception message {}",
343 xid.getValue(), deviceDataException.getMessage());
348 public void processFlowRemovedMessage(final FlowRemoved flowRemoved) {
349 //TODO: will be defined later
353 public void processPortStatusMessage(final PortStatusMessage portStatus) {
354 messageSpy.spyMessage(portStatus.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS);
355 final TranslatorKey translatorKey = new TranslatorKey(portStatus.getVersion(), PortGrouping.class.getName());
356 final MessageTranslator<PortGrouping, FlowCapableNodeConnector> messageTranslator = translatorLibrary.lookupTranslator(translatorKey);
357 final FlowCapableNodeConnector flowCapableNodeConnector = messageTranslator.translate(portStatus, this, null);
359 final KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> iiToNodeConnector = provideIIToNodeConnector(portStatus.getPortNo(), portStatus.getVersion());
360 if (portStatus.getReason().equals(PortReason.OFPPRADD) || portStatus.getReason().equals(PortReason.OFPPRMODIFY)) {
361 // because of ADD status node connector has to be created
362 final NodeConnectorBuilder nConnectorBuilder = new NodeConnectorBuilder().setKey(iiToNodeConnector.getKey());
363 nConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build());
364 nConnectorBuilder.addAugmentation(FlowCapableNodeConnector.class, flowCapableNodeConnector);
365 writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToNodeConnector, nConnectorBuilder.build());
366 } else if (portStatus.getReason().equals(PortReason.OFPPRDELETE)) {
367 addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, iiToNodeConnector);
371 private KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> provideIIToNodeConnector(final long portNo, final short version) {
372 final InstanceIdentifier<Node> iiToNodes = deviceState.getNodeInstanceIdentifier();
373 final BigInteger dataPathId = deviceState.getFeatures().getDatapathId();
374 final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(dataPathId.toString(), portNo, version);
375 return iiToNodes.child(NodeConnector.class, new NodeConnectorKey(nodeConnectorId));
379 public void processPacketInMessage(final PacketInMessage packetInMessage) {
380 messageSpy.spyMessage(packetInMessage.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH);
381 final ConnectionAdapter connectionAdapter = this.getPrimaryConnectionContext().getConnectionAdapter();
382 if (connectionAdapter.isAutoRead()) {
383 final TranslatorKey translatorKey = new TranslatorKey(packetInMessage.getVersion(), PacketIn.class.getName());
384 final MessageTranslator<PacketInMessage, PacketReceived> messageTranslator = translatorLibrary.lookupTranslator(translatorKey);
385 final PacketReceived packetReceived = messageTranslator.translate(packetInMessage, this, null);
387 if (packetReceived != null) {
388 messageSpy.spyMessage(packetInMessage.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_OUT_SUCCESS);
390 messageSpy.spyMessage(packetInMessage.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_SRC_FAILURE);
392 if (!notificationPublishService.offerNotification(packetReceived)) {
393 LOG.debug("Notification offer refused by notification service.");
394 messageSpy.spyMessage(packetInMessage.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE);
395 LOG.debug("Notification offer refused by notification service.");
396 connectionAdapter.setAutoRead(false);
397 LOG.debug("Throttling primary connection for {}", connectionAdapter.getRemoteAddress());
398 this.throttledConnectionsHolder.storeThrottledConnection(connectionAdapter);
400 messageSpy.spyMessage(packetInMessage.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS);
408 public TranslatorLibrary oook() {
409 return translatorLibrary;
413 public void setTranslatorLibrary(final TranslatorLibrary translatorLibrary) {
414 this.translatorLibrary = translatorLibrary;
418 public HashedWheelTimer getTimer() {
419 return hashedWheelTimer;
423 public void close() throws Exception {
424 deviceState.setValid(false);
426 LOG.trace("Removing node {} from operational DS.", getDeviceState().getNodeId());
427 addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, getDeviceState().getNodeInstanceIdentifier());
429 deviceGroupRegistry.close();
430 deviceFlowRegistry.close();
431 deviceMeterRegistry.close();
433 if (primaryConnectionContext.getConnectionAdapter().isAlive()) {
434 primaryConnectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
435 primaryConnectionContext.getConnectionAdapter().disconnect();
437 for (final Map.Entry<Long, RequestContext> entry : requests.entrySet()) {
438 RequestContextUtil.closeRequestContextWithRpcError(entry.getValue(), DEVICE_DISCONNECTED);
440 for (final ConnectionContext connectionContext : auxiliaryConnectionContexts.values()) {
441 if (connectionContext.getConnectionAdapter().isAlive()) {
442 connectionContext.getConnectionAdapter().disconnect();
445 for (final DeviceContextClosedHandler deviceContextClosedHandler : closeHandlers) {
446 deviceContextClosedHandler.onDeviceContextClosed(this);
452 public void onDeviceDisconnected(final ConnectionContext connectionContext) {
453 if (this.getPrimaryConnectionContext().equals(connectionContext)) {
456 } catch (final Exception e) {
457 LOG.trace("Error closing device context.");
459 if (null != deviceDisconnectedHandler) {
460 deviceDisconnectedHandler.onDeviceDisconnected(connectionContext);
463 final SwitchConnectionDistinguisher connectionDistinguisher = createConnectionDistinguisher(connectionContext);
464 auxiliaryConnectionContexts.remove(connectionDistinguisher);
469 private class XidGenerator {
471 private final AtomicLong xid = new AtomicLong(0);
473 public Xid generate() {
474 return new Xid(xid.incrementAndGet());
479 public RequestContext extractNextOutstandingMessage(final long barrierXid) {
480 RequestContext nextMessage = null;
481 synchronized (requests) {
482 final Iterator<Long> keyIterator = requests.keySet().iterator();
483 if (keyIterator.hasNext()) {
484 final Long oldestXid = keyIterator.next();
485 if (oldestXid < barrierXid) {
486 nextMessage = requests.remove(oldestXid);
494 public void setCurrentBarrierTimeout(final Timeout timeout) {
495 barrierTaskTimeout = timeout;
499 public Timeout getBarrierTaskTimeout() {
500 return barrierTaskTimeout;
504 public void setNotificationService(final NotificationService notificationServiceParam) {
505 notificationService = notificationServiceParam;
509 public void setNotificationPublishService(final NotificationPublishService notificationPublishService) {
510 this.notificationPublishService = notificationPublishService;
514 public MessageSpy getMessageSpy() {
519 public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
520 this.deviceDisconnectedHandler = deviceDisconnectedHandler;
524 public void addDeviceContextClosedHandler(final DeviceContextClosedHandler deviceContextClosedHandler) {
525 this.closeHandlers.add(deviceContextClosedHandler);
529 public void startGatheringOperationsToOneTransaction() {
530 txChainManager.startGatheringOperationsToOneTransaction();
534 public void commitOperationsGatheredInOneTransaction() {
535 txChainManager.commitOperationsGatheredInOneTransaction();