Github user neykov commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/78#discussion_r15118769
--- Diff:
software/base/src/main/java/brooklyn/entity/brooklynnode/BrooklynEntityMirrorImpl.java
---
@@ -0,0 +1,175 @@
+/*
+ * 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 brooklyn.entity.brooklynnode;
+
+import java.net.URI;
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.HttpClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Function;
+import com.google.common.base.Preconditions;
+import com.google.gson.Gson;
+
+import brooklyn.entity.basic.AbstractEntity;
+import brooklyn.entity.basic.Attributes;
+import brooklyn.entity.basic.BrooklynTaskTags;
+import brooklyn.entity.basic.Entities;
+import brooklyn.entity.basic.EntityFunctions;
+import brooklyn.entity.basic.Lifecycle;
+import brooklyn.entity.brooklynnode.BrooklynNode;
+import brooklyn.entity.effector.EffectorBody;
+import brooklyn.entity.effector.Effectors;
+import brooklyn.entity.trait.Startable;
+import brooklyn.event.AttributeSensor;
+import brooklyn.event.basic.Sensors;
+import brooklyn.event.feed.http.HttpFeed;
+import brooklyn.event.feed.http.HttpPollConfig;
+import brooklyn.util.collections.Jsonya;
+import brooklyn.util.collections.MutableMap;
+import brooklyn.util.config.ConfigBag;
+import brooklyn.util.exceptions.Exceptions;
+import brooklyn.util.http.HttpTool;
+import brooklyn.util.http.HttpTool.HttpClientBuilder;
+import brooklyn.util.http.HttpToolResponse;
+import brooklyn.util.net.Urls;
+import brooklyn.util.stream.Streams;
+import brooklyn.util.task.Tasks;
+
+public class BrooklynEntityMirrorImpl extends AbstractEntity implements
BrooklynEntityMirror {
+
+ private static final Logger log =
LoggerFactory.getLogger(BrooklynEntityMirrorImpl.class);
+
+ private HttpFeed mirror;
+
+ @Override
+ public void init() {
+ super.init();
+ connectSensors();
+ // tag this child with the tenant owner name
+ this.addTag(String.format("ibm.mms.tenant.entity:%s",
getConfig(BrooklynNode.MANAGEMENT_USER)));
+
getMutableEntityType().addEffector(Effectors.effector(Startable.STOP).impl(new
StopAndExpungeEffector()).build());
+ }
+
+ protected void connectSensors() {
+ Function<HttpToolResponse, Void> mirrorSensors = new
Function<HttpToolResponse,Void>() {
+ @SuppressWarnings("rawtypes")
+ @Override
+ public Void apply(HttpToolResponse input) {
+ Map sensors = new
Gson().fromJson(input.getContentAsString(), Map.class);
+ for (Object kv: sensors.entrySet())
+ setAttribute(Sensors.newSensor(Object.class,
""+((Map.Entry)kv).getKey()), ((Map.Entry)kv).getValue());
+ setAttribute(MIRROR_STATUS, "normal");
+ return null;
+ }
+ };
+
+ String sensorsUri = Urls.mergePaths(
+ Preconditions.checkNotNull(getConfig(MIRRORED_ENTITY_URL),
"Required config: "+MIRRORED_ENTITY_URL),
+ "sensors/current-state");
+
+ mirror = HttpFeed.builder().entity(this)
+ .baseUri(sensorsUri)
+ .credentialsIfNotNull(getConfig(BrooklynNode.MANAGEMENT_USER),
getConfig(BrooklynNode.MANAGEMENT_PASSWORD))
+ .period(getConfig(POLL_PERIOD))
+ .poll(HttpPollConfig.forMultiple()
+ .onSuccess(mirrorSensors)
+
.onFailureOrException(EntityFunctions.settingSensorsConstantFunction(this,
MutableMap.<AttributeSensor<?>,Object>of(
+ Attributes.SERVICE_STATE, Lifecycle.ON_FIRE,
+ MIRROR_STATUS, "error contacting service"
+ ))) )
+ .build();
+ }
+
+ protected void disconnectSensors() {
+ if (mirror != null) mirror.stop();
+ }
+
+ @Override
+ public void destroy() {
+ disconnectSensors();
+ }
+
+ public static class RemoteEffector<T> extends EffectorBody<T> {
+ public final String remoteEffectorName;
+ public final Function<byte[], T> resultParser;
+
+ /** creates an effector implementation which POSTs to a remote
effector endpoint, optionally converting
+ * the byte[] response (if resultParser is null then null is
returned) */
+ public RemoteEffector(String remoteEffectorName, @Nullable
Function<byte[],T> resultParser) {
+ this.remoteEffectorName = remoteEffectorName;
+ this.resultParser = resultParser;
+ }
+
+ @Override
+ public T call(ConfigBag parameters) {
+ String baseUri =
Preconditions.checkNotNull(entity().getConfig(MIRRORED_ENTITY_URL), "Cannot be
invoked without an entity URL");
+ HttpClientBuilder builder = HttpTool.httpClientBuilder()
+ .trustAll()
+ .laxRedirect(true)
+ .uri(baseUri);
+ if (entity().getConfig(MANAGEMENT_USER)!=null)
+ builder.credentials(new
UsernamePasswordCredentials(entity().getConfig(MANAGEMENT_USER),
entity().getConfig(MANAGEMENT_PASSWORD)));
+ HttpClient client = builder.build();
+
+ byte[] result = submit(client,
URI.create(Urls.mergePaths(baseUri, "effectors/"+remoteEffectorName)),
parameters.getAllConfig());
--- End diff --
Path escape remoteEffectorName?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---