| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.softlayer.config; |
| 20 | |
| 21 | import java.lang.reflect.Type; |
| 22 | import java.util.Date; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import javax.inject.Singleton; |
| 26 | |
| 27 | import org.jclouds.json.config.GsonModule.DateAdapter; |
| 28 | import org.jclouds.json.config.GsonModule.Iso8601DateAdapter; |
| 29 | import org.jclouds.softlayer.domain.Datacenter; |
| 30 | import org.jclouds.softlayer.domain.OperatingSystem; |
| 31 | import org.jclouds.softlayer.domain.PowerState; |
| 32 | import org.jclouds.softlayer.domain.VirtualGuest; |
| 33 | |
| 34 | import com.google.common.collect.ImmutableMap; |
| 35 | import com.google.gson.JsonDeserializationContext; |
| 36 | import com.google.gson.JsonDeserializer; |
| 37 | import com.google.gson.JsonElement; |
| 38 | import com.google.gson.JsonParseException; |
| 39 | import com.google.gson.JsonSerializationContext; |
| 40 | import com.google.gson.JsonSerializer; |
| 41 | import com.google.inject.AbstractModule; |
| 42 | import com.google.inject.Provides; |
| 43 | |
| 44 | /** |
| 45 | * |
| 46 | * @author Adrian Cole |
| 47 | */ |
| 48 | public class SoftLayerParserModule extends AbstractModule { |
| 49 | |
| 50 | @Singleton |
| 51 | public static class VirtualGuestAdapter implements JsonSerializer<VirtualGuest>, JsonDeserializer<VirtualGuest> { |
| 52 | |
| 53 | public JsonElement serialize(VirtualGuest src, Type typeOfSrc, JsonSerializationContext context) { |
| 54 | return context.serialize(src); |
| 55 | } |
| 56 | |
| 57 | public VirtualGuest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) |
| 58 | throws JsonParseException { |
| 59 | return apply(context.<VirtualGuestInternal> deserialize(json, VirtualGuestInternal.class)); |
| 60 | } |
| 61 | |
| 62 | public VirtualGuest apply(VirtualGuestInternal in) { |
| 63 | return in; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Internal class that flattens billingItem into billingItemId |
| 68 | */ |
| 69 | public static class VirtualGuestInternal extends VirtualGuest { |
| 70 | private BillingItem billingItem; |
| 71 | |
| 72 | public VirtualGuestInternal(int accountId, Date createDate, boolean dedicatedAccountHostOnly, String domain, |
| 73 | String fullyQualifiedDomainName, String hostname, int id, Date lastVerifiedDate, int maxCpu, |
| 74 | String maxCpuUnits, int maxMemory, Date metricPollDate, Date modifyDate, String notes, |
| 75 | boolean privateNetworkOnly, int startCpus, int statusId, String uuid, String primaryBackendIpAddress, |
| 76 | String primaryIpAddress, int billingItemId, OperatingSystem operatingSystem, Datacenter datacenter, |
| 77 | PowerState powerState) { |
| 78 | super(accountId, createDate, dedicatedAccountHostOnly, domain, fullyQualifiedDomainName, hostname, id, |
| 79 | lastVerifiedDate, maxCpu, maxCpuUnits, maxMemory, metricPollDate, modifyDate, notes, |
| 80 | privateNetworkOnly, startCpus, statusId, uuid, primaryBackendIpAddress, primaryIpAddress, |
| 81 | billingItemId, operatingSystem, datacenter, powerState); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int getBillingItemId() { |
| 86 | return billingItem != null ? billingItem.id : -1; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public static class BillingItem { |
| 91 | |
| 92 | private int id = -1; |
| 93 | |
| 94 | // for deserializer |
| 95 | BillingItem() { |
| 96 | |
| 97 | } |
| 98 | |
| 99 | public BillingItem(int id) { |
| 100 | this.id = id; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String toString() { |
| 105 | return "[id=" + id + "]"; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | @Provides |
| 111 | @Singleton |
| 112 | public Map<Type, Object> provideCustomAdapterBindings() { |
| 113 | return ImmutableMap.<Type, Object> of(VirtualGuest.class, new VirtualGuestAdapter()); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | protected void configure() { |
| 118 | bind(DateAdapter.class).to(Iso8601DateAdapter.class); |
| 119 | } |
| 120 | |
| 121 | } |