| 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.domain.network.nat.rules; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import org.jclouds.vcloud.domain.network.nat.NatProtocol; |
| 24 | import org.jclouds.vcloud.domain.network.nat.NatRule; |
| 25 | |
| 26 | /** |
| 27 | * The PortForwardingRule element describes a NAT rule that maps an IP address and port in an |
| 28 | * organization network to an external IP address and port. |
| 29 | * |
| 30 | * @since vcloud 0.8 |
| 31 | * @author Adrian Cole |
| 32 | */ |
| 33 | public class PortForwardingRule implements NatRule { |
| 34 | private final String externalIP; |
| 35 | private final int externalPort; |
| 36 | private final String internalIP; |
| 37 | private final int internalPort; |
| 38 | private final NatProtocol protocol; |
| 39 | |
| 40 | public PortForwardingRule(String externalIP, int externalPort, String internalIP, int internalPort, |
| 41 | NatProtocol protocol) { |
| 42 | this.externalIP = checkNotNull(externalIP, "externalIP"); |
| 43 | this.externalPort = externalPort; |
| 44 | this.internalIP = checkNotNull(internalIP, "internalIP"); |
| 45 | this.internalPort = internalPort; |
| 46 | this.protocol = checkNotNull(protocol, "protocol"); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * IP address to which this NAT rule maps the IP address specified in the InternalIp element. |
| 52 | */ |
| 53 | @Override |
| 54 | public String getExternalIP() { |
| 55 | return externalIP; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * network port to which this NAT rule maps the port number specified in the InternalPort element |
| 60 | */ |
| 61 | public int getExternalPort() { |
| 62 | return externalPort; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * IP address to which this NAT rule maps the IP address specified in the ExternalIp element. |
| 67 | */ |
| 68 | public String getInternalIP() { |
| 69 | return internalIP; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * network port to which this NAT rule maps the port number specified in the ExternalPort |
| 74 | * element. |
| 75 | */ |
| 76 | public int getInternalPort() { |
| 77 | return internalPort; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * specifies the network protocol to which this rule applies |
| 82 | */ |
| 83 | public NatProtocol getProtocol() { |
| 84 | return protocol; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int hashCode() { |
| 89 | final int prime = 31; |
| 90 | int result = 1; |
| 91 | result = prime * result + ((externalIP == null) ? 0 : externalIP.hashCode()); |
| 92 | result = prime * result + externalPort; |
| 93 | result = prime * result + ((internalIP == null) ? 0 : internalIP.hashCode()); |
| 94 | result = prime * result + internalPort; |
| 95 | result = prime * result + ((protocol == null) ? 0 : protocol.hashCode()); |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean equals(Object obj) { |
| 101 | if (this == obj) |
| 102 | return true; |
| 103 | if (obj == null) |
| 104 | return false; |
| 105 | if (getClass() != obj.getClass()) |
| 106 | return false; |
| 107 | PortForwardingRule other = (PortForwardingRule) obj; |
| 108 | if (externalIP == null) { |
| 109 | if (other.externalIP != null) |
| 110 | return false; |
| 111 | } else if (!externalIP.equals(other.externalIP)) |
| 112 | return false; |
| 113 | if (externalPort != other.externalPort) |
| 114 | return false; |
| 115 | if (internalIP == null) { |
| 116 | if (other.internalIP != null) |
| 117 | return false; |
| 118 | } else if (!internalIP.equals(other.internalIP)) |
| 119 | return false; |
| 120 | if (internalPort != other.internalPort) |
| 121 | return false; |
| 122 | if (protocol == null) { |
| 123 | if (other.protocol != null) |
| 124 | return false; |
| 125 | } else if (!protocol.equals(other.protocol)) |
| 126 | return false; |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public String toString() { |
| 132 | return "[externalIP=" + externalIP + ", externalPort=" + externalPort + ", internalIP=" + internalIP |
| 133 | + ", internalPort=" + internalPort + ", protocol=" + protocol + "]"; |
| 134 | } |
| 135 | |
| 136 | } |