*/
package org.opendaylight.openflowplugin.impl.device;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Sets;
-import com.google.common.util.concurrent.AsyncFunction;
-import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
-import io.netty.util.HashedWheelTimer;
-import java.math.BigInteger;
-import java.net.Inet4Address;
-import java.net.Inet6Address;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
-import java.util.Arrays;
-import java.util.Collection;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Sets;
+import io.netty.util.HashedWheelTimer;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
import org.opendaylight.controller.md.sal.binding.api.NotificationService;
import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
-import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
-import org.opendaylight.openflowplugin.api.ConnectionException;
-import org.opendaylight.openflowplugin.api.OFConstants;
import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
-import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
-import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
-import org.opendaylight.openflowplugin.api.openflow.device.Xid;
import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
-import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
-import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
-import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
-import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
import org.opendaylight.openflowplugin.impl.connection.OutboundQueueProviderImpl;
import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
-import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
-import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortDescCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.MultipartReplyPortDesc;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.RpcError;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
}
}
- private void initializeDeviceContext(final ConnectionContext connectionContext, final TransactionChainManager transactionChainManager) {
-
+ private void initializeDeviceContext(final ConnectionContext connectionContext,
+ final TransactionChainManager transactionChainManager) {
+ LOG.info("Initializing New Connection DeviceContext for node:{}", connectionContext.getNodeId());
// Cache this for clarity
final ConnectionAdapter connectionAdapter = connectionContext.getConnectionAdapter();
final DeviceContext deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker,
hashedWheelTimer, messageIntelligenceAgency, outboundQueueProvider, translatorLibrary, transactionChainManager);
+ deviceContext.addDeviceContextClosedHandler(this);
((ExtensionConverterProviderKeeper) deviceContext).setExtensionConverterProvider(extensionConverterProvider);
deviceContext.setNotificationService(notificationService);
deviceContext.setNotificationPublishService(notificationPublishService);
- final NodeBuilder nodeBuilder = new NodeBuilder().setId(deviceState.getNodeId()).setNodeConnector(Collections.<NodeConnector>emptyList());
- try {
- deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, deviceState.getNodeInstanceIdentifier(), nodeBuilder.build());
- } catch (final Exception e) {
- LOG.debug("Failed to write node to DS ", e);
- }
- connectionContext.setDeviceDisconnectedHandler(deviceContext);
- deviceContext.addDeviceContextClosedHandler(this);
deviceContexts.add(deviceContext);
updatePacketInRateLimiters();
connectionAdapter, deviceContext);
connectionAdapter.setMessageListener(messageListener);
- final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture;
- if (OFConstants.OFP_VERSION_1_0 == version) {
- final CapabilitiesV10 capabilitiesV10 = connectionContext.getFeatures().getCapabilitiesV10();
-
- DeviceStateUtil.setDeviceStateBasedOnV10Capabilities(deviceState, capabilitiesV10);
-
- deviceFeaturesFuture = createDeviceFeaturesForOF10(deviceContext, deviceState);
- // create empty tables after device description is processed
- chainTableTrunkWriteOF10(deviceContext, deviceFeaturesFuture);
-
- final short ofVersion = deviceContext.getDeviceState().getVersion();
- final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName());
- final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = deviceContext.oook().lookupTranslator(translatorKey);
- final BigInteger dataPathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
-
- for (final PortGrouping port : connectionContext.getFeatures().getPhyPort()) {
- final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, deviceContext, null);
-
- final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(dataPathId.toString(), port.getPortNo(), ofVersion);
- final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId);
- ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector);
- ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build());
- final NodeConnector connector = ncBuilder.build();
- final InstanceIdentifier<NodeConnector> connectorII = deviceState.getNodeInstanceIdentifier().child(NodeConnector.class, connector.getKey());
- try {
- deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector);
- } catch (final Exception e) {
- LOG.debug("Failed to write node {} to DS ", deviceContext.getDeviceState().getNodeId().toString(), e);
- }
-
- }
- } else if (OFConstants.OFP_VERSION_1_3 == version) {
- final Capabilities capabilities = connectionContext.getFeatures().getCapabilities();
- LOG.debug("Setting capabilities for device {}", deviceContext.getDeviceState().getNodeId());
- DeviceStateUtil.setDeviceStateBasedOnV13Capabilities(deviceState, capabilities);
- deviceFeaturesFuture = createDeviceFeaturesForOF13(deviceContext, deviceState);
- } else {
- deviceFeaturesFuture = Futures.immediateFailedFuture(new ConnectionException("Unsupported version " + version));
- }
-
- Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
- @Override
- public void onSuccess(final List<RpcResult<List<MultipartReply>>> result) {
- deviceCtxLevelUp(deviceContext);
- }
-
- @Override
- public void onFailure(final Throwable t) {
- LOG.trace("Device capabilities gathering future failed.");
- LOG.trace("more info in exploration failure..", t);
- try {
- deviceContext.close();
- } catch (Exception e) {
- LOG.warn("Failed to close device context: {}", deviceContext.getDeviceState().getNodeId(), t);
- }
- }
- });
+ deviceCtxLevelUp(deviceContext);
}
private void updatePacketInRateLimiters() {
deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
}
- static void chainTableTrunkWriteOF10(final DeviceContext deviceContext, final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture) {
- Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
- @Override
- public void onSuccess(final List<RpcResult<List<MultipartReply>>> results) {
- boolean allSucceeded = true;
- for (final RpcResult<List<MultipartReply>> rpcResult : results) {
- allSucceeded &= rpcResult.isSuccessful();
- }
- if (allSucceeded) {
- createEmptyFlowCapableNodeInDs(deviceContext);
- makeEmptyTables(deviceContext, deviceContext.getDeviceState().getNodeInstanceIdentifier(),
- deviceContext.getDeviceState().getFeatures().getTables());
- }
- }
-
- @Override
- public void onFailure(final Throwable t) {
- //NOOP
- }
- });
- }
-
-
- static ListenableFuture<List<RpcResult<List<MultipartReply>>>> createDeviceFeaturesForOF10(final DeviceContext deviceContext,
- final DeviceState deviceState) {
- final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo(MultipartType.OFPMPDESC,
- deviceContext,
- deviceState.getNodeInstanceIdentifier(),
- deviceState.getVersion());
-
- return Futures.allAsList(Arrays.asList(replyDesc));
- }
-
- ListenableFuture<List<RpcResult<List<MultipartReply>>>> createDeviceFeaturesForOF13(final DeviceContext deviceContext,
- final DeviceState deviceState) {
-
- final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo(MultipartType.OFPMPDESC,
- deviceContext,
- deviceState.getNodeInstanceIdentifier(),
- deviceState.getVersion());
-
- //first process description reply, write data to DS and write consequent data if successful
- return Futures.transform(replyDesc, new AsyncFunction<RpcResult<List<MultipartReply>>, List<RpcResult<List<MultipartReply>>>>() {
- @Override
- public ListenableFuture<List<RpcResult<List<MultipartReply>>>> apply(final RpcResult<List<MultipartReply>> rpcResult) throws Exception {
-
- translateAndWriteReply(MultipartType.OFPMPDESC, deviceContext, deviceState.getNodeInstanceIdentifier(), rpcResult.getResult());
-
- final ListenableFuture<RpcResult<List<MultipartReply>>> replyMeterFeature = getNodeStaticInfo(MultipartType.OFPMPMETERFEATURES,
- deviceContext,
- deviceState.getNodeInstanceIdentifier(),
- deviceState.getVersion());
-
- createSuccessProcessingCallback(MultipartType.OFPMPMETERFEATURES,
- deviceContext,
- deviceState.getNodeInstanceIdentifier(),
- replyMeterFeature);
-
- final ListenableFuture<RpcResult<List<MultipartReply>>> replyGroupFeatures = getNodeStaticInfo(MultipartType.OFPMPGROUPFEATURES,
- deviceContext,
- deviceState.getNodeInstanceIdentifier(),
- deviceState.getVersion());
- createSuccessProcessingCallback(MultipartType.OFPMPGROUPFEATURES,
- deviceContext,
- deviceState.getNodeInstanceIdentifier(),
- replyGroupFeatures);
-
- final ListenableFuture<RpcResult<List<MultipartReply>>> replyTableFeatures = getNodeStaticInfo(MultipartType.OFPMPTABLEFEATURES,
- deviceContext,
- deviceState.getNodeInstanceIdentifier(),
- deviceState.getVersion());
- createSuccessProcessingCallback(MultipartType.OFPMPTABLEFEATURES,
- deviceContext,
- deviceState.getNodeInstanceIdentifier(),
- replyTableFeatures);
-
- final ListenableFuture<RpcResult<List<MultipartReply>>> replyPortDescription = getNodeStaticInfo(MultipartType.OFPMPPORTDESC,
- deviceContext,
- deviceState.getNodeInstanceIdentifier(),
- deviceState.getVersion());
- createSuccessProcessingCallback(MultipartType.OFPMPPORTDESC,
- deviceContext,
- deviceState.getNodeInstanceIdentifier(),
- replyPortDescription);
- if (switchFeaturesMandatory) {
- return Futures.allAsList(Arrays.asList(
- replyMeterFeature,
- replyGroupFeatures,
- replyTableFeatures,
- replyPortDescription));
- } else {
- return Futures.successfulAsList(Arrays.asList(
- replyMeterFeature,
- replyGroupFeatures,
- replyTableFeatures,
- replyPortDescription));
- }
- }
- });
-
- }
-
@Override
public TranslatorLibrary oook() {
return translatorLibrary;
this.translatorLibrary = translatorLibrary;
}
- static ListenableFuture<RpcResult<List<MultipartReply>>> getNodeStaticInfo(final MultipartType type, final DeviceContext deviceContext,
- final InstanceIdentifier<Node> nodeII, final short version) {
-
- final OutboundQueue queue = deviceContext.getPrimaryConnectionContext().getOutboundQueueProvider();
-
- final Long reserved = deviceContext.getReservedXid();
- final RequestContext<List<MultipartReply>> requestContext = new AbstractRequestContext<List<MultipartReply>>(reserved) {
- @Override
- public void close() {
- //NOOP
- }
- };
-
- final Xid xid = requestContext.getXid();
-
- LOG.trace("Hooking xid {} to device context - precaution.", reserved);
-
- final MultiMsgCollector multiMsgCollector = deviceContext.getMultiMsgCollector(requestContext);
- queue.commitEntry(xid.getValue(), MultipartRequestInputFactory.makeMultipartRequestInput(xid.getValue(), version, type), new FutureCallback<OfHeader>() {
- @Override
- public void onSuccess(final OfHeader ofHeader) {
- if (ofHeader instanceof MultipartReply) {
- final MultipartReply multipartReply = (MultipartReply) ofHeader;
- multiMsgCollector.addMultipartMsg(multipartReply);
- } else if (null != ofHeader) {
- LOG.info("Unexpected response type received {}.", ofHeader.getClass());
- } else {
- multiMsgCollector.endCollecting();
- LOG.info("Response received is null.");
- }
- }
-
- @Override
- public void onFailure(final Throwable t) {
- LOG.info("Fail response from OutboundQueue for multipart type {}.", type);
- final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.<List<MultipartReply>>failed().build();
- requestContext.setResult(rpcResult);
- if (MultipartType.OFPMPTABLEFEATURES.equals(type)) {
- makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext().getFeatures().getTables());
- }
- requestContext.close();
- }
- });
-
- return requestContext.getFuture();
- }
-
- static void createSuccessProcessingCallback(final MultipartType type, final DeviceContext deviceContext, final InstanceIdentifier<Node> nodeII, final ListenableFuture<RpcResult<List<MultipartReply>>> requestContextFuture) {
- Futures.addCallback(requestContextFuture, new FutureCallback<RpcResult<List<MultipartReply>>>() {
- @Override
- public void onSuccess(final RpcResult<List<MultipartReply>> rpcResult) {
- final List<MultipartReply> result = rpcResult.getResult();
- if (result != null) {
- LOG.info("Static node {} info: {} collected", deviceContext.getDeviceState().getNodeId(), type);
- translateAndWriteReply(type, deviceContext, nodeII, result);
- } else {
- final Iterator<RpcError> rpcErrorIterator = rpcResult.getErrors().iterator();
- while (rpcErrorIterator.hasNext()) {
- final RpcError rpcError = rpcErrorIterator.next();
- LOG.info("Failed to retrieve static node {} info: {}", type, rpcError.getMessage());
- if (null != rpcError.getCause()) {
- LOG.trace("Detailed error:", rpcError.getCause());
- }
- }
- if (MultipartType.OFPMPTABLEFEATURES.equals(type)) {
- makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext().getFeatures().getTables());
- }
- }
- }
-
- @Override
- public void onFailure(final Throwable throwable) {
- LOG.info("Request of type {} for static info of node {} failed.", type, nodeII);
- }
- });
- }
-
- // FIXME : remove after ovs tableFeatures fix
- static void makeEmptyTables(final DeviceContext dContext, final InstanceIdentifier<Node> nodeII, final Short nrOfTables) {
- LOG.debug("About to create {} empty tables.", nrOfTables);
- for (int i = 0; i < nrOfTables; i++) {
- final short tId = (short) i;
- final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tId));
- final TableBuilder tableBuilder = new TableBuilder().setId(tId).addAugmentation(FlowTableStatisticsData.class, new FlowTableStatisticsDataBuilder().build());
-
- try {
- dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
- } catch (final Exception e) {
- LOG.debug("Failed to write node {} to DS ", dContext.getDeviceState().getNodeId().toString(), e);
- }
-
- }
- }
-
- private static IpAddress getIpAddressOf(final DeviceContext deviceContext) {
-
- InetSocketAddress remoteAddress = deviceContext.getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress();
-
- if (remoteAddress == null) {
- LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", deviceContext.getDeviceState().getNodeId());
- return null;
- }
- LOG.info("IP address of switch is :"+remoteAddress);
-
- final InetAddress address = remoteAddress.getAddress();
- String hostAddress = address.getHostAddress();
- if (address instanceof Inet4Address) {
- return new IpAddress(new Ipv4Address(hostAddress));
- }
- if (address instanceof Inet6Address) {
- return new IpAddress(new Ipv6Address(hostAddress));
- }
- LOG.info("Illegal IP address {} of switch:{} ", address, deviceContext.getDeviceState().getNodeId());
- return null;
-
- }
-
- static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext,
- final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result) {
- try {
- for (final MultipartReply reply : result) {
- final MultipartReplyBody body = reply.getMultipartReplyBody();
- switch (type) {
- case OFPMPDESC:
- Preconditions.checkArgument(body instanceof MultipartReplyDescCase);
- final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc();
- final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc, getIpAddressOf(dContext));
- final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
- dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
- break;
-
- case OFPMPTABLEFEATURES:
- Preconditions.checkArgument(body instanceof MultipartReplyTableFeaturesCase);
- final MultipartReplyTableFeatures tableFeatures = ((MultipartReplyTableFeaturesCase) body).getMultipartReplyTableFeatures();
- final List<TableFeatures> tables = NodeStaticReplyTranslatorUtil.nodeTableFeatureTranslator(tableFeatures);
- for (final TableFeatures table : tables) {
- final Short tableId = table.getTableId();
- final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId));
- final TableBuilder tableBuilder = new TableBuilder().setId(tableId).setTableFeatures(Collections.singletonList(table));
- tableBuilder.addAugmentation(FlowTableStatisticsData.class, new FlowTableStatisticsDataBuilder().build());
- dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
- }
- break;
-
- case OFPMPMETERFEATURES:
- Preconditions.checkArgument(body instanceof MultipartReplyMeterFeaturesCase);
- final MultipartReplyMeterFeatures meterFeatures = ((MultipartReplyMeterFeaturesCase) body).getMultipartReplyMeterFeatures();
- final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil.nodeMeterFeatureTranslator(meterFeatures);
- final InstanceIdentifier<NodeMeterFeatures> mFeatureII = nodeII.augmentation(NodeMeterFeatures.class);
- dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature);
- if (0L < mFeature.getMeterFeatures().getMaxMeter().getValue()) {
- dContext.getDeviceState().setMeterAvailable(true);
- }
- break;
-
- case OFPMPGROUPFEATURES:
- Preconditions.checkArgument(body instanceof MultipartReplyGroupFeaturesCase);
- final MultipartReplyGroupFeatures groupFeatures = ((MultipartReplyGroupFeaturesCase) body).getMultipartReplyGroupFeatures();
- final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil.nodeGroupFeatureTranslator(groupFeatures);
- final InstanceIdentifier<NodeGroupFeatures> gFeatureII = nodeII.augmentation(NodeGroupFeatures.class);
- dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature);
- break;
-
- case OFPMPPORTDESC:
- Preconditions.checkArgument(body instanceof MultipartReplyPortDescCase);
- final MultipartReplyPortDesc portDesc = ((MultipartReplyPortDescCase) body).getMultipartReplyPortDesc();
- for (final PortGrouping port : portDesc.getPorts()) {
- final short ofVersion = dContext.getDeviceState().getVersion();
- final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName());
- final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = dContext.oook().lookupTranslator(translatorKey);
- final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, dContext, null);
-
- final BigInteger dataPathId = dContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
- final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(dataPathId.toString(), port.getPortNo(), ofVersion);
- final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId);
- ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector);
-
- ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build());
- final NodeConnector connector = ncBuilder.build();
-
- final InstanceIdentifier<NodeConnector> connectorII = nodeII.child(NodeConnector.class, connector.getKey());
- dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector);
- }
-
- break;
-
- default:
- throw new IllegalArgumentException("Unnexpected MultipartType " + type);
- }
- }
- } catch (final Exception e) {
- LOG.debug("Failed to write node {} to DS ", dContext.getDeviceState().getNodeId().toString(), e);
- }
- }
-
@Override
public void setNotificationService(final NotificationService notificationServiceParam) {
notificationService = notificationServiceParam;
}
}
- static void createEmptyFlowCapableNodeInDs(final DeviceContext deviceContext) {
- final FlowCapableNodeBuilder flowCapableNodeBuilder = new FlowCapableNodeBuilder();
- final InstanceIdentifier<FlowCapableNode> fNodeII = deviceContext.getDeviceState().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
- try {
- deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, flowCapableNodeBuilder.build());
- } catch (final Exception e) {
- LOG.debug("Failed to write node {} to DS ", deviceContext.getDeviceState().getNodeId().toString(), e);
- }
- }
-
@Override
public void onDeviceContextClosed(final DeviceContext deviceContext) {
deviceContexts.remove(deviceContext);
--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowplugin.impl.util;
+
+import java.math.BigInteger;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.Future;
+
+import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.AsyncFunction;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
+import org.opendaylight.openflowplugin.api.ConnectionException;
+import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
+import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
+import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
+import org.opendaylight.openflowplugin.api.openflow.device.Xid;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
+import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
+import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
+import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
+import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortDescCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.MultipartReplyPortDesc;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DeviceInitializationUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(DeviceInitializationUtils.class);
+
+ /**
+ * InitializationNodeInformation is good to call only for MASTER otherwise we will have not empty transaction
+ * for every Cluster Node (SLAVE too) and we will get race-condition by closing Connection.
+ *
+ * @param deviceContext
+ * @param switchFeaturesMandatory
+ * @return future - recommended to have blocking call for this future
+ */
+ public static Future<Void> initializeNodeInformation(final DeviceContext deviceContext, final boolean switchFeaturesMandatory) {
+ Preconditions.checkArgument(deviceContext != null);
+ final DeviceState deviceState = Preconditions.checkNotNull(deviceContext.getDeviceState());
+ final ConnectionContext connectionContext = Preconditions.checkNotNull(deviceContext.getPrimaryConnectionContext());
+ final short version = deviceState.getVersion();
+ LOG.trace("initalizeNodeInformation for node {}", deviceState.getNodeId());
+ final SettableFuture<Void> returnFuture = SettableFuture.<Void> create();
+ addNodeToOperDS(deviceContext, returnFuture);
+ final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture;
+ if (OFConstants.OFP_VERSION_1_0 == version) {
+ final CapabilitiesV10 capabilitiesV10 = connectionContext.getFeatures().getCapabilitiesV10();
+
+ DeviceStateUtil.setDeviceStateBasedOnV10Capabilities(deviceState, capabilitiesV10);
+
+ deviceFeaturesFuture = createDeviceFeaturesForOF10(deviceContext, deviceState);
+ // create empty tables after device description is processed
+ chainTableTrunkWriteOF10(deviceContext, deviceFeaturesFuture);
+
+ final short ofVersion = deviceContext.getDeviceState().getVersion();
+ final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName());
+ final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = deviceContext.oook()
+ .lookupTranslator(translatorKey);
+ final BigInteger dataPathId = deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId();
+
+ for (final PortGrouping port : connectionContext.getFeatures().getPhyPort()) {
+ final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, deviceContext, null);
+
+ final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(
+ dataPathId.toString(), port.getPortNo(), ofVersion);
+ final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId);
+ ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector);
+ ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class,
+ new FlowCapableNodeConnectorStatisticsDataBuilder().build());
+ final NodeConnector connector = ncBuilder.build();
+ final InstanceIdentifier<NodeConnector> connectorII = deviceState.getNodeInstanceIdentifier().child(
+ NodeConnector.class, connector.getKey());
+ try {
+ deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector);
+ } catch (final Exception e) {
+ LOG.debug("Failed to write node {} to DS ", deviceContext.getDeviceState().getNodeId().toString(),
+ e);
+ }
+
+ }
+ } else if (OFConstants.OFP_VERSION_1_3 == version) {
+ final Capabilities capabilities = connectionContext.getFeatures().getCapabilities();
+ LOG.debug("Setting capabilities for device {}", deviceContext.getDeviceState().getNodeId());
+ DeviceStateUtil.setDeviceStateBasedOnV13Capabilities(deviceState, capabilities);
+ deviceFeaturesFuture = createDeviceFeaturesForOF13(deviceContext, deviceState, switchFeaturesMandatory);
+ } else {
+ deviceFeaturesFuture = Futures.immediateFailedFuture(new ConnectionException("Unsupported version "
+ + version));
+ }
+
+ Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
+ @Override
+ public void onSuccess(final List<RpcResult<List<MultipartReply>>> result) {
+ LOG.debug("All init data for node {} is in submited.", deviceState.getNodeId());
+ returnFuture.set(null);
+ }
+
+ @Override
+ public void onFailure(final Throwable t) {
+ // FIXME : remove session
+ LOG.trace("Device capabilities gathering future failed.");
+ LOG.trace("more info in exploration failure..", t);
+ LOG.debug("All init data for node {} was not submited correctly - connection has to go down.", deviceState.getNodeId());
+ returnFuture.cancel(true);
+ }
+ });
+ return returnFuture;
+ }
+
+ private static void addNodeToOperDS(final DeviceContext deviceContext, final SettableFuture<Void> future) {
+ Preconditions.checkArgument(deviceContext != null);
+ final DeviceState deviceState = deviceContext.getDeviceState();
+ final NodeBuilder nodeBuilder = new NodeBuilder().setId(deviceState.getNodeId()).setNodeConnector(
+ Collections.<NodeConnector> emptyList());
+ try {
+ deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, deviceState.getNodeInstanceIdentifier(),
+ nodeBuilder.build());
+ } catch (final Exception e) {
+ LOG.warn("Failed to write node {} to DS ", deviceState.getNodeId(), e);
+ future.cancel(true);
+ }
+ }
+
+ private static ListenableFuture<List<RpcResult<List<MultipartReply>>>> createDeviceFeaturesForOF10(
+ final DeviceContext deviceContext, final DeviceState deviceState) {
+ final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo(MultipartType.OFPMPDESC,
+ deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
+
+ return Futures.allAsList(Arrays.asList(replyDesc));
+ }
+
+ private static ListenableFuture<List<RpcResult<List<MultipartReply>>>> createDeviceFeaturesForOF13(
+ final DeviceContext deviceContext, final DeviceState deviceState, final boolean switchFeaturesMandatory) {
+
+ final ListenableFuture<RpcResult<List<MultipartReply>>> replyDesc = getNodeStaticInfo(MultipartType.OFPMPDESC,
+ deviceContext, deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
+
+ //first process description reply, write data to DS and write consequent data if successful
+ return Futures.transform(replyDesc,
+ new AsyncFunction<RpcResult<List<MultipartReply>>, List<RpcResult<List<MultipartReply>>>>() {
+ @Override
+ public ListenableFuture<List<RpcResult<List<MultipartReply>>>> apply(
+ final RpcResult<List<MultipartReply>> rpcResult) throws Exception {
+
+ translateAndWriteReply(MultipartType.OFPMPDESC, deviceContext,
+ deviceState.getNodeInstanceIdentifier(), rpcResult.getResult());
+
+ final ListenableFuture<RpcResult<List<MultipartReply>>> replyMeterFeature = getNodeStaticInfo(
+ MultipartType.OFPMPMETERFEATURES, deviceContext,
+ deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
+
+ createSuccessProcessingCallback(MultipartType.OFPMPMETERFEATURES, deviceContext,
+ deviceState.getNodeInstanceIdentifier(), replyMeterFeature);
+
+ final ListenableFuture<RpcResult<List<MultipartReply>>> replyGroupFeatures = getNodeStaticInfo(
+ MultipartType.OFPMPGROUPFEATURES, deviceContext,
+ deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
+ createSuccessProcessingCallback(MultipartType.OFPMPGROUPFEATURES, deviceContext,
+ deviceState.getNodeInstanceIdentifier(), replyGroupFeatures);
+
+ final ListenableFuture<RpcResult<List<MultipartReply>>> replyTableFeatures = getNodeStaticInfo(
+ MultipartType.OFPMPTABLEFEATURES, deviceContext,
+ deviceState.getNodeInstanceIdentifier(), deviceState.getVersion());
+ createSuccessProcessingCallback(MultipartType.OFPMPTABLEFEATURES, deviceContext,
+ deviceState.getNodeInstanceIdentifier(), replyTableFeatures);
+
+ final ListenableFuture<RpcResult<List<MultipartReply>>> replyPortDescription = getNodeStaticInfo(
+ MultipartType.OFPMPPORTDESC, deviceContext, deviceState.getNodeInstanceIdentifier(),
+ deviceState.getVersion());
+ createSuccessProcessingCallback(MultipartType.OFPMPPORTDESC, deviceContext,
+ deviceState.getNodeInstanceIdentifier(), replyPortDescription);
+ if (switchFeaturesMandatory) {
+ return Futures.allAsList(Arrays.asList(replyMeterFeature, replyGroupFeatures,
+ replyTableFeatures, replyPortDescription));
+ } else {
+ return Futures.successfulAsList(Arrays.asList(replyMeterFeature, replyGroupFeatures,
+ replyTableFeatures, replyPortDescription));
+ }
+ }
+ });
+
+ }
+
+ static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext,
+ final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result) {
+ try {
+ for (final MultipartReply reply : result) {
+ final MultipartReplyBody body = reply.getMultipartReplyBody();
+ switch (type) {
+ case OFPMPDESC:
+ Preconditions.checkArgument(body instanceof MultipartReplyDescCase);
+ final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc();
+ final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc,
+ getIpAddressOf(dContext));
+ final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
+ dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
+ break;
+
+ case OFPMPTABLEFEATURES:
+ Preconditions.checkArgument(body instanceof MultipartReplyTableFeaturesCase);
+ final MultipartReplyTableFeatures tableFeatures = ((MultipartReplyTableFeaturesCase) body)
+ .getMultipartReplyTableFeatures();
+ final List<TableFeatures> tables = NodeStaticReplyTranslatorUtil
+ .nodeTableFeatureTranslator(tableFeatures);
+ for (final TableFeatures table : tables) {
+ final Short tableId = table.getTableId();
+ final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(
+ Table.class, new TableKey(tableId));
+ final TableBuilder tableBuilder = new TableBuilder().setId(tableId).setTableFeatures(
+ Collections.singletonList(table));
+ tableBuilder.addAugmentation(FlowTableStatisticsData.class,
+ new FlowTableStatisticsDataBuilder().build());
+ dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
+ }
+ break;
+
+ case OFPMPMETERFEATURES:
+ Preconditions.checkArgument(body instanceof MultipartReplyMeterFeaturesCase);
+ final MultipartReplyMeterFeatures meterFeatures = ((MultipartReplyMeterFeaturesCase) body)
+ .getMultipartReplyMeterFeatures();
+ final NodeMeterFeatures mFeature = NodeStaticReplyTranslatorUtil
+ .nodeMeterFeatureTranslator(meterFeatures);
+ final InstanceIdentifier<NodeMeterFeatures> mFeatureII = nodeII
+ .augmentation(NodeMeterFeatures.class);
+ dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, mFeatureII, mFeature);
+ if (0L < mFeature.getMeterFeatures().getMaxMeter().getValue()) {
+ dContext.getDeviceState().setMeterAvailable(true);
+ }
+ break;
+
+ case OFPMPGROUPFEATURES:
+ Preconditions.checkArgument(body instanceof MultipartReplyGroupFeaturesCase);
+ final MultipartReplyGroupFeatures groupFeatures = ((MultipartReplyGroupFeaturesCase) body)
+ .getMultipartReplyGroupFeatures();
+ final NodeGroupFeatures gFeature = NodeStaticReplyTranslatorUtil
+ .nodeGroupFeatureTranslator(groupFeatures);
+ final InstanceIdentifier<NodeGroupFeatures> gFeatureII = nodeII
+ .augmentation(NodeGroupFeatures.class);
+ dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gFeatureII, gFeature);
+ break;
+
+ case OFPMPPORTDESC:
+ Preconditions.checkArgument(body instanceof MultipartReplyPortDescCase);
+ final MultipartReplyPortDesc portDesc = ((MultipartReplyPortDescCase) body)
+ .getMultipartReplyPortDesc();
+ for (final PortGrouping port : portDesc.getPorts()) {
+ final short ofVersion = dContext.getDeviceState().getVersion();
+ final TranslatorKey translatorKey = new TranslatorKey(ofVersion, PortGrouping.class.getName());
+ final MessageTranslator<PortGrouping, FlowCapableNodeConnector> translator = dContext.oook()
+ .lookupTranslator(translatorKey);
+ final FlowCapableNodeConnector fcNodeConnector = translator.translate(port, dContext, null);
+
+ final BigInteger dataPathId = dContext.getPrimaryConnectionContext().getFeatures()
+ .getDatapathId();
+ final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(
+ dataPathId.toString(), port.getPortNo(), ofVersion);
+ final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder().setId(nodeConnectorId);
+ ncBuilder.addAugmentation(FlowCapableNodeConnector.class, fcNodeConnector);
+
+ ncBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class,
+ new FlowCapableNodeConnectorStatisticsDataBuilder().build());
+ final NodeConnector connector = ncBuilder.build();
+
+ final InstanceIdentifier<NodeConnector> connectorII = nodeII.child(NodeConnector.class,
+ connector.getKey());
+ dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, connectorII, connector);
+ }
+
+ break;
+
+ default:
+ throw new IllegalArgumentException("Unnexpected MultipartType " + type);
+ }
+ }
+ } catch (final Exception e) {
+ LOG.debug("Failed to write node {} to DS ", dContext.getDeviceState().getNodeId().toString(), e);
+ }
+ }
+
+ private static void createEmptyFlowCapableNodeInDs(final DeviceContext deviceContext) {
+ final FlowCapableNodeBuilder flowCapableNodeBuilder = new FlowCapableNodeBuilder();
+ final InstanceIdentifier<FlowCapableNode> fNodeII = deviceContext.getDeviceState().getNodeInstanceIdentifier()
+ .augmentation(FlowCapableNode.class);
+ try {
+ deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, flowCapableNodeBuilder.build());
+ } catch (final Exception e) {
+ LOG.debug("Failed to write node {} to DS ", deviceContext.getDeviceState().getNodeId().toString(), e);
+ }
+ }
+
+ private static IpAddress getIpAddressOf(final DeviceContext deviceContext) {
+
+ final InetSocketAddress remoteAddress = deviceContext.getPrimaryConnectionContext().getConnectionAdapter()
+ .getRemoteAddress();
+
+ if (remoteAddress == null) {
+ LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", deviceContext
+ .getDeviceState().getNodeId());
+ return null;
+ }
+ LOG.info("IP address of switch is :" + remoteAddress);
+
+ final InetAddress address = remoteAddress.getAddress();
+ final String hostAddress = address.getHostAddress();
+ if (address instanceof Inet4Address) {
+ return new IpAddress(new Ipv4Address(hostAddress));
+ }
+ if (address instanceof Inet6Address) {
+ return new IpAddress(new Ipv6Address(hostAddress));
+ }
+ LOG.info("Illegal IP address {} of switch:{} ", address, deviceContext.getDeviceState().getNodeId());
+ return null;
+
+ }
+
+ // FIXME : remove after ovs tableFeatures fix
+ private static void makeEmptyTables(final DeviceContext dContext, final InstanceIdentifier<Node> nodeII,
+ final Short nrOfTables) {
+ LOG.debug("About to create {} empty tables.", nrOfTables);
+ for (int i = 0; i < nrOfTables; i++) {
+ final short tId = (short) i;
+ final InstanceIdentifier<Table> tableII = nodeII.augmentation(FlowCapableNode.class).child(Table.class,
+ new TableKey(tId));
+ final TableBuilder tableBuilder = new TableBuilder().setId(tId).addAugmentation(
+ FlowTableStatisticsData.class, new FlowTableStatisticsDataBuilder().build());
+
+ try {
+ dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableII, tableBuilder.build());
+ } catch (final Exception e) {
+ LOG.debug("Failed to write node {} to DS ", dContext.getDeviceState().getNodeId().toString(), e);
+ }
+
+ }
+ }
+
+ static void createSuccessProcessingCallback(final MultipartType type, final DeviceContext deviceContext,
+ final InstanceIdentifier<Node> nodeII,
+ final ListenableFuture<RpcResult<List<MultipartReply>>> requestContextFuture) {
+ Futures.addCallback(requestContextFuture, new FutureCallback<RpcResult<List<MultipartReply>>>() {
+ @Override
+ public void onSuccess(final RpcResult<List<MultipartReply>> rpcResult) {
+ final List<MultipartReply> result = rpcResult.getResult();
+ if (result != null) {
+ LOG.info("Static node {} info: {} collected", deviceContext.getDeviceState().getNodeId(), type);
+ translateAndWriteReply(type, deviceContext, nodeII, result);
+ } else {
+ final Iterator<RpcError> rpcErrorIterator = rpcResult.getErrors().iterator();
+ while (rpcErrorIterator.hasNext()) {
+ final RpcError rpcError = rpcErrorIterator.next();
+ LOG.info("Failed to retrieve static node {} info: {}", type, rpcError.getMessage());
+ if (null != rpcError.getCause()) {
+ LOG.trace("Detailed error:", rpcError.getCause());
+ }
+ }
+ if (MultipartType.OFPMPTABLEFEATURES.equals(type)) {
+ makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext()
+ .getFeatures().getTables());
+ }
+ }
+ }
+
+ @Override
+ public void onFailure(final Throwable throwable) {
+ LOG.info("Request of type {} for static info of node {} failed.", type, nodeII);
+ }
+ });
+ }
+
+ private static ListenableFuture<RpcResult<List<MultipartReply>>> getNodeStaticInfo(final MultipartType type,
+ final DeviceContext deviceContext, final InstanceIdentifier<Node> nodeII, final short version) {
+
+ final OutboundQueue queue = deviceContext.getPrimaryConnectionContext().getOutboundQueueProvider();
+
+ final Long reserved = deviceContext.getReservedXid();
+ final RequestContext<List<MultipartReply>> requestContext = new AbstractRequestContext<List<MultipartReply>>(
+ reserved) {
+ @Override
+ public void close() {
+ //NOOP
+ }
+ };
+
+ final Xid xid = requestContext.getXid();
+
+ LOG.trace("Hooking xid {} to device context - precaution.", reserved);
+
+ final MultiMsgCollector multiMsgCollector = deviceContext.getMultiMsgCollector(requestContext);
+ queue.commitEntry(xid.getValue(),
+ MultipartRequestInputFactory.makeMultipartRequestInput(xid.getValue(), version, type),
+ new FutureCallback<OfHeader>() {
+ @Override
+ public void onSuccess(final OfHeader ofHeader) {
+ if (ofHeader instanceof MultipartReply) {
+ final MultipartReply multipartReply = (MultipartReply) ofHeader;
+ multiMsgCollector.addMultipartMsg(multipartReply);
+ } else if (null != ofHeader) {
+ LOG.info("Unexpected response type received {}.", ofHeader.getClass());
+ } else {
+ multiMsgCollector.endCollecting();
+ LOG.info("Response received is null.");
+ }
+ }
+
+ @Override
+ public void onFailure(final Throwable t) {
+ LOG.info("Fail response from OutboundQueue for multipart type {}.", type);
+ final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder
+ .<List<MultipartReply>> failed().build();
+ requestContext.setResult(rpcResult);
+ if (MultipartType.OFPMPTABLEFEATURES.equals(type)) {
+ makeEmptyTables(deviceContext, nodeII, deviceContext.getPrimaryConnectionContext()
+ .getFeatures().getTables());
+ }
+ requestContext.close();
+ }
+ });
+
+ return requestContext.getFuture();
+ }
+
+ static void chainTableTrunkWriteOF10(final DeviceContext deviceContext,
+ final ListenableFuture<List<RpcResult<List<MultipartReply>>>> deviceFeaturesFuture) {
+ Futures.addCallback(deviceFeaturesFuture, new FutureCallback<List<RpcResult<List<MultipartReply>>>>() {
+ @Override
+ public void onSuccess(final List<RpcResult<List<MultipartReply>>> results) {
+ boolean allSucceeded = true;
+ for (final RpcResult<List<MultipartReply>> rpcResult : results) {
+ allSucceeded &= rpcResult.isSuccessful();
+ }
+ if (allSucceeded) {
+ createEmptyFlowCapableNodeInDs(deviceContext);
+ makeEmptyTables(deviceContext, deviceContext.getDeviceState().getNodeInstanceIdentifier(),
+ deviceContext.getDeviceState().getFeatures().getTables());
+ }
+ }
+
+ @Override
+ public void onFailure(final Throwable t) {
+ //NOOP
+ }
+ });
+ }
+}
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
-import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-import com.google.common.collect.Lists;
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
import java.lang.reflect.Field;
import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.Collection;
import java.util.Collections;
-import java.util.List;
import java.util.Set;
+
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.FutureCallback;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ActionType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupCapabilities;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupTypes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterBandTypeBitmap;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPortBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortDescCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDescBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeaturesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeaturesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.MultipartReplyPortDescBuilder;
-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.PortsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeaturesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.multipart.reply.table.features.TableFeatures;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.multipart.reply.table.features.TableFeaturesBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.RpcError;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
@RunWith(MockitoJUnitRunner.class)
public class DeviceManagerImplTest {
order.verify(mockConnectionContext).setOutboundQueueHandleRegistration(
Mockito.<OutboundQueueHandlerRegistration<OutboundQueueProvider>>any());
order.verify(mockConnectionContext).getNodeId();
- order.verify(mockConnectionContext).setDeviceDisconnectedHandler(any(DeviceContext.class));
-
Mockito.verify(deviceInitPhaseHandler).onDeviceContextLevelUp(Matchers.<DeviceContext>any());
}
order.verify(mockConnectionContext).setOutboundQueueHandleRegistration(
Mockito.<OutboundQueueHandlerRegistration<OutboundQueueProvider>>any());
order.verify(mockConnectionContext).getNodeId();
- order.verify(mockConnectionContext).setDeviceDisconnectedHandler(any(DeviceContext.class));
-
Mockito.verify(deviceInitPhaseHandler).onDeviceContextLevelUp(Matchers.<DeviceContext>any());
}
deviceManager.setTranslatorLibrary(translatorLibrary);
}
- @Test
- public void chainTableTrunkWriteOF10Test() {
- DeviceState mockedDeviceState = mock(DeviceState.class);
-
- GetFeaturesOutput mockedFeatures = mock(GetFeaturesOutput.class);
- when(mockedFeatures.getTables()).thenReturn((short) 2);
- when(mockedDeviceState.getFeatures()).thenReturn(mockedFeatures);
-
- when(mockedDeviceState.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_II);
- when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
-
- RpcResult<List<MultipartReply>> mockedRpcResult = mock(RpcResult.class);
- when(mockedRpcResult.isSuccessful()).thenReturn(true);
- List<RpcResult<List<MultipartReply>>> data = new ArrayList<RpcResult<List<MultipartReply>>>();
- data.add(mockedRpcResult);
- data.add(mockedRpcResult);
-
- DeviceManagerImpl.chainTableTrunkWriteOF10(mockedDeviceContext, Futures.immediateFuture(data));
- verify(mockedDeviceContext, times(3))
- .writeToTransaction(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(FlowCapableNode.class));
- }
-
- @Test
- public void testTranslateAndWriteReplyTypeDesc() {
- final ConnectionContext connectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_3);
- Mockito.when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
- DeviceState deviceState = Mockito.mock(DeviceState.class);
- Mockito.when(mockedDeviceContext.getDeviceState()).thenReturn(deviceState);
-
- Collection<MultipartReply> multipartReplyMessages = prepareDataforTypeDesc(mockedDeviceContext);
-
- DeviceManagerImpl.translateAndWriteReply(MultipartType.OFPMPDESC, mockedDeviceContext, DUMMY_NODE_II, multipartReplyMessages);
- verify(mockedDeviceContext)
- .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), eq(DUMMY_NODE_II.augmentation(FlowCapableNode.class)), any(FlowCapableNode.class));
- }
-
- private Collection<MultipartReply> prepareDataforTypeDesc(final DeviceContext mockedDeviceContext) {
- MultipartReplyDesc multipartReplyDesc = new MultipartReplyDescBuilder().build();
-
- MultipartReplyDescCaseBuilder multipartReplyDescCaseBuilder = new MultipartReplyDescCaseBuilder();
- multipartReplyDescCaseBuilder.setMultipartReplyDesc(multipartReplyDesc);
-
- MultipartReplyMessage multipartReplyMessage = new MultipartReplyMessageBuilder().setMultipartReplyBody(multipartReplyDescCaseBuilder.build()).build();
- return Collections.<MultipartReply>singleton(multipartReplyMessage);
-
- }
-
- @Test
- public void translateAndWriteReplyTypeTableFeatures() {
- TableFeaturesBuilder tableFeature = new TableFeaturesBuilder();
- tableFeature.setTableId(DUMMY_TABLE_ID);
- List<TableFeatures> tableFeatures = new ArrayList<>();
- tableFeatures.add(tableFeature.build());
-
- MultipartReplyTableFeatures multipartReplyTableFeatures = new MultipartReplyTableFeaturesBuilder().setTableFeatures(tableFeatures).build();
- MultipartReplyTableFeaturesCaseBuilder multipartReplyTableFeaturesCaseBuilder = new MultipartReplyTableFeaturesCaseBuilder();
- multipartReplyTableFeaturesCaseBuilder.setMultipartReplyTableFeatures(multipartReplyTableFeatures);
-
- MultipartReplyMessage multipartReplyMessage = new MultipartReplyMessageBuilder().setMultipartReplyBody(multipartReplyTableFeaturesCaseBuilder.build()).build();
- Set<MultipartReply> multipartReplyMessages = Collections.<MultipartReply>singleton(multipartReplyMessage);
- DeviceManagerImpl.translateAndWriteReply(MultipartType.OFPMPTABLEFEATURES, mockedDeviceContext, DUMMY_NODE_II, multipartReplyMessages);
- verify(mockedDeviceContext)
- .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
- eq(DUMMY_NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID))), any(Table.class));
-
- }
-
- @Test
- public void translateAndWriteReplyTypeMeterFeatures() {
- DeviceState mockedDeviceState = mock(DeviceState.class);
- when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
-
- MultipartReplyMeterFeaturesBuilder multipartReplyMeterFeaturesBuilder = new MultipartReplyMeterFeaturesBuilder();
- multipartReplyMeterFeaturesBuilder.setBandTypes(new MeterBandTypeBitmap(true, true));
- multipartReplyMeterFeaturesBuilder.setCapabilities(new MeterFlags(true, true, true, true));
- multipartReplyMeterFeaturesBuilder.setMaxMeter(DUMMY_MAX_METER);
-
- MultipartReplyMeterFeaturesCaseBuilder multipartReplyMeterFeaturesCaseBuilder = new MultipartReplyMeterFeaturesCaseBuilder();
- multipartReplyMeterFeaturesCaseBuilder.setMultipartReplyMeterFeatures(multipartReplyMeterFeaturesBuilder.build());
-
- MultipartReplyMessage multipartReplyMessage = new MultipartReplyMessageBuilder().setMultipartReplyBody(multipartReplyMeterFeaturesCaseBuilder.build()).build();
- Set<MultipartReply> multipartReplyMessages = Collections.<MultipartReply>singleton(multipartReplyMessage);
- DeviceManagerImpl.translateAndWriteReply(MultipartType.OFPMPMETERFEATURES, mockedDeviceContext, DUMMY_NODE_II, multipartReplyMessages);
- verify(mockedDeviceContext)
- .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), eq(DUMMY_NODE_II.augmentation(NodeMeterFeatures.class)), any(NodeMeterFeatures.class));
- verify(mockedDeviceState).setMeterAvailable(eq(true));
- }
-
- @Test
- public void translateAndWriteReplyTypeGroupFeatures() {
- MultipartReplyGroupFeaturesBuilder multipartReplyGroupFeaturesBuilder = new MultipartReplyGroupFeaturesBuilder();
- multipartReplyGroupFeaturesBuilder.setTypes(new GroupTypes(true, true, true, true));
- multipartReplyGroupFeaturesBuilder.setCapabilities(new GroupCapabilities(true, true, true, true));
- ActionType actionType = new ActionType(true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true);
- multipartReplyGroupFeaturesBuilder.setActionsBitmap(Lists.newArrayList(actionType));
-
- MultipartReplyGroupFeatures multipartReplyGroupFeatures = multipartReplyGroupFeaturesBuilder.build();
-
- MultipartReplyGroupFeaturesCaseBuilder multipartReplyGroupFeaturesCaseBuilder = new MultipartReplyGroupFeaturesCaseBuilder();
- multipartReplyGroupFeaturesCaseBuilder.setMultipartReplyGroupFeatures(multipartReplyGroupFeatures);
-
- MultipartReplyMessage multipartReplyMessage = new MultipartReplyMessageBuilder().setMultipartReplyBody(multipartReplyGroupFeaturesCaseBuilder.build()).build();
- Set<MultipartReply> multipartReplyMessages = Collections.<MultipartReply>singleton(multipartReplyMessage);
-
- DeviceManagerImpl.translateAndWriteReply(MultipartType.OFPMPGROUPFEATURES, mockedDeviceContext, DUMMY_NODE_II, multipartReplyMessages);
- verify(mockedDeviceContext)
- .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), eq(DUMMY_NODE_II.augmentation(NodeGroupFeatures.class)), any(NodeGroupFeatures.class));
- }
-
-
- @Test
- public void translateAndWriteReplyTypePortDesc() {
- ConnectionContext mockedPrimaryConnectionContext = mock(ConnectionContext.class);
- FeaturesReply mockedFeatures = mock(FeaturesReply.class);
- when(mockedFeatures.getDatapathId()).thenReturn(new BigInteger(DUMMY_DATAPATH_ID));
- when(mockedPrimaryConnectionContext.getFeatures()).thenReturn(mockedFeatures);
- when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimaryConnectionContext);
- DeviceState mockedDeviceState = mock(DeviceState.class);
- when(mockedDeviceState.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0);
- when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
- MessageTranslator mockedTranslator = mock(MessageTranslator.class);
- when(translatorLibrary.lookupTranslator(any(TranslatorKey.class))).thenReturn(mockedTranslator);
- when(mockedDeviceContext.oook()).thenReturn(translatorLibrary);
-
- MultipartReplyPortDescBuilder multipartReplyPortDescBuilder = new MultipartReplyPortDescBuilder();
-
- PortsBuilder portsBuilder = new PortsBuilder();
- portsBuilder.setPortNo(DUMMY_PORT_NUMBER);
-
- multipartReplyPortDescBuilder.setPorts(Lists.newArrayList(portsBuilder.build()));
-
- MultipartReplyPortDescCaseBuilder multipartReplyPortDescCaseBuilder = new MultipartReplyPortDescCaseBuilder();
- multipartReplyPortDescCaseBuilder.setMultipartReplyPortDesc(multipartReplyPortDescBuilder.build());
-
- MultipartReplyMessage multipartReplyMessage = new MultipartReplyMessageBuilder().setMultipartReplyBody(multipartReplyPortDescCaseBuilder.build()).build();
- Set<MultipartReply> multipartReplyMessages = Collections.<MultipartReply>singleton(multipartReplyMessage);
-
- OpenflowPortsUtil.init();
- DeviceManagerImpl.translateAndWriteReply(MultipartType.OFPMPPORTDESC, mockedDeviceContext, DUMMY_NODE_II, multipartReplyMessages);
- verify(mockedDeviceContext)
- .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), any(NodeConnector.class));
- }
-
- @Test
- public void createSuccessProcessingCallbackTest() {
- DeviceState mockedDeviceState = mock(DeviceState.class);
- when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
-
- final ConnectionContext connectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_3);
-
- List<MultipartReply> multipartReplies = new ArrayList<>(prepareDataforTypeDesc(mockedDeviceContext));
- RpcResult<List<MultipartReply>> result = RpcResultBuilder.<List<MultipartReply>>success(multipartReplies).build();
- ListenableFuture<RpcResult<List<MultipartReply>>> mockedRequestContextFuture = Futures.immediateFuture(result);
-
- DeviceManagerImpl.createSuccessProcessingCallback(MultipartType.OFPMPDESC, mockedDeviceContext, DUMMY_NODE_II, mockedRequestContextFuture);
- verify(mockedDeviceContext).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), eq(DUMMY_NODE_II.augmentation(FlowCapableNode.class)), any(FlowCapableNode.class));
-
- RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.PROTOCOL, "dummy error").build();
- mockedRequestContextFuture = Futures.immediateFuture(rpcResult);
- DeviceManagerImpl.createSuccessProcessingCallback(MultipartType.OFPMPDESC, mockedDeviceContext, DUMMY_NODE_II, mockedRequestContextFuture);
- verify(mockedDeviceContext).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), eq(DUMMY_NODE_II.augmentation(FlowCapableNode.class)), any(FlowCapableNode.class));
- }
-
@Test
public void testClose() throws Exception {
DeviceContext deviceContext = Mockito.mock(DeviceContext.class);
--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowplugin.impl.util;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.stubbing.Answer;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
+import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
+import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandler;
+import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
+import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
+import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
+import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
+import org.opendaylight.openflowplugin.impl.device.DeviceContextImpl;
+import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ActionType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupCapabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupTypes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterBandTypeBitmap;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortDescCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDescBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeaturesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeaturesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.MultipartReplyPortDescBuilder;
+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.PortsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeaturesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.multipart.reply.table.features.TableFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.multipart.reply.table.features.TableFeaturesBuilder;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+
+@RunWith(MockitoJUnitRunner.class)
+public class DeviceInitializationUtilsTest {
+
+ private static final boolean TEST_VALUE_SWITCH_FEATURE_MANDATORY = true;
+ private static final long TEST_VALUE_GLOBAL_NOTIFICATION_QUOTA = 2000l;
+ private static final KeyedInstanceIdentifier<Node, NodeKey> DUMMY_NODE_II = InstanceIdentifier.create(Nodes.class)
+ .child(Node.class, new NodeKey(new NodeId("dummyNodeId")));
+ private static final Short DUMMY_TABLE_ID = 1;
+ private static final Long DUMMY_MAX_METER = 544L;
+ private static final String DUMMY_DATAPATH_ID = "44";
+ private static final Long DUMMY_PORT_NUMBER = 21L;
+
+ @Mock
+ CheckedFuture<Void, TransactionCommitFailedException> mockedFuture;
+ @Mock
+ private FeaturesReply mockFeatures;
+ @Mock
+ private OutboundQueue outboundQueueProvider;
+ @Mock
+ private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
+ @Mock
+ private TranslatorLibrary translatorLibrary;
+ @Mock
+ private ConnectionContext mockConnectionContext;
+ @Mock
+ private ConnectionAdapter mockedConnectionAdapter;
+ @Mock
+ private DeviceContextImpl mockedDeviceContext;
+ @Mock
+ private DeviceInitializationUtils deviceInitializationUtils;
+
+ @Before
+ public void setUp() throws Exception {
+ OpenflowPortsUtil.init();
+
+ when(mockConnectionContext.getNodeId()).thenReturn(new NodeId("dummyNodeId"));
+ when(mockConnectionContext.getFeatures()).thenReturn(mockFeatures);
+ when(mockConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
+ when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
+
+ final Capabilities capabilitiesV13 = Mockito.mock(Capabilities.class);
+ final CapabilitiesV10 capabilitiesV10 = Mockito.mock(CapabilitiesV10.class);
+ when(mockFeatures.getCapabilities()).thenReturn(capabilitiesV13);
+ when(mockFeatures.getCapabilitiesV10()).thenReturn(capabilitiesV10);
+ when(mockFeatures.getDatapathId()).thenReturn(BigInteger.valueOf(21L));
+ }
+ @Test
+ public void chainTableTrunkWriteOF10Test() {
+ DeviceState mockedDeviceState = mock(DeviceState.class);
+
+ GetFeaturesOutput mockedFeatures = mock(GetFeaturesOutput.class);
+ when(mockedFeatures.getTables()).thenReturn((short) 2);
+ when(mockedDeviceState.getFeatures()).thenReturn(mockedFeatures);
+
+ when(mockedDeviceState.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_II);
+ when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
+
+ RpcResult<List<MultipartReply>> mockedRpcResult = mock(RpcResult.class);
+ when(mockedRpcResult.isSuccessful()).thenReturn(true);
+ List<RpcResult<List<MultipartReply>>> data = new ArrayList<RpcResult<List<MultipartReply>>>();
+ data.add(mockedRpcResult);
+ data.add(mockedRpcResult);
+
+ DeviceInitializationUtils.chainTableTrunkWriteOF10(mockedDeviceContext, Futures.immediateFuture(data));
+ verify(mockedDeviceContext, times(3))
+ .writeToTransaction(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(FlowCapableNode.class));
+ }
+
+ @Test
+ public void testTranslateAndWriteReplyTypeDesc() {
+ final ConnectionContext connectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_3);
+ Mockito.when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
+ DeviceState deviceState = Mockito.mock(DeviceState.class);
+ Mockito.when(mockedDeviceContext.getDeviceState()).thenReturn(deviceState);
+
+ Collection<MultipartReply> multipartReplyMessages = prepareDataforTypeDesc(mockedDeviceContext);
+
+ DeviceInitializationUtils.translateAndWriteReply(MultipartType.OFPMPDESC, mockedDeviceContext, DUMMY_NODE_II, multipartReplyMessages);
+ verify(mockedDeviceContext)
+ .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), eq(DUMMY_NODE_II.augmentation(FlowCapableNode.class)), any(FlowCapableNode.class));
+ }
+
+ @Test
+ public void translateAndWriteReplyTypeTableFeatures() {
+ TableFeaturesBuilder tableFeature = new TableFeaturesBuilder();
+ tableFeature.setTableId(DUMMY_TABLE_ID);
+ List<TableFeatures> tableFeatures = new ArrayList<>();
+ tableFeatures.add(tableFeature.build());
+
+ MultipartReplyTableFeatures multipartReplyTableFeatures = new MultipartReplyTableFeaturesBuilder().setTableFeatures(tableFeatures).build();
+ MultipartReplyTableFeaturesCaseBuilder multipartReplyTableFeaturesCaseBuilder = new MultipartReplyTableFeaturesCaseBuilder();
+ multipartReplyTableFeaturesCaseBuilder.setMultipartReplyTableFeatures(multipartReplyTableFeatures);
+
+ MultipartReplyMessage multipartReplyMessage = new MultipartReplyMessageBuilder().setMultipartReplyBody(multipartReplyTableFeaturesCaseBuilder.build()).build();
+ Set<MultipartReply> multipartReplyMessages = Collections.<MultipartReply>singleton(multipartReplyMessage);
+ DeviceInitializationUtils.translateAndWriteReply(MultipartType.OFPMPTABLEFEATURES, mockedDeviceContext, DUMMY_NODE_II, multipartReplyMessages);
+ verify(mockedDeviceContext)
+ .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL),
+ eq(DUMMY_NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID))), any(Table.class));
+
+ }
+
+ @Test
+ public void translateAndWriteReplyTypeMeterFeatures() {
+ DeviceState mockedDeviceState = mock(DeviceState.class);
+ when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
+
+ MultipartReplyMeterFeaturesBuilder multipartReplyMeterFeaturesBuilder = new MultipartReplyMeterFeaturesBuilder();
+ multipartReplyMeterFeaturesBuilder.setBandTypes(new MeterBandTypeBitmap(true, true));
+ multipartReplyMeterFeaturesBuilder.setCapabilities(new MeterFlags(true, true, true, true));
+ multipartReplyMeterFeaturesBuilder.setMaxMeter(DUMMY_MAX_METER);
+
+ MultipartReplyMeterFeaturesCaseBuilder multipartReplyMeterFeaturesCaseBuilder = new MultipartReplyMeterFeaturesCaseBuilder();
+ multipartReplyMeterFeaturesCaseBuilder.setMultipartReplyMeterFeatures(multipartReplyMeterFeaturesBuilder.build());
+
+ MultipartReplyMessage multipartReplyMessage = new MultipartReplyMessageBuilder().setMultipartReplyBody(multipartReplyMeterFeaturesCaseBuilder.build()).build();
+ Set<MultipartReply> multipartReplyMessages = Collections.<MultipartReply>singleton(multipartReplyMessage);
+ DeviceInitializationUtils.translateAndWriteReply(MultipartType.OFPMPMETERFEATURES, mockedDeviceContext, DUMMY_NODE_II, multipartReplyMessages);
+ verify(mockedDeviceContext)
+ .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), eq(DUMMY_NODE_II.augmentation(NodeMeterFeatures.class)), any(NodeMeterFeatures.class));
+ verify(mockedDeviceState).setMeterAvailable(eq(true));
+ }
+
+ @Test
+ public void translateAndWriteReplyTypeGroupFeatures() {
+ MultipartReplyGroupFeaturesBuilder multipartReplyGroupFeaturesBuilder = new MultipartReplyGroupFeaturesBuilder();
+ multipartReplyGroupFeaturesBuilder.setTypes(new GroupTypes(true, true, true, true));
+ multipartReplyGroupFeaturesBuilder.setCapabilities(new GroupCapabilities(true, true, true, true));
+ ActionType actionType = new ActionType(true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true);
+ multipartReplyGroupFeaturesBuilder.setActionsBitmap(Lists.newArrayList(actionType));
+
+ MultipartReplyGroupFeatures multipartReplyGroupFeatures = multipartReplyGroupFeaturesBuilder.build();
+
+ MultipartReplyGroupFeaturesCaseBuilder multipartReplyGroupFeaturesCaseBuilder = new MultipartReplyGroupFeaturesCaseBuilder();
+ multipartReplyGroupFeaturesCaseBuilder.setMultipartReplyGroupFeatures(multipartReplyGroupFeatures);
+
+ MultipartReplyMessage multipartReplyMessage = new MultipartReplyMessageBuilder().setMultipartReplyBody(multipartReplyGroupFeaturesCaseBuilder.build()).build();
+ Set<MultipartReply> multipartReplyMessages = Collections.<MultipartReply>singleton(multipartReplyMessage);
+
+ DeviceInitializationUtils.translateAndWriteReply(MultipartType.OFPMPGROUPFEATURES, mockedDeviceContext, DUMMY_NODE_II, multipartReplyMessages);
+ verify(mockedDeviceContext)
+ .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), eq(DUMMY_NODE_II.augmentation(NodeGroupFeatures.class)), any(NodeGroupFeatures.class));
+ }
+
+
+ @Test
+ public void translateAndWriteReplyTypePortDesc() {
+ ConnectionContext mockedPrimaryConnectionContext = mock(ConnectionContext.class);
+ FeaturesReply mockedFeatures = mock(FeaturesReply.class);
+ when(mockedFeatures.getDatapathId()).thenReturn(new BigInteger(DUMMY_DATAPATH_ID));
+ when(mockedPrimaryConnectionContext.getFeatures()).thenReturn(mockedFeatures);
+ when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimaryConnectionContext);
+ DeviceState mockedDeviceState = mock(DeviceState.class);
+ when(mockedDeviceState.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0);
+ when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
+ MessageTranslator mockedTranslator = mock(MessageTranslator.class);
+ when(translatorLibrary.lookupTranslator(any(TranslatorKey.class))).thenReturn(mockedTranslator);
+ when(mockedDeviceContext.oook()).thenReturn(translatorLibrary);
+
+ MultipartReplyPortDescBuilder multipartReplyPortDescBuilder = new MultipartReplyPortDescBuilder();
+
+ PortsBuilder portsBuilder = new PortsBuilder();
+ portsBuilder.setPortNo(DUMMY_PORT_NUMBER);
+
+ multipartReplyPortDescBuilder.setPorts(Lists.newArrayList(portsBuilder.build()));
+
+ MultipartReplyPortDescCaseBuilder multipartReplyPortDescCaseBuilder = new MultipartReplyPortDescCaseBuilder();
+ multipartReplyPortDescCaseBuilder.setMultipartReplyPortDesc(multipartReplyPortDescBuilder.build());
+
+ MultipartReplyMessage multipartReplyMessage = new MultipartReplyMessageBuilder().setMultipartReplyBody(multipartReplyPortDescCaseBuilder.build()).build();
+ Set<MultipartReply> multipartReplyMessages = Collections.<MultipartReply>singleton(multipartReplyMessage);
+
+ OpenflowPortsUtil.init();
+ DeviceInitializationUtils.translateAndWriteReply(MultipartType.OFPMPPORTDESC, mockedDeviceContext, DUMMY_NODE_II, multipartReplyMessages);
+ verify(mockedDeviceContext)
+ .writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), any(NodeConnector.class));
+ }
+
+ @Test
+ public void createSuccessProcessingCallbackTest() {
+ DeviceState mockedDeviceState = mock(DeviceState.class);
+ when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
+
+ final ConnectionContext connectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_3);
+
+ List<MultipartReply> multipartReplies = new ArrayList<>(prepareDataforTypeDesc(mockedDeviceContext));
+ RpcResult<List<MultipartReply>> result = RpcResultBuilder.<List<MultipartReply>>success(multipartReplies).build();
+ ListenableFuture<RpcResult<List<MultipartReply>>> mockedRequestContextFuture = Futures.immediateFuture(result);
+
+ DeviceInitializationUtils.createSuccessProcessingCallback(MultipartType.OFPMPDESC, mockedDeviceContext, DUMMY_NODE_II, mockedRequestContextFuture);
+ verify(mockedDeviceContext).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), eq(DUMMY_NODE_II.augmentation(FlowCapableNode.class)), any(FlowCapableNode.class));
+
+ RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.PROTOCOL, "dummy error").build();
+ mockedRequestContextFuture = Futures.immediateFuture(rpcResult);
+ DeviceInitializationUtils.createSuccessProcessingCallback(MultipartType.OFPMPDESC, mockedDeviceContext, DUMMY_NODE_II, mockedRequestContextFuture);
+ verify(mockedDeviceContext).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), eq(DUMMY_NODE_II.augmentation(FlowCapableNode.class)), any(FlowCapableNode.class));
+ }
+
+ private Collection<MultipartReply> prepareDataforTypeDesc(final DeviceContext mockedDeviceContext) {
+ MultipartReplyDesc multipartReplyDesc = new MultipartReplyDescBuilder().build();
+
+ MultipartReplyDescCaseBuilder multipartReplyDescCaseBuilder = new MultipartReplyDescCaseBuilder();
+ multipartReplyDescCaseBuilder.setMultipartReplyDesc(multipartReplyDesc);
+
+ MultipartReplyMessage multipartReplyMessage = new MultipartReplyMessageBuilder().setMultipartReplyBody(multipartReplyDescCaseBuilder.build()).build();
+ return Collections.<MultipartReply>singleton(multipartReplyMessage);
+
+ }
+
+ protected ConnectionContext buildMockConnectionContext(short ofpVersion) {
+ when(mockFeatures.getVersion()).thenReturn(ofpVersion);
+ when(outboundQueueProvider.reserveEntry()).thenReturn(43L);
+ Mockito.doAnswer(new Answer<Void>() {
+ @Override
+ public Void answer(InvocationOnMock invocation) throws Throwable {
+ final FutureCallback<OfHeader> callBack = (FutureCallback<OfHeader>) invocation.getArguments()[2];
+ callBack.onSuccess(null);
+ return null;
+ }
+ })
+ .when(outboundQueueProvider)
+ .commitEntry(Matchers.anyLong(), Matchers.<MultipartRequestInput>any(), Matchers.<FutureCallback<OfHeader>>any());
+
+ when(mockedConnectionAdapter.registerOutboundQueueHandler(Matchers.<OutboundQueueHandler>any(), Matchers.anyInt(), Matchers.anyLong()))
+ .thenAnswer(new Answer<OutboundQueueHandlerRegistration<OutboundQueueHandler>>() {
+ @Override
+ public OutboundQueueHandlerRegistration<OutboundQueueHandler> answer(InvocationOnMock invocation) throws Throwable {
+ OutboundQueueHandler handler = (OutboundQueueHandler) invocation.getArguments()[0];
+ handler.onConnectionQueueChanged(outboundQueueProvider);
+ return null;
+ }
+ });
+
+ when(mockConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueueProvider);
+ return mockConnectionContext;
+ }
+
+
+
+}