| 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.savvis.vpdc.xml; |
| 20 | |
| 21 | import static org.jclouds.savvis.vpdc.util.Utils.newResource; |
| 22 | import static org.jclouds.util.SaxUtils.equalsOrSuffix; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Provider; |
| 28 | |
| 29 | import org.jclouds.ovf.NetworkSection; |
| 30 | import org.jclouds.ovf.Section; |
| 31 | import org.jclouds.ovf.xml.NetworkSectionHandler; |
| 32 | import org.jclouds.ovf.xml.OperatingSystemSectionHandler; |
| 33 | import org.jclouds.ovf.xml.ProductSectionHandler; |
| 34 | import org.jclouds.ovf.xml.SectionHandler; |
| 35 | import org.jclouds.ovf.xml.VirtualHardwareSectionHandler; |
| 36 | import org.jclouds.ovf.xml.internal.BaseVirtualSystemHandler; |
| 37 | import org.jclouds.savvis.vpdc.domain.NetworkConfigSection; |
| 38 | import org.jclouds.savvis.vpdc.domain.NetworkConnectionSection; |
| 39 | import org.jclouds.savvis.vpdc.domain.Resource; |
| 40 | import org.jclouds.savvis.vpdc.domain.VM; |
| 41 | import org.jclouds.savvis.vpdc.util.Utils; |
| 42 | import org.xml.sax.Attributes; |
| 43 | import org.xml.sax.SAXException; |
| 44 | |
| 45 | import com.google.common.collect.ImmutableMap; |
| 46 | |
| 47 | /** |
| 48 | * @author Kedar Dave |
| 49 | */ |
| 50 | public class VMHandler extends BaseVirtualSystemHandler<VM, VM.Builder> { |
| 51 | |
| 52 | @SuppressWarnings("unchecked") |
| 53 | @Inject |
| 54 | public VMHandler(Provider<VM.Builder> builderProvider, OperatingSystemSectionHandler osHandler, |
| 55 | VirtualHardwareSectionHandler hardwareHandler, ProductSectionHandler productHandler, |
| 56 | Provider<NetworkSectionHandler> networkSectionHandler, |
| 57 | Provider<NetworkConfigSectionHandler> networkConfigSectionHandler, |
| 58 | Provider<NetworkConnectionSectionHandler> networkConnectionSectionHandler) { |
| 59 | super(builderProvider, osHandler, hardwareHandler, productHandler); |
| 60 | this.extensionHandlers = ImmutableMap.<String, Provider<? extends SectionHandler>> of("ovf:NetworkSection", |
| 61 | networkSectionHandler, "vApp:NetworkConfigSectionType", networkConfigSectionHandler, |
| 62 | "vApp:NetworkConnectionType", networkConnectionSectionHandler); |
| 63 | } |
| 64 | |
| 65 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 66 | Map<String, String> attributes = Utils.cleanseAttributes(attrs); |
| 67 | if (equalsOrSuffix(qName, "VApp")) { |
| 68 | // savvis doesn't add href in the header for some reason |
| 69 | if (!attributes.containsKey("href") && getRequest() != null) |
| 70 | attributes = ImmutableMap.<String, String> builder().putAll(attributes).put("href", |
| 71 | getRequest().getEndpoint().toASCIIString()).build(); |
| 72 | Resource vApp = newResource(attributes); |
| 73 | builder.name(vApp.getName()).type(vApp.getType()).id(vApp.getId()).href(vApp.getHref()); |
| 74 | builder.status(VM.Status.fromValue(attributes.get("status"))); |
| 75 | } |
| 76 | super.startElement(uri, localName, qName, attrs); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | @SuppressWarnings("unchecked") |
| 81 | protected void addAdditionalSection(String qName, Section additionalSection) { |
| 82 | if (additionalSection instanceof NetworkSection) { |
| 83 | builder.networkSection(NetworkSection.class.cast(additionalSection)); |
| 84 | } else if (additionalSection instanceof NetworkConfigSection) { |
| 85 | builder.networkConfigSection(NetworkConfigSection.class.cast(additionalSection)); |
| 86 | } else if (additionalSection instanceof NetworkConnectionSection) { |
| 87 | builder.networkConnectionSection(NetworkConnectionSection.class.cast(additionalSection)); |
| 88 | } else { |
| 89 | builder.additionalSection(qName, additionalSection); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | } |