added hardware profiles to deltacloud

This commit is contained in:
Adrian Cole 2010-12-28 11:35:47 +01:00
parent df878f75dc
commit 9b2f8138cf
16 changed files with 1270 additions and 0 deletions

View File

@ -0,0 +1,40 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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 {
}

View File

@ -0,0 +1,55 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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<Object> availableValues;
public EnumHardwareProperty(String name, String unit, Object value, HardwareParameter param,
Set<Object> availableValues) {
super(Kind.FIXED, name, unit, value, param);
this.availableValues = ImmutableSet.copyOf(checkNotNull(availableValues, "availableValues"));
}
/**
*
* @return a set of available values
*/
public Set<Object> getAvailableValues() {
return availableValues;
}
@Override
public String toString() {
return "[kind=" + getKind() + ", name=" + getName() + ", unit=" + getUnit() + ", value=" + getValue()
+ ", param=" + getParam() + ", availableValues=" + availableValues + "]";
}
}

View File

@ -0,0 +1,34 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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);
}
}

View File

@ -0,0 +1,123 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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 + "]";
}
}

View File

@ -0,0 +1,129 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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<? extends HardwareProperty> properties;
public HardwareProfile(URI href, String id, String name, Set<? extends HardwareProperty> 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<? extends HardwareProperty> 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 + "]";
}
}

View File

@ -0,0 +1,79 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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();
}

View File

@ -0,0 +1,77 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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() + "]";
}
}

View File

@ -0,0 +1,92 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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;
}
}

View File

@ -0,0 +1,120 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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 + "]";
}
}

View File

@ -0,0 +1,103 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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<HardwareProfile> {
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<HardwareProperty> 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<String, String> 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);
}
}

View File

@ -0,0 +1,69 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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<Set<? extends HardwareProfile>> {
private StringBuilder currentText = new StringBuilder();
private Set<HardwareProfile> hardwareProfiles = Sets.newLinkedHashSet();
private final HardwareProfileHandler hardwareProfileHandler;
@Inject
public HardwareProfilesHandler(HardwareProfileHandler locationHandler) {
this.hardwareProfileHandler = locationHandler;
}
public Set<? extends HardwareProfile> 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);
}
}

View File

@ -0,0 +1,121 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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<HardwareProperty> {
private Kind kind;
private String name;
private String unit;
private Object value;
private HardwareParameter param;
private Set<Object> 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<String, String> 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;
}
}

View File

@ -0,0 +1,80 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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<HardwareProfile> createParser() {
Injector injector = Guice.createInjector(new SaxParserModule());
ParseSax<HardwareProfile> parser = (ParseSax<HardwareProfile>) 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.<HardwareProperty> 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.<Object> of(new Long(1024), new Long(2048), new Long(4096))),
new FixedHardwareProperty("architecture", "label", "x86_64"))
);
assertEquals(parseHardwareProfile(), expects);
}
}

View File

@ -0,0 +1,84 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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<? extends HardwareProfile> expects = ImmutableSet.of(
new HardwareProfile(URI.create("http://localhost:3001/api/hardware_profiles/m1-small"), "m1-small",
"m1-small", ImmutableSet.<HardwareProperty> 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.<HardwareProperty> 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.<Object> 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.<HardwareProperty> 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.<Object> 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.<HardwareProperty> 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);
}
}

View File

@ -0,0 +1,18 @@
<?xml version='1.0' encoding='utf-8' ?>
<hardware_profile href='http://localhost:3001/api/hardware_profiles/m1-xlarge' id='m1-xlarge'>
<name>m1-xlarge</name>
<property kind='fixed' name='cpu' unit='count' value='4' />
<property kind='range' name='memory' unit='MB' value='12288'>
<param href='http://localhost:3001/api/instances' method='post' name='hwp_memory' operation='create' />
<range first='12288' last='32768' />
</property>
<property kind='enum' name='storage' unit='GB' value='1024'>
<param href='http://localhost:3001/api/instances' method='post' name='hwp_storage' operation='create' />
<enum>
<entry value='1024' />
<entry value='2048' />
<entry value='4096' />
</enum>
</property>
<property kind='fixed' name='architecture' unit='label' value='x86_64' />
</hardware_profile>

View File

@ -0,0 +1,46 @@
<?xml version='1.0' encoding='utf-8' ?>
<hardware_profiles>
<hardware_profile href='http://localhost:3001/api/hardware_profiles/m1-small' id='m1-small'>
<name>m1-small</name>
<property kind='fixed' name='cpu' unit='count' value='1' />
<property kind='fixed' name='memory' unit='MB' value='1740.8' />
<property kind='fixed' name='storage' unit='GB' value='160' />
<property kind='fixed' name='architecture' unit='label' value='i386' />
</hardware_profile>
<hardware_profile href='http://localhost:3001/api/hardware_profiles/m1-large' id='m1-large'>
<name>m1-large</name>
<property kind='fixed' name='cpu' unit='count' value='2' />
<property kind='range' name='memory' unit='MB' value='10240'>
<param href='http://localhost:3001/api/instances' method='post' name='hwp_memory' operation='create' />
<range first='7680.0' last='15360' />
</property>
<property kind='enum' name='storage' unit='GB' value='850'>
<param href='http://localhost:3001/api/instances' method='post' name='hwp_storage' operation='create' />
<enum>
<entry value='850' />
<entry value='1024' />
</enum>
</property>
<property kind='fixed' name='architecture' unit='label' value='x86_64' />
</hardware_profile>
<hardware_profile href='http://localhost:3001/api/hardware_profiles/m1-xlarge' id='m1-xlarge'>
<name>m1-xlarge</name>
<property kind='fixed' name='cpu' unit='count' value='4' />
<property kind='range' name='memory' unit='MB' value='12288'>
<param href='http://localhost:3001/api/instances' method='post' name='hwp_memory' operation='create' />
<range first='12288' last='32768' />
</property>
<property kind='enum' name='storage' unit='GB' value='1024'>
<param href='http://localhost:3001/api/instances' method='post' name='hwp_storage' operation='create' />
<enum>
<entry value='1024' />
<entry value='2048' />
<entry value='4096' />
</enum>
</property>
<property kind='fixed' name='architecture' unit='label' value='x86_64' />
</hardware_profile>
<hardware_profile href='http://localhost:3001/api/hardware_profiles/opaque' id='opaque'>
<name>opaque</name>
</hardware_profile>
</hardware_profiles>