use of com.emc.storageos.vnxe.requests.HostIpPortRequests in project coprhd-controller by CoprHD.
the class VNXeApiClient method getHosts.
/**
* get list host instances based on the endpoints
*
* @param endpoints
* ipAddress, hostname or subnet
* @return List of host instances
* @throws VNXeException
*/
private List<VNXeBase> getHosts(List<String> endpoints) throws VNXeException {
List<VNXeBase> hosts = null;
if (endpoints != null) {
hosts = new ArrayList<VNXeBase>();
for (String endpoint : endpoints) {
String ipAddress = null;
boolean isSubnet = false;
String netMask = null;
boolean isValid = true;
try {
if (VNXeUtils.isHostType(endpoint)) {
ipAddress = VNXeUtils.getHostIp(endpoint);
} else if (VNXeUtils.isIPV4Type(endpoint) || VNXeUtils.isIPV6Type(endpoint)) {
ipAddress = endpoint;
} else {
// check if subnet
String[] ends = endpoint.split("/");
if (ends != null && ends.length == 2) {
ipAddress = ends[0];
endpoint = ipAddress;
String mask = ends[1];
try {
// CIDR format?
int cidr = Integer.parseInt(mask);
netMask = VNXeUtils.convertCIDRToNetmask(cidr);
isSubnet = true;
} catch (NumberFormatException e) {
if (VNXeUtils.isIPV4Type(mask) || VNXeUtils.isIPV6Type(mask)) {
netMask = mask;
isSubnet = true;
} else {
isValid = false;
}
}
} else {
isValid = false;
}
}
} catch (UnknownHostException e) {
_logger.error("Could not resolve the host: " + endpoint);
throw VNXeException.exceptions.vnxeCommandFailed("Could not resolve the host: " + endpoint);
}
if (!isValid) {
_logger.error("Unsupported endpoint type: " + endpoint);
throw VNXeException.exceptions.vnxeCommandFailed("Unsupported endpoint type: " + endpoint);
}
HostIpPortRequests ipReq = new HostIpPortRequests(_khClient);
VNXeHostIpPort ipPort = ipReq.getIpPortByIpAddress(ipAddress);
VNXeBase host = null;
if (ipPort != null) {
// HostIPPort found
host = ipPort.getHost();
hosts.add(host);
} else {
// create host and ipPort
HostListRequest hostReq = new HostListRequest(_khClient);
HostCreateParam hostCreateParm = new HostCreateParam();
hostCreateParm.setName(endpoint);
if (isSubnet) {
hostCreateParm.setType(HostTypeEnum.SUBNET.getValue());
} else {
hostCreateParm.setType(HostTypeEnum.HOSTMANUAL.getValue());
}
VNXeCommandResult result = hostReq.createHost(hostCreateParm);
String hostId = result.getId();
if (hostId != null) {
HostIpPortRequests ipReq2 = new HostIpPortRequests(_khClient);
HostIpPortCreateParam ipCreateParm = new HostIpPortCreateParam();
host = new VNXeBase(hostId);
ipCreateParm.setHost(host);
ipCreateParm.setAddress(ipAddress);
if (isSubnet) {
ipCreateParm.setSubnetMask(netMask);
}
ipReq2.createHostIpPort(ipCreateParm);
hosts.add(host);
}
}
}
}
return hosts;
}
Aggregations