2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.impl.util;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import java.math.BigInteger;
14 import java.util.List;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.openflowplugin.api.OFConstants;
18 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
19 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPort;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketIn;
27 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
30 * Created by Tomas Slusny on 23.3.2016.
32 public class NodeConnectorRefToPortTranslator {
34 * Converts {@link PacketIn} to {@link NodeConnectorRef}.
35 * @param packetIn Packet input
36 * @param dataPathId Data path id
37 * @return packet input converted to node connector reference
40 public static NodeConnectorRef toNodeConnectorRef(@Nonnull PacketIn packetIn, BigInteger dataPathId) {
41 Preconditions.checkNotNull(packetIn);
43 NodeConnectorRef ref = null;
44 Long port = getPortNoFromPacketIn(packetIn);
47 OpenflowVersion version = OpenflowVersion.get(packetIn.getVersion());
49 ref = InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dataPathId, port, version);
56 * Gets port number from {@link NodeConnectorRef}.
57 * @param nodeConnectorRef Node connector reference
58 * @param version Openflow version
61 @SuppressWarnings("unchecked")
63 public static Long fromNodeConnectorRef(@Nonnull NodeConnectorRef nodeConnectorRef, short version) {
64 Preconditions.checkNotNull(nodeConnectorRef);
68 if (nodeConnectorRef.getValue() instanceof KeyedInstanceIdentifier) {
69 KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> identifier =
70 (KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey>) nodeConnectorRef.getValue();
72 OpenflowVersion ofVersion = OpenflowVersion.get(version);
73 String nodeConnectorId = identifier.getKey().getId().getValue();
75 port = InventoryDataServiceUtil.portNumberfromNodeConnectorId(ofVersion, nodeConnectorId);
83 static Long getPortNoFromPacketIn(@Nonnull PacketIn packetIn) {
84 Preconditions.checkNotNull(packetIn);
88 if (packetIn.getVersion() == OFConstants.OFP_VERSION_1_0 && packetIn.getInPort() != null) {
89 port = packetIn.getInPort().longValue();
90 } else if (packetIn.getVersion() == OFConstants.OFP_VERSION_1_3) {
91 if (packetIn.getMatch() != null && packetIn.getMatch().getMatchEntry() != null) {
92 List<MatchEntry> entries = packetIn.getMatch().getMatchEntry();
94 for (MatchEntry entry : entries) {
95 if (entry.getMatchEntryValue() instanceof InPortCase) {
96 InPortCase inPortCase = (InPortCase) entry.getMatchEntryValue();
98 InPort inPort = inPortCase.getInPort();
100 if (inPort != null) {
101 port = inPort.getPortNumber().getValue();