|
10 | 10 | // limitations under the License. |
11 | 11 | // |
12 | 12 | // Automatically generated by addcopyright.py at 04/03/2012 |
13 | | -package com.cloud.hypervisor.vmware; |
| 13 | + |
| 14 | +package com.cloud.hypervisor.vmware; |
14 | 15 |
|
15 | 16 | import java.net.URI; |
16 | 17 | import java.net.URLDecoder; |
|
35 | 36 | import com.cloud.exception.DiscoveryException; |
36 | 37 | import com.cloud.host.HostVO; |
37 | 38 | import com.cloud.host.dao.HostDao; |
38 | | -import com.cloud.hypervisor.Hypervisor; |
39 | | -import com.cloud.hypervisor.Hypervisor.HypervisorType; |
40 | | -import com.cloud.hypervisor.vmware.manager.VmwareManager; |
41 | | -import com.cloud.hypervisor.vmware.mo.ClusterMO; |
42 | | -import com.cloud.hypervisor.vmware.mo.HostMO; |
43 | | -import com.cloud.hypervisor.vmware.resource.VmwareContextFactory; |
44 | | -import com.cloud.hypervisor.vmware.resource.VmwareResource; |
45 | | -import com.cloud.hypervisor.vmware.util.VmwareContext; |
| 39 | +import com.cloud.hypervisor.Hypervisor; |
| 40 | +import com.cloud.hypervisor.Hypervisor.HypervisorType; |
| 41 | +import com.cloud.hypervisor.vmware.manager.VmwareManager; |
| 42 | +import com.cloud.hypervisor.vmware.mo.ClusterMO; |
| 43 | +import com.cloud.hypervisor.vmware.mo.HostMO; |
| 44 | +import com.cloud.hypervisor.vmware.resource.VmwareContextFactory; |
| 45 | +import com.cloud.hypervisor.vmware.resource.VmwareResource; |
| 46 | +import com.cloud.hypervisor.vmware.util.VmwareContext; |
| 47 | +import com.cloud.network.NetworkManager; |
46 | 48 | import com.cloud.network.dao.CiscoNexusVSMDeviceDao; |
47 | | -import com.cloud.resource.Discoverer; |
| 49 | +import com.cloud.resource.Discoverer; |
48 | 50 | import com.cloud.resource.DiscovererBase; |
49 | 51 | import com.cloud.resource.ResourceManager; |
50 | 52 | import com.cloud.resource.ResourceStateAdapter; |
51 | 53 | import com.cloud.resource.ServerResource; |
52 | 54 | import com.cloud.resource.UnableDeleteHostException; |
53 | | -import com.cloud.resource.ResourceStateAdapter.DeleteHostAnswer; |
54 | 55 | import com.cloud.storage.Storage.ImageFormat; |
55 | 56 | import com.cloud.storage.Storage.TemplateType; |
56 | 57 | import com.cloud.storage.VMTemplateVO; |
|
61 | 62 | import com.cloud.utils.component.Inject; |
62 | 63 | import com.vmware.vim25.ClusterDasConfigInfo; |
63 | 64 | import com.vmware.vim25.ManagedObjectReference; |
64 | | - |
65 | | -@Local(value=Discoverer.class) |
66 | | -public class VmwareServerDiscoverer extends DiscovererBase implements Discoverer, ResourceStateAdapter { |
67 | | - private static final Logger s_logger = Logger.getLogger(VmwareServerDiscoverer.class); |
68 | | - |
69 | | - @Inject ClusterDao _clusterDao; |
70 | | - @Inject VmwareManager _vmwareMgr; |
71 | | - @Inject AlertManager _alertMgr; |
| 65 | + |
| 66 | +@Local(value=Discoverer.class) |
| 67 | +public class VmwareServerDiscoverer extends DiscovererBase implements Discoverer, ResourceStateAdapter { |
| 68 | + private static final Logger s_logger = Logger.getLogger(VmwareServerDiscoverer.class); |
| 69 | + |
| 70 | + @Inject ClusterDao _clusterDao; |
| 71 | + @Inject VmwareManager _vmwareMgr; |
| 72 | + @Inject AlertManager _alertMgr; |
72 | 73 | @Inject VMTemplateDao _tmpltDao; |
73 | 74 | @Inject ClusterDetailsDao _clusterDetailsDao; |
74 | 75 | @Inject HostDao _hostDao; |
75 | 76 | @Inject ResourceManager _resourceMgr; |
76 | 77 |
|
77 | 78 | @Inject CiscoNexusVSMDeviceDao _nexusDao; |
| 79 | + @Inject |
| 80 | + NetworkManager _netmgr; |
78 | 81 |
|
79 | 82 | @Override |
80 | 83 | public Map<? extends ServerResource, Map<String, String>> find(long dcId, Long podId, Long clusterId, URI url, |
@@ -102,80 +105,110 @@ public Map<? extends ServerResource, Map<String, String>> find(long dcId, Long p |
102 | 105 | s_logger.error(msg); |
103 | 106 | throw new DiscoveredWithErrorException(msg); |
104 | 107 | } |
105 | | - |
106 | | - VmwareContext context = null; |
107 | | - try { |
| 108 | + |
| 109 | + String privateTrafficLabel = null; |
| 110 | + Map<String, String> vsmCredentials = null; |
| 111 | + if (_vmwareMgr.getNexusVSwitchGlobalParameter()) { |
| 112 | + // Get physical network label |
| 113 | + privateTrafficLabel = _netmgr.getDefaultManagementTrafficLabel(dcId, HypervisorType.VMware); |
| 114 | + if (privateTrafficLabel != null) { |
| 115 | + s_logger.info("Detected private network label : " + privateTrafficLabel); |
| 116 | + } |
| 117 | + // Get credentials |
| 118 | + vsmCredentials = _vmwareMgr.getNexusVSMCredentialsByClusterId(clusterId); |
| 119 | + } |
| 120 | + |
| 121 | + VmwareContext context = null; |
| 122 | + try { |
108 | 123 | context = VmwareContextFactory.create(url.getHost(), username, password); |
109 | | - List<ManagedObjectReference> morHosts = _vmwareMgr.addHostToPodCluster(context, dcId, podId, clusterId, |
| 124 | + if (_vmwareMgr.getNexusVSwitchGlobalParameter()) { |
| 125 | + // Get physical network label |
| 126 | + privateTrafficLabel = _netmgr.getDefaultManagementTrafficLabel(dcId, HypervisorType.VMware); |
| 127 | + if (privateTrafficLabel != null) { |
| 128 | + context.registerStockObject("privateTrafficLabel", privateTrafficLabel); |
| 129 | + s_logger.info("Detected private network label : " + privateTrafficLabel); |
| 130 | + } |
| 131 | + // Get credentials |
| 132 | + vsmCredentials = _vmwareMgr.getNexusVSMCredentialsByClusterId(clusterId); |
| 133 | + if (vsmCredentials != null) |
| 134 | + context.registerStockObject("vsmCredentials", vsmCredentials); |
| 135 | + } |
| 136 | + List<ManagedObjectReference> morHosts = _vmwareMgr.addHostToPodCluster(context, dcId, podId, clusterId, |
110 | 137 | URLDecoder.decode(url.getPath())); |
111 | | - if(morHosts == null) { |
112 | | - s_logger.error("Unable to find host or cluster based on url: " + URLDecoder.decode(url.getPath())); |
113 | | - return null; |
114 | | - } |
115 | | - |
116 | | - ManagedObjectReference morCluster = null; |
117 | | - Map<String, String> clusterDetails = _clusterDetailsDao.findDetails(clusterId); |
118 | | - if(clusterDetails.get("url") != null) { |
119 | | - URI uriFromCluster = new URI(UriUtils.encodeURIComponent(clusterDetails.get("url"))); |
120 | | - morCluster = context.getHostMorByPath(URLDecoder.decode(uriFromCluster.getPath())); |
121 | | - |
122 | | - if(morCluster == null || !morCluster.getType().equalsIgnoreCase("ClusterComputeResource")) { |
123 | | - s_logger.warn("Cluster url does not point to a valid vSphere cluster, url: " + clusterDetails.get("url")); |
124 | | - return null; |
125 | | - } else { |
126 | | - ClusterMO clusterMo = new ClusterMO(context, morCluster); |
127 | | - ClusterDasConfigInfo dasConfig = clusterMo.getDasConfig(); |
128 | | - if(dasConfig != null && dasConfig.getEnabled() != null && dasConfig.getEnabled().booleanValue()) { |
129 | | - clusterDetails.put("NativeHA", "true"); |
130 | | - _clusterDetailsDao.persist(clusterId, clusterDetails); |
131 | | - } |
132 | | - } |
133 | | - } |
134 | | - |
135 | | - if(!validateDiscoveredHosts(context, morCluster, morHosts)) { |
136 | | - if(morCluster == null) |
137 | | - s_logger.warn("The discovered host is not standalone host, can not be added to a standalone cluster"); |
138 | | - else |
139 | | - s_logger.warn("The discovered host does not belong to the cluster"); |
140 | | - return null; |
141 | | - } |
142 | | - |
143 | | - Map<VmwareResource, Map<String, String>> resources = new HashMap<VmwareResource, Map<String, String>>(); |
144 | | - for(ManagedObjectReference morHost : morHosts) { |
145 | | - Map<String, String> details = new HashMap<String, String>(); |
146 | | - Map<String, Object> params = new HashMap<String, Object>(); |
147 | | - |
148 | | - HostMO hostMo = new HostMO(context, morHost); |
149 | | - details.put("url", hostMo.getHostName()); |
150 | | - details.put("username", username); |
151 | | - details.put("password", password); |
152 | | - String guid = morHost.getType() + ":" + morHost.get_value() + "@"+ url.getHost(); |
153 | | - details.put("guid", guid); |
154 | | - |
155 | | - params.put("url", hostMo.getHostName()); |
156 | | - params.put("username", username); |
157 | | - params.put("password", password); |
158 | | - params.put("zone", Long.toString(dcId)); |
159 | | - params.put("pod", Long.toString(podId)); |
160 | | - params.put("cluster", Long.toString(clusterId)); |
| 138 | + if (privateTrafficLabel != null) |
| 139 | + context.uregisterStockObject("privateTrafficLabel"); |
| 140 | + |
| 141 | + if(morHosts == null) { |
| 142 | + s_logger.error("Unable to find host or cluster based on url: " + URLDecoder.decode(url.getPath())); |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + ManagedObjectReference morCluster = null; |
| 147 | + Map<String, String> clusterDetails = _clusterDetailsDao.findDetails(clusterId); |
| 148 | + if(clusterDetails.get("url") != null) { |
| 149 | + URI uriFromCluster = new URI(UriUtils.encodeURIComponent(clusterDetails.get("url"))); |
| 150 | + morCluster = context.getHostMorByPath(URLDecoder.decode(uriFromCluster.getPath())); |
| 151 | + |
| 152 | + if(morCluster == null || !morCluster.getType().equalsIgnoreCase("ClusterComputeResource")) { |
| 153 | + s_logger.warn("Cluster url does not point to a valid vSphere cluster, url: " + clusterDetails.get("url")); |
| 154 | + return null; |
| 155 | + } else { |
| 156 | + ClusterMO clusterMo = new ClusterMO(context, morCluster); |
| 157 | + ClusterDasConfigInfo dasConfig = clusterMo.getDasConfig(); |
| 158 | + if(dasConfig != null && dasConfig.getEnabled() != null && dasConfig.getEnabled().booleanValue()) { |
| 159 | + clusterDetails.put("NativeHA", "true"); |
| 160 | + _clusterDetailsDao.persist(clusterId, clusterDetails); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if(!validateDiscoveredHosts(context, morCluster, morHosts)) { |
| 166 | + if(morCluster == null) |
| 167 | + s_logger.warn("The discovered host is not standalone host, can not be added to a standalone cluster"); |
| 168 | + else |
| 169 | + s_logger.warn("The discovered host does not belong to the cluster"); |
| 170 | + return null; |
| 171 | + } |
| 172 | + |
| 173 | + Map<VmwareResource, Map<String, String>> resources = new HashMap<VmwareResource, Map<String, String>>(); |
| 174 | + for(ManagedObjectReference morHost : morHosts) { |
| 175 | + Map<String, String> details = new HashMap<String, String>(); |
| 176 | + Map<String, Object> params = new HashMap<String, Object>(); |
| 177 | + |
| 178 | + HostMO hostMo = new HostMO(context, morHost); |
| 179 | + details.put("url", hostMo.getHostName()); |
| 180 | + details.put("username", username); |
| 181 | + details.put("password", password); |
| 182 | + String guid = morHost.getType() + ":" + morHost.get_value() + "@"+ url.getHost(); |
| 183 | + details.put("guid", guid); |
| 184 | + |
| 185 | + params.put("url", hostMo.getHostName()); |
| 186 | + params.put("username", username); |
| 187 | + params.put("password", password); |
| 188 | + params.put("zone", Long.toString(dcId)); |
| 189 | + params.put("pod", Long.toString(podId)); |
| 190 | + params.put("cluster", Long.toString(clusterId)); |
161 | 191 | params.put("guid", guid); |
162 | | - |
163 | | - VmwareResource resource = new VmwareResource(); |
164 | | - try { |
165 | | - resource.configure("VMware", params); |
166 | | - } catch (ConfigurationException e) { |
167 | | - _alertMgr.sendAlert(AlertManager.ALERT_TYPE_HOST, dcId, podId, "Unable to add " + url.getHost(), "Error is " + e.getMessage()); |
168 | | - s_logger.warn("Unable to instantiate " + url.getHost(), e); |
169 | | - } |
170 | | - resource.start(); |
171 | | - |
172 | | - resources.put(resource, details); |
173 | | - } |
174 | | - |
175 | | - // place a place holder guid derived from cluster ID |
176 | | - cluster.setGuid(UUID.nameUUIDFromBytes(String.valueOf(clusterId).getBytes()).toString()); |
177 | | - _clusterDao.update(clusterId, cluster); |
178 | | - |
| 192 | + if (privateTrafficLabel != null) { |
| 193 | + params.put("private.network.vswitch.name", privateTrafficLabel); |
| 194 | + } |
| 195 | + |
| 196 | + VmwareResource resource = new VmwareResource(); |
| 197 | + try { |
| 198 | + resource.configure("VMware", params); |
| 199 | + } catch (ConfigurationException e) { |
| 200 | + _alertMgr.sendAlert(AlertManager.ALERT_TYPE_HOST, dcId, podId, "Unable to add " + url.getHost(), "Error is " + e.getMessage()); |
| 201 | + s_logger.warn("Unable to instantiate " + url.getHost(), e); |
| 202 | + } |
| 203 | + resource.start(); |
| 204 | + |
| 205 | + resources.put(resource, details); |
| 206 | + } |
| 207 | + |
| 208 | + // place a place holder guid derived from cluster ID |
| 209 | + cluster.setGuid(UUID.nameUUIDFromBytes(String.valueOf(clusterId).getBytes()).toString()); |
| 210 | + _clusterDao.update(clusterId, cluster); |
| 211 | + |
179 | 212 | return resources; |
180 | 213 | } catch (DiscoveredWithErrorException e) { |
181 | 214 | throw e; |
|
0 commit comments