40ffe4754422ff8bda1e7baf764494ef1f2e6ecb
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / NodeConnectorRefToPortTranslator.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.util;
10
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;
28
29 /**
30  * Created by Tomas Slusny on 23.3.2016.
31  */
32 public class NodeConnectorRefToPortTranslator {
33     /**
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
38      */
39     @Nullable
40     public static NodeConnectorRef toNodeConnectorRef(@Nonnull PacketIn packetIn, BigInteger dataPathId) {
41         Preconditions.checkNotNull(packetIn);
42
43         NodeConnectorRef ref = null;
44         Long port = getPortNoFromPacketIn(packetIn);
45
46         if (port != null) {
47             OpenflowVersion version = OpenflowVersion.get(packetIn.getVersion());
48
49             ref = InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dataPathId, port, version);
50         }
51
52         return ref;
53     }
54
55     /**
56      * Gets port number from {@link NodeConnectorRef}.
57      * @param nodeConnectorRef Node connector reference
58      * @param version Openflow version
59      * @return port number
60      */
61     @SuppressWarnings("unchecked")
62     @Nullable
63     public static Long fromNodeConnectorRef(@Nonnull NodeConnectorRef nodeConnectorRef, short version) {
64         Preconditions.checkNotNull(nodeConnectorRef);
65
66         Long port = null;
67
68         if (nodeConnectorRef.getValue() instanceof KeyedInstanceIdentifier) {
69             KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> identifier =
70                     (KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey>) nodeConnectorRef.getValue();
71
72             OpenflowVersion ofVersion = OpenflowVersion.get(version);
73             String nodeConnectorId = identifier.getKey().getId().getValue();
74
75             port = InventoryDataServiceUtil.portNumberfromNodeConnectorId(ofVersion, nodeConnectorId);
76         }
77
78         return port;
79     }
80
81     @VisibleForTesting
82     @Nullable
83     static Long getPortNoFromPacketIn(@Nonnull PacketIn packetIn) {
84         Preconditions.checkNotNull(packetIn);
85
86         Long port = null;
87
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();
93
94                 for (MatchEntry entry : entries) {
95                     if (entry.getMatchEntryValue() instanceof InPortCase) {
96                         InPortCase inPortCase = (InPortCase) entry.getMatchEntryValue();
97
98                         InPort inPort = inPortCase.getInPort();
99
100                         if (inPort != null) {
101                             port = inPort.getPortNumber().getValue();
102                             break;
103                         }
104                     }
105                 }
106             }
107         }
108
109         return port;
110     }
111 }