Merge pull request #137 from andreaturli/master

issue 384: added GetIPAdressFromMAC and ScanNetworkWithPing statements
This commit is contained in:
Adrian Cole 2011-11-11 09:55:36 -08:00
commit 6aafbd98eb
4 changed files with 233 additions and 0 deletions

View File

@ -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<OsFamily, String> 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<String> 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();
}
}

View File

@ -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<OsFamily, String> 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<String> 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();
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}