/**
* @Description: 创建外网
* @param:@param pn
* @param:@param np
* @param:@return
* @return:Network
* @throws:
* @author: lihaiyong
*/
public static Network createPublicNet(PublicNet pn, Map<String, String> params) {
NetworkingService networkingService = ConnOpenstackUtil.getNetworkingService();
NetworkService networkSer = networkingService.network();
//TODO 此处tenantid应该设置为租户Id
NetworkBuilder network2 = Builders.network().name(pn.getNetName()).tenantId(AccountUtis
.getOpenstackCurrentTenantId()).adminStateUp(true);
Network network = network2.networkType(NetworkType.VLAN)
.isRouterExternal(true)
.isShared(true)
.addAvailabilityZoneHints("nova")
.build();
Network result = networkSer.create(network);
return result;
}
/**
* @Description: 创建内网
* @param:
* @return:void
* @throws:
* @author: lihaiyong
*/
public static Network createPrivateNet(PrivateNet pn , Map<String, String> params) {
NetworkingService networkingService = ConnOpenstackUtil.getNetworkingService();
NetworkService networkSer = networkingService.network();
//TODO 此处tenantid应该设置为租户Id
NetworkBuilder network2 = Builders.network().name(pn.getNetName()).tenantId(AccountUtis
.getOpenstackCurrentTenantId()).adminStateUp(true);
Network network = network2.networkType(NetworkType.LOCAL)
.isRouterExternal(false)
.isShared(false)
.addAvailabilityZoneHints("nova")
.build();
Network result = networkSer.create(network);
return result;
}
/**
* @Description: 获取Network
* @param:@param pn
* @param:@param params
* @param:@return
* @return:Network
* @throws:
* @author: lihaiyong
*/
public static Network getNetwork(String networkId) {
try {
NetworkingService networkingService = ConnOpenstackUtil.getNetworkingService();
NetworkService networkSer = networkingService.network();
Network network = networkSer.get(networkId);
return network;
} catch (Exception e) {
String [] exStr = new String[5];
exStr[0] = "获取服务器网络错误";
exStr[1] = "网络Id: "+networkId;
throw new NetManageException("17006",exStr);
}
}
/**
* @Description: 创建外网子网
* @param:@param pcn
* @param:@param network
* @param:@param map
* @return:void
* @throws:
* @author: lihaiyong
*/
public static Subnet createPubChildNet(PublicChildNet pcn , Network network, Map<String, String> map) {
NetworkingService networkingService = ConnOpenstackUtil.getNetworkingService();
SubnetService subnetService = networkingService.subnet();
SubnetBuilder subnetBuilder = Builders.subnet().name(pcn.getNetName())
.tenantId(network.getTenantId())
.cidr(pcn.getCidr()+"/"+pcn.getMask())
.ipVersion(IPVersionType.V4)
.network(network)
.enableDHCP(true)
.addPool(pcn.getIpPoolStart(), pcn.getIpPoolEnd());
String dnsServer = pcn.getDNSServer();
String[] split = dnsServer.split("_");
for (String string : split) {
subnetBuilder.addDNSNameServer(string);
}
Subnet subnet = subnetService.create(subnetBuilder.build());
return subnet;
}
/**
* @Description: 创建内网子网
* @param:@param pcn
* @param:@param network
* @param:@param map
* @return:void
* @throws:
* @author: lihaiyong
*/
public static Subnet createPriChildNet(PrivateChildNet pcn , Network network, Map<String, String> map) {
NetworkingService networkingService = ConnOpenstackUtil.getNetworkingService();
SubnetService subnetService = networkingService.subnet();
SubnetBuilder subnetBuilder = Builders.subnet().name(pcn.getNetName())
.tenantId(network.getTenantId())
.cidr(pcn.getCidr()+"/"+pcn.getMask())
.ipVersion(IPVersionType.V4)
.network(network)
.enableDHCP(true)
.addPool(pcn.getIpPoolStart(), pcn.getIpPoolEnd());
String dnsServer = pcn.getDNSServer();
String[] split = dnsServer.split("_");
for (String string : split) {
subnetBuilder.addDNSNameServer(string);
}
Subnet subnet = subnetService.create(subnetBuilder.build());
return subnet;
}
/**
* @Description: 删除网络
* @param:@param net
* @return:void
* @throws:
* @author: lihaiyong
*/
public static void deleteNetworkById(String netId) {
NetworkingService networkingService = ConnOpenstackUtil.getNetworkingService();
NetworkService networkSer = networkingService.network();
networkSer.delete(netId);
}
/**
* @Description: 获取Subnet
* @param:@param pn
* @param:@param params
* @param:@return
* @return:Network
* @throws:
* @author: lihaiyong
*/
public static Subnet getSubNet(String subnetId) {
try {
NetworkingService networkingService = ConnOpenstackUtil.getNetworkingService();
SubnetService subnetSer = networkingService.subnet();
Subnet subnet = subnetSer.get(subnetId);
return subnet;
} catch (Exception e) {
String [] exStr = new String[5];
exStr[0] = "获取服务器网络错误";
exStr[1] = "子网络Id: "+subnetId;
throw new NetManageException("17006",exStr);
}
}
/**
* @Description: 删除子网
* @param:@param subnet
* @return:void
* @throws:
* @author: lihaiyong
*/
public static void deleteSubnetById(String netId) {
NetworkingService networkingService = ConnOpenstackUtil.getNetworkingService();
SubnetService subnetService = networkingService.subnet();
try {
subnetService.delete(netId);
} catch (Exception e) {
String [] exStr = new String[5];
exStr[0] = "未找到服务器相关资源";
throw new NetManageException("17007",exStr);
}
}
/**
* @Description: 修改网络
* @param:@param net
* @return:void
* @throws:
* @author: lihaiyong
*/
public static void updateNetworkById(String netId, NetworkUpdate builders) {
NetworkingService networkingService = ConnOpenstackUtil.getNetworkingService();
NetworkService networkSer = networkingService.network();
try {
networkSer.update(netId, builders);
} catch (Exception e) {
String [] exStr = new String[5];
exStr[0] = "未找到服务器相关资源";
throw new NetManageException("17007",exStr);
}
}
/**
* @Description: 修改子网络
* @param:@param net
* @return:void
* @throws:
* @author: lihaiyong
*/
public static void updateSubnetById(String netId, Subnet subnet) {
NetworkingService networkingService = ConnOpenstackUtil.getNetworkingService();
SubnetService subnetSer = networkingService.subnet();
try {
subnetSer.update(netId, subnet);
} catch (Exception e) {
String [] exStr = new String[5];
exStr[0] = "未找到服务器相关资源"+subnet.toString();
throw new NetManageException("17007",exStr);
}
}
/**
* @Description: 获取弹性Ip
* @throws:
* @version: v1.0.0
* @author: lihaiyong
* @date:2018年5月2日
*/
public static FloatingIP allocateIP(String poolName) {
OSClient<?> osClient = ConnOpenstackUtil.getOSClient();
FloatingIP ip = osClient.compute().floatingIps().allocateIP(poolName);
return ip;
}
/**
* @Description: 删除弹性Ip
* @throws:
* @version: v1.0.0
* @author: lihaiyong
* @date:2018年5月2日
*/
public static boolean deallocateIP(String floatIpId) {
OSClient<?> osClient = ConnOpenstackUtil.getOSClient();
ActionResponse deallocateIP = osClient.compute().floatingIps().deallocateIP(floatIpId);
boolean success = deallocateIP.isSuccess();
return success;
}