-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExternalIpAddressAllocator.java
More file actions
167 lines (143 loc) · 5.66 KB
/
ExternalIpAddressAllocator.java
File metadata and controls
167 lines (143 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.network;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import javax.ejb.Local;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import com.cloud.dc.dao.VlanDao;
import com.cloud.network.dao.IPAddressDao;
import com.cloud.utils.component.AdapterBase;
import com.cloud.utils.exception.CloudRuntimeException;
@Local(value = IpAddrAllocator.class)
public class ExternalIpAddressAllocator extends AdapterBase implements IpAddrAllocator {
private static final Logger s_logger = Logger.getLogger(ExternalIpAddressAllocator.class);
@Inject
ConfigurationDao _configDao = null;
@Inject
IPAddressDao _ipAddressDao = null;
@Inject
VlanDao _vlanDao;
private boolean _isExternalIpAllocatorEnabled = false;
private String _externalIpAllocatorUrl = null;
@Override
public IpAddr getPrivateIpAddress(String macAddr, long dcId, long podId) {
if (_externalIpAllocatorUrl == null || _externalIpAllocatorUrl.equalsIgnoreCase("")) {
return new IpAddr();
}
String urlString = _externalIpAllocatorUrl + "?command=getIpAddr&mac=" + macAddr + "&dc=" + dcId + "&pod=" + podId;
s_logger.debug("getIP:" + urlString);
BufferedReader in = null;
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
conn.setReadTimeout(30000);
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
s_logger.debug(inputLine);
String[] tokens = inputLine.split(",");
if (tokens.length != 3) {
s_logger.debug("the return value should be: mac,netmask,gateway");
return new IpAddr();
}
return new IpAddr(tokens[0], tokens[1], tokens[2]);
}
return new IpAddr();
} catch (MalformedURLException e) {
throw new CloudRuntimeException("URL is malformed " + urlString, e);
} catch (IOException e) {
return new IpAddr();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
}
@Override
public IpAddr getPublicIpAddress(String macAddr, long dcId, long podId) {
/*TODO: call API to get ip address from external DHCP server*/
return getPrivateIpAddress(macAddr, dcId, podId);
}
@Override
public boolean releasePrivateIpAddress(String ip, long dcId, long podId) {
/*TODO: call API to release the ip address from external DHCP server*/
if (_externalIpAllocatorUrl == null || _externalIpAllocatorUrl.equalsIgnoreCase("")) {
return false;
}
String urlString = _externalIpAllocatorUrl + "?command=releaseIpAddr&ip=" + ip + "&dc=" + dcId + "&pod=" + podId;
s_logger.debug("releaseIP:" + urlString);
BufferedReader in = null;
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
conn.setReadTimeout(30000);
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
return true;
} catch (MalformedURLException e) {
throw new CloudRuntimeException("URL is malformed " + urlString, e);
} catch (IOException e) {
return false;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
}
@Override
public boolean releasePublicIpAddress(String ip, long dcId, long podId) {
/*TODO: call API to release the ip address from external DHCP server*/
return releasePrivateIpAddress(ip, dcId, podId);
}
@Override
public boolean externalIpAddressAllocatorEnabled() {
return _isExternalIpAllocatorEnabled;
}
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
_isExternalIpAllocatorEnabled = Boolean.parseBoolean(_configDao.getValue("direct.attach.network.externalIpAllocator.enabled"));
_externalIpAllocatorUrl = _configDao.getValue("direct.attach.network.externalIpAllocator.url");
_name = name;
return true;
}
@Override
public String getName() {
return _name;
}
@Override
public boolean start() {
return true;
}
@Override
public boolean stop() {
return true;
}
}