BUG-272: remove trailing whitespace from Java files
[controller.git] / opendaylight / md-sal / topology-lldp-discovery / src / main / java / org / opendaylight / md / controller / topology / lldp / utils / LLDPDiscoveryUtils.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.md.controller.topology.lldp.utils;
9
10 import java.nio.charset.Charset;
11
12 import org.opendaylight.controller.sal.packet.Ethernet;
13 import org.opendaylight.controller.sal.packet.LLDP;
14 import org.opendaylight.controller.sal.packet.LLDPTLV;
15 import org.opendaylight.controller.sal.utils.NetUtils;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class LLDPDiscoveryUtils {
29     static Logger LOG = LoggerFactory.getLogger(LLDPDiscoveryUtils.class);
30
31     public static final Long LLDP_INTERVAL = (long) (1000*5); // Send LLDP every five seconds
32     public static final Long LLDP_EXPIRATION_TIME = LLDP_INTERVAL*3; // Let up to three intervals pass before we decide we are expired.
33
34     public static String macToString(byte[] mac) {
35         StringBuilder b = new StringBuilder();
36         for (int i = 0; i < mac.length; i++) {
37             b.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
38         }
39
40         return b.toString();
41     }
42
43     public static NodeConnectorRef lldpToNodeConnectorRef(byte[] payload)  {
44         Ethernet ethPkt = new Ethernet();
45         try {
46             ethPkt.deserialize(payload, 0,payload.length * NetUtils.NumBitsInAByte);
47         } catch (Exception e) {
48             LOG.warn("Failed to decode LLDP packet {}", e);
49         }
50
51         if (ethPkt.getPayload() instanceof LLDP) {
52             LLDP lldp = (LLDP) ethPkt.getPayload();
53
54             try {
55                 NodeId srcNodeId = null;
56                 NodeConnectorId srcNodeConnectorId = null;
57                 for (LLDPTLV lldptlv : lldp.getOptionalTLVList()) {
58                     if (lldptlv.getType() == LLDPTLV.TLVType.Custom.getValue()) {
59                         srcNodeConnectorId = new NodeConnectorId(LLDPTLV.getCustomString(lldptlv.getValue(), lldptlv.getLength()));
60                     }
61                     if (lldptlv.getType() == LLDPTLV.TLVType.SystemName.getValue()) {
62                         String srcNodeIdString = new String(lldptlv.getValue(),Charset.defaultCharset());
63                         srcNodeId = new NodeId(srcNodeIdString);
64                     }
65                 }
66                 if(srcNodeId != null && srcNodeConnectorId != null) {
67                     InstanceIdentifier<NodeConnector> srcInstanceId = InstanceIdentifier.builder(Nodes.class)
68                             .child(Node.class,new NodeKey(srcNodeId))
69                             .child(NodeConnector.class, new NodeConnectorKey(srcNodeConnectorId))
70                             .toInstance();
71                     return new NodeConnectorRef(srcInstanceId);
72                 }
73             } catch (Exception e) {
74                 LOG.warn("Caught exception ", e);
75             }
76         }
77         return null;
78     }
79 }