Skip to content

Commit

Permalink
Remove some deprecation warnings
Browse files Browse the repository at this point in the history
(But really this needs serious attention, and deletion of some of the
“legacy” pieces - so not going through everything).
  • Loading branch information
aledsage committed Dec 6, 2016
1 parent d4a02c7 commit ad23488
Show file tree
Hide file tree
Showing 41 changed files with 174 additions and 262 deletions.
101 changes: 21 additions & 80 deletions cloudstack/src/main/java/brooklyn/networking/cloudstack/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,12 @@
import java.security.cert.X509Certificate;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.http.HttpTool;
import org.apache.brooklyn.util.http.HttpToolResponse;
import org.apache.http.auth.Credentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.bouncycastle.util.io.Streams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -41,53 +34,36 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;

import org.apache.brooklyn.util.http.HttpToolResponse;
import org.apache.brooklyn.util.exceptions.Exceptions;

/**
* HTTP convenience static methods.
* <p>
* Uses the Apache {@link HttpClient} for connections.
*
* @deprecated since 0.10.0; instead use {@link HttpTool}.
*/
@Deprecated
public class HttpUtil {

public static final Logger LOG = LoggerFactory.getLogger(HttpUtil.class);

public static HttpClient createHttpClient(URI uri, Optional<Credentials> credentials) {
final DefaultHttpClient httpClient = new DefaultHttpClient();

// TODO if supplier returns null, we may wish to defer initialization until url available?
if (uri != null && "https".equalsIgnoreCase(uri.getScheme())) {
try {
int port = (uri.getPort() >= 0) ? uri.getPort() : 443;
SSLSocketFactory socketFactory = new SSLSocketFactory(
new TrustAllStrategy(), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme sch = new Scheme("https", port, socketFactory);
httpClient.getConnectionManager().getSchemeRegistry().register(sch);
} catch (Exception e) {
LOG.warn("Error in HTTP Feed of {}, setting trust for uri {}", uri);
throw Exceptions.propagate(e);
}
}

// Set credentials
if (uri != null && credentials.isPresent()) {
String hostname = uri.getHost();
int port = uri.getPort();
httpClient.getCredentialsProvider().setCredentials(new AuthScope(hostname, port), credentials.get());
}

return httpClient;
return HttpTool.httpClientBuilder()
.uri(uri)
.credential(credentials)
.build();
}

public static HttpToolResponse invoke(org.jclouds.http.HttpRequest request) {
HttpClient client = HttpUtil.createHttpClient(request.getEndpoint(), Optional.<Credentials>absent());
HttpClient client = HttpTool.httpClientBuilder()
.uri(request.getEndpoint())
.build();
String method = request.getMethod();
try {
if ("GET".equalsIgnoreCase(method)) {
return HttpUtil.httpGet(client, request.getEndpoint(), request.getHeaders());
return HttpTool.httpGet(client, request.getEndpoint(), request.getHeaders());
} else if ("POST".equalsIgnoreCase(method)) {
return HttpUtil.httpPost(client, request.getEndpoint(), request.getHeaders(), Streams.readAll(request.getPayload().openStream()));
return HttpTool.httpPost(client, request.getEndpoint(), request.getHeaders(), Streams.readAll(request.getPayload().openStream()));
// return HttpUtil.httpPost(client, request.getEndpoint(), request.getHeaders(), Streams.readAll(request.getPayload().openStream()));
} else {
// TODO being lazy!
throw new UnsupportedOperationException("Unsupported method: "+method+" for "+request);
Expand All @@ -99,58 +75,23 @@ public static HttpToolResponse invoke(org.jclouds.http.HttpRequest request) {

public static HttpToolResponse httpGet(URI uri, Multimap<String,String> headers) {
HttpClient client = HttpUtil.createHttpClient(uri, Optional.<Credentials>absent());
return HttpUtil.httpGet(client, uri, headers);
return HttpTool.httpGet(client, uri, headers);
}

public static HttpToolResponse httpGet(HttpClient httpClient, URI uri, Map<String,String> headers) {
return httpGet(httpClient, uri, Multimaps.forMap(headers));
return HttpTool.httpGet(httpClient, uri, Multimaps.forMap(headers));
}

public static HttpToolResponse httpGet(HttpClient httpClient, URI uri, Multimap<String,String> headers) {
HttpGet httpGet = new HttpGet(uri);
for (Map.Entry<String,String> entry : headers.entries()) {
httpGet.addHeader(entry.getKey(), entry.getValue());
}

long startTime = System.currentTimeMillis();
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
try {
return new HttpToolResponse(httpResponse, startTime);
} finally {
EntityUtils.consume(httpResponse.getEntity());
}
} catch (Exception e) {
throw Exceptions.propagate(e);
}
return HttpTool.httpGet(httpClient, uri, headers);
}

public static HttpToolResponse httpPost(HttpClient httpClient, URI uri, Map<String,String> headers, byte[] body) {
return httpPost(httpClient, uri, Multimaps.forMap(headers), body);
return HttpTool.httpPost(httpClient, uri, Multimaps.forMap(headers), body);
}

public static HttpToolResponse httpPost(HttpClient httpClient, URI uri, Multimap<String,String> headers, byte[] body) {
HttpPost httpPost = new HttpPost(uri);
for (Map.Entry<String,String> entry : headers.entries()) {
httpPost.addHeader(entry.getKey(), entry.getValue());
}
if (body != null) {
HttpEntity httpEntity = new ByteArrayEntity(body);
httpPost.setEntity(httpEntity);
}

long startTime = System.currentTimeMillis();
try {
HttpResponse httpResponse = httpClient.execute(httpPost);

try {
return new HttpToolResponse(httpResponse, startTime);
} finally {
EntityUtils.consume(httpResponse.getEntity());
}
} catch (Exception e) {
throw Exceptions.propagate(e);
}
return HttpTool.httpPost(httpClient, uri, headers, body);
}

public static class TrustAllStrategy implements TrustStrategy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected void applyDefaultConfig() {
setIfNotAlreadySet(USE_SUBNET, true);
// FIXME not safe for persistence
if (config().getRaw(LegacyJcloudsCloudstackSubnetLocation.PORT_FORWARDING_MANAGER).isAbsent()) {
PortForwardManager pfm = (PortForwardManager) getManagementContext().getLocationRegistry().resolve("portForwardManager(scope=global)");
PortForwardManager pfm = (PortForwardManager) getManagementContext().getLocationRegistry().getLocationManaged("portForwardManager(scope=global)");
config().set(LegacyJcloudsCloudstackSubnetLocation.PORT_FORWARDING_MANAGER, pfm);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import org.apache.brooklyn.util.text.Strings;
import org.apache.brooklyn.util.time.Duration;
import org.apache.brooklyn.util.time.Time;
import org.apache.commons.lang3.tuple.Pair;
import org.jclouds.cloudstack.CloudStackApi;
import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
Expand Down Expand Up @@ -92,7 +91,6 @@
*/
public class LegacyJcloudsCloudstackSubnetLocation extends JcloudsLocation {

private static final long serialVersionUID = -6097237757668759966L;
private static final Logger LOG = LoggerFactory.getLogger(LegacyJcloudsCloudstackSubnetLocation.class);

/* Config on the subnet jclouds location */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import java.util.Map;

import org.jclouds.cloudstack.domain.FirewallRule;

import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.entity.ImplementedBy;
import org.apache.brooklyn.api.sensor.AttributeSensor;
Expand All @@ -29,7 +27,11 @@
import org.apache.brooklyn.core.location.access.BrooklynAccessUtils;
import org.apache.brooklyn.core.location.access.PortForwardManager;
import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
import org.apache.brooklyn.core.sensor.Sensors;
import org.apache.brooklyn.util.net.Cidr;
import org.jclouds.cloudstack.domain.FirewallRule;

import com.google.common.reflect.TypeToken;

@ImplementedBy(LegacySubnetTierImpl.class)
public interface LegacySubnetTier extends Entity, Startable {
Expand Down Expand Up @@ -64,7 +66,9 @@ public interface LegacySubnetTier extends Entity, Startable {
public static final AttributeSensor<String> PRIVATE_HOSTNAME = new BasicAttributeSensor<String>(String.class, "hostname.private",
"A private hostname or IP within the subnet (used so don't lose private address when transforming hostname etc on an entity)");

public static final AttributeSensor<Map<String,String>> PUBLIC_HOSTNAME_IP_IDS = new BasicAttributeSensor(Map.class, "subnet.publicips.mapping",
@SuppressWarnings("serial")
public static final AttributeSensor<Map<String,String>> PUBLIC_HOSTNAME_IP_IDS = Sensors.newSensor(
new TypeToken<Map<String,String>>() {}, "subnet.publicips.mapping",
"Provides a mapping from ip ID to actual public IP");

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.jclouds.cloudstack.options.CreateNetworkOptions;

import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.entity.EntityLocal;
import org.apache.brooklyn.api.location.Location;
import org.apache.brooklyn.api.sensor.AttributeSensor;
import org.apache.brooklyn.api.sensor.SensorEvent;
Expand Down Expand Up @@ -227,7 +226,7 @@ private synchronized PortForwardManager getPortForwardManager() {
pfw = getConfig(PORT_FORWARDING_MANAGER);
if (pfw==null) {
// FIXME not safe for persistence
pfw = (PortForwardManager) getManagementContext().getLocationRegistry().resolve("portForwardManager(scope=global)");
pfw = (PortForwardManager) getManagementContext().getLocationRegistry().getLocationManaged("portForwardManager(scope=global)");
setConfigEvenIfOwned(PORT_FORWARDING_MANAGER, pfw);
}
sensors().set(SUBNET_SERVICE_PORT_FORWARDS, pfw);
Expand Down Expand Up @@ -710,7 +709,7 @@ public void onEvent(SensorEvent<Object> event) {
apply(event.getSource(), event.getValue());
}
public void apply(Entity source, Object valueIgnored) {
Location targetVm = Iterables.getOnlyElement(((EntityLocal)serviceToForward).getLocations(), null);
Location targetVm = Iterables.getOnlyElement(serviceToForward.getLocations(), null);
if (targetVm==null) {
log.warn("Skipping port forward rule for "+serviceToForward+" because it does not have a location");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.brooklyn.api.sensor.AttributeSensor;
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.BasicConfigKey;
import org.apache.brooklyn.core.entity.AbstractEntity;
import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
import org.apache.brooklyn.location.jclouds.JcloudsLocation;
import org.apache.brooklyn.util.collections.MutableMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public interface CloudStackLoadBalancer extends AbstractNonProvisionedController

/** @deprecated in CloudStack; open firewall explicitly */
@Deprecated
@SuppressWarnings("serial")
ConfigKey<Set<String>> ALLOWED_SOURCE_CIDRs = ConfigKeys.newConfigKey(
new TypeToken<Set<String>>() { }, "cloudstack.loadbalancer.cidr", "List of allowed source CIDRs (Deprecated in CloudStack)");
new TypeToken<Set<String>>() {}, "cloudstack.loadbalancer.cidr", "List of allowed source CIDRs (Deprecated in CloudStack)");

/** @deprecated in CloudStack; open firewall explicitly */
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.config.ConfigKey.HasConfigKey;
import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
import org.apache.brooklyn.core.feed.ConfigToAttributes;
import org.apache.brooklyn.core.location.access.BrooklynAccessUtils;
import org.apache.brooklyn.core.location.cloud.names.BasicCloudMachineNamer;
import org.apache.brooklyn.entity.proxy.AbstractNonProvisionedControllerImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public CloudstackPortForwarder(PortForwardManager portForwardManager) {
@Override
public void setManagementContext(ManagementContext managementContext) {
if (portForwardManager == null) {
portForwardManager = (PortForwardManager) managementContext.getLocationRegistry().resolve("portForwardManager(scope=global)");
portForwardManager = (PortForwardManager) managementContext.getLocationRegistry().getLocationManaged("portForwardManager(scope=global)");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void main(String[] args) {

public Cleanup(String accountName) {
managementContext = new LocalManagementContext();
loc = (JcloudsLocation) managementContext.getLocationRegistry().resolve("citrix-cloudplatform");
loc = (JcloudsLocation) managementContext.getLocationRegistry().getLocationManaged("citrix-cloudplatform");
networking = new CloudstackNetworking(loc);
client = CloudstackNew40FeaturesClient.newInstance(loc);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class CloudStackPrivateNetworkLocationCustomizerLiveTest {
public void setUp() throws Exception {
brooklynProperties = BrooklynProperties.Factory.newDefault();
managementContext = new LocalManagementContext(brooklynProperties);
loc = (JcloudsLocation) managementContext.getLocationRegistry().resolve("citrix-cloudplatform");
loc = (JcloudsLocation) managementContext.getLocationRegistry().getLocationManaged("citrix-cloudplatform");
}

@AfterMethod(alwaysRun=true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class CloudstackNew40FeaturesClientLiveTest {
public void setUp() throws Exception {

managementContext = Entities.newManagementContext();
loc = (JcloudsLocation) managementContext.getLocationRegistry().resolve("bt-fast1");
loc = (JcloudsLocation) managementContext.getLocationRegistry().getLocationManaged("bt-fast1");

client = CloudstackNew40FeaturesClient.newInstance(loc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
import org.apache.brooklyn.location.jclouds.JcloudsLocation;
import org.apache.brooklyn.location.jclouds.JcloudsSshMachineLocation;
import org.apache.brooklyn.test.Asserts;
import org.apache.brooklyn.test.HttpTestUtils;
import org.apache.brooklyn.util.core.config.ConfigBag;
import org.apache.brooklyn.util.http.HttpAsserts;
import org.apache.brooklyn.util.net.Cidr;
import org.apache.brooklyn.util.text.Identifiers;

Expand Down Expand Up @@ -244,14 +244,14 @@ public void testLoadBalancerWithHttpTargets() throws Exception {

// double-check that jboss really is reachable (so don't complain about ELB if it's not ELB's fault!)
String directurl = appserver.getAttribute(JBoss7Server.ROOT_URL);
HttpTestUtils.assertHttpStatusCodeEventuallyEquals(directurl, 200);
HttpTestUtils.assertContentContainsText(directurl, "Hello");
HttpAsserts.assertHttpStatusCodeEventuallyEquals(directurl, 200);
HttpAsserts.assertContentContainsText(directurl, "Hello");

Asserts.succeedsEventually(ImmutableMap.of("timeout", 5*60*1000), new Runnable() {
@Override public void run() {
String url = "http://"+lb.getAttribute(CloudStackLoadBalancer.HOSTNAME)+":80/";
HttpTestUtils.assertHttpStatusCodeEventuallyEquals(url, 200);
HttpTestUtils.assertContentContainsText(url, "Hello");
HttpAsserts.assertHttpStatusCodeEventuallyEquals(url, 200);
HttpAsserts.assertContentContainsText(url, "Hello");
}});

assertEquals(lb.getAttribute(CloudStackLoadBalancer.SERVER_POOL_TARGETS), ImmutableSet.of(machine.getNode().getProviderId()));
Expand Down
9 changes: 4 additions & 5 deletions common/src/main/java/brooklyn/networking/AttributeMunger.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.google.common.collect.Maps;

import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.entity.EntityLocal;
import org.apache.brooklyn.api.sensor.AttributeSensor;
import org.apache.brooklyn.api.sensor.SensorEvent;
import org.apache.brooklyn.api.sensor.SensorEventListener;
Expand All @@ -37,9 +36,9 @@ public class AttributeMunger {

private static final Logger log = LoggerFactory.getLogger(AttributeMunger.class);

private final EntityLocal adjunctEntity;
private final Entity adjunctEntity;

public AttributeMunger(EntityLocal adjunctEntity) {
public AttributeMunger(Entity adjunctEntity) {
this.adjunctEntity = adjunctEntity;
}

Expand Down Expand Up @@ -135,11 +134,11 @@ public static <T> void setAttributeIfChanged(EntityAndAttribute<T> entityAndAttr
public static <T> void setAttributeIfChanged(Entity entity, AttributeSensor<T> attribute, T val) {
Object oldval = entity.getAttribute(attribute);
if (!Objects.equal(oldval, val)) {
((EntityLocal)entity).setAttribute(attribute, val);
entity.sensors().set(attribute, val);
}
}

private <T> void subscribe(Entity target, AttributeSensor<T> sensor, SensorEventListener<? super T> listener) {
adjunctEntity.subscribe(target, sensor, listener);
adjunctEntity.subscriptions().subscribe(target, sensor, listener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.concurrent.atomic.AtomicReference;

import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.entity.EntityLocal;
import org.apache.brooklyn.api.location.MachineLocation;
import org.apache.brooklyn.api.location.PortRange;
import org.apache.brooklyn.api.mgmt.Task;
Expand Down Expand Up @@ -58,10 +57,10 @@ public class PortForwarderAsyncImpl implements PortForwarderAsync {

private static final Logger log = LoggerFactory.getLogger(PortForwarderAsyncImpl.class);

private final EntityLocal adjunctEntity;
private final Entity adjunctEntity;
private final PortForwarder portForwarder;

public PortForwarderAsyncImpl(EntityLocal adjunctEntity, PortForwarder portForwarder, PortForwardManager portForwardManager) {
public PortForwarderAsyncImpl(Entity adjunctEntity, PortForwarder portForwarder, PortForwardManager portForwardManager) {
this.adjunctEntity = adjunctEntity;
this.portForwarder = portForwarder;
}
Expand Down
Loading

0 comments on commit ad23488

Please sign in to comment.