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.domain.network.nat.rules;
20  
21  import static com.google.common.base.Preconditions.checkNotNull;
22  
23  import javax.annotation.Nullable;
24  
25  import org.jclouds.vcloud.domain.network.nat.NatProtocol;
26  import org.jclouds.vcloud.domain.network.nat.NatRule;
27  
28  /**
29   * The VmRule element describes a NAT rule that maps an IP address and port in a vApp network to an
30   * external IP address and port. The external IP address, external port, and internal port are
31   * specified in the element. The internal IP address is discovered by looking up the specified
32   * VmReference and VmNicId.
33   * 
34   * @since vcloud 0.9
35   * @author Adrian Cole
36   */
37  public class VmRule implements NatRule {
38     @Nullable
39     private final String externalIP;
40     private final int externalPort;
41     @Nullable
42     private final String vAppScopedLocalId;
43     private final int vmNicId;
44     private final int internalPort;
45     private final NatProtocol protocol;
46  
47     public VmRule(@Nullable String externalIP, int externalPort, @Nullable String vAppScopedLocalId, int vmNicId,
48              int internalPort, NatProtocol protocol) {
49        this.externalIP = externalIP;
50        this.externalPort = externalPort;
51        this.vAppScopedLocalId = vAppScopedLocalId;
52        this.vmNicId = vmNicId;
53        this.internalPort = internalPort;
54        this.protocol = checkNotNull(protocol, "protocol");
55     }
56  
57     /**
58      * IP address to which this NAT rule maps the IP address specified in the InternalIp element.
59      */
60     @Nullable
61     public String getExternalIP() {
62        return externalIP;
63     }
64  
65     /**
66      * network port to which this NAT rule maps the port number specified in the InternalPort element
67      */
68     public Integer getExternalPort() {
69        return externalPort;
70     }
71  
72     /**
73      * @return read‐only identifier created on import
74      * @since vcloud 0.9
75      */
76     @Nullable
77     public String getVAppScopedLocalId() {
78        return vAppScopedLocalId;
79     }
80  
81     /**
82      * @return device number of the NIC on the referenced virtual machine
83      * @since vcloud 0.9
84      */
85     public int getVmNicId() {
86        return vmNicId;
87     }
88  
89     /**
90      * network port to which this NAT rule maps the port number specified in the ExternalPort
91      * element.
92      */
93     public Integer getInternalPort() {
94        return internalPort;
95     }
96  
97     /**
98      * specifies the network protocol to which this rule applies
99      */
100    public NatProtocol getProtocol() {
101       return protocol;
102    }
103 
104    @Override
105    public int hashCode() {
106       final int prime = 31;
107       int result = 1;
108       result = prime * result + ((externalIP == null) ? 0 : externalIP.hashCode());
109       result = prime * result + externalPort;
110       result = prime * result + internalPort;
111       result = prime * result + ((protocol == null) ? 0 : protocol.hashCode());
112       result = prime * result + ((vAppScopedLocalId == null) ? 0 : vAppScopedLocalId.hashCode());
113       result = prime * result + vmNicId;
114       return result;
115    }
116 
117    @Override
118    public boolean equals(Object obj) {
119       if (this == obj)
120          return true;
121       if (obj == null)
122          return false;
123       if (getClass() != obj.getClass())
124          return false;
125       VmRule other = (VmRule) obj;
126       if (externalIP == null) {
127          if (other.externalIP != null)
128             return false;
129       } else if (!externalIP.equals(other.externalIP))
130          return false;
131       if (externalPort != other.externalPort)
132          return false;
133       if (internalPort != other.internalPort)
134          return false;
135       if (protocol == null) {
136          if (other.protocol != null)
137             return false;
138       } else if (!protocol.equals(other.protocol))
139          return false;
140       if (vAppScopedLocalId == null) {
141          if (other.vAppScopedLocalId != null)
142             return false;
143       } else if (!vAppScopedLocalId.equals(other.vAppScopedLocalId))
144          return false;
145       if (vmNicId != other.vmNicId)
146          return false;
147       return true;
148    }
149 
150    @Override
151    public String toString() {
152       return "[externalIP=" + externalIP + ", externalPort=" + externalPort + ", internalPort=" + internalPort
153                + ", protocol=" + protocol + ", vAppScopedLocalId=" + vAppScopedLocalId + ", vmNicId=" + vmNicId + "]";
154    }
155 
156 }