From 9b2f8138cf6d079cc58c6f8c6979a5e35d35be15 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 28 Dec 2010 11:35:47 +0100 Subject: [PATCH] added hardware profiles to deltacloud --- .../collections/HardwareProfiles.java | 40 ++++++ .../domain/EnumHardwareProperty.java | 55 ++++++++ .../domain/FixedHardwareProperty.java | 34 +++++ .../deltacloud/domain/HardwareParameter.java | 123 +++++++++++++++++ .../deltacloud/domain/HardwareProfile.java | 129 ++++++++++++++++++ .../deltacloud/domain/HardwareProperty.java | 79 +++++++++++ .../domain/ParameterizedHardwareProperty.java | 77 +++++++++++ .../domain/RangeHardwareProperty.java | 92 +++++++++++++ .../domain/internal/BaseHardwareProperty.java | 120 ++++++++++++++++ .../xml/HardwareProfileHandler.java | 103 ++++++++++++++ .../xml/HardwareProfilesHandler.java | 69 ++++++++++ .../xml/HardwarePropertyHandler.java | 121 ++++++++++++++++ .../xml/HardwareProfileHandlerTest.java | 80 +++++++++++ .../xml/HardwareProfilesHandlerTest.java | 84 ++++++++++++ .../resources/test_get_hardware_profile.xml | 18 +++ .../resources/test_list_hardware_profiles.xml | 46 +++++++ 16 files changed, 1270 insertions(+) create mode 100644 sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/collections/HardwareProfiles.java create mode 100644 sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/EnumHardwareProperty.java create mode 100644 sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/FixedHardwareProperty.java create mode 100644 sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/HardwareParameter.java create mode 100644 sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/HardwareProfile.java create mode 100644 sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/HardwareProperty.java create mode 100644 sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/ParameterizedHardwareProperty.java create mode 100644 sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/RangeHardwareProperty.java create mode 100644 sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/internal/BaseHardwareProperty.java create mode 100644 sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/xml/HardwareProfileHandler.java create mode 100644 sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/xml/HardwareProfilesHandler.java create mode 100644 sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/xml/HardwarePropertyHandler.java create mode 100644 sandbox/deltacloud/src/test/java/org/jclouds/deltacloud/xml/HardwareProfileHandlerTest.java create mode 100644 sandbox/deltacloud/src/test/java/org/jclouds/deltacloud/xml/HardwareProfilesHandlerTest.java create mode 100644 sandbox/deltacloud/src/test/resources/test_get_hardware_profile.xml create mode 100644 sandbox/deltacloud/src/test/resources/test_list_hardware_profiles.xml diff --git a/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/collections/HardwareProfiles.java b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/collections/HardwareProfiles.java new file mode 100644 index 0000000000..0ad67533e7 --- /dev/null +++ b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/collections/HardwareProfiles.java @@ -0,0 +1,40 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.collections; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +/** + * The images collection will return a set of all hardware profiles available to the current user. + * + * @author Adrian Cole + * + */ +@Retention(value = RetentionPolicy.RUNTIME) +@Target(value = { ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD }) +@Qualifier +public @interface HardwareProfiles { + +} \ No newline at end of file diff --git a/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/EnumHardwareProperty.java b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/EnumHardwareProperty.java new file mode 100644 index 0000000000..a9e7c6c0b2 --- /dev/null +++ b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/EnumHardwareProperty.java @@ -0,0 +1,55 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Set; + +import com.google.common.collect.ImmutableSet; + +/** + * + * @author Adrian Cole + */ +public class EnumHardwareProperty extends ParameterizedHardwareProperty { + + private final Set availableValues; + + public EnumHardwareProperty(String name, String unit, Object value, HardwareParameter param, + Set availableValues) { + super(Kind.FIXED, name, unit, value, param); + this.availableValues = ImmutableSet.copyOf(checkNotNull(availableValues, "availableValues")); + } + + /** + * + * @return a set of available values + */ + public Set getAvailableValues() { + return availableValues; + } + + @Override + public String toString() { + return "[kind=" + getKind() + ", name=" + getName() + ", unit=" + getUnit() + ", value=" + getValue() + + ", param=" + getParam() + ", availableValues=" + availableValues + "]"; + } +} diff --git a/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/FixedHardwareProperty.java b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/FixedHardwareProperty.java new file mode 100644 index 0000000000..03485c9932 --- /dev/null +++ b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/FixedHardwareProperty.java @@ -0,0 +1,34 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.domain; + +import org.jclouds.deltacloud.domain.internal.BaseHardwareProperty; + +/** + * + * @author Adrian Cole + */ +public class FixedHardwareProperty extends BaseHardwareProperty { + + public FixedHardwareProperty(String name, String unit, Object value) { + super(Kind.FIXED, name, unit, value); + } + +} diff --git a/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/HardwareParameter.java b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/HardwareParameter.java new file mode 100644 index 0000000000..24a4516d5e --- /dev/null +++ b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/HardwareParameter.java @@ -0,0 +1,123 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.net.URI; + +/** + * A parameter corresponding to a hardware option. + * + * @author Adrian Cole + */ +public class HardwareParameter { + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((href == null) ? 0 : href.hashCode()); + result = prime * result + ((method == null) ? 0 : method.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((operation == null) ? 0 : operation.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + HardwareParameter other = (HardwareParameter) obj; + if (href == null) { + if (other.href != null) + return false; + } else if (!href.equals(other.href)) + return false; + if (method == null) { + if (other.method != null) + return false; + } else if (!method.equals(other.method)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (operation == null) { + if (other.operation != null) + return false; + } else if (!operation.equals(other.operation)) + return false; + return true; + } + + private final URI href; + private final String method; + private final String name; + private final String operation; + + public HardwareParameter(URI href, String method, String name, String operation) { + this.href = checkNotNull(href, "href"); + this.method = checkNotNull(method, "method"); + this.name = checkNotNull(name, "name"); + this.operation = checkNotNull(operation, "operation"); + } + + /** + * + * @return URI of the action this applies to + */ + public URI getHref() { + return href; + } + + /** + * + * @return HTTP method of the action this applies to + */ + public String getMethod() { + return method; + } + + /** + * + * @return name of the HTTP request parameter related to this + */ + public String getName() { + return name; + } + + /** + * + * @return name of the action this applies to + */ + public String getOperation() { + return operation; + } + + @Override + public String toString() { + return "[href=" + href + ", method=" + method + ", name=" + name + ", operation=" + operation + "]"; + } +} diff --git a/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/HardwareProfile.java b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/HardwareProfile.java new file mode 100644 index 0000000000..23cf9798cc --- /dev/null +++ b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/HardwareProfile.java @@ -0,0 +1,129 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.net.URI; +import java.util.Set; + +import com.google.common.collect.ImmutableSet; + +/** + * A hardware profile represents a configuration of resources upon which a machine may be deployed. + * It defines aspects such as local disk storage, available RAM, and architecture. Each provider is + * free to define as many (or as few) hardware profiles as desired. + * + * @author Adrian Cole + */ +public class HardwareProfile { + private final URI href; + private final String id; + private final String name; + private final Set properties; + + public HardwareProfile(URI href, String id, String name, Set properties) { + this.href = checkNotNull(href, "href"); + this.id = checkNotNull(id, "id"); + this.name = checkNotNull(name, "name"); + this.properties = ImmutableSet.copyOf(checkNotNull(properties, "properties")); + } + + /** + * + * @return URL to manipulate a specific profile + */ + public URI getHref() { + return href; + } + + /** + * + * @return A unique identifier for the profile + */ + public String getId() { + return id; + } + + /** + * + * @return A short label + */ + public String getName() { + return name; + } + + /** + * + * @return properties included in this hardware profile + */ + public Set getProperties() { + return properties; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((href == null) ? 0 : href.hashCode()); + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((properties == null) ? 0 : properties.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + HardwareProfile other = (HardwareProfile) obj; + if (href == null) { + if (other.href != null) + return false; + } else if (!href.equals(other.href)) + return false; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (properties == null) { + if (other.properties != null) + return false; + } else if (!properties.equals(other.properties)) + return false; + return true; + } + + @Override + public String toString() { + return "[href=" + href + ", id=" + id + ", name=" + name + ", properties=" + properties + "]"; + } + +} diff --git a/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/HardwareProperty.java b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/HardwareProperty.java new file mode 100644 index 0000000000..b5aae40959 --- /dev/null +++ b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/HardwareProperty.java @@ -0,0 +1,79 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * + * @author Adrian Cole + */ +public interface HardwareProperty { + public static enum Kind { + /** + * only the value specified in the property is available + */ + FIXED, + /** + * a list of available values is provided + */ + ENUM, + /** + * available values are described by a numeric range + */ + RANGE, + /** + * type returned as something besides the above. + */ + UNRECOGNIZED; + + public static Kind fromValue(String kind) { + try { + return valueOf(checkNotNull(kind, "kind").toUpperCase()); + } catch (IllegalArgumentException e) { + return UNRECOGNIZED; + } + } + } + + /** + * + * @return describes the values to chose from. + */ + Kind getKind(); + + /** + * + * @return the type of the property: e.g. memory or storage + */ + String getName(); + + /** + * + * @return the units in which the value is specified: MB, GB, count or label + */ + String getUnit(); + + /** + * + * @return the actual value of the property. It depends on the specified unit: 1024, 2 on x86_64 + */ + Object getValue(); +} diff --git a/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/ParameterizedHardwareProperty.java b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/ParameterizedHardwareProperty.java new file mode 100644 index 0000000000..0a084bb270 --- /dev/null +++ b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/ParameterizedHardwareProperty.java @@ -0,0 +1,77 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +import org.jclouds.deltacloud.domain.internal.BaseHardwareProperty; + +/** + * + * @author Adrian Cole + */ +public class ParameterizedHardwareProperty extends BaseHardwareProperty { + + private final HardwareParameter param; + + public ParameterizedHardwareProperty(Kind kind, String name, String unit, Object value, HardwareParameter param) { + super(kind, name, unit, value); + this.param = checkNotNull(param, "param"); + } + + /** + * + * @return how to associate a non-default value with a request against an instance. + */ + public HardwareParameter getParam() { + return param; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((param == null) ? 0 : param.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + ParameterizedHardwareProperty other = (ParameterizedHardwareProperty) obj; + if (param == null) { + if (other.param != null) + return false; + } else if (!param.equals(other.param)) + return false; + return true; + } + + @Override + public String toString() { + return "[kind=" + getKind() + ", name=" + getName() + ", unit=" + getUnit() + ", value=" + getValue() + + ", param=" + getParam() + "]"; + } +} diff --git a/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/RangeHardwareProperty.java b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/RangeHardwareProperty.java new file mode 100644 index 0000000000..1fbf12abda --- /dev/null +++ b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/RangeHardwareProperty.java @@ -0,0 +1,92 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * + * @author Adrian Cole + */ +public class RangeHardwareProperty extends ParameterizedHardwareProperty { + + private final Number first; + private final Number last; + + public RangeHardwareProperty(String name, String unit, Number value, HardwareParameter param, Number first, + Number last) { + super(Kind.FIXED, name, unit, value, param); + this.first = checkNotNull(first, "first"); + this.last = checkNotNull(last, "last"); + } + + /** + * + * @return minimum value + */ + public Number getFirst() { + return first; + } + + /** + * + * @return maximum value + */ + public Number getLast() { + return last; + } + + @Override + public String toString() { + return "[kind=" + getKind() + ", name=" + getName() + ", unit=" + getUnit() + ", value=" + getValue() + + ", param=" + getParam() + ", first=" + first + ", last=" + last + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((first == null) ? 0 : first.hashCode()); + result = prime * result + ((last == null) ? 0 : last.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + RangeHardwareProperty other = (RangeHardwareProperty) obj; + if (first == null) { + if (other.first != null) + return false; + } else if (!first.equals(other.first)) + return false; + if (last == null) { + if (other.last != null) + return false; + } else if (!last.equals(other.last)) + return false; + return true; + } +} diff --git a/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/internal/BaseHardwareProperty.java b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/internal/BaseHardwareProperty.java new file mode 100644 index 0000000000..ac66972670 --- /dev/null +++ b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/domain/internal/BaseHardwareProperty.java @@ -0,0 +1,120 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.domain.internal; + +import static com.google.common.base.Preconditions.checkNotNull; + +import org.jclouds.deltacloud.domain.HardwareProperty; + +/** + * + * @author Adrian Cole + */ +public class BaseHardwareProperty implements HardwareProperty { + private final Kind kind; + private final String name; + private final String unit; + private final Object value; + + public BaseHardwareProperty(Kind kind, String name, String unit, Object value) { + this.kind = checkNotNull(kind, "kind"); + this.name = checkNotNull(name, "name"); + this.unit = checkNotNull(unit, "unit"); + this.value = checkNotNull(value, "value"); + } + + /** + * {@inheritDoc} + */ + @Override + public Kind getKind() { + return kind; + } + + /** + * {@inheritDoc} + */ + @Override + public String getName() { + return name; + } + + /** + * {@inheritDoc} + */ + @Override + public String getUnit() { + return unit; + } + + /** + * {@inheritDoc} + */ + @Override + public Object getValue() { + return value; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((kind == null) ? 0 : kind.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((unit == null) ? 0 : unit.hashCode()); + result = prime * result + ((value == null) ? 0 : value.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BaseHardwareProperty other = (BaseHardwareProperty) obj; + if (kind != other.kind) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (unit == null) { + if (other.unit != null) + return false; + } else if (!unit.equals(other.unit)) + return false; + if (value == null) { + if (other.value != null) + return false; + } else if (!value.equals(other.value)) + return false; + return true; + } + + @Override + public String toString() { + return "[kind=" + kind + ", name=" + name + ", unit=" + unit + ", value=" + value + "]"; + } + +} diff --git a/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/xml/HardwareProfileHandler.java b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/xml/HardwareProfileHandler.java new file mode 100644 index 0000000000..cb582384f5 --- /dev/null +++ b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/xml/HardwareProfileHandler.java @@ -0,0 +1,103 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.xml; + +import java.net.URI; +import java.util.Map; +import java.util.Set; + +import javax.inject.Inject; + +import org.jclouds.deltacloud.domain.HardwareProfile; +import org.jclouds.deltacloud.domain.HardwareProperty; +import org.jclouds.http.functions.ParseSax; +import org.jclouds.util.Utils; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +import com.google.common.collect.Sets; + +/** + * @author Adrian Cole + */ +public class HardwareProfileHandler extends ParseSax.HandlerWithResult { + private StringBuilder currentText = new StringBuilder(); + private final HardwarePropertyHandler propertyHandler; + + @Inject + HardwareProfileHandler(HardwarePropertyHandler propertyHandler) { + this.propertyHandler = propertyHandler; + } + + private URI href; + private String id; + private String name; + private Set properties = Sets.newLinkedHashSet(); + private boolean inProperty; + + private HardwareProfile profile; + + public HardwareProfile getResult() { + return profile; + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = Utils.cleanseAttributes(attrs); + if (qName.equals("property")) { + inProperty = true; + } + + if (inProperty) { + propertyHandler.startElement(uri, localName, qName, attrs); + } else if (qName.equals("hardware_profile")) { + String href = attributes.get("href"); + if (href != null) { + this.href = URI.create(href); + } + this.id = attributes.get("id"); + } + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + + if (inProperty) + propertyHandler.endElement(uri, localName, qName); + + if (qName.endsWith("property")) { + inProperty = false; + this.properties.add(propertyHandler.getResult()); + } else if (qName.equalsIgnoreCase("name")) { + this.name = currentText.toString().trim(); + } else if (qName.equalsIgnoreCase("hardware_profile")) { + this.profile = new HardwareProfile(href, id, name, properties); + this.href = null; + this.id = null; + this.name = null; + this.properties = Sets.newLinkedHashSet(); + } + currentText = new StringBuilder(); + } + + public void characters(char ch[], int start, int length) { + currentText.append(ch, start, length); + } +} diff --git a/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/xml/HardwareProfilesHandler.java b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/xml/HardwareProfilesHandler.java new file mode 100644 index 0000000000..fc06357e58 --- /dev/null +++ b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/xml/HardwareProfilesHandler.java @@ -0,0 +1,69 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.xml; + +import java.util.Set; + +import javax.inject.Inject; + +import org.jclouds.deltacloud.domain.HardwareProfile; +import org.jclouds.http.functions.ParseSax; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +import com.google.common.collect.Sets; + +/** + * @author Adrian Cole + */ +public class HardwareProfilesHandler extends ParseSax.HandlerWithResult> { + private StringBuilder currentText = new StringBuilder(); + + private Set hardwareProfiles = Sets.newLinkedHashSet(); + private final HardwareProfileHandler hardwareProfileHandler; + + @Inject + public HardwareProfilesHandler(HardwareProfileHandler locationHandler) { + this.hardwareProfileHandler = locationHandler; + } + + public Set getResult() { + return hardwareProfiles; + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + hardwareProfileHandler.startElement(uri, localName, qName, attributes); + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + hardwareProfileHandler.endElement(uri, localName, qName); + if (qName.equals("hardware_profile") && currentText.toString().trim().equals("")) { + this.hardwareProfiles.add(hardwareProfileHandler.getResult()); + } + currentText = new StringBuilder(); + } + + public void characters(char ch[], int start, int length) { + hardwareProfileHandler.characters(ch, start, length); + currentText.append(ch, start, length); + } +} diff --git a/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/xml/HardwarePropertyHandler.java b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/xml/HardwarePropertyHandler.java new file mode 100644 index 0000000000..efe9993544 --- /dev/null +++ b/sandbox/deltacloud/src/main/java/org/jclouds/deltacloud/xml/HardwarePropertyHandler.java @@ -0,0 +1,121 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.links/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.jclouds.deltacloud.xml; + +import java.net.URI; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; + +import javax.annotation.Nullable; + +import org.jclouds.deltacloud.domain.EnumHardwareProperty; +import org.jclouds.deltacloud.domain.FixedHardwareProperty; +import org.jclouds.deltacloud.domain.HardwareParameter; +import org.jclouds.deltacloud.domain.HardwareProperty; +import org.jclouds.deltacloud.domain.HardwareProperty.Kind; +import org.jclouds.deltacloud.domain.RangeHardwareProperty; +import org.jclouds.http.functions.ParseSax; +import org.jclouds.util.Utils; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +import com.google.common.collect.Sets; + +/** + * @author Adrian Cole + */ +public class HardwarePropertyHandler extends ParseSax.HandlerWithResult { + private Kind kind; + private String name; + private String unit; + private Object value; + private HardwareParameter param; + private Set availableValues = Sets.newLinkedHashSet(); + private Number first; + private Number last; + + /** + * resets state of the handler when called. + * + * @return property or null + */ + public HardwareProperty getResult() { + try { + switch (kind) { + case FIXED: + return new FixedHardwareProperty(name, unit, value); + case ENUM: + return new EnumHardwareProperty(name, unit, value, param, availableValues); + case RANGE: + return new RangeHardwareProperty(name, unit, (Number) value, param, first, last); + default: + return null; + } + } finally { + this.kind = null; + this.name = null; + this.unit = null; + this.value = null; + this.param = null; + this.availableValues = Sets.newLinkedHashSet(); + this.first = null; + this.last = null; + } + } + + private static final Pattern LONG = Pattern.compile("^[0-9]+$"); + private static final Pattern DOUBLE = Pattern.compile("^[0-9]+\\.[0-9]+$"); + + public static @Nullable + Number parseNumberOrNull(String in) { + if (DOUBLE.matcher(in).matches()) + return new Double(in); + else if (LONG.matcher(in).matches()) + return new Long(in); + return null; + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { + Map attributes = Utils.cleanseAttributes(attrs); + if (qName.equals("property")) { + this.kind = Kind.fromValue(attributes.get("kind")); + this.name = attributes.get("name"); + this.unit = attributes.get("unit"); + if (attributes.containsKey("value")) { + this.value = stringOrNumber(attributes.get("value")); + } + } else if (qName.equals("param")) { + this.param = new HardwareParameter(URI.create(attributes.get("href")), attributes.get("method"), + attributes.get("name"), attributes.get("operation")); + } else if (qName.equals("range")) { + this.first = parseNumberOrNull(attributes.get("first")); + this.last = parseNumberOrNull(attributes.get("last")); + } else if (qName.equals("entry")) { + this.availableValues.add(stringOrNumber(attributes.get("value"))); + } + } + + public static Object stringOrNumber(String in) { + Number number = parseNumberOrNull(in); + return number != null ? number : in; + } +} diff --git a/sandbox/deltacloud/src/test/java/org/jclouds/deltacloud/xml/HardwareProfileHandlerTest.java b/sandbox/deltacloud/src/test/java/org/jclouds/deltacloud/xml/HardwareProfileHandlerTest.java new file mode 100644 index 0000000000..15323dc44f --- /dev/null +++ b/sandbox/deltacloud/src/test/java/org/jclouds/deltacloud/xml/HardwareProfileHandlerTest.java @@ -0,0 +1,80 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.xml; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.net.URI; + +import org.jclouds.deltacloud.domain.EnumHardwareProperty; +import org.jclouds.deltacloud.domain.FixedHardwareProperty; +import org.jclouds.deltacloud.domain.HardwareParameter; +import org.jclouds.deltacloud.domain.HardwareProfile; +import org.jclouds.deltacloud.domain.HardwareProperty; +import org.jclouds.deltacloud.domain.RangeHardwareProperty; +import org.jclouds.http.functions.ParseSax; +import org.jclouds.http.functions.config.SaxParserModule; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; +import com.google.inject.Guice; +import com.google.inject.Injector; + +/** + * Tests behavior of {@code HardwareProfileHandler} + * + * @author Adrian Cole + */ +@Test(groups = "unit") +public class HardwareProfileHandlerTest { + + static ParseSax createParser() { + Injector injector = Guice.createInjector(new SaxParserModule()); + ParseSax parser = (ParseSax) injector.getInstance(ParseSax.Factory.class) + .create(injector.getInstance(HardwareProfileHandler.class)); + return parser; + } + + public static HardwareProfile parseHardwareProfile() { + return parseHardwareProfile("/test_get_hardware_profile.xml"); + } + + public static HardwareProfile parseHardwareProfile(String resource) { + InputStream is = HardwareProfileHandlerTest.class.getResourceAsStream(resource); + return createParser().parse(is); + } + + public void test() { + HardwareProfile expects = new HardwareProfile( + URI.create("http://localhost:3001/api/hardware_profiles/m1-xlarge"), "m1-xlarge", "m1-xlarge", + ImmutableSet. of( + new FixedHardwareProperty("cpu", "count", new Long(4)), + new RangeHardwareProperty("memory", "MB", new Long(12288), new HardwareParameter(URI + .create("http://localhost:3001/api/instances"), "post", "hwp_memory", "create"), + new Long(12288), new Long(32768)), + new EnumHardwareProperty("storage", "GB", new Long(1024), new HardwareParameter(URI + .create("http://localhost:3001/api/instances"), "post", "hwp_storage", "create"), + ImmutableSet. of(new Long(1024), new Long(2048), new Long(4096))), + new FixedHardwareProperty("architecture", "label", "x86_64")) + ); + assertEquals(parseHardwareProfile(), expects); + } +} diff --git a/sandbox/deltacloud/src/test/java/org/jclouds/deltacloud/xml/HardwareProfilesHandlerTest.java b/sandbox/deltacloud/src/test/java/org/jclouds/deltacloud/xml/HardwareProfilesHandlerTest.java new file mode 100644 index 0000000000..b2b0638e99 --- /dev/null +++ b/sandbox/deltacloud/src/test/java/org/jclouds/deltacloud/xml/HardwareProfilesHandlerTest.java @@ -0,0 +1,84 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed 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.jclouds.deltacloud.xml; + +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.net.URI; +import java.util.Set; + +import org.jclouds.deltacloud.domain.EnumHardwareProperty; +import org.jclouds.deltacloud.domain.FixedHardwareProperty; +import org.jclouds.deltacloud.domain.HardwareParameter; +import org.jclouds.deltacloud.domain.HardwareProfile; +import org.jclouds.deltacloud.domain.HardwareProperty; +import org.jclouds.deltacloud.domain.RangeHardwareProperty; +import org.jclouds.http.functions.BaseHandlerTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableSet; + +/** + * Tests behavior of {@code HardwareProfilesHandler} + * + * @author Adrian Cole + */ +@Test(groups = "unit") +public class HardwareProfilesHandlerTest extends BaseHandlerTest { + + @Test + public void test() { + InputStream is = getClass().getResourceAsStream("/test_list_hardware_profiles.xml"); + Set expects = ImmutableSet.of( + new HardwareProfile(URI.create("http://localhost:3001/api/hardware_profiles/m1-small"), "m1-small", + "m1-small", ImmutableSet. of( + new FixedHardwareProperty("cpu", "count", new Long(1)), new FixedHardwareProperty("memory", + "MB", new Double(1740.8)), new FixedHardwareProperty("storage", "GB", new Long(160)), + new FixedHardwareProperty("architecture", "label", "i386"))), + new HardwareProfile(URI.create("http://localhost:3001/api/hardware_profiles/m1-large"), "m1-large", + "m1-large", ImmutableSet. of( + new FixedHardwareProperty("cpu", "count", new Long(2)), + new RangeHardwareProperty("memory", "MB", new Long(10240), new HardwareParameter(URI + .create("http://localhost:3001/api/instances"), "post", "hwp_memory", "create"), + new Double(7680.0), new Long(15360)), new EnumHardwareProperty("storage", "GB", new Long( + 850), new HardwareParameter(URI.create("http://localhost:3001/api/instances"), "post", + "hwp_storage", "create"), ImmutableSet. of(new Long(850), new Long(1024))), + new FixedHardwareProperty("architecture", "label", "x86_64"))), + new HardwareProfile(URI.create("http://localhost:3001/api/hardware_profiles/m1-xlarge"), "m1-xlarge", + "m1-xlarge", ImmutableSet. of( + new FixedHardwareProperty("cpu", "count", new Long(4)), + new RangeHardwareProperty("memory", "MB", new Long(12288), new HardwareParameter(URI + .create("http://localhost:3001/api/instances"), "post", "hwp_memory", "create"), + new Long(12288), new Long(32768)), + new EnumHardwareProperty("storage", "GB", new Long(1024), new HardwareParameter(URI + .create("http://localhost:3001/api/instances"), "post", "hwp_storage", "create"), + ImmutableSet. of(new Long(1024), new Long(2048), new Long(4096))), + new FixedHardwareProperty("architecture", "label", "x86_64"))), + new HardwareProfile(URI.create("http://localhost:3001/api/hardware_profiles/opaque"), "opaque", "opaque", + ImmutableSet. of())); + System.out.println(factory); + System.out.println(injector); + + // not sure why this isn't always automatically called from surefire. + setUpInjector(); + assertEquals(factory.create(injector.getInstance(HardwareProfilesHandler.class)).parse(is), expects); + } +} diff --git a/sandbox/deltacloud/src/test/resources/test_get_hardware_profile.xml b/sandbox/deltacloud/src/test/resources/test_get_hardware_profile.xml new file mode 100644 index 0000000000..5797cdae4f --- /dev/null +++ b/sandbox/deltacloud/src/test/resources/test_get_hardware_profile.xml @@ -0,0 +1,18 @@ + + + m1-xlarge + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sandbox/deltacloud/src/test/resources/test_list_hardware_profiles.xml b/sandbox/deltacloud/src/test/resources/test_list_hardware_profiles.xml new file mode 100644 index 0000000000..2028833898 --- /dev/null +++ b/sandbox/deltacloud/src/test/resources/test_list_hardware_profiles.xml @@ -0,0 +1,46 @@ + + + + m1-small + + + + + + + m1-large + + + + + + + + + + + + + + + + m1-xlarge + + + + + + + + + + + + + + + + + opaque + + \ No newline at end of file