2 * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. 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
8 package org.opendaylight.vpnservice.alivenessmonitor.internal;
10 import java.math.BigInteger;
11 import java.nio.charset.Charset;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.atomic.AtomicInteger;
17 import java.util.concurrent.Future;
19 import org.apache.commons.lang3.StringUtils;
20 import org.opendaylight.controller.liblldp.EtherTypes;
21 import org.opendaylight.controller.liblldp.LLDP;
22 import org.opendaylight.controller.liblldp.LLDPTLV;
23 import org.opendaylight.controller.liblldp.LLDPTLV.TLVType;
24 import org.opendaylight.controller.liblldp.Packet;
25 import org.opendaylight.controller.liblldp.PacketException;
26 import org.opendaylight.vpnservice.interfacemgr.globals.IfmConstants;
27 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
28 import org.opendaylight.vpnservice.mdsalutil.ActionType;
29 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
30 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
31 import org.opendaylight.vpnservice.mdsalutil.packet.Ethernet;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.endpoint.EndpointType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.endpoint.endpoint.type.Interface;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.monitor.configs.MonitoringInfo;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetDpidFromInterfaceInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetDpidFromInterfaceInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetDpidFromInterfaceOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetInterfaceFromIfIndexInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetInterfaceFromIfIndexInputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetInterfaceFromIfIndexOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetPortFromInterfaceInputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetPortFromInterfaceInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetPortFromInterfaceOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
51 import org.opendaylight.yangtools.yang.common.RpcResult;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import com.google.common.base.Strings;
57 public class AlivenessProtocolHandlerLLDP extends AbstractAlivenessProtocolHandler {
58 private static final Logger LOG = LoggerFactory.getLogger(AlivenessProtocolHandlerLLDP.class);
59 private AtomicInteger packetId = new AtomicInteger(0);
61 public AlivenessProtocolHandlerLLDP(ServiceProvider serviceProvider) {
62 super(serviceProvider);
66 public Class<?> getPacketClass() {
71 public String handlePacketIn(Packet protocolPacket, PacketReceived packetReceived) {
72 String sSourceDpnId = null;
73 String sPortNumber = null;
81 LLDP lldpPacket = (LLDP)protocolPacket;
83 LLDPTLV lldpTlvCur = lldpPacket.getSystemNameId();
84 if (lldpTlvCur != null) {
85 sSourceDpnId = new String(lldpTlvCur.getValue(), Charset.defaultCharset());
88 lldpTlvCur = lldpPacket.getPortId();
89 if (lldpTlvCur != null) {
90 sPortNumber = new String(lldpTlvCur.getValue(), Charset.defaultCharset());
93 for (LLDPTLV lldpTlv : lldpPacket.getOptionalTLVList()) {
94 lldpTlvTypeCur = lldpTlv.getType();
96 if (lldpTlvTypeCur == LLDPTLV.TLVType.SystemName.getValue()) {
97 sSourceDpnId = new String(lldpTlvCur.getValue(), Charset.defaultCharset());
101 for (LLDPTLV lldpTlv : lldpPacket.getCustomTlvList()) {
102 lldpTlvTypeCur = lldpTlv.getType();
104 if (lldpTlvTypeCur == LLDPTLV.TLVType.Custom.getValue()) {
105 sTmp = new String(lldpTlv.getValue());
110 String interfaceName = null;
112 //TODO: Check if the below fields are required
113 if (!Strings.isNullOrEmpty(sTmp) && sTmp.contains("#")) {
114 String[] asTmp = sTmp.split("#");
115 interfaceName = asTmp[0];
116 packetId = Integer.parseInt(asTmp[1]);
117 LOG.debug("Custom LLDP Value on received packet: " + sTmp);
120 if(!Strings.isNullOrEmpty(interfaceName)) {
121 String monitorKey = new StringBuilder().append(interfaceName).append(EtherTypes.LLDP).toString();
124 LOG.debug("No associated interface found to handle received LLDP Packet");
130 public void sendPacketOut(MonitoringInfo monitorInfo) {
131 String sourceInterface;
133 EndpointType source = monitorInfo.getSource().getEndpointType();
134 if( source instanceof Interface) {
135 Interface intf = (Interface)source;
136 sourceInterface = intf.getInterfaceName();
138 LOG.warn("Invalid source endpoint. Could not retrieve source interface to send LLDP Packet");
142 //Get Mac Address for the source interface
143 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState = getInterfaceFromOperDS(sourceInterface);
144 byte[] sourceMac = getMacAddress(interfaceState, sourceInterface);
145 if(sourceMac == null) {
146 LOG.error("Could not read mac address for the source interface {} from the Inventory. "
147 + "LLDP packet cannot be send.", sourceInterface);
151 OdlInterfaceRpcService interfaceService = serviceProvider.getInterfaceManager();
153 long nodeId = -1, portNum = -1;
155 String lowerLayerIf = interfaceState.getLowerLayerIf().get(0);
156 NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
157 nodeId = Long.valueOf(getDpnFromNodeConnectorId(nodeConnectorId));
158 portNum = Long.valueOf(getPortNoFromNodeConnectorId(nodeConnectorId));
159 }catch(Exception e) {
160 LOG.error("Failed to retrieve node id and port number ", e);
164 Ethernet ethenetLLDPPacket = makeLLDPPacket(Long.toString(nodeId), portNum, 0, sourceMac, sourceInterface);
167 List<ActionInfo> actions = getInterfaceActions(interfaceState, portNum);
168 if(actions.isEmpty()) {
169 LOG.error("No interface actions to send packet out over interface {}", sourceInterface);
172 TransmitPacketInput transmitPacketInput = MDSALUtil.getPacketOut(actions,
173 ethenetLLDPPacket.serialize(), nodeId, MDSALUtil.getNodeConnRef(BigInteger.valueOf(nodeId), "0xfffffffd"));
174 serviceProvider.getPacketProcessingService().transmitPacket(transmitPacketInput);
175 } catch (Exception e) {
176 LOG.error("Error while sending LLDP Packet", e);
180 public static String getDpnFromNodeConnectorId(NodeConnectorId portId) {
182 * NodeConnectorId is of form 'openflow:dpnid:portnum'
184 String[] split = portId.getValue().split(IfmConstants.OF_URI_SEPARATOR);
188 public static String getPortNoFromNodeConnectorId(NodeConnectorId portId) {
190 * NodeConnectorId is of form 'openflow:dpnid:portnum'
192 String[] split = portId.getValue().split(IfmConstants.OF_URI_SEPARATOR);
195 private List<ActionInfo> getInterfaceActions(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState,long portNum) throws InterruptedException, ExecutionException {
196 Class<? extends InterfaceType> intfType;
197 if(interfaceState != null) {
198 intfType = interfaceState.getType();
200 LOG.error("Could not retrieve port type for interface {} to construct actions", interfaceState.getName());
201 return Collections.emptyList();
204 List<ActionInfo> actionInfos = new ArrayList<ActionInfo>();
205 // Set the LLDP service Id which is 0
206 if(Tunnel.class.equals(intfType)) {
207 actionInfos.add(new ActionInfo(ActionType.set_field_tunnel_id, new BigInteger[] {
208 BigInteger.valueOf(0)}));
210 actionInfos.add(new ActionInfo(ActionType.output, new String[] { Long.toString(portNum) }));
214 private static LLDPTLV buildLLDTLV(LLDPTLV.TLVType tlvType,byte[] abyTLV) {
215 return new LLDPTLV().setType(tlvType.getValue()).setLength((short) abyTLV.length).setValue(abyTLV);
218 private int getPacketId() {
219 int id = packetId.incrementAndGet();
221 LOG.debug("Resetting the LLDP Packet Id counter");
228 public Ethernet makeLLDPPacket(String nodeId,
229 long portNum, int serviceId, byte[] srcMac, String sourceInterface) {
231 // Create LLDP TTL TLV
232 LLDPTLV lldpTlvTTL = buildLLDTLV(LLDPTLV.TLVType.TTL, new byte[] { (byte) 0, (byte) 120 });
234 LLDPTLV lldpTlvChassisId = buildLLDTLV(LLDPTLV.TLVType.ChassisID, LLDPTLV.createChassisIDTLVValue(colonize(StringUtils
235 .leftPad(Long.toHexString(MDSALUtil.getDpnIdFromNodeName(nodeId).longValue()), 16,
237 LLDPTLV lldpTlvSystemName = buildLLDTLV(TLVType.SystemName, LLDPTLV.createSystemNameTLVValue(nodeId));
239 LLDPTLV lldpTlvPortId = buildLLDTLV(TLVType.PortID, LLDPTLV.createPortIDTLVValue(
240 Long.toHexString(portNum)));
242 String customValue = sourceInterface + "#" + getPacketId();
244 LOG.debug("Sending LLDP packet, custome value " + customValue);
246 LLDPTLV lldpTlvCustom = buildLLDTLV(TLVType.Custom, customValue.getBytes());
248 List<LLDPTLV> lstLLDPTLVCustom = new ArrayList<>();
249 lstLLDPTLVCustom.add(lldpTlvCustom);
251 LLDP lldpDiscoveryPacket = new LLDP();
252 lldpDiscoveryPacket.setChassisId(lldpTlvChassisId)
253 .setPortId(lldpTlvPortId)
255 .setSystemNameId(lldpTlvSystemName)
256 .setOptionalTLVList(lstLLDPTLVCustom);
258 byte[] destMac = LLDP.LLDPMulticastMac;
260 Ethernet ethernetPacket = new Ethernet();
261 ethernetPacket.setSourceMACAddress(srcMac)
262 .setDestinationMACAddress(destMac)
263 .setEtherType(EtherTypes.LLDP.shortValue())
264 .setPayload(lldpDiscoveryPacket);
266 return ethernetPacket;
269 private String colonize(String orig) {
270 return orig.replaceAll("(?<=..)(..)", ":$1");
274 public String getUniqueMonitoringKey(MonitoringInfo monitorInfo) {
275 String interfaceName = getInterfaceName(monitorInfo.getSource().getEndpointType());
276 return new StringBuilder().append(interfaceName).append(EtherTypes.LLDP).toString();
279 private String getInterfaceName(EndpointType endpoint) {
280 String interfaceName = null;
281 if(endpoint instanceof Interface) {
282 interfaceName = ((Interface)endpoint).getInterfaceName();
284 return interfaceName;