http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/parse/DslParser.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/parse/DslParser.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/parse/DslParser.java deleted file mode 100644 index 43a4d04..0000000 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/parse/DslParser.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * 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 io.brooklyn.camp.brooklyn.spi.dsl.parse; - -import java.util.Collection; -import java.util.List; - -import brooklyn.util.collections.MutableList; - -public class DslParser { - private final String expression; - int index = -1; - - public DslParser(String expression) { - this.expression = expression; - } - - public synchronized Object parse() { - if (index>=0) - throw new IllegalStateException("Parser can only be used once"); - - index++; - Object result = next(); - - if (index < expression.length()) - throw new IllegalStateException("Unexpected character at position "+index+" in "+expression); - - return result; - } - - @SuppressWarnings("unchecked") - public Object next() { - int start = index; - - skipWhitespace(); - if (index >= expression.length()) - throw new IllegalStateException("Unexpected end of expression to parse, looking for content since position "+start); - - if (expression.charAt(index)=='"') { - // assume a string - int stringStart = index; - index++; - do { - if (index >= expression.length()) - throw new IllegalStateException("Unexpected end of expression to parse, looking for close quote since position "+stringStart); - char c = expression.charAt(index); - if (c=='"') break; - if (c=='\\') index++; - index++; - } while (true); - index++; - return new QuotedString(expression.substring(stringStart, index)); - } - - // not a string, must be a function (or chain thereof) - List<FunctionWithArgs> result = new MutableList<FunctionWithArgs>(); - - int fnStart = index; - do { - if (index >= expression.length()) - break; - char c = expression.charAt(index); - if (Character.isJavaIdentifierPart(c)) ; - // these chars also permitted - else if (".:".indexOf(c)>=0) ; - // other things e.g. whitespace, parentheses, etc, skip - else break; - index++; - } while (true); - String fn = expression.substring(fnStart, index); - if (fn.length()==0) - throw new IllegalStateException("Expected a function name at position "+start); - skipWhitespace(); - - if (index < expression.length() && expression.charAt(index)=='(') { - // collect arguments - int parenStart = index; - List<Object> args = new MutableList<Object>(); - index ++; - do { - skipWhitespace(); - if (index >= expression.length()) - throw new IllegalStateException("Unexpected end of arguments to function '"+fn+"', no close parenthesis matching character at position "+parenStart); - char c = expression.charAt(index); - if (c==')') break; - if (c==',') { - if (args.isEmpty()) - throw new IllegalStateException("Invalid character at position"+index); - index++; - } else { - if (!args.isEmpty()) - throw new IllegalStateException("Expected , before position"+index); - } - args.add(next()); - } while (true); - result.add(new FunctionWithArgs(fn, args)); - index++; - skipWhitespace(); - if (index >= expression.length()) - return result; - char c = expression.charAt(index); - if (c=='.') { - // chained expression - int chainStart = index; - index++; - Object next = next(); - if (next instanceof List) { - result.addAll((Collection<? extends FunctionWithArgs>) next); - return result; - } else { - throw new IllegalStateException("Expected functions following position"+chainStart); - } - } else { - // following word not something handled at this level; assume parent will handle (or throw) - e.g. a , or extra ) - return result; - } - } else { - // it is just a word; return it with args as null - return new FunctionWithArgs(fn, null); - } - } - - private void skipWhitespace() { - while (index<expression.length() && Character.isWhitespace(expression.charAt(index))) - index++; - } - -} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/parse/FunctionWithArgs.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/parse/FunctionWithArgs.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/parse/FunctionWithArgs.java deleted file mode 100644 index 973b7ba..0000000 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/parse/FunctionWithArgs.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 io.brooklyn.camp.brooklyn.spi.dsl.parse; - -import java.util.List; - -import com.google.common.collect.ImmutableList; - -public class FunctionWithArgs { - private final String function; - private final List<Object> args; - - public FunctionWithArgs(String function, List<Object> args) { - this.function = function; - this.args = args==null ? null : ImmutableList.copyOf(args); - } - - public String getFunction() { - return function; - } - - /** - * arguments (typically {@link QuotedString} or more {@link FunctionWithArgs}). - * - * null means it is a function in a map key which expects map value to be the arguments -- specified without parentheses; - * empty means parentheses already applied, with 0 args. - */ - public List<Object> getArgs() { - return args; - } - - @Override - public String toString() { - return function+(args==null ? "" : args); - } - - public Object arg(int i) { - return args.get(i); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/parse/QuotedString.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/parse/QuotedString.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/parse/QuotedString.java deleted file mode 100644 index 485d00a..0000000 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/dsl/parse/QuotedString.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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 io.brooklyn.camp.brooklyn.spi.dsl.parse; - -import static com.google.common.base.Preconditions.checkNotNull; -import brooklyn.util.text.StringEscapes.JavaStringEscapes; - -import com.google.common.base.Objects; - -public class QuotedString { - private final String s; - - public QuotedString(String s) { - this.s = checkNotNull(s, "string"); - } - @Override - public String toString() { - return s; - } - public String unwrapped() { - return JavaStringEscapes.unwrapJavaString(s); - } - - @Override - public boolean equals(Object obj) { - return (obj instanceof QuotedString) && ((QuotedString)obj).toString().equals(toString()); - } - - @Override - public int hashCode() { - return Objects.hashCode(s); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AbstractBrooklynResourceLookup.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AbstractBrooklynResourceLookup.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AbstractBrooklynResourceLookup.java deleted file mode 100644 index 5e0e3f0..0000000 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AbstractBrooklynResourceLookup.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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 io.brooklyn.camp.brooklyn.spi.lookup; - -import io.brooklyn.camp.spi.AbstractResource; -import io.brooklyn.camp.spi.PlatformRootSummary; -import io.brooklyn.camp.spi.collection.AbstractResourceLookup; -import brooklyn.management.ManagementContext; - -public abstract class AbstractBrooklynResourceLookup<T extends AbstractResource> extends AbstractResourceLookup<T> { - - protected final PlatformRootSummary root; - protected final ManagementContext bmc; - - public AbstractBrooklynResourceLookup(PlatformRootSummary root, ManagementContext bmc) { - this.root = root; - this.bmc = bmc; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AbstractTemplateBrooklynLookup.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AbstractTemplateBrooklynLookup.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AbstractTemplateBrooklynLookup.java deleted file mode 100644 index a15b178..0000000 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AbstractTemplateBrooklynLookup.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 io.brooklyn.camp.brooklyn.spi.lookup; - -import io.brooklyn.camp.spi.AbstractResource; -import io.brooklyn.camp.spi.PlatformRootSummary; -import io.brooklyn.camp.spi.collection.ResolvableLink; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.apache.brooklyn.catalog.CatalogItem; -import brooklyn.catalog.internal.CatalogUtils; -import brooklyn.entity.Entity; -import brooklyn.entity.proxying.EntitySpec; -import brooklyn.management.ManagementContext; - -public abstract class AbstractTemplateBrooklynLookup<T extends AbstractResource> extends AbstractBrooklynResourceLookup<T> { - - private static final Logger log = LoggerFactory.getLogger(AbstractTemplateBrooklynLookup.class); - - public AbstractTemplateBrooklynLookup(PlatformRootSummary root, ManagementContext bmc) { - super(root, bmc); - } - - @Override - public T get(String id) { - CatalogItem<?,?> item = getCatalogItem(id); - if (item==null) { - log.warn("Could not find item '"+id+"' in Brooklyn catalog; returning null"); - return null; - } - return adapt(item); - } - - private CatalogItem<?, ?> getCatalogItem(String versionedId) { - return CatalogUtils.getCatalogItemOptionalVersion(bmc, versionedId); - } - - public abstract T adapt(CatalogItem<?,?> item); - - protected ResolvableLink<T> newLink(CatalogItem<? extends Entity,EntitySpec<?>> li) { - return newLink(li.getId(), li.getDisplayName()); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AssemblyBrooklynLookup.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AssemblyBrooklynLookup.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AssemblyBrooklynLookup.java deleted file mode 100644 index 2f0380f..0000000 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AssemblyBrooklynLookup.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 io.brooklyn.camp.brooklyn.spi.lookup; - -import io.brooklyn.camp.spi.Assembly; -import io.brooklyn.camp.spi.PlatformRootSummary; -import io.brooklyn.camp.spi.collection.ResolvableLink; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -import brooklyn.entity.Application; -import brooklyn.entity.Entity; -import brooklyn.management.ManagementContext; - - -public class AssemblyBrooklynLookup extends AbstractBrooklynResourceLookup<Assembly> { - - private PlatformComponentBrooklynLookup pcs; - - public AssemblyBrooklynLookup(PlatformRootSummary root, ManagementContext bmc, PlatformComponentBrooklynLookup pcs) { - super(root, bmc); - this.pcs = pcs; - } - - @Override - public Assembly get(String id) { - Entity entity = bmc.getEntityManager().getEntity(id); - if (!(entity instanceof Application)) - throw new IllegalArgumentException("Element for "+id+" is not an Application ("+entity+")"); - Assembly.Builder<? extends Assembly> builder = Assembly.builder() - .created(new Date(entity.getCreationTime())) - .id(entity.getId()) - .name(entity.getDisplayName()); - - builder.customAttribute("externalManagementUri", BrooklynUrlLookup.getUrl(bmc, entity)); - - for (Entity child: entity.getChildren()) - // FIXME this walks the whole damn tree! - builder.add( pcs.get(child.getId() )); - return builder.build(); - } - - @Override - public List<ResolvableLink<Assembly>> links() { - List<ResolvableLink<Assembly>> result = new ArrayList<ResolvableLink<Assembly>>(); - for (Application app: bmc.getApplications()) - result.add(newLink(app.getId(), app.getDisplayName())); - return result; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AssemblyTemplateBrooklynLookup.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AssemblyTemplateBrooklynLookup.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AssemblyTemplateBrooklynLookup.java deleted file mode 100644 index 24c40bc..0000000 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/AssemblyTemplateBrooklynLookup.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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 io.brooklyn.camp.brooklyn.spi.lookup; - -import io.brooklyn.camp.brooklyn.spi.creation.BrooklynAssemblyTemplateInstantiator; -import io.brooklyn.camp.spi.AssemblyTemplate; -import io.brooklyn.camp.spi.PlatformRootSummary; -import io.brooklyn.camp.spi.collection.ResolvableLink; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.brooklyn.catalog.CatalogItem; -import brooklyn.catalog.CatalogPredicates; -import brooklyn.entity.Application; -import brooklyn.entity.Entity; -import brooklyn.entity.proxying.EntitySpec; -import brooklyn.management.ManagementContext; - -public class AssemblyTemplateBrooklynLookup extends AbstractTemplateBrooklynLookup<AssemblyTemplate> { - - public AssemblyTemplateBrooklynLookup(PlatformRootSummary root, ManagementContext bmc) { - super(root, bmc); - } - - @Override - public AssemblyTemplate adapt(CatalogItem<?,?> item) { - return AssemblyTemplate.builder(). - name(item.getDisplayName()). - id(item.getId()). - description(item.getDescription()). - created(root.getCreated()). - instantiator(BrooklynAssemblyTemplateInstantiator.class). - build(); - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - // why can I not pass an EntitySpec<? extends Application> to newLink(EntitySpec<?> spec) ? - // feels to me (alexheneveld) that `? extends Application` should be both covariant and contravariant to `?` .. - // but it's not, so we introduce this conversion method - protected ResolvableLink<AssemblyTemplate> newApplicationLink(CatalogItem<? extends Entity, EntitySpec<? extends Application>> li) { - return super.newLink((CatalogItem)li); - } - - @Override - public List<ResolvableLink<AssemblyTemplate>> links() { - Iterable<CatalogItem<Application,EntitySpec<? extends Application>>> l = bmc.getCatalog().getCatalogItems(CatalogPredicates.IS_TEMPLATE); - List<ResolvableLink<AssemblyTemplate>> result = new ArrayList<ResolvableLink<AssemblyTemplate>>(); - for (CatalogItem<Application,EntitySpec<? extends Application>> li: l) - result.add(newApplicationLink(li)); - return result; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/BrooklynUrlLookup.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/BrooklynUrlLookup.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/BrooklynUrlLookup.java deleted file mode 100644 index a27be03..0000000 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/BrooklynUrlLookup.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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 io.brooklyn.camp.brooklyn.spi.lookup; - -import brooklyn.config.ConfigKey; -import brooklyn.entity.Entity; -import brooklyn.entity.basic.ConfigKeys; -import brooklyn.management.ManagementContext; -import brooklyn.util.net.Urls; - -public class BrooklynUrlLookup { - - public static ConfigKey<String> BROOKLYN_ROOT_URL = ConfigKeys.newStringConfigKey("brooklyn.root.url"); - - public static String getUrl(ManagementContext bmc, Entity entity) { - String root = bmc.getConfig().getConfig(BROOKLYN_ROOT_URL); - if (root==null) return null; - return Urls.mergePaths(root, "#/", - "/v1/applications/"+entity.getApplicationId()+"/entities/"+entity.getId()); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/PlatformComponentBrooklynLookup.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/PlatformComponentBrooklynLookup.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/PlatformComponentBrooklynLookup.java deleted file mode 100644 index 90e5113..0000000 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/PlatformComponentBrooklynLookup.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 io.brooklyn.camp.brooklyn.spi.lookup; - -import io.brooklyn.camp.spi.PlatformComponent; -import io.brooklyn.camp.spi.PlatformComponent.Builder; -import io.brooklyn.camp.spi.PlatformRootSummary; -import io.brooklyn.camp.spi.collection.ResolvableLink; - -import java.util.Collections; -import java.util.Date; -import java.util.List; - -import brooklyn.entity.Entity; -import brooklyn.management.ManagementContext; - - -public class PlatformComponentBrooklynLookup extends AbstractBrooklynResourceLookup<PlatformComponent> { - - public PlatformComponentBrooklynLookup(PlatformRootSummary root, ManagementContext bmc) { - super(root, bmc); - } - - @Override - public PlatformComponent get(String id) { - Entity entity = bmc.getEntityManager().getEntity(id); - Builder<? extends PlatformComponent> builder = PlatformComponent.builder() - .created(new Date(entity.getCreationTime())) - .id(entity.getId()) - .name(entity.getDisplayName()) - .externalManagementUri(BrooklynUrlLookup.getUrl(bmc, entity)); - - for (Entity child: entity.getChildren()) - // FIXME this walks the whole damn tree! - builder.add( get(child.getId() )); - return builder.build(); - } - - // platform components are not listed at the top level -- you have to walk the assemblies - @Override - public List<ResolvableLink<PlatformComponent>> links() { - return Collections.emptyList(); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/PlatformComponentTemplateBrooklynLookup.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/PlatformComponentTemplateBrooklynLookup.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/PlatformComponentTemplateBrooklynLookup.java deleted file mode 100644 index 7abb8ad..0000000 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/lookup/PlatformComponentTemplateBrooklynLookup.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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 io.brooklyn.camp.brooklyn.spi.lookup; - -import io.brooklyn.camp.spi.PlatformComponentTemplate; -import io.brooklyn.camp.spi.PlatformRootSummary; -import io.brooklyn.camp.spi.collection.ResolvableLink; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.brooklyn.catalog.CatalogItem; -import brooklyn.catalog.CatalogPredicates; -import brooklyn.entity.Entity; -import brooklyn.entity.proxying.EntitySpec; -import brooklyn.management.ManagementContext; - -public class PlatformComponentTemplateBrooklynLookup extends AbstractTemplateBrooklynLookup<PlatformComponentTemplate> { - - public PlatformComponentTemplateBrooklynLookup(PlatformRootSummary root, ManagementContext bmc) { - super(root, bmc); - } - - @Override - public PlatformComponentTemplate adapt(CatalogItem<?,?> item) { - return PlatformComponentTemplate.builder(). - name(item.getDisplayName()). - id(item.getId()). - description(item.getDescription()). - created(root.getCreated()). - build(); - } - - @Override - public List<ResolvableLink<PlatformComponentTemplate>> links() { - Iterable<CatalogItem<Entity,EntitySpec<?>>> l = bmc.getCatalog().getCatalogItems(CatalogPredicates.IS_ENTITY); - List<ResolvableLink<PlatformComponentTemplate>> result = new ArrayList<ResolvableLink<PlatformComponentTemplate>>(); - for (CatalogItem<Entity,EntitySpec<?>> li: l) - result.add(newLink(li)); - return result; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/platform/BrooklynImmutableCampPlatform.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/platform/BrooklynImmutableCampPlatform.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/platform/BrooklynImmutableCampPlatform.java deleted file mode 100644 index e5369cd..0000000 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/platform/BrooklynImmutableCampPlatform.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * 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 io.brooklyn.camp.brooklyn.spi.platform; - -import io.brooklyn.camp.CampPlatform; -import io.brooklyn.camp.brooklyn.spi.lookup.AssemblyBrooklynLookup; -import io.brooklyn.camp.brooklyn.spi.lookup.AssemblyTemplateBrooklynLookup; -import io.brooklyn.camp.brooklyn.spi.lookup.PlatformComponentBrooklynLookup; -import io.brooklyn.camp.brooklyn.spi.lookup.PlatformComponentTemplateBrooklynLookup; -import io.brooklyn.camp.spi.ApplicationComponent; -import io.brooklyn.camp.spi.ApplicationComponentTemplate; -import io.brooklyn.camp.spi.Assembly; -import io.brooklyn.camp.spi.AssemblyTemplate; -import io.brooklyn.camp.spi.PlatformComponent; -import io.brooklyn.camp.spi.PlatformComponentTemplate; -import io.brooklyn.camp.spi.PlatformRootSummary; -import io.brooklyn.camp.spi.PlatformTransaction; -import io.brooklyn.camp.spi.collection.BasicResourceLookup; -import io.brooklyn.camp.spi.collection.ResourceLookup; -import io.brooklyn.camp.spi.collection.ResourceLookup.EmptyResourceLookup; -import brooklyn.camp.brooklyn.api.HasBrooklynManagementContext; -import brooklyn.management.ManagementContext; - -/** Immutable CAMP platform which reflects things in the underlying Brooklyn system */ -public class BrooklynImmutableCampPlatform extends CampPlatform implements HasBrooklynManagementContext { - - private final ManagementContext bmc; - private final AssemblyTemplateBrooklynLookup ats; - private final PlatformComponentTemplateBrooklynLookup pcts; - private final BasicResourceLookup<ApplicationComponentTemplate> acts; - private final PlatformComponentBrooklynLookup pcs; - private final AssemblyBrooklynLookup assemblies; - - public BrooklynImmutableCampPlatform(PlatformRootSummary root, ManagementContext managementContext) { - super(root); - this.bmc = managementContext; - - // these come from brooklyn - pcts = new PlatformComponentTemplateBrooklynLookup(root(), getBrooklynManagementContext()); - ats = new AssemblyTemplateBrooklynLookup(root(), getBrooklynManagementContext()); - pcs = new PlatformComponentBrooklynLookup(root(), getBrooklynManagementContext()); - assemblies = new AssemblyBrooklynLookup(root(), getBrooklynManagementContext(), pcs); - - // ACT's are not known in brooklyn (everything comes in as config) -- to be extended to support! - acts = new BasicResourceLookup<ApplicationComponentTemplate>(); - } - - // --- brooklyn setup - - public ManagementContext getBrooklynManagementContext() { - return bmc; - } - - // --- camp comatibility setup - - @Override - public ResourceLookup<PlatformComponentTemplate> platformComponentTemplates() { - return pcts; - } - - @Override - public ResourceLookup<ApplicationComponentTemplate> applicationComponentTemplates() { - return acts; - } - - @Override - public ResourceLookup<AssemblyTemplate> assemblyTemplates() { - return ats; - } - - @Override - public ResourceLookup<PlatformComponent> platformComponents() { - return pcs; - } - - @Override - public ResourceLookup<ApplicationComponent> applicationComponents() { - return new EmptyResourceLookup<ApplicationComponent>(); - } - - @Override - public ResourceLookup<Assembly> assemblies() { - return assemblies; - } - - @Override - public PlatformTransaction transaction() { - throw new IllegalStateException(this+" does not support adding new items"); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampConstants.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampConstants.java b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampConstants.java new file mode 100644 index 0000000..4a2078b --- /dev/null +++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampConstants.java @@ -0,0 +1,50 @@ +/* + * 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.camp.brooklyn; + +import io.brooklyn.camp.CampPlatform; + +import java.util.Set; + +import brooklyn.config.BrooklynServerConfig; +import brooklyn.config.ConfigInheritance; +import brooklyn.config.ConfigKey; +import brooklyn.entity.basic.ConfigKeys; + +import com.google.common.collect.ImmutableSet; + +public class BrooklynCampConstants { + + public static final String PLAN_ID_FLAG = "planId"; + + public static final ConfigKey<String> PLAN_ID = ConfigKeys.builder(String.class, "camp.plan.id") + .description("Identifier supplied in the deployment plan for component to which this entity corresponds " + + "(human-readable, for correlating across plan, template, and instance)") + .inheritance(ConfigInheritance.NONE) + .build(); + + public static final ConfigKey<String> TEMPLATE_ID = ConfigKeys.builder(String.class, "camp.template.id") + .description("UID of the component in the CAMP template from which this entity was created") + .inheritance(ConfigInheritance.NONE) + .build(); + + public static final ConfigKey<CampPlatform> CAMP_PLATFORM = BrooklynServerConfig.CAMP_PLATFORM; + + public static final Set<String> YAML_URL_PROTOCOL_WHITELIST = ImmutableSet.of("classpath", "http"); +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampPlatform.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampPlatform.java b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampPlatform.java new file mode 100644 index 0000000..6f941aa --- /dev/null +++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampPlatform.java @@ -0,0 +1,78 @@ +/* + * 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.camp.brooklyn; + +import io.brooklyn.camp.AggregatingCampPlatform; +import io.brooklyn.camp.CampPlatform; +import io.brooklyn.camp.spi.PlatformRootSummary; + +import org.apache.brooklyn.camp.brooklyn.spi.creation.BrooklynEntityMatcher; +import org.apache.brooklyn.camp.brooklyn.spi.dsl.BrooklynDslInterpreter; +import org.apache.brooklyn.camp.brooklyn.spi.platform.BrooklynImmutableCampPlatform; + +import brooklyn.camp.brooklyn.api.HasBrooklynManagementContext; +import brooklyn.config.BrooklynProperties; +import brooklyn.management.ManagementContext; +import brooklyn.management.ManagementContext.PropertiesReloadListener; + +/** {@link CampPlatform} implementation which includes Brooklyn entities + * (via {@link BrooklynImmutableCampPlatform}) + * and allows customisation / additions */ +public class BrooklynCampPlatform extends AggregatingCampPlatform implements HasBrooklynManagementContext { + + private final ManagementContext bmc; + + public BrooklynCampPlatform(PlatformRootSummary root, ManagementContext managementContext) { + super(root); + addPlatform(new BrooklynImmutableCampPlatform(root, managementContext)); + + this.bmc = managementContext; + + addMatchers(); + addInterpreters(); + + managementContext.addPropertiesReloadListener(new PropertiesReloadListener() { + private static final long serialVersionUID = -3739276553334749184L; + @Override public void reloaded() { + setConfigKeyAtManagmentContext(); + } + }); + } + + // --- brooklyn setup + + public ManagementContext getBrooklynManagementContext() { + return bmc; + } + + protected void addMatchers() { + // TODO artifacts + pdp().addMatcher(new BrooklynEntityMatcher(bmc)); + } + + protected void addInterpreters() { + pdp().addInterpreter(new BrooklynDslInterpreter()); + } + + public BrooklynCampPlatform setConfigKeyAtManagmentContext() { + ((BrooklynProperties)bmc.getConfig()).put(BrooklynCampConstants.CAMP_PLATFORM, this); + return this; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampPlatformLauncherAbstract.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampPlatformLauncherAbstract.java b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampPlatformLauncherAbstract.java new file mode 100644 index 0000000..d1c3cf2 --- /dev/null +++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampPlatformLauncherAbstract.java @@ -0,0 +1,73 @@ +/* + * 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.camp.brooklyn; + +import io.brooklyn.camp.spi.PlatformRootSummary; +import brooklyn.management.ManagementContext; +import brooklyn.management.internal.LocalManagementContext; + +import com.google.common.annotations.Beta; + +/** launcher for {@link BrooklynCampPlatform}, which may or may not start a (web) server depending on children */ +@Beta +public abstract class BrooklynCampPlatformLauncherAbstract { + + protected BrooklynCampPlatform platform; + protected ManagementContext mgmt; + + public BrooklynCampPlatformLauncherAbstract useManagementContext(ManagementContext mgmt) { + if (this.mgmt!=null && mgmt!=this.mgmt) + throw new IllegalStateException("Attempt to change mgmt context; not supported."); + + this.mgmt = mgmt; + + return this; + } + + public BrooklynCampPlatformLauncherAbstract launch() { + if (platform!=null) + throw new IllegalStateException("platform already created"); + + if (getBrooklynMgmt()==null) + useManagementContext(newMgmtContext()); + + platform = new BrooklynCampPlatform( + PlatformRootSummary.builder().name("Brooklyn CAMP Platform").build(), + getBrooklynMgmt()) + .setConfigKeyAtManagmentContext(); + + return this; + } + + protected LocalManagementContext newMgmtContext() { + return new LocalManagementContext(); + } + + public ManagementContext getBrooklynMgmt() { + return mgmt; + } + + public BrooklynCampPlatform getCampPlatform() { + return platform; + } + + /** stops any servers (camp and brooklyn) launched by this launcher */ + public abstract void stopServers() throws Exception; + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampPlatformLauncherNoServer.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampPlatformLauncherNoServer.java b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampPlatformLauncherNoServer.java new file mode 100644 index 0000000..bbfe2ab --- /dev/null +++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampPlatformLauncherNoServer.java @@ -0,0 +1,36 @@ +/* + * 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.camp.brooklyn; + +import com.google.common.annotations.Beta; + + +/** launcher for {@link BrooklynCampPlatform}, which does not start a server (and can live in this project) */ +@Beta +public class BrooklynCampPlatformLauncherNoServer extends BrooklynCampPlatformLauncherAbstract { + + public void stopServers() { + // nothing to do + } + + public static void main(String[] args) { + new BrooklynCampPlatformLauncherNoServer().launch(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampReservedKeys.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampReservedKeys.java b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampReservedKeys.java new file mode 100644 index 0000000..7fe6030 --- /dev/null +++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/BrooklynCampReservedKeys.java @@ -0,0 +1,29 @@ +/* + * 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.camp.brooklyn; + +public interface BrooklynCampReservedKeys { + public static final String BROOKLYN_CONFIG = "brooklyn.config"; + public static final String BROOKLYN_FLAGS = "brooklyn.flags"; + public static final String BROOKLYN_POLICIES = "brooklyn.policies"; + public static final String BROOKLYN_ENRICHERS = "brooklyn.enrichers"; + public static final String BROOKLYN_CHILDREN = "brooklyn.children"; + public static final String BROOKLYN_INITIALIZERS = "brooklyn.initializers"; + public static final String BROOKLYN_CATALOG = "brooklyn.catalog"; +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/YamlLauncherAbstract.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/YamlLauncherAbstract.java b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/YamlLauncherAbstract.java new file mode 100644 index 0000000..9e54c49 --- /dev/null +++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/YamlLauncherAbstract.java @@ -0,0 +1,133 @@ +/* + * 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.camp.brooklyn; + +import io.brooklyn.camp.spi.Assembly; +import io.brooklyn.camp.spi.AssemblyTemplate; + +import java.io.Reader; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import brooklyn.entity.Application; +import brooklyn.entity.Entity; +import brooklyn.entity.basic.BrooklynShutdownHooks; +import brooklyn.entity.basic.BrooklynTaskTags; +import brooklyn.entity.basic.Entities; +import brooklyn.management.ManagementContext; +import brooklyn.management.Task; +import brooklyn.util.ResourceUtils; +import brooklyn.util.exceptions.Exceptions; +import brooklyn.util.stream.Streams; + +import com.google.common.annotations.Beta; + +/** convenience for launching YAML files directly */ +@Beta +public abstract class YamlLauncherAbstract { + + private static final Logger log = LoggerFactory.getLogger(YamlLauncherAbstract.class); + + protected final BrooklynCampPlatformLauncherAbstract platformLauncher; + + protected final BrooklynCampPlatform platform; + protected final ManagementContext brooklynMgmt; + protected boolean shutdownAppsOnExit = false; + + public YamlLauncherAbstract() { + this.platformLauncher = newPlatformLauncher(); + platformLauncher.launch(); + this.platform = platformLauncher.getCampPlatform(); + this.brooklynMgmt = platformLauncher.getBrooklynMgmt(); + } + + public ManagementContext getManagementContext() { + return brooklynMgmt; + } + + public boolean getShutdownAppsOnExit() { + return shutdownAppsOnExit; + } + + public void setShutdownAppsOnExit(boolean shutdownAppsOnExit) { + this.shutdownAppsOnExit = shutdownAppsOnExit; + } + + protected abstract BrooklynCampPlatformLauncherAbstract newPlatformLauncher(); + + public Application launchAppYaml(String url) { + return launchAppYaml(url, true); + } + + public Application launchAppYaml(String url, boolean waitForTasksToComplete) { + try { + Reader input = Streams.reader(new ResourceUtils(this).getResourceFromUrl(url)); + Application app = launchAppYaml(input, waitForTasksToComplete); + log.info("Application started from YAML file "+url+": "+app); + return app; + } catch (Exception e) { + throw Exceptions.propagate(e); + } + } + + public Application launchAppYaml(Reader input) { + return launchAppYaml(input, true); + } + + public Application launchAppYaml(Reader input, boolean waitForTasksToComplete) { + try { + AssemblyTemplate at = platform.pdp().registerDeploymentPlan(input); + + Assembly assembly = at.getInstantiator().newInstance().instantiate(at, platform); + Entity app = brooklynMgmt.getEntityManager().getEntity(assembly.getId()); + log.info("Launching "+app); + + if (getShutdownAppsOnExit()) BrooklynShutdownHooks.invokeStopOnShutdown(app); + + if (waitForTasksToComplete) { + Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(brooklynMgmt.getExecutionManager(), app); + log.info("Waiting on "+tasks.size()+" task(s)"); + for (Task<?> t: tasks) { + t.blockUntilEnded(); + } + } + + log.info("Application started from YAML: "+app); + Entities.dumpInfo(app); + return (Application)app; + } catch (Exception e) { + throw Exceptions.propagate(e); + } + } + + /** kills all apps, web servers, and mgmt context + * <p> + * this launcher does not support being used again subsequently */ + public void destroyAll() { + Entities.destroyAll(getManagementContext()); + try { + platformLauncher.stopServers(); + } catch (Exception e) { + log.warn("Unable to stop servers (ignoring): "+e); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/YamlLauncherNoServer.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/YamlLauncherNoServer.java b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/YamlLauncherNoServer.java new file mode 100644 index 0000000..1ec3c8d --- /dev/null +++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/YamlLauncherNoServer.java @@ -0,0 +1,39 @@ +/* + * 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.camp.brooklyn; + +import com.google.common.annotations.Beta; + +/** convenience for launching YAML files directly */ +@Beta +public class YamlLauncherNoServer extends YamlLauncherAbstract { + + @Override + protected BrooklynCampPlatformLauncherAbstract newPlatformLauncher() { + return new BrooklynCampPlatformLauncherNoServer(); + } + + public static void main(String[] args) { + YamlLauncherNoServer l = new YamlLauncherNoServer(); + l.setShutdownAppsOnExit(true); + + l.launchAppYaml("java-web-app-and-db-with-function.yaml"); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e406d1ad/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java new file mode 100644 index 0000000..3254e20 --- /dev/null +++ b/usage/camp/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java @@ -0,0 +1,283 @@ +/* + * 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.camp.brooklyn.spi.creation; + +import io.brooklyn.camp.CampPlatform; +import io.brooklyn.camp.spi.Assembly; +import io.brooklyn.camp.spi.AssemblyTemplate; +import io.brooklyn.camp.spi.AssemblyTemplate.Builder; +import io.brooklyn.camp.spi.PlatformComponentTemplate; +import io.brooklyn.camp.spi.collection.ResolvableLink; +import io.brooklyn.camp.spi.instantiate.AssemblyTemplateInstantiator; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import brooklyn.camp.brooklyn.api.AssemblyTemplateSpecInstantiator; +import brooklyn.camp.brooklyn.api.HasBrooklynManagementContext; + +import org.apache.brooklyn.camp.brooklyn.BrooklynCampConstants; +import org.apache.brooklyn.catalog.CatalogItem; +import brooklyn.catalog.internal.BasicBrooklynCatalog.BrooklynLoaderTracker; +import brooklyn.catalog.internal.CatalogUtils; +import brooklyn.config.BrooklynServerConfig; +import brooklyn.entity.Application; +import brooklyn.entity.Entity; +import brooklyn.entity.basic.BasicApplicationImpl; +import brooklyn.entity.proxying.EntitySpec; +import brooklyn.management.ManagementContext; +import brooklyn.management.classloading.BrooklynClassLoadingContext; +import brooklyn.management.internal.EntityManagementUtils; +import brooklyn.management.internal.EntityManagementUtils.CreationResult; +import brooklyn.util.ResourceUtils; +import brooklyn.util.exceptions.Exceptions; +import brooklyn.util.flags.TypeCoercions; +import brooklyn.util.net.Urls; + +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + +public class BrooklynAssemblyTemplateInstantiator implements AssemblyTemplateSpecInstantiator { + + private static final Logger log = LoggerFactory.getLogger(BrooklynAssemblyTemplateInstantiator.class); + + public static final String NEVER_UNWRAP_APPS_PROPERTY = "wrappedApp"; + + @Override + public Assembly instantiate(AssemblyTemplate template, CampPlatform platform) { + Application app = create(template, platform); + CreationResult<Application, Void> start = EntityManagementUtils.start(app); + log.debug("CAMP created "+app+"; starting in "+start.task()); + return platform.assemblies().get(app.getApplicationId()); + } + + public Application create(AssemblyTemplate template, CampPlatform platform) { + Application instance = EntityManagementUtils.createUnstarted(getBrooklynManagementContext(platform), template); + log.debug("CAMP created {}", instance); + return instance; + } + + private ManagementContext getBrooklynManagementContext(CampPlatform platform) { + return ((HasBrooklynManagementContext)platform).getBrooklynManagementContext(); + } + + @SuppressWarnings("unchecked") + public EntitySpec<? extends Application> createSpec(AssemblyTemplate template, CampPlatform platform, BrooklynClassLoadingContext loader, boolean autoUnwrapIfPossible) { + log.debug("CAMP creating application instance for {} ({})", template.getId(), template); + + // AssemblyTemplates created via PDP, _specifying_ then entities to put in + + BrooklynComponentTemplateResolver resolver = BrooklynComponentTemplateResolver.Factory.newInstance( + loader, buildWrapperAppTemplate(template)); + EntitySpec<? extends Application> app = resolver.resolveSpec(null); + + // first build the children into an empty shell app + List<EntitySpec<?>> childSpecs = buildTemplateServicesAsSpecs(loader, template, platform); + for (EntitySpec<?> childSpec : childSpecs) { + app.child(childSpec); + } + + if (autoUnwrapIfPossible && shouldUnwrap(template, app)) { + EntitySpec<? extends Application> oldApp = app; + app = (EntitySpec<? extends Application>) Iterables.getOnlyElement( app.getChildren() ); + + // if promoted, apply the transformations done to the app + // (transformations will be done by the resolveSpec call above, but we are collapsing oldApp so transfer to app=newApp) + EntityManagementUtils.collapseSpec(oldApp, app); + } else { + app.configure(EntityManagementUtils.WRAPPER_APP_MARKER, Boolean.TRUE); + } + + return app; + } + + private AssemblyTemplate buildWrapperAppTemplate(AssemblyTemplate template) { + Builder<? extends AssemblyTemplate> builder = AssemblyTemplate.builder(); + builder.type("brooklyn:" + BasicApplicationImpl.class.getName()); + builder.id(template.getId()); + builder.name(template.getName()); + builder.sourceCode(template.getSourceCode()); + for (Entry<String, Object> entry : template.getCustomAttributes().entrySet()) { + builder.customAttribute(entry.getKey(), entry.getValue()); + } + builder.instantiator(template.getInstantiator()); + AssemblyTemplate wrapTemplate = builder.build(); + return wrapTemplate; + } + + protected boolean shouldUnwrap(AssemblyTemplate template, EntitySpec<? extends Application> app) { + Object leaveWrapped = template.getCustomAttributes().get(NEVER_UNWRAP_APPS_PROPERTY); + if (leaveWrapped!=null) { + if (TypeCoercions.coerce(leaveWrapped, Boolean.class)) + return false; + } + + if (app.getChildren().size()!=1) + return false; + + EntitySpec<?> childSpec = Iterables.getOnlyElement(app.getChildren()); + if (childSpec.getType()==null || !Application.class.isAssignableFrom(childSpec.getType())) + return false; + + return EntityManagementUtils.hasNoNameOrCustomKeysOrRoot(template, app); + } + + private List<EntitySpec<?>> buildTemplateServicesAsSpecs(BrooklynClassLoadingContext loader, AssemblyTemplate template, CampPlatform platform) { + return buildTemplateServicesAsSpecsImpl(loader, template, platform, Sets.<String>newLinkedHashSet()); + } + + private List<EntitySpec<?>> buildTemplateServicesAsSpecsImpl(BrooklynClassLoadingContext loader, AssemblyTemplate template, CampPlatform platform, Set<String> encounteredCatalogTypes) { + List<EntitySpec<?>> result = Lists.newArrayList(); + + for (ResolvableLink<PlatformComponentTemplate> ctl: template.getPlatformComponentTemplates().links()) { + PlatformComponentTemplate appChildComponentTemplate = ctl.resolve(); + BrooklynComponentTemplateResolver entityResolver = BrooklynComponentTemplateResolver.Factory.newInstance(loader, appChildComponentTemplate); + EntitySpec<?> spec = resolveSpec(ResourceUtils.create(this), entityResolver, encounteredCatalogTypes); + + result.add(spec); + } + return result; + } + + static EntitySpec<?> resolveSpec(ResourceUtils ru, + BrooklynComponentTemplateResolver entityResolver, + Set<String> encounteredCatalogTypes) { + String brooklynType = entityResolver.getServiceTypeResolver().getBrooklynType(entityResolver.getDeclaredType()); + CatalogItem<Entity, EntitySpec<?>> item = entityResolver.getServiceTypeResolver().getCatalogItem(entityResolver, entityResolver.getDeclaredType()); + + if (log.isTraceEnabled()) log.trace("Building CAMP template services: type="+brooklynType+"; item="+item+"; loader="+entityResolver.getLoader()+"; encounteredCatalogTypes="+encounteredCatalogTypes); + + EntitySpec<?> spec = null; + String protocol = Urls.getProtocol(brooklynType); + if (protocol != null) { + if (BrooklynCampConstants.YAML_URL_PROTOCOL_WHITELIST.contains(protocol)) { + spec = tryResolveYamlUrlReferenceSpec(ru, brooklynType, entityResolver.getLoader(), encounteredCatalogTypes); + if (spec != null) { + entityResolver.populateSpec(spec); + } + } else { + // TODO support https above + // TODO this will probably be logged if we refer to chef:cookbook or other service types which BCTR accepts; + // better would be to have BCTR supporting the calls above + log.debug("The reference " + brooklynType + " looks like a URL (running the CAMP Brooklyn assembly-template instantiator) but the protocol " + + protocol + " isn't white listed (" + BrooklynCampConstants.YAML_URL_PROTOCOL_WHITELIST + "). " + + "Will try to load it as catalog item or java type."); + } + } + + if (spec == null) { + // load from java or yaml + spec = entityResolver.resolveSpec(encounteredCatalogTypes); + } + + return spec; + } + + private static EntitySpec<?> tryResolveYamlUrlReferenceSpec( + ResourceUtils ru, + String brooklynType, BrooklynClassLoadingContext itemLoader, + Set<String> encounteredCatalogTypes) { + ManagementContext mgmt = itemLoader.getManagementContext(); + Reader yaml; + try { + yaml = new InputStreamReader(ru.getResourceFromUrl(brooklynType), "UTF-8"); + } catch (Exception e) { + log.warn("AssemblyTemplate type " + brooklynType + " which looks like a URL can't be fetched.", e); + return null; + } + try { + return createNestedSpec(mgmt, encounteredCatalogTypes, yaml, itemLoader); + } finally { + try { + yaml.close(); + } catch (IOException e) { + throw Exceptions.propagate(e); + } + } + } + + static EntitySpec<?> resolveCatalogYamlReferenceSpec( + ManagementContext mgmt, + CatalogItem<Entity, EntitySpec<?>> item, + Set<String> encounteredCatalogTypes) { + + String yaml = item.getPlanYaml(); + Reader input = new StringReader(yaml); + BrooklynClassLoadingContext itemLoader = CatalogUtils.newClassLoadingContext(mgmt, item); + + return createNestedSpec(mgmt, encounteredCatalogTypes, input, itemLoader); + } + + private static EntitySpec<?> createNestedSpec(ManagementContext mgmt, + Set<String> encounteredCatalogTypes, Reader input, + BrooklynClassLoadingContext itemLoader) { + CampPlatform platform = BrooklynServerConfig.getCampPlatform(mgmt).get(); + + AssemblyTemplate at; + BrooklynLoaderTracker.setLoader(itemLoader); + try { + at = platform.pdp().registerDeploymentPlan(input); + } finally { + BrooklynLoaderTracker.unsetLoader(itemLoader); + } + return createNestedSpecStatic(at, platform, itemLoader, encounteredCatalogTypes); + } + + @Override + public EntitySpec<?> createNestedSpec( + AssemblyTemplate template, + CampPlatform platform, + BrooklynClassLoadingContext itemLoader, + Set<String> encounteredCatalogTypes) { + return createNestedSpecStatic(template, platform, itemLoader, encounteredCatalogTypes); + } + + private static EntitySpec<?> createNestedSpecStatic( + AssemblyTemplate template, + CampPlatform platform, + BrooklynClassLoadingContext itemLoader, + Set<String> encounteredCatalogTypes) { + // In case we want to allow multiple top-level entities in a catalog we need to think + // about what it would mean to subsequently call buildChildrenEntitySpecs on the list of top-level entities! + try { + AssemblyTemplateInstantiator ati = template.getInstantiator().newInstance(); + if (ati instanceof BrooklynAssemblyTemplateInstantiator) { + List<EntitySpec<?>> specs = ((BrooklynAssemblyTemplateInstantiator)ati).buildTemplateServicesAsSpecsImpl(itemLoader, template, platform, encounteredCatalogTypes); + if (specs.size() > 1) { + throw new UnsupportedOperationException("Only supporting single service in catalog item currently: got "+specs); + } + return specs.get(0); + } else { + throw new IllegalStateException("Cannot create application with instantiator: " + ati); + } + } catch (Exception e) { + throw Exceptions.propagate(e); + } + } + +}
