forked from AnythingLinux/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscovererBase.java
More file actions
195 lines (170 loc) · 7.51 KB
/
DiscovererBase.java
File metadata and controls
195 lines (170 loc) · 7.51 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// 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.resource;
import com.cloud.configuration.Config;
import com.cloud.dc.ClusterVO;
import com.cloud.dc.dao.ClusterDao;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.network.NetworkModel;
import com.cloud.utils.component.AdapterBase;
import com.cloud.utils.net.UrlUtil;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.log4j.Logger;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public abstract class DiscovererBase extends AdapterBase implements Discoverer {
protected Map<String, String> _params;
private static final Logger s_logger = Logger.getLogger(DiscovererBase.class);
@Inject
protected ClusterDao _clusterDao;
@Inject
protected ConfigurationDao _configDao;
@Inject
protected NetworkModel _networkMgr;
@Inject
protected HostDao _hostDao;
@Inject
protected ResourceManager _resourceMgr;
@Inject
protected DataCenterDao _dcDao;
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
_params = _configDao.getConfiguration(params);
return true;
}
protected Map<String, String> resolveInputParameters(URL url) {
Map<String, String> params = UrlUtil.parseQueryParameters(url);
return null;
}
@Override
public void putParam(Map<String, String> params) {
if (_params == null) {
_params = new HashMap<String, String>();
}
_params.putAll(params);
}
@Override
public boolean start() {
return true;
}
@Override
public boolean stop() {
return true;
}
protected ServerResource getResource(String resourceName) {
ServerResource resource = null;
try {
Class<?> clazz = Class.forName(resourceName);
Constructor constructor = clazz.getConstructor();
resource = (ServerResource)constructor.newInstance();
} catch (ClassNotFoundException e) {
s_logger.warn("Unable to find class " + resourceName, e);
} catch (InstantiationException e) {
s_logger.warn("Unablet to instantiate class " + resourceName, e);
} catch (IllegalAccessException e) {
s_logger.warn("Illegal access " + resourceName, e);
} catch (SecurityException e) {
s_logger.warn("Security error on " + resourceName, e);
} catch (NoSuchMethodException e) {
s_logger.warn("NoSuchMethodException error on " + resourceName, e);
} catch (IllegalArgumentException e) {
s_logger.warn("IllegalArgumentException error on " + resourceName, e);
} catch (InvocationTargetException e) {
s_logger.warn("InvocationTargetException error on " + resourceName, e);
}
return resource;
}
protected HashMap<String, Object> buildConfigParams(HostVO host) {
HashMap<String, Object> params = new HashMap<String, Object>(host.getDetails().size() + 5);
params.putAll(host.getDetails());
params.put("guid", host.getGuid());
params.put("zone", Long.toString(host.getDataCenterId()));
if (host.getPodId() != null) {
params.put("pod", Long.toString(host.getPodId()));
}
if (host.getClusterId() != null) {
params.put("cluster", Long.toString(host.getClusterId()));
String guid = null;
ClusterVO cluster = _clusterDao.findById(host.getClusterId());
if (cluster.getGuid() == null) {
guid = host.getDetail("pool");
} else {
guid = cluster.getGuid();
}
if (guid != null && !guid.isEmpty()) {
params.put("pool", guid);
}
}
params.put("ipaddress", host.getPrivateIpAddress());
params.put("secondary.storage.vm", "false");
params.put("max.template.iso.size", _configDao.getValue(Config.MaxTemplateAndIsoSize.toString()));
params.put("migratewait", _configDao.getValue(Config.MigrateWait.toString()));
params.put(Config.XenServerMaxNics.toString().toLowerCase(), _configDao.getValue(Config.XenServerMaxNics.toString()));
params.put(Config.XenServerHeartBeatInterval.toString().toLowerCase(), _configDao.getValue(Config.XenServerHeartBeatInterval.toString()));
params.put(Config.XenServerHeartBeatTimeout.toString().toLowerCase(), _configDao.getValue(Config.XenServerHeartBeatTimeout.toString()));
params.put("router.aggregation.command.each.timeout", _configDao.getValue(Config.RouterAggregationCommandEachTimeout.toString()));
return params;
}
@Override
public ServerResource reloadResource(HostVO host) {
String resourceName = host.getResource();
ServerResource resource = getResource(resourceName);
if (resource != null) {
_hostDao.loadDetails(host);
updateNetworkLabels(host);
HashMap<String, Object> params = buildConfigParams(host);
try {
resource.configure(host.getName(), params);
} catch (ConfigurationException e) {
s_logger.warn("Unable to configure resource due to " + e.getMessage());
return null;
}
if (!resource.start()) {
s_logger.warn("Unable to start the resource");
return null;
}
}
return resource;
}
private void updateNetworkLabels(HostVO host) {
//check if networkLabels need to be updated in details
//we send only private and storage network label to the resource.
String privateNetworkLabel = _networkMgr.getDefaultManagementTrafficLabel(host.getDataCenterId(), host.getHypervisorType());
String storageNetworkLabel = _networkMgr.getDefaultStorageTrafficLabel(host.getDataCenterId(), host.getHypervisorType());
String privateDevice = host.getDetail("private.network.device");
String storageDevice = host.getDetail("storage.network.device1");
boolean update = false;
if (privateNetworkLabel != null && !privateNetworkLabel.equalsIgnoreCase(privateDevice)) {
host.setDetail("private.network.device", privateNetworkLabel);
update = true;
}
if (storageNetworkLabel != null && !storageNetworkLabel.equalsIgnoreCase(storageDevice)) {
host.setDetail("storage.network.device1", storageNetworkLabel);
update = true;
}
if (update) {
_hostDao.saveDetails(host);
}
}
}