Merge "Update config-module-archetype."
[controller.git] / opendaylight / md-sal / samples / l2switch / implementation / src / main / java / org / opendaylight / controller / sample / l2switch / md / packet / PacketHandler.java
1 /*
2  * Copyright (c) 2014 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.controller.sample.l2switch.md.packet;
9
10 import org.opendaylight.controller.sample.l2switch.md.addresstracker.AddressTracker;
11 import org.opendaylight.controller.sample.l2switch.md.flow.FlowWriterService;
12 import org.opendaylight.controller.sample.l2switch.md.inventory.InventoryService;
13 import org.opendaylight.controller.sample.l2switch.md.util.InstanceIdentifierUtils;
14 import org.opendaylight.controller.sal.packet.Ethernet;
15 import org.opendaylight.controller.sal.packet.LLDP;
16 import org.opendaylight.controller.sal.packet.LinkEncap;
17 import org.opendaylight.controller.sal.packet.Packet;
18 import org.opendaylight.controller.sal.packet.RawPacket;
19 import org.opendaylight.controller.sal.utils.HexEncode;
20 import org.opendaylight.controller.sal.utils.NetUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.*;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.address.tracker.rev140402.l2.addresses.L2Address;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInputBuilder;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import java.util.HashMap;
36 import java.util.List;
37
38 /**
39  * PacketHandler examines Ethernet packets to find L2Addresses (mac, nodeConnector) pairings
40  * of the sender and learns them.
41  * It also forwards the data packets appropriately dependending upon whether it knows about the
42  * target or not.
43  */
44 public class PacketHandler implements PacketProcessingListener {
45
46   private final static Logger _logger = LoggerFactory.getLogger(PacketHandler.class);
47
48   private PacketProcessingService packetProcessingService;
49   private AddressTracker addressTracker;
50   private FlowWriterService flowWriterService;
51   private InventoryService inventoryService;
52
53   public void setAddressTracker(AddressTracker addressTracker) {
54     this.addressTracker = addressTracker;
55   }
56
57   public void setPacketProcessingService(PacketProcessingService packetProcessingService) {
58     this.packetProcessingService = packetProcessingService;
59   }
60
61   public void setFlowWriterService(FlowWriterService flowWriterService) {
62     this.flowWriterService = flowWriterService;
63   }
64
65   public void setInventoryService(InventoryService inventoryService) {
66     this.inventoryService = inventoryService;
67   }
68
69   /**
70    * The handler function for all incoming packets.
71    * @param packetReceived  The incoming packet.
72    */
73   @Override
74   public void onPacketReceived(PacketReceived packetReceived) {
75
76     if(packetReceived == null) return;
77
78     try {
79       byte[] payload = packetReceived.getPayload();
80       RawPacket rawPacket = new RawPacket(payload);
81       NodeConnectorRef ingress = packetReceived.getIngress();
82
83       Packet packet = decodeDataPacket(rawPacket);
84
85       if(!(packet instanceof Ethernet)) return;
86
87       handleEthernetPacket(packet, ingress);
88
89     } catch(Throwable _e) {
90       _e.printStackTrace();
91     }
92   }
93
94   /**
95    * The handler function for Ethernet packets.
96    * @param packet  The incoming Ethernet packet.
97    * @param ingress  The NodeConnector where the Ethernet packet came from.
98    */
99   private void handleEthernetPacket(Packet packet, NodeConnectorRef ingress) {
100     byte[] srcMac = ((Ethernet) packet).getSourceMACAddress();
101     byte[] destMac = ((Ethernet) packet).getDestinationMACAddress();
102
103     if (srcMac  == null || srcMac.length  == 0) return;
104
105     Object enclosedPacket = packet.getPayload();
106
107     if (enclosedPacket instanceof LLDP)
108       return; // LLDP packets are handled by OpenFlowPlugin
109
110     // get l2address by src mac
111     // if unknown, add l2address
112     MacAddress srcMacAddress = toMacAddress(srcMac);
113     L2Address src = addressTracker.getAddress(srcMacAddress);
114     boolean isSrcKnown = (src != null);
115     if (!isSrcKnown) {
116       addressTracker.addAddress(srcMacAddress, ingress);
117     }
118
119     // get host by dest mac
120     // if known set dest known to true
121     MacAddress destMacAddress = toMacAddress(destMac);
122     L2Address dest = addressTracker.getAddress(destMacAddress);
123     boolean isDestKnown = (dest != null);
124
125     byte[] payload = packet.getRawPayload();
126     // if (src and dest known)
127     // sendpacket to dest and add src<->dest flow
128     if(isSrcKnown & isDestKnown) {
129       flowWriterService.addMacToMacFlowsUsingShortestPath(srcMacAddress, src.getNodeConnectorRef(),
130           destMacAddress, dest.getNodeConnectorRef());
131       sendPacketOut(payload, getControllerNodeConnector(dest.getNodeConnectorRef()), dest.getNodeConnectorRef());
132     } else {
133       // if (dest unknown)
134       // sendpacket to external links minus ingress
135       floodExternalPorts(payload, ingress);
136     }
137   }
138
139   /**
140    * Floods the specified payload on external ports, which are ports not connected to switches.
141    * @param payload  The payload to be flooded.
142    * @param ingress  The NodeConnector where the payload came from.
143    */
144   private void floodExternalPorts(byte[] payload, NodeConnectorRef ingress) {
145     List<NodeConnectorRef> externalPorts = inventoryService.getExternalNodeConnectors();
146     externalPorts.remove(ingress);
147
148     for (NodeConnectorRef egress : externalPorts) {
149       sendPacketOut(payload, getControllerNodeConnector(egress), egress);
150     }
151   }
152
153   /**
154    * Sends the specified packet on the specified port.
155    * @param payload  The payload to be sent.
156    * @param ingress  The NodeConnector where the payload came from.
157    * @param egress  The NodeConnector where the payload will go.
158    */
159   private void sendPacketOut(byte[] payload, NodeConnectorRef ingress, NodeConnectorRef egress) {
160     if (ingress == null || egress == null)  return;
161     InstanceIdentifier<Node> egressNodePath = InstanceIdentifierUtils.getNodePath(egress.getValue());
162     TransmitPacketInput input = new TransmitPacketInputBuilder() //
163         .setPayload(payload) //
164         .setNode(new NodeRef(egressNodePath)) //
165         .setEgress(egress) //
166         .setIngress(ingress) //
167         .build();
168     packetProcessingService.transmitPacket(input);
169   }
170
171   /**
172    * Decodes an incoming packet.
173    * @param raw  The raw packet to be decoded.
174    * @return  The decoded form of the raw packet.
175    */
176   private Packet decodeDataPacket(RawPacket raw) {
177     if(raw == null) {
178       return null;
179     }
180     byte[] data = raw.getPacketData();
181     if(data.length <= 0) {
182       return null;
183     }
184     if(raw.getEncap().equals(LinkEncap.ETHERNET)) {
185       Ethernet res = new Ethernet();
186       try {
187         res.deserialize(data, 0, data.length * NetUtils.NumBitsInAByte);
188         res.setRawPayload(raw.getPacketData());
189       } catch(Exception e) {
190         _logger.warn("Failed to decode packet: {}", e.getMessage());
191       }
192       return res;
193     }
194     return null;
195   }
196
197   /**
198    * Creates a MacAddress object out of a byte array.
199    * @param dataLinkAddress  The byte-array form of a MacAddress
200    * @return  MacAddress of the specified dataLinkAddress.
201    */
202   private MacAddress toMacAddress(byte[] dataLinkAddress) {
203     return new MacAddress(HexEncode.bytesToHexStringFormat(dataLinkAddress));
204   }
205
206   /**
207    * Gets the NodeConnector that connects the controller & switch for a specified switch port/node connector.
208    * @param nodeConnectorRef  The nodeConnector of a switch.
209    * @return  The NodeConnector that that connects the controller & switch.
210    */
211   private NodeConnectorRef getControllerNodeConnector(NodeConnectorRef nodeConnectorRef) {
212     NodeConnectorRef controllerSwitchNodeConnector = null;
213     HashMap<String, NodeConnectorRef> controllerSwitchConnectors = inventoryService.getControllerSwitchConnectors();
214     InstanceIdentifier<Node> nodePath = InstanceIdentifierUtils.getNodePath(nodeConnectorRef.getValue());
215     if (nodePath != null) {
216       NodeKey nodeKey = InstanceIdentifierUtils.getNodeKey(nodePath);
217       if (nodeKey != null) {
218         controllerSwitchNodeConnector = controllerSwitchConnectors.get(nodeKey.getId().getValue());
219       }
220     }
221     return controllerSwitchNodeConnector;
222   }
223 }