| 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.vcloud.xml; |
| 20 | |
| 21 | import static org.jclouds.vcloud.util.Utils.newReferenceType; |
| 22 | import static org.jclouds.vcloud.util.Utils.putReferenceType; |
| 23 | |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | |
| 29 | import org.jclouds.http.functions.ParseSax; |
| 30 | import org.jclouds.util.SaxUtils; |
| 31 | import org.jclouds.vcloud.domain.AllocationModel; |
| 32 | import org.jclouds.vcloud.domain.Capacity; |
| 33 | import org.jclouds.vcloud.domain.ReferenceType; |
| 34 | import org.jclouds.vcloud.domain.Task; |
| 35 | import org.jclouds.vcloud.domain.VDC; |
| 36 | import org.jclouds.vcloud.domain.VDCStatus; |
| 37 | import org.jclouds.vcloud.domain.internal.VDCImpl; |
| 38 | import org.xml.sax.Attributes; |
| 39 | import org.xml.sax.SAXException; |
| 40 | |
| 41 | import com.google.common.collect.Lists; |
| 42 | import com.google.common.collect.Maps; |
| 43 | |
| 44 | /** |
| 45 | * @author Adrian Cole |
| 46 | */ |
| 47 | public class VDCHandler extends ParseSax.HandlerWithResult<VDC> { |
| 48 | |
| 49 | protected final TaskHandler taskHandler; |
| 50 | |
| 51 | @Inject |
| 52 | public VDCHandler(TaskHandler taskHandler) { |
| 53 | this.taskHandler = taskHandler; |
| 54 | } |
| 55 | |
| 56 | protected StringBuilder currentText = new StringBuilder(); |
| 57 | |
| 58 | protected ReferenceType vDC; |
| 59 | protected VDCStatus status = VDCStatus.READY; |
| 60 | protected ReferenceType org; |
| 61 | protected String description; |
| 62 | protected List<Task> tasks = Lists.newArrayList(); |
| 63 | protected AllocationModel allocationModel = AllocationModel.UNRECOGNIZED; |
| 64 | |
| 65 | protected Capacity storageCapacity; |
| 66 | protected Capacity cpuCapacity; |
| 67 | protected Capacity memoryCapacity; |
| 68 | |
| 69 | protected String units; |
| 70 | protected long allocated = 0; |
| 71 | protected long limit = 0; |
| 72 | protected int used = 0; |
| 73 | protected long overhead = 0; |
| 74 | |
| 75 | protected Map<String, ReferenceType> resourceEntities = Maps.newLinkedHashMap(); |
| 76 | protected Map<String, ReferenceType> availableNetworks = Maps.newLinkedHashMap(); |
| 77 | |
| 78 | protected int nicQuota; |
| 79 | protected int networkQuota; |
| 80 | protected int vmQuota; |
| 81 | protected boolean isEnabled = true; |
| 82 | |
| 83 | public VDC getResult() { |
| 84 | return new VDCImpl(vDC.getName(), vDC.getType(), vDC.getHref(), status, org, description, tasks, allocationModel, |
| 85 | storageCapacity, cpuCapacity, memoryCapacity, resourceEntities, availableNetworks, nicQuota, |
| 86 | networkQuota, vmQuota, isEnabled); |
| 87 | } |
| 88 | |
| 89 | void resetCapacity() { |
| 90 | units = null; |
| 91 | allocated = 0; |
| 92 | limit = 0; |
| 93 | used = 0; |
| 94 | overhead = 0; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 99 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 100 | if (qName.endsWith("Vdc")) { |
| 101 | vDC = newReferenceType(attributes); |
| 102 | String status = attributes.get("status"); |
| 103 | if (status != null) |
| 104 | this.status = VDCStatus.fromValue(Integer.parseInt(status)); |
| 105 | } else if (qName.endsWith("Network")) { |
| 106 | putReferenceType(availableNetworks, attributes); |
| 107 | } else if (qName.endsWith("ResourceEntity")) { |
| 108 | putReferenceType(resourceEntities, attributes); |
| 109 | } else if (qName.endsWith("Link") && "up".equals(attributes.get("rel"))) { |
| 110 | org = newReferenceType(attributes); |
| 111 | } else { |
| 112 | taskHandler.startElement(uri, localName, qName, attrs); |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | |
| 117 | public void endElement(String uri, String name, String qName) { |
| 118 | taskHandler.endElement(uri, name, qName); |
| 119 | if (qName.endsWith("Task")) { |
| 120 | this.tasks.add(taskHandler.getResult()); |
| 121 | } else if (qName.endsWith("Description")) { |
| 122 | description = currentOrNull(); |
| 123 | } else if (qName.endsWith("AllocationModel")) { |
| 124 | allocationModel = AllocationModel.fromValue(currentOrNull()); |
| 125 | } else if (qName.endsWith("Units")) { |
| 126 | units = currentOrNull(); |
| 127 | } else if (qName.endsWith("Allocated")) { |
| 128 | allocated = Integer.parseInt(currentOrNull()); |
| 129 | } else if (qName.endsWith("Used")) { |
| 130 | used = Integer.parseInt(currentOrNull()); |
| 131 | } else if (qName.endsWith("Limit")) { |
| 132 | limit = Integer.parseInt(currentOrNull()); |
| 133 | } else if (qName.endsWith("Overhead")) { |
| 134 | overhead = Integer.parseInt(currentOrNull()); |
| 135 | } else if (qName.endsWith("StorageCapacity")) { |
| 136 | storageCapacity = new Capacity(units, allocated, limit, used, overhead); |
| 137 | resetCapacity(); |
| 138 | } else if (qName.endsWith("Cpu")) { |
| 139 | cpuCapacity = new Capacity(units, allocated, limit, used, overhead); |
| 140 | resetCapacity(); |
| 141 | } else if (qName.endsWith("Memory")) { |
| 142 | memoryCapacity = new Capacity(units, allocated, limit, used, overhead); |
| 143 | resetCapacity(); |
| 144 | } else if (qName.endsWith("DeployedVmsQuota")) { |
| 145 | vmQuota = (int) limit; |
| 146 | // vcloud express doesn't have the zero is unlimited rule |
| 147 | if (vmQuota == -1) |
| 148 | vmQuota = 0; |
| 149 | } else if (qName.endsWith("VmQuota")) { |
| 150 | vmQuota = Integer.parseInt(currentOrNull()); |
| 151 | } else if (qName.endsWith("NicQuota")) { |
| 152 | nicQuota = Integer.parseInt(currentOrNull()); |
| 153 | } else if (qName.endsWith("NetworkQuota")) { |
| 154 | networkQuota = Integer.parseInt(currentOrNull()); |
| 155 | } else if (qName.endsWith("IsEnabled")) { |
| 156 | isEnabled = Boolean.parseBoolean(currentOrNull()); |
| 157 | } |
| 158 | currentText = new StringBuilder(); |
| 159 | } |
| 160 | |
| 161 | public void characters(char ch[], int start, int length) { |
| 162 | currentText.append(ch, start, length); |
| 163 | } |
| 164 | |
| 165 | protected String currentOrNull() { |
| 166 | String returnVal = currentText.toString().trim(); |
| 167 | return returnVal.equals("") ? null : returnVal; |
| 168 | } |
| 169 | } |