Bump mdsal to 5.0.2
[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 package org.opendaylight.openflowplugin.impl.util;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import java.util.List;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
17 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPortCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.port._case.InPort;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketIn;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.common.Uint32;
28 import org.opendaylight.yangtools.yang.common.Uint64;
29
30 /**
31  * Created by Tomas Slusny on 23.3.2016.
32  */
33 public final class NodeConnectorRefToPortTranslator {
34
35     private NodeConnectorRefToPortTranslator() {
36     }
37
38     /**
39      * Converts {@link PacketIn} to {@link NodeConnectorRef}.
40      * @param packetIn Packet input
41      * @param dataPathId Data path id
42      * @return packet input converted to node connector reference
43      */
44     @Nullable
45     public static NodeConnectorRef toNodeConnectorRef(@Nonnull final PacketIn packetIn, final Uint64 dataPathId) {
46         Preconditions.checkNotNull(packetIn);
47
48         NodeConnectorRef ref = null;
49         Uint32 port = getPortNoFromPacketIn(packetIn);
50
51         if (port != null) {
52             OpenflowVersion version = OpenflowVersion.get(packetIn.getVersion());
53
54             ref = InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dataPathId, port, version);
55         }
56
57         return ref;
58     }
59
60     /**
61      * Gets port number from {@link NodeConnectorRef}.
62      * @param nodeConnectorRef Node connector reference
63      * @param version Openflow version
64      * @return port number
65      */
66     @SuppressWarnings("unchecked")
67     @Nullable
68     public static Uint32 fromNodeConnectorRef(@Nonnull final NodeConnectorRef nodeConnectorRef, final short version) {
69         Preconditions.checkNotNull(nodeConnectorRef);
70
71         Uint32 port = null;
72
73         final InstanceIdentifier<?> value = nodeConnectorRef.getValue();
74         if (value instanceof KeyedInstanceIdentifier) {
75             KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> identifier =
76                     (KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey>) value;
77
78             OpenflowVersion ofVersion = OpenflowVersion.get(version);
79             String nodeConnectorId = identifier.getKey().getId().getValue();
80
81             port = InventoryDataServiceUtil.portNumberfromNodeConnectorId(ofVersion, nodeConnectorId);
82         }
83
84         return port;
85     }
86
87     @VisibleForTesting
88     @Nullable
89     static Uint32 getPortNoFromPacketIn(@Nonnull final PacketIn packetIn) {
90         Preconditions.checkNotNull(packetIn);
91
92         Uint32 port = null;
93
94         if (packetIn.getVersion().toJava() == OFConstants.OFP_VERSION_1_0 && packetIn.getInPort() != null) {
95             port = Uint32.valueOf(packetIn.getInPort());
96         } else if (packetIn.getVersion().toJava() == OFConstants.OFP_VERSION_1_3) {
97             if (packetIn.getMatch() != null && packetIn.getMatch().getMatchEntry() != null) {
98                 List<MatchEntry> entries = packetIn.getMatch().getMatchEntry();
99
100                 for (MatchEntry entry : entries) {
101                     if (entry.getMatchEntryValue() instanceof InPortCase) {
102                         InPortCase inPortCase = (InPortCase) entry.getMatchEntryValue();
103
104                         InPort inPort = inPortCase.getInPort();
105
106                         if (inPort != null) {
107                             port = inPort.getPortNumber().getValue();
108                             break;
109                         }
110                     }
111                 }
112             }
113         }
114
115         return port;
116     }
117 }