All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.brooklyn.camp.spi.pdp.DeploymentPlan Maven / Gradle / Ivy

The newest version!
/*
 * 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.spi.pdp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.brooklyn.util.collections.MutableList;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.exceptions.UserFacingException;
import org.apache.brooklyn.util.guava.Maybe;
import org.apache.brooklyn.util.javalang.JavaClassNames;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class DeploymentPlan {

    String name;
    String origin;
    String description;
    String sourceCode;
    
    List artifacts;
    List services;
    Map customAttributes;

    @SuppressWarnings("unchecked")
    public static DeploymentPlan of(Map root, String optionalSourceCode) {
        Map attrs = MutableMap.copyOf(root);
        
        DeploymentPlan result = new DeploymentPlan();
        result.name = (String) attrs.remove("name");
        result.description = (String) attrs.remove("description");
        result.origin = (String) attrs.remove("origin");
        result.sourceCode = optionalSourceCode;
        // TODO version
        
        result.services = new ArrayList();
        Object services = attrs.remove("services");
        if (services instanceof Iterable) {
            for (Object service: (Iterable)services) {
                if (service instanceof Map) {
                    result.services.add(Service.of((Map) service));
                } else {
                    throw new UserFacingException("Services list should have a map for each entry, not "+JavaClassNames.superSimpleClassName(service));
                }
            }
        } else if (services!=null) {
            // TODO "map" short form
            throw new UserFacingException("Services block should be a list, not "+JavaClassNames.superSimpleClassName(services));
        }
        
        result.artifacts = new ArrayList();
        Object artifacts = attrs.remove("artifacts");
        if (artifacts instanceof Iterable) {
            for (Object artifact: (Iterable)artifacts) {
                if (artifact instanceof Map) {
                    result.artifacts.add(Artifact.of((Map) artifact));
                } else {
                    throw new UserFacingException("Artifacts list should contain map items, not "+JavaClassNames.superSimpleClassName(artifact));
                }
            }
        } else if (artifacts!=null) {
            // TODO "map" short form
            throw new UserFacingException("Artifacts block should contain a list, not "+JavaClassNames.superSimpleClassName(artifacts));
        }
        
        result.customAttributes = attrs;
        
        return result;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public String getOrigin() {
        return origin;
    }

    public String getSourceCode() {
        return sourceCode;
    }
    
    public List getArtifacts() {
        return MutableList.copyOf(artifacts).asUnmodifiable();
    }

    public List getServices() {
        return MutableList.copyOf(services).asUnmodifiable();
    }

    public Map getCustomAttributes() {
        return MutableMap.copyOf(customAttributes).asUnmodifiable();
    }

    /**
     * Returns a present {@link Maybe} of the custom attribute with the given name if the attribute is
     * non-null and is an instance of the given type. Otherwise returns absent.
     * 

* Does not remove the attribute from the custom attribute map. */ @SuppressWarnings("unchecked") public Maybe getCustomAttribute(String attributeName, Class type, boolean throwIfTypeMismatch) { Object attribute = customAttributes.get(attributeName); if (attribute == null) { return Maybe.absent("Custom attributes does not contain " + attributeName); } else if (!type.isAssignableFrom(attribute.getClass())) { String message = "Custom attribute " + attributeName + " is not of expected type: " + "expected=" + type.getName() + " actual=" + attribute.getClass().getName(); if (throwIfTypeMismatch) { throw new IllegalArgumentException(message); } return Maybe.absent(message); } else { return Maybe.of((T) attribute); } } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } }