From 7748d915f1c6349a8f1d61a48094deb33da1be1d Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Fri, 11 Nov 2011 16:37:22 +0000 Subject: [PATCH] issue 384: added 2 statements: GetIPAdressFromMAC and ScanNetworkWithPing + Tests, as initial steps to refactor a IMachineToIpAddress function --- .../virtualbox/domain/GetIPAdressFromMAC.java | 75 +++++++++++++++++++ .../domain/ScanNetworkWithPing.java | 64 ++++++++++++++++ .../domain/GetIPAddressFromMACTest.java | 53 +++++++++++++ .../domain/ScanNetworkWithPingTest.java | 41 ++++++++++ 4 files changed, 233 insertions(+) create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/GetIPAdressFromMAC.java create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/ScanNetworkWithPing.java create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromMACTest.java create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/ScanNetworkWithPingTest.java diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/GetIPAdressFromMAC.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/GetIPAdressFromMAC.java new file mode 100644 index 0000000000..3e21fa68a2 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/GetIPAdressFromMAC.java @@ -0,0 +1,75 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.jclouds.virtualbox.domain; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; + +import org.jclouds.scriptbuilder.domain.OsFamily; +import org.jclouds.scriptbuilder.domain.Statement; +import org.jclouds.scriptbuilder.util.Utils; + +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +/** + * @author Andrea Turli + */ +public class GetIPAdressFromMAC implements Statement { + + public static final Map OS_TO_ARP = ImmutableMap + .of(OsFamily.UNIX, + "MAC={macAddress} && [[ `uname -s` = \"Darwin\" ]] && MAC={macAddressBsd}\n arp -an | grep $MAC\n", + OsFamily.WINDOWS, "set MAC={macAddress} arp -a | Findstr %MAC%"); + + private String macAddress; + private String macAddressBsd; + + public GetIPAdressFromMAC(String macAddress) { + this(Joiner.on(":").join(Splitter.fixedLength(2).split(macAddress)).toLowerCase(), + MacAddressToBSD.INSTANCE.apply(Joiner.on(":").join(Splitter.fixedLength(2).split(macAddress)).toLowerCase())); + } + + public GetIPAdressFromMAC(String macAddress, String macAddressBsd) { + checkNotNull(macAddress, "macAddress"); + checkArgument(macAddress.length() == 17); + this.macAddress = macAddress; + checkNotNull(macAddressBsd, "macAddressBsd"); + this.macAddressBsd = macAddressBsd; + } + + @Override + public Iterable functionDependencies(OsFamily family) { + return ImmutableList.of(); + } + + @Override + public String render(OsFamily family) { + StringBuilder arp = new StringBuilder(); + arp.append(Utils.replaceTokens(OS_TO_ARP.get(family), ImmutableMap.of( + "macAddress", macAddress, "macAddressBsd", macAddressBsd))); + return arp.toString(); + } + +} \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/ScanNetworkWithPing.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/ScanNetworkWithPing.java new file mode 100644 index 0000000000..75748a10de --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/ScanNetworkWithPing.java @@ -0,0 +1,64 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.jclouds.virtualbox.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; + +import org.jclouds.scriptbuilder.domain.OsFamily; +import org.jclouds.scriptbuilder.domain.Statement; +import org.jclouds.scriptbuilder.util.Utils; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +/** + * @author Andrea Turli + */ +public class ScanNetworkWithPing implements Statement { + + public static final Map OS_TO_PING = ImmutableMap + .of(OsFamily.UNIX, + "for i in {1..254} ; do ping -c 1 -t 1 {network}.$i & done", + OsFamily.WINDOWS, "TODO"); + + private String network; + + public ScanNetworkWithPing(String network) { + this.network = checkNotNull(network, "network"); + } + + @Override + public Iterable functionDependencies(OsFamily family) { + return ImmutableList.of(); + } + + @Override + public String render(OsFamily family) { + network = network.substring(0, network.lastIndexOf(".")); + StringBuilder arp = new StringBuilder(); + arp.append(Utils.replaceTokens(OS_TO_PING.get(family), ImmutableMap.of( + "network", network))); + + return arp.toString(); + } + +} diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromMACTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromMACTest.java new file mode 100644 index 0000000000..fc847b2e2c --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromMACTest.java @@ -0,0 +1,53 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.virtualbox.domain; + +import static org.testng.Assert.assertEquals; + +import org.jclouds.scriptbuilder.domain.OsFamily; +import org.testng.annotations.Test; + +/** + * @author Andrea Turli + */ +@Test(groups = "unit") +public class GetIPAddressFromMACTest { + + private static final String macAddressWith00 = "0800271A9806"; + private static final String unixMacAddressWith00 = "08:00:27:1a:98:06"; + private static final String bsdMacAddressWith00 = "8:0:27:1a:98:6"; + private static final String macAddressWith0c = "0811271A9806"; + private static final String unixMacAddressWith0c = "08:11:27:1a:98:06"; + private static final String bsdMacAddressWith0c = "8:11:27:1a:98:6"; + + public void testGetIPAdressFromMacAddressUnix() { + GetIPAdressFromMAC statement = new GetIPAdressFromMAC(macAddressWith00); + assertEquals(statement.render(OsFamily.UNIX), "MAC=" + + unixMacAddressWith00 + + " && [[ `uname -s` = \"Darwin\" ]] && MAC=" + bsdMacAddressWith00 + + "\n arp -an | grep $MAC\n"); + + statement = new GetIPAdressFromMAC(macAddressWith0c); + assertEquals(statement.render(OsFamily.UNIX), "MAC=" + + unixMacAddressWith0c + + " && [[ `uname -s` = \"Darwin\" ]] && MAC=" + bsdMacAddressWith0c + + "\n arp -an | grep $MAC\n"); + } + +} \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/ScanNetworkWithPingTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/ScanNetworkWithPingTest.java new file mode 100644 index 0000000000..f3801b0d22 --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/ScanNetworkWithPingTest.java @@ -0,0 +1,41 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.virtualbox.domain; + +import static org.testng.Assert.assertEquals; + +import org.jclouds.scriptbuilder.domain.OsFamily; +import org.testng.annotations.Test; + +/** + * @author Andrea Turli + */ +@Test(groups = "unit") +public class ScanNetworkWithPingTest { + + private static final String network = "192.168.1.1"; + private static final String networkWithoutLastNumber = "192.168.1"; + + public void testGetIPAdressFromMacAddressUnix() { + ScanNetworkWithPing statement = new ScanNetworkWithPing(network); + assertEquals(statement.render(OsFamily.UNIX), "for i in {1..254} ; do ping -c 1 -t 1 " + networkWithoutLastNumber + ".$i & done"); + + } + +} \ No newline at end of file