View Javadoc

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.equalsOrSuffix;
22  import static org.jclouds.vcloud.util.Utils.newReferenceType;
23  
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import javax.inject.Inject;
29  
30  import org.jclouds.http.functions.ParseSax;
31  import org.jclouds.util.SaxUtils;
32  import org.jclouds.vcloud.domain.ReferenceType;
33  import org.jclouds.vcloud.domain.Task;
34  import org.jclouds.vcloud.domain.network.DhcpService;
35  import org.jclouds.vcloud.domain.network.Features;
36  import org.jclouds.vcloud.domain.network.FenceMode;
37  import org.jclouds.vcloud.domain.network.FirewallService;
38  import org.jclouds.vcloud.domain.network.IpRange;
39  import org.jclouds.vcloud.domain.network.IpScope;
40  import org.jclouds.vcloud.domain.network.NatService;
41  import org.jclouds.vcloud.domain.network.OrgNetwork;
42  import org.jclouds.vcloud.domain.network.firewall.FirewallPolicy;
43  import org.jclouds.vcloud.domain.network.firewall.FirewallProtocols;
44  import org.jclouds.vcloud.domain.network.firewall.FirewallRule;
45  import org.jclouds.vcloud.domain.network.internal.OrgNetworkImpl;
46  import org.jclouds.vcloud.domain.network.nat.NatPolicy;
47  import org.jclouds.vcloud.domain.network.nat.NatProtocol;
48  import org.jclouds.vcloud.domain.network.nat.NatRule;
49  import org.jclouds.vcloud.domain.network.nat.NatType;
50  import org.jclouds.vcloud.domain.network.nat.rules.MappingMode;
51  import org.jclouds.vcloud.domain.network.nat.rules.OneToOneVmRule;
52  import org.jclouds.vcloud.domain.network.nat.rules.PortForwardingRule;
53  import org.jclouds.vcloud.domain.network.nat.rules.VmRule;
54  import org.xml.sax.Attributes;
55  import org.xml.sax.SAXException;
56  
57  import com.google.common.collect.Iterables;
58  import com.google.common.collect.Lists;
59  import com.google.common.collect.Sets;
60  
61  /**
62   * @author Adrian Cole
63   */
64  public class OrgNetworkHandler extends ParseSax.HandlerWithResult<OrgNetwork> {
65  
66     protected final TaskHandler taskHandler;
67  
68     @Inject
69     public OrgNetworkHandler(TaskHandler taskHandler) {
70        this.taskHandler = taskHandler;
71     }
72  
73     protected StringBuilder currentText = new StringBuilder();
74  
75     protected ReferenceType network;
76     protected ReferenceType org;
77     protected String orgDescription;
78     protected List<Task> tasks = Lists.newArrayList();
79  
80     protected String startAddress;
81     protected String endAddress;
82  
83     protected boolean inherited;
84     protected String gateway;
85     protected String netmask;
86     protected String dns1;
87     protected String dns2;
88     protected String dnsSuffix;
89     protected Set<IpRange> ipRanges = Sets.newLinkedHashSet();
90     protected Set<String> allocatedIpAddresses = Sets.newLinkedHashSet();
91  
92     protected IpScope ipScope;
93     protected ReferenceType parentNetwork;
94     protected FenceMode fenceMode;
95  
96     protected boolean serviceEnabled;
97     protected Integer defaultLeaseTime;
98     protected Integer maxLeaseTime;
99  
100    protected DhcpService dhcpService;
101 
102    protected boolean inFirewallRule;
103    protected boolean firewallRuleEnabled;
104    protected String firewallRuleDescription;
105    protected FirewallPolicy firewallPolicy;
106 
107    protected boolean tcp;
108    protected boolean udp;
109    protected FirewallProtocols protocols;
110    protected int port;
111    protected String destinationIp;
112 
113    protected List<FirewallRule> firewallRules = Lists.newArrayList();
114    protected FirewallService firewallService;
115 
116    protected NatType natType;
117    protected NatPolicy natPolicy;
118 
119    protected MappingMode mappingMode;
120    protected String externalIP;
121    protected String vAppScopedVmId;
122    protected int vmNicId;
123 
124    protected int externalPort;
125    protected String internalIP;
126    protected int internalPort;
127    protected NatProtocol natProtocol;
128 
129    protected String vAppScopedLocalId;
130 
131    protected List<NatRule> natRules = Lists.newArrayList();
132    protected NatService natService;
133 
134    protected Features features;
135    protected OrgNetwork.Configuration configuration;
136 
137    protected ReferenceType networkPool;
138    protected Set<String> allowedExternalIpAddresses = Sets.newLinkedHashSet();
139 
140    public OrgNetwork getResult() {
141       return new OrgNetworkImpl(network.getName(), network.getType(), network.getHref(), org, orgDescription, tasks,
142                configuration, networkPool, allowedExternalIpAddresses);
143    }
144 
145    @Override
146    public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
147       Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs);
148       if (qName.equals("OrgNetwork")) {
149          network = newReferenceType(attributes);
150       } else if (qName.equals("FirewallRule")) {
151          this.inFirewallRule = true;
152       } else if (qName.equals("ParentNetwork")) {
153          parentNetwork = newReferenceType(attributes);
154       } else if (qName.equals("Link") && "up".equals(attributes.get("rel"))) {
155          org = newReferenceType(attributes);
156       } else {
157          taskHandler.startElement(uri, localName, qName, attrs);
158       }
159       String type = attributes.get("type");
160       if (type != null) {
161          if (type.indexOf("networkPool+xml") != -1) {
162             networkPool = newReferenceType(attributes);
163          }
164       }
165    }
166 
167    public void endElement(String uri, String name, String qName) {
168       taskHandler.endElement(uri, name, qName);
169       if (qName.equals("Task")) {
170          this.tasks.add(taskHandler.getResult());
171       } else if (qName.equals("Description")) {
172          if (inFirewallRule)
173             firewallRuleDescription = currentOrNull();
174          else
175             orgDescription = currentOrNull();
176       } else if (qName.equals("FenceMode")) {
177          fenceMode = FenceMode.fromValue(currentOrNull());
178       } else if (qName.equals("StartAddress")) {
179          startAddress = currentOrNull();
180       } else if (qName.equals("EndAddress")) {
181          endAddress = currentOrNull();
182       } else if (qName.equals("AllocatedIpAddress")) {
183          allocatedIpAddresses.add(currentOrNull());
184       } else if (qName.equals("IpRange")) {
185          ipRanges.add(new IpRange(startAddress, endAddress));
186          this.startAddress = null;
187          this.endAddress = null;
188       } else if (qName.equals("IsInherited")) {
189          inherited = Boolean.parseBoolean(currentOrNull());
190       } else if (qName.equals("Gateway")) {
191          gateway = currentOrNull();
192       } else if (qName.equals("Netmask")) {
193          netmask = currentOrNull();
194       } else if (qName.equals("Dns1")) {
195          dns1 = currentOrNull();
196       } else if (qName.equals("Dns2")) {
197          dns2 = currentOrNull();
198       } else if (qName.equals("DnsSuffix")) {
199          dnsSuffix = currentOrNull();
200       } else if (qName.equals("IpScope")) {
201          ipScope = new IpScope(inherited, gateway, netmask, dns1, dns2, dnsSuffix, ipRanges, allocatedIpAddresses);
202          this.inherited = false;
203          this.gateway = null;
204          this.netmask = null;
205          this.dns1 = null;
206          this.dns2 = null;
207          this.dnsSuffix = null;
208          this.ipRanges = Sets.newLinkedHashSet();
209          this.allocatedIpAddresses = Sets.newLinkedHashSet();
210       } else if (qName.equals("IsEnabled")) {
211          if (inFirewallRule)
212             firewallRuleEnabled = Boolean.parseBoolean(currentOrNull());
213          else
214             serviceEnabled = Boolean.parseBoolean(currentOrNull());
215       } else if (qName.equals("DefaultLeaseTime")) {
216          defaultLeaseTime = Integer.parseInt(currentOrNull());
217       } else if (qName.equals("MaxLeaseTime")) {
218          maxLeaseTime = Integer.parseInt(currentOrNull());
219       } else if (qName.equals("DhcpService")) {
220          this.dhcpService = new DhcpService(serviceEnabled, defaultLeaseTime, maxLeaseTime, Iterables
221                   .getOnlyElement(ipRanges));
222          this.serviceEnabled = false;
223          this.defaultLeaseTime = null;
224          this.maxLeaseTime = null;
225          this.ipRanges = Sets.newLinkedHashSet();
226       } else if (qName.equals("Policy")) {
227          if (inFirewallRule)
228             firewallPolicy = FirewallPolicy.fromValue(currentOrNull());
229          else
230             natPolicy = NatPolicy.fromValue(currentOrNull());
231       } else if (qName.equals("Tcp")) {
232          tcp = Boolean.parseBoolean(currentOrNull());
233       } else if (qName.equals("Udp")) {
234          udp = Boolean.parseBoolean(currentOrNull());
235       } else if (qName.equals("Protocols")) {
236          this.protocols = new FirewallProtocols(tcp, udp);
237          this.tcp = false;
238          this.udp = false;
239       } else if (qName.equals("DestinationIp")) {
240          this.destinationIp = currentOrNull();
241       } else if (qName.equals("FirewallRule")) {
242          this.inFirewallRule = false;
243          this.firewallRules.add(new FirewallRule(firewallRuleEnabled, firewallRuleDescription, firewallPolicy,
244                   protocols, port, destinationIp));
245          this.firewallRuleEnabled = false;
246          this.firewallRuleDescription = null;
247          this.firewallPolicy = null;
248          this.protocols = null;
249          this.port = -1;
250          this.destinationIp = null;
251       } else if (qName.equals("FirewallService")) {
252          firewallService = new FirewallService(serviceEnabled, firewallRules);
253          this.serviceEnabled = false;
254          this.firewallRules = Lists.newArrayList();
255       } else if (qName.equals("NatType")) {
256          natType = NatType.fromValue(currentOrNull());
257       } else if (qName.equals("MappingMode")) {
258          mappingMode = MappingMode.fromValue(currentOrNull());
259       } else if (qName.equalsIgnoreCase("ExternalIP")) {
260          externalIP = currentOrNull();
261       } else if (qName.equalsIgnoreCase("VAppScopedVmId")) {
262          vAppScopedVmId = currentOrNull();
263       } else if (qName.equalsIgnoreCase("VAppScopedLocalId")) {
264          vAppScopedLocalId = currentOrNull();
265       } else if (qName.equalsIgnoreCase("vmNicId")) {
266          vmNicId = Integer.parseInt(currentOrNull());
267       } else if (qName.equals("OneToOneVmRule")) {
268          natRules.add(new OneToOneVmRule(mappingMode, externalIP, vAppScopedVmId, vmNicId));
269          this.mappingMode = null;
270          this.externalIP = null;
271          this.vAppScopedVmId = null;
272          this.vmNicId = -1;
273       } else if (qName.equalsIgnoreCase("ExternalPort")) {
274          externalPort = Integer.parseInt(currentOrNull());
275       } else if (qName.equalsIgnoreCase("InternalIP")) {
276          internalIP = currentOrNull();
277       } else if (qName.equalsIgnoreCase("InternalPort")) {
278          internalPort = Integer.parseInt(currentOrNull());
279       } else if (equalsOrSuffix(qName, "Protocol")) {
280          natProtocol = NatProtocol.valueOf(currentOrNull());
281       } else if (qName.equals("PortForwardingRule")) {
282          natRules.add(new PortForwardingRule(externalIP, externalPort, internalIP, internalPort, natProtocol));
283          this.externalIP = null;
284          this.externalPort = -1;
285          this.internalIP = null;
286          this.internalPort = -1;
287          this.natProtocol = null;
288       } else if (qName.equals("VmRule")) {
289          natRules.add(new VmRule(externalIP, externalPort, vAppScopedLocalId, vmNicId, internalPort, natProtocol));
290          this.externalIP = null;
291          this.externalPort = -1;
292          this.vAppScopedLocalId = null;
293          this.vmNicId = -1;
294          this.internalPort = -1;
295          this.natProtocol = null;
296       } else if (qName.equals("NatService")) {
297          this.natService = new NatService(serviceEnabled, natType, natPolicy, natRules);
298          this.serviceEnabled = false;
299          this.natType = null;
300          this.natPolicy = null;
301          this.natRules = Lists.newArrayList();
302       } else if (qName.equals("Features")) {
303          this.features = new Features(dhcpService, firewallService, natService);
304          this.dhcpService = null;
305          this.firewallService = null;
306          this.natService = null;
307       } else if (qName.equals("Configuration")) {
308          configuration = new OrgNetworkImpl.ConfigurationImpl(ipScope, parentNetwork, fenceMode, features);
309          this.ipScope = null;
310          this.parentNetwork = null;
311          this.fenceMode = null;
312          this.features = null;
313       } else if (qName.equals("AllowedExternalIpAddress")) {
314          allowedExternalIpAddresses.add(currentOrNull());
315       }
316       currentText = new StringBuilder();
317    }
318 
319    public void characters(char ch[], int start, int length) {
320       currentText.append(ch, start, length);
321    }
322 
323    protected String currentOrNull() {
324       String returnVal = currentText.toString().trim();
325       return returnVal.equals("") ? null : returnVal;
326    }
327 }