Merge "Fix issues related to checkstyle enforcement on openflow-impl module"
[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 final class NodeConnectorRefToPortTranslator {
33
34     private NodeConnectorRefToPortTranslator() {
35     }
36
37     /**
38      * Converts {@link PacketIn} to {@link NodeConnectorRef}.
39      * @param packetIn Packet input
40      * @param dataPathId Data path id
41      * @return packet input converted to node connector reference
42      */
43     @Nullable
44     public static NodeConnectorRef toNodeConnectorRef(@Nonnull PacketIn packetIn, BigInteger dataPathId) {
45         Preconditions.checkNotNull(packetIn);
46
47         NodeConnectorRef ref = null;
48         Long port = getPortNoFromPacketIn(packetIn);
49
50         if (port != null) {
51             OpenflowVersion version = OpenflowVersion.get(packetIn.getVersion());
52
53             ref = InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dataPathId, port, version);
54         }
55
56         return ref;
57     }
58
59     /**
60      * Gets port number from {@link NodeConnectorRef}.
61      * @param nodeConnectorRef Node connector reference
62      * @param version Openflow version
63      * @return port number
64      */
65     @SuppressWarnings("unchecked")
66     @Nullable
67     public static Long fromNodeConnectorRef(@Nonnull NodeConnectorRef nodeConnectorRef, short version) {
68         Preconditions.checkNotNull(nodeConnectorRef);
69
70         Long port = null;
71
72         if (nodeConnectorRef.getValue() instanceof KeyedInstanceIdentifier) {
73             KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> identifier =
74                     (KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey>) nodeConnectorRef.getValue();
75
76             OpenflowVersion ofVersion = OpenflowVersion.get(version);
77             String nodeConnectorId = identifier.getKey().getId().getValue();
78
79             port = InventoryDataServiceUtil.portNumberfromNodeConnectorId(ofVersion, nodeConnectorId);
80         }
81
82         return port;
83     }
84
85     @VisibleForTesting
86     @Nullable
87     static Long getPortNoFromPacketIn(@Nonnull PacketIn packetIn) {
88         Preconditions.checkNotNull(packetIn);
89
90         Long port = null;
91
92         if (packetIn.getVersion() == OFConstants.OFP_VERSION_1_0 && packetIn.getInPort() != null) {
93             port = packetIn.getInPort().longValue();
94         } else if (packetIn.getVersion() == OFConstants.OFP_VERSION_1_3) {
95             if (packetIn.getMatch() != null && packetIn.getMatch().getMatchEntry() != null) {
96                 List<MatchEntry> entries = packetIn.getMatch().getMatchEntry();
97
98                 for (MatchEntry entry : entries) {
99                     if (entry.getMatchEntryValue() instanceof InPortCase) {
100                         InPortCase inPortCase = (InPortCase) entry.getMatchEntryValue();
101
102                         InPort inPort = inPortCase.getInPort();
103
104                         if (inPort != null) {
105                             port = inPort.getPortNumber().getValue();
106                             break;
107                         }
108                     }
109                 }
110             }
111         }
112
113         return port;
114     }
115 }