[BROOKLYN-182] Decouple ByonLocationResolver from MachineLocation subtypes The ByonLocationResolver acts as a registry for windows/linux machine types. For now we eliminate the compile-time dependency towards SshMachineLocation and WinRmMachineLocation, while maintaining the runtmie dependency.
Further on we'll reverse the dependency towards a more consistent location type registry. Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/2cb87d34 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/2cb87d34 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/2cb87d34 Branch: refs/heads/master Commit: 2cb87d34021a299e9a569ea7cdb72598ecef8086 Parents: 676b48e Author: Ciprian Ciubotariu <[email protected]> Authored: Wed Oct 14 18:41:30 2015 +0300 Committer: Ciprian Ciubotariu <[email protected]> Committed: Wed Oct 14 23:36:34 2015 +0300 ---------------------------------------------------------------------- .../location/byon/ByonLocationResolver.java | 20 +++-- .../location/byon/ByonLocationResolverTest.java | 24 ----- .../winrm/ByonLocationResolverTest.java | 95 ++++++++++++++++++++ 3 files changed, 109 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2cb87d34/core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java b/core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java index 7245faf..c747c35 100644 --- a/core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java +++ b/core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java @@ -34,8 +34,6 @@ import org.apache.brooklyn.core.config.ConfigKeys; import org.apache.brooklyn.core.config.Sanitizer; import org.apache.brooklyn.core.location.AbstractLocationResolver; import org.apache.brooklyn.core.mgmt.internal.LocalLocationManager; -import org.apache.brooklyn.location.ssh.SshMachineLocation; -import org.apache.brooklyn.location.winrm.WinRmMachineLocation; import org.apache.brooklyn.util.collections.MutableMap; import org.apache.brooklyn.util.core.config.ConfigBag; import org.apache.brooklyn.util.core.flags.TypeCoercions; @@ -72,9 +70,14 @@ public class ByonLocationResolver extends AbstractLocationResolver { public static final ConfigKey<String> OS_FAMILY = ConfigKeys.newStringConfigKey("osFamily", "OS Family of the machine, either windows or linux", "linux"); - public static final Map<String, Class<? extends MachineLocation>> OS_TO_MACHINE_LOCATION_TYPE = ImmutableMap.<String, Class<? extends MachineLocation>>of( - "windows", WinRmMachineLocation.class, - "linux", SshMachineLocation.class); + /** + * @todo Reimplement via a registry: + * {@link org.apache.brooklyn.location.winrm.WinRmMachineLocation} + * {@link org.apache.brooklyn.location.ssh.SshMachineLocation} + */ + public static final Map<String, String> OS_TO_MACHINE_LOCATION_TYPE = ImmutableMap.of( + "windows", "org.apache.brooklyn.location.winrm.WinRmMachineLocation", + "linux", "org.apache.brooklyn.location.ssh.SshMachineLocation"); @Override public String getPrefix() { @@ -207,7 +210,12 @@ public class ByonLocationResolver extends AbstractLocationResolver { } private Class<? extends MachineLocation> getLocationClass(String osFamily) { - return osFamily == null ? null : OS_TO_MACHINE_LOCATION_TYPE.get(osFamily.toLowerCase(Locale.ENGLISH)); + try { + if (osFamily != null) { + return Class.forName(OS_TO_MACHINE_LOCATION_TYPE.get(osFamily.toLowerCase(Locale.ENGLISH))).asSubclass(MachineLocation.class); + } + } catch (ClassNotFoundException ex) {} + return null; } protected LocationSpec<? extends MachineLocation> parseMachine(String val, Class<? extends MachineLocation> locationClass, Map<String, ?> defaults, String specForErrMsg) { http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2cb87d34/core/src/test/java/org/apache/brooklyn/location/byon/ByonLocationResolverTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/location/byon/ByonLocationResolverTest.java b/core/src/test/java/org/apache/brooklyn/location/byon/ByonLocationResolverTest.java index 05102a5..8a46a64 100644 --- a/core/src/test/java/org/apache/brooklyn/location/byon/ByonLocationResolverTest.java +++ b/core/src/test/java/org/apache/brooklyn/location/byon/ByonLocationResolverTest.java @@ -42,9 +42,7 @@ import org.apache.brooklyn.core.location.NamedLocationResolver; import org.apache.brooklyn.core.location.internal.LocationInternal; import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; -import org.apache.brooklyn.location.byon.FixedListMachineProvisioningLocation; import org.apache.brooklyn.location.ssh.SshMachineLocation; -import org.apache.brooklyn.location.winrm.WinRmMachineLocation; import org.apache.brooklyn.test.Asserts; import org.apache.brooklyn.util.collections.MutableMap; import org.apache.brooklyn.util.net.Networking; @@ -333,28 +331,6 @@ public class ByonLocationResolverTest { Assert.assertEquals("1.1.1.1", location1.getAddress().getHostAddress()); } - @DataProvider(name = "windowsOsFamilies") - public Object[][] getWindowsOsFamilies() { - return new Object[][]{{"windows"}, {"WINDOWS"}, {"wInDoWs"}}; - } - - @Test(dataProvider = "windowsOsFamilies") - public void testWindowsMachines(String osFamily) throws Exception { - brooklynProperties.put("brooklyn.location.byon.user", "myuser"); - brooklynProperties.put("brooklyn.location.byon.password", "mypassword"); - String spec = "byon"; - Map<String, ?> flags = ImmutableMap.of( - "hosts", ImmutableList.of("1.1.1.1", "2.2.2.2"), - "osFamily", osFamily - ); - MachineProvisioningLocation<MachineLocation> provisioner = resolve(spec, flags); - WinRmMachineLocation location = (WinRmMachineLocation) provisioner.obtain(ImmutableMap.of()); - - assertEquals(location.config().get(WinRmMachineLocation.USER), "myuser"); - assertEquals(location.config().get(WinRmMachineLocation.PASSWORD), "mypassword"); - assertEquals(location.config().get(WinRmMachineLocation.ADDRESS).getHostAddress(), "1.1.1.1"); - } - @Test public void testNonWindowsMachines() throws Exception { String spec = "byon"; http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2cb87d34/software/winrm/src/test/java/org/apache/brooklyn/location/winrm/ByonLocationResolverTest.java ---------------------------------------------------------------------- diff --git a/software/winrm/src/test/java/org/apache/brooklyn/location/winrm/ByonLocationResolverTest.java b/software/winrm/src/test/java/org/apache/brooklyn/location/winrm/ByonLocationResolverTest.java new file mode 100644 index 0000000..62aa66d --- /dev/null +++ b/software/winrm/src/test/java/org/apache/brooklyn/location/winrm/ByonLocationResolverTest.java @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.apache.brooklyn.location.winrm; + +import static org.testng.Assert.assertEquals; +import java.util.Map; + +import org.apache.brooklyn.api.location.MachineLocation; +import org.apache.brooklyn.api.location.MachineProvisioningLocation; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.internal.BrooklynProperties; +import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext; +import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; +import org.apache.brooklyn.util.text.StringPredicates; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.apache.brooklyn.location.byon.FixedListMachineProvisioningLocation; + +public class ByonLocationResolverTest { + + private static final Logger log = LoggerFactory.getLogger(ByonLocationResolverTest.class); + + private BrooklynProperties brooklynProperties; + private LocalManagementContext managementContext; + private Predicate<CharSequence> defaultNamePredicate; + + @BeforeMethod(alwaysRun=true) + public void setUp() throws Exception { + managementContext = LocalManagementContextForTests.newInstance(); + brooklynProperties = managementContext.getBrooklynProperties(); + defaultNamePredicate = StringPredicates.startsWith(FixedListMachineProvisioningLocation.class.getSimpleName()); + } + + @AfterMethod(alwaysRun=true) + public void tearDown() throws Exception { + if (managementContext != null) Entities.destroyAll(managementContext); + } + + @DataProvider(name = "windowsOsFamilies") + public Object[][] getWindowsOsFamilies() { + return new Object[][]{{"windows"}, {"WINDOWS"}, {"wInDoWs"}}; + } + + @Test(dataProvider = "windowsOsFamilies") + public void testWindowsMachines(String osFamily) throws Exception { + brooklynProperties.put("brooklyn.location.byon.user", "myuser"); + brooklynProperties.put("brooklyn.location.byon.password", "mypassword"); + String spec = "byon"; + Map<String, ?> flags = ImmutableMap.of( + "hosts", ImmutableList.of("1.1.1.1", "2.2.2.2"), + "osFamily", osFamily + ); + MachineProvisioningLocation<MachineLocation> provisioner = resolve(spec, flags); + WinRmMachineLocation location = (WinRmMachineLocation) provisioner.obtain(ImmutableMap.of()); + + assertEquals(location.config().get(WinRmMachineLocation.USER), "myuser"); + assertEquals(location.config().get(WinRmMachineLocation.PASSWORD), "mypassword"); + assertEquals(location.config().get(WinRmMachineLocation.ADDRESS).getHostAddress(), "1.1.1.1"); + } + + @SuppressWarnings("unchecked") + private FixedListMachineProvisioningLocation<MachineLocation> resolve(String val) { + return (FixedListMachineProvisioningLocation<MachineLocation>) managementContext.getLocationRegistry().resolve(val); + } + + @SuppressWarnings("unchecked") + private FixedListMachineProvisioningLocation<MachineLocation> resolve(String val, Map<?, ?> locationFlags) { + return (FixedListMachineProvisioningLocation<MachineLocation>) managementContext.getLocationRegistry().resolve(val, locationFlags); + } + +}
