PageRenderTime 65ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/vcloud/src/main/java/org/jclouds/vcloud/xml/OrgNetworkHandler.java

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