| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.vcloud.xml; |
| 20 | |
| 21 | import static org.jclouds.util.SaxUtils.cleanseAttributes; |
| 22 | import static org.jclouds.util.SaxUtils.currentOrNull; |
| 23 | import static org.jclouds.vcloud.util.Utils.newReferenceType; |
| 24 | |
| 25 | import java.util.Map; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import javax.annotation.Resource; |
| 29 | |
| 30 | import org.jclouds.http.functions.ParseSax; |
| 31 | import org.jclouds.logging.Logger; |
| 32 | import org.jclouds.vcloud.domain.ReferenceType; |
| 33 | import org.jclouds.vcloud.domain.network.FenceMode; |
| 34 | import org.jclouds.vcloud.domain.network.VCloudExpressNetwork; |
| 35 | import org.jclouds.vcloud.domain.network.firewall.FirewallPolicy; |
| 36 | import org.jclouds.vcloud.domain.network.firewall.FirewallRule; |
| 37 | import org.jclouds.vcloud.domain.network.internal.VCloudExpressNetworkImpl; |
| 38 | import org.jclouds.vcloud.domain.network.nat.NatProtocol; |
| 39 | import org.jclouds.vcloud.domain.network.nat.rules.PortForwardingRule; |
| 40 | import org.xml.sax.Attributes; |
| 41 | import org.xml.sax.SAXException; |
| 42 | |
| 43 | import com.google.common.collect.Sets; |
| 44 | |
| 45 | /** |
| 46 | * @author Adrian Cole |
| 47 | */ |
| 48 | public class VCloudExpressNetworkHandler extends ParseSax.HandlerWithResult<VCloudExpressNetwork> { |
| 49 | |
| 50 | @Resource |
| 51 | protected Logger logger = Logger.NULL; |
| 52 | |
| 53 | protected StringBuilder currentText = new StringBuilder(); |
| 54 | |
| 55 | protected ReferenceType network; |
| 56 | |
| 57 | protected String description; |
| 58 | |
| 59 | protected Set<String> dnsServers = Sets.newLinkedHashSet(); |
| 60 | protected String gateway; |
| 61 | protected String netmask; |
| 62 | protected Set<FenceMode> fenceModes = Sets.newLinkedHashSet(); |
| 63 | protected Boolean dhcp; |
| 64 | protected Set<PortForwardingRule> natRules = Sets.newLinkedHashSet(); |
| 65 | protected Set<FirewallRule> firewallRules = Sets.newLinkedHashSet(); |
| 66 | |
| 67 | protected String externalIP; |
| 68 | protected Integer externalPort; |
| 69 | protected String internalIP; |
| 70 | protected Integer internalPort; |
| 71 | |
| 72 | protected FirewallPolicy policy; |
| 73 | protected String sourceIP; |
| 74 | protected int sourcePort; |
| 75 | |
| 76 | public VCloudExpressNetwork getResult() { |
| 77 | return new VCloudExpressNetworkImpl(network.getName(), network.getType(), network.getHref(), description, |
| 78 | dnsServers, gateway, netmask, fenceModes, dhcp, natRules, firewallRules); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 83 | Map<String, String> attributes = cleanseAttributes(attrs); |
| 84 | if (qName.equals("Network")) { |
| 85 | network = newReferenceType(attributes); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public void endElement(String uri, String name, String qName) { |
| 90 | if (qName.equals("Description")) { |
| 91 | description = currentOrNull(currentText); |
| 92 | } else if (qName.equals("Dns")) { |
| 93 | dnsServers.add(currentOrNull(currentText)); |
| 94 | } else if (qName.equals("Gateway")) { |
| 95 | gateway = currentOrNull(currentText); |
| 96 | } else if (qName.equals("Netmask")) { |
| 97 | netmask = currentOrNull(currentText); |
| 98 | } else if (qName.equals("FenceMode")) { |
| 99 | try { |
| 100 | fenceModes.add(FenceMode.fromValue(currentOrNull(currentText))); |
| 101 | } catch (IllegalArgumentException e) { |
| 102 | fenceModes.add(FenceMode.BRIDGED); |
| 103 | } |
| 104 | } else if (qName.equals("Dhcp")) { |
| 105 | dhcp = new Boolean(currentOrNull(currentText)); |
| 106 | } else if (qName.equals("NatRule")) { |
| 107 | natRules.add(new PortForwardingRule(externalIP, externalPort, internalIP, internalPort, NatProtocol.TCP_UDP)); |
| 108 | externalIP = null; |
| 109 | externalPort = null; |
| 110 | internalIP = null; |
| 111 | internalPort = null; |
| 112 | } else if (qName.equals("ExternalIP")) { |
| 113 | externalIP = currentOrNull(currentText); |
| 114 | } else if (qName.equals("ExternalPort")) { |
| 115 | externalPort = Integer.parseInt(currentOrNull(currentText)); |
| 116 | } else if (qName.equals("InternalIP")) { |
| 117 | internalIP = currentOrNull(currentText); |
| 118 | } else if (qName.equals("InternalPort")) { |
| 119 | internalPort = Integer.parseInt(currentOrNull(currentText)); |
| 120 | } else if (qName.equals("FirewallRule")) { |
| 121 | firewallRules.add(new FirewallRule(true, null, policy, null, sourcePort, sourceIP)); |
| 122 | policy = null; |
| 123 | sourceIP = null; |
| 124 | sourcePort = -1; |
| 125 | } else if (qName.equals("Policy")) { |
| 126 | policy = FirewallPolicy.fromValue(currentOrNull(currentText)); |
| 127 | } else if (qName.equals("SourceIp")) { |
| 128 | sourceIP = currentOrNull(currentText); |
| 129 | } else if (qName.equals("SourcePort")) { |
| 130 | sourcePort = Integer.parseInt(currentOrNull(currentText)); |
| 131 | } |
| 132 | |
| 133 | currentText = new StringBuilder(); |
| 134 | } |
| 135 | |
| 136 | public void characters(char ch[], int start, int length) { |
| 137 | currentText.append(ch, start, length); |
| 138 | } |
| 139 | |
| 140 | } |