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.savvis.vpdc.domain;
20  
21  import static com.google.common.base.Preconditions.checkNotNull;
22  
23  import java.net.URI;
24  import java.util.Map;
25  
26  import javax.annotation.Nullable;
27  
28  import com.google.common.collect.ImmutableMap;
29  import com.google.common.collect.Maps;
30  
31  /**
32   * Various network features such NAT Public IP, Gateway and Netmask.
33   * 
34   * @author Adrian Cole
35   */
36  public class Network extends ResourceImpl {
37     public static Builder builder() {
38        return new Builder();
39     }
40  
41     public static class Builder extends ResourceImpl.Builder {
42        private String gateway;
43        private String netmask;
44        private Map<String, String> internalToExternalNATRules = Maps.newLinkedHashMap();
45  
46        public Builder gateway(String gateway) {
47           this.gateway = gateway;
48           return this;
49        }
50  
51        public Builder netmask(String netmask) {
52           this.netmask = netmask;
53           return this;
54        }
55  
56        public Builder internalToExternalNATRule(String internalIP, String externalIP) {
57           this.internalToExternalNATRules.put(checkNotNull(internalIP, "internalIP"),
58                 checkNotNull(externalIP, "externalIP"));
59           return this;
60        }
61  
62        public Builder internalToExternalNATRules(Map<String, String> internalToExternalNATRules) {
63           this.internalToExternalNATRules.putAll(checkNotNull(internalToExternalNATRules, "internalToExternalNATRules"));
64           return this;
65        }
66  
67        @Override
68        public Network build() {
69           return new Network(id, name, type, href, gateway, netmask, internalToExternalNATRules);
70        }
71  
72        public static Builder fromNetwork(Network in) {
73           return new Builder().id(in.getId()).name(in.getName()).type(in.getType()).href(in.getHref())
74                 .gateway(in.getGateway()).internalToExternalNATRules(in.getInternalToExternalNATRules())
75                 .netmask(in.getNetmask());
76        }
77  
78        @Override
79        public Builder id(String id) {
80           return Builder.class.cast(super.id(id));
81        }
82  
83        @Override
84        public Builder name(String name) {
85           return Builder.class.cast(super.name(name));
86        }
87  
88        @Override
89        public Builder type(String type) {
90           return Builder.class.cast(super.type(type));
91        }
92  
93        @Override
94        public Builder href(URI href) {
95           return Builder.class.cast(super.href(href));
96        }
97  
98     }
99  
100    @Nullable
101    private final String gateway;
102    private final String netmask;
103    private final Map<String, String> internalToExternalNATRules;
104 
105    public Network(String id, String name, String type, URI href, @Nullable String gateway, String netmask,
106          Map<String, String> internalToExternalNATRules) {
107       super(id, name, type, href);
108       this.gateway = gateway;
109       this.netmask = netmask;
110       this.internalToExternalNATRules = ImmutableMap.copyOf(checkNotNull(internalToExternalNATRules,
111             "internalToExternalNATRules"));
112    }
113 
114    /**
115     * @return IP of the network's gateway
116     */
117    public String getGateway() {
118       return gateway;
119    }
120 
121    /**
122     * @return IP of the network's netmask
123     */
124    public String getNetmask() {
125       return netmask;
126    }
127 
128    /**
129     * @return map of internal to external ip when it has any nat1to1 enabled deployed VApp
130     */
131    public Map<String, String> getInternalToExternalNATRules() {
132       return internalToExternalNATRules;
133    }
134 
135    @Override
136    public Builder toBuilder() {
137       return Builder.fromNetwork(this);
138    }
139 
140    @Override
141    public String toString() {
142       return "[id=" + id + ", href=" + href + ", name=" + name + ", type=" + type + ", gateway=" + gateway
143             + ", netmask=" + netmask + ", internalToExternalNATRules=" + internalToExternalNATRules + "]";
144    }
145 
146 }