From 0c5f529bf0bad926f5f17de63eae2a810144b066 Mon Sep 17 00:00:00 2001 From: snoop Date: Wed, 4 Apr 2018 15:49:06 +0900 Subject: [PATCH] fixed discovery result save --- .../commons/utils/StringConvertor.java | 29 +++++++++++++++++++ .../service/TargetDiscoveryService.java | 2 +- .../service/TargetDiscoveryServiceTest.java | 6 ++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/loafle/overflow/commons/utils/StringConvertor.java b/src/main/java/com/loafle/overflow/commons/utils/StringConvertor.java index efbb7c0..599345b 100644 --- a/src/main/java/com/loafle/overflow/commons/utils/StringConvertor.java +++ b/src/main/java/com/loafle/overflow/commons/utils/StringConvertor.java @@ -28,4 +28,33 @@ public class StringConvertor { return result; } + + // https://github.com/Sovietaced/floodlight/blob/master/src/main/java/net/floodlightcontroller/util/MACAddress.java + + public static final int MAC_ADDRESS_LENGTH = 6; + + public static long macStrToLong(String address) { + String[] elements = address.split(":"); + if (elements.length != MAC_ADDRESS_LENGTH) { + throw new IllegalArgumentException( + "Specified MAC Address must contain 12 hex digits" + + " separated pairwise by :'s."); + } + + byte[] addressInBytes = new byte[MAC_ADDRESS_LENGTH]; + for (int i = 0; i < MAC_ADDRESS_LENGTH; i++) { + String element = elements[i]; + addressInBytes[i] = (byte)Integer.parseInt(element, 16); + } + + long mac = 0; + for (int i = 0; i < 6; i++) { + long t = (addressInBytes[i] & 0xffL) << ((5 - i) * 8); + mac |= t; + } + return mac; + +// return new MACAddress(addressInBytes); + } + } diff --git a/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java b/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java index 800fd3c..0fcc479 100644 --- a/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java +++ b/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java @@ -200,7 +200,7 @@ public class TargetDiscoveryService { InfraHost newInfraHost = new InfraHost(); newInfraHost.setIp(StringConvertor.ipToLong(host.getIp())); // newInfraHost.setMac(StringConvertor.ipToLong(host.getMac())); - newInfraHost.setMac(123123123); + newInfraHost.setMac(StringConvertor.macStrToLong(host.getMac())); newInfraHost.setOs(infraOS); newInfraHost.setInfraType(typeHost); newInfraHost.setProbe(probe); diff --git a/src/test/java/com/loafle/overflow/module/target/service/TargetDiscoveryServiceTest.java b/src/test/java/com/loafle/overflow/module/target/service/TargetDiscoveryServiceTest.java index bda3c3c..bb6d4a9 100644 --- a/src/test/java/com/loafle/overflow/module/target/service/TargetDiscoveryServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/target/service/TargetDiscoveryServiceTest.java @@ -158,10 +158,16 @@ public class TargetDiscoveryServiceTest { String nnn = o.getClass().getSimpleName(); System.out.println(nnn); + } + @Test + public void macAddrConvert() throws Exception { + String mac = "08:00:27:65:8d:13"; + long aa = StringConvertor.macStrToLong(mac); + System.out.println(aa); }