public static L3NetworkInventory valueOf(L3NetworkVO vo) { L3NetworkInventory inv = new L3NetworkInventory(); inv.setUuid(vo.getUuid()); inv.setCreateDate(vo.getCreateDate()); inv.setDescription(vo.getDescription()); inv.setL2NetworkUuid(vo.getL2NetworkUuid()); inv.setName(vo.getName()); inv.setType(vo.getType()); inv.setDnsDomain(vo.getDnsDomain()); inv.setZoneUuid(vo.getZoneUuid()); inv.setState(vo.getState().toString()); vo.getDns().stream().sorted(Comparator.comparingLong(L3NetworkDnsVO::getId)).collect(Collectors.toList()) .forEach(dnsVo -> inv.getDns().add(dnsVo.getDns())); inv.setSystem(vo.isSystem()); inv.setIpRanges(IpRangeInventory.valueOf(vo.getIpRanges())); inv.setLastOpDate(vo.getLastOpDate()); inv.setNetworkServices(NetworkServiceL3NetworkRefInventory.valueOf(vo.getNetworkServices())); inv.setCategory(vo.getCategory().toString()); inv.setHostRoute(L3NetworkHostRouteInventory.valueOf(vo.getHostRoutes())); inv.setIpVersion(vo.getIpVersion()); return inv; }
@Override protected L3NetworkInventory scripts() { vo.setAccountUuid(msg.getSession().getAccountUuid()); L3NetworkInventory inv = factory.createL3Network(vo, msg); tagMgr.createTagsFromAPICreateMessage(msg, vo.getUuid(), L3NetworkVO.class.getSimpleName()); return inv; } }.execute();
private void handle(final AttachNetworkServiceToL3Msg msg) { MessageReply reply = new MessageReply(); for (Map.Entry<String, List<String>> e : msg.getNetworkServices().entrySet()) { for (String nsType : e.getValue()) { NetworkServiceL3NetworkRefVO ref = new NetworkServiceL3NetworkRefVO(); ref.setL3NetworkUuid(self.getUuid()); ref.setNetworkServiceProviderUuid(e.getKey()); ref.setNetworkServiceType(nsType); dbf.persist(ref); } logger.debug(String.format("successfully attached network service provider[uuid:%s] to l3network[uuid:%s, name:%s] with services%s", e.getKey(), self.getUuid(), self.getName(), e.getValue())); } bus.reply(msg, reply); }
if (l3Vo.getIpVersion() != IPv6Constants.IPv6) { throw new ApiMessageInterceptionException(argerr("l3 network [uuid %s: name %s] is not a ipv6 network", l3Vo.getUuid(), l3Vo.getName())); List<String> l3Uuids = Q.New(L3NetworkVO.class).eq(L3NetworkVO_.l2NetworkUuid, l3Vo.getL2NetworkUuid()).select(L3NetworkVO_.uuid).listValues(); SimpleQuery<IpRangeVO> q = dbf.createQuery(IpRangeVO.class); q.add(IpRangeVO_.l3NetworkUuid, Op.IN, l3Uuids);
if (l3Vo.getIpVersion() == IPv6Constants.IPv4) { for (UsedIpVO ipVO : vmNicVO.getUsedIps()) { if (ipVO.getIpVersion() == IPv6Constants.IPv4) { if (l3Vo.getIpVersion() == IPv6Constants.IPv4) { validateStaticIPv4(vmNicVO, l3Vo, msg.getStaticIp()); } else if (l3Vo.getIpVersion() == IPv6Constants.IPv6) { validateStaticIPv6(vmNicVO, l3Vo, msg.getStaticIp()); if (!oldL3.getL2NetworkUuid().equals(l3Vo.getL2NetworkUuid())) { throw new ApiMessageInterceptionException(argerr("l2Network [uuid:%s] to be attached is different from " + "l2Network [uuid:%s] of the nic", l3Vo.getL2NetworkUuid(), oldL3.getL2NetworkUuid()));
private List<String> getL3NetworkDns(String l3NetworkUuid){ List<String> dns = Q.New(L3NetworkDnsVO.class).eq(L3NetworkDnsVO_.l3NetworkUuid, l3NetworkUuid) .select(L3NetworkDnsVO_.dns).orderBy(L3NetworkDnsVO_.id, SimpleQuery.Od.ASC).listValues(); if (dns == null) { dns = new ArrayList<String>(); } L3NetworkVO l3VO = Q.New(L3NetworkVO.class).eq(L3NetworkVO_.uuid, l3NetworkUuid).find(); if (FlatNetwordProviderGlobalConfig.ALLOW_DEFAULT_DNS.value(Boolean.class) && l3VO.getIpVersion() == IPv6Constants.IPv4) { String dhcpIp = getDHCPServerIP(l3NetworkUuid); if (dhcpIp != null) { dns.add(dhcpIp); } } return dns; }
for (String l3Uuid : newAddedL3Uuids) { L3NetworkVO l3Vo = dbf.findByUuid(l3Uuid, L3NetworkVO.class); if (l3Vo.getState() == L3NetworkState.Disabled) { throw new ApiMessageInterceptionException(operr("unable to attach a L3 network. The L3 network[uuid:%s] is disabled", l3Uuid)); if (VmInstanceConstant.USER_VM_TYPE.equals(type) && l3Vo.isSystem()) { throw new ApiMessageInterceptionException(operr("unable to attach a L3 network. The L3 network[uuid:%s] is a system network and vm is a user vm", l3Uuid)); if (l3Vo.getIpVersion() == IPv6Constants.IPv4) { ipv4Count++; } else {
L2NetworkVO l2NetworkVO = Q.New(L2NetworkVO.class).eq(L2NetworkVO_.uuid, l3NetworkVO.getL2NetworkUuid()).find(); mto.setCategory(l3NetworkVO.getCategory().toString()); mto.setL2type(l2NetworkVO.getType()); mto.setPhysicalInterface(l2NetworkVO.getPhysicalInterface()); l2NetworkVO = Q.New(L2NetworkVO.class).eq(L2NetworkVO_.uuid, l3NetworkVO.getL2NetworkUuid()).find(); t.setCategory(l3NetworkVO.getCategory().toString()); t.setL2type(l2NetworkVO.getType()); t.setVni(l2NetworkGetVniExtensionPointMap.get(l2NetworkVO.getType()).getL2NetworkVni(l2NetworkVO.getUuid())); nto.setDeviceName(String.format("eth%s", deviceId)); l3NetworkVO = Q.New(L3NetworkVO.class).eq(L3NetworkVO_.uuid, nic.getL3NetworkUuid()).find(); l2NetworkVO = Q.New(L2NetworkVO.class).eq(L2NetworkVO_.uuid, l3NetworkVO.getL2NetworkUuid()).find(); nto.setCategory(l3NetworkVO.getCategory().toString()); nto.setL2type(l2NetworkVO.getType()); nto.setVni(l2NetworkGetVniExtensionPointMap.get(l2NetworkVO.getType()).getL2NetworkVni(l2NetworkVO.getUuid()));
Map<String, L3NetworkVO> l3Map = new HashMap<String, L3NetworkVO>(); for (L3NetworkVO l3 : l3s) { l3Map.put(l3.getUuid(), l3); info.dnsDomain = l3.getDnsDomain(); info.dns = getL3NetworkDns(ip.getL3NetworkUuid()); info.firstIp = NetworkUtils.getSmallestIp(l3.getIpRanges().stream().map(IpRangeAO::getStartIp).collect(Collectors.toList())); info.endIp = NetworkUtils.getBiggesttIp(l3.getIpRanges().stream().map(IpRangeAO::getEndIp).collect(Collectors.toList())); info.prefixLength = l3.getIpRanges().stream().findAny().map(IpRangeAO::getPrefixLen).orElse(null); info.l3NetworkUuid = l3.getUuid(); info.hostRoutes = getL3NetworkHostRoute(ip.getL3NetworkUuid());
private Boolean isNetworkAddressEqual(String networkUuid1, String networkUuid2) { if (networkUuid1.equals(networkUuid2)) { return true; } L3NetworkVO l3vo1 = Q.New(L3NetworkVO.class).eq(L3NetworkVO_.uuid, networkUuid1).find(); L3NetworkVO l3vo2 = Q.New(L3NetworkVO.class).eq(L3NetworkVO_.uuid, networkUuid2).find(); if (!l3vo1.getIpVersion().equals(l3vo2.getIpVersion())) { return false; } List<IpRangeInventory> ipInvs1 = IpRangeInventory.valueOf(l3vo1.getIpRanges()); List<IpRangeInventory> ipInvs2 = IpRangeInventory.valueOf(l3vo2.getIpRanges()); if (l3vo1.getIpVersion() == IPv6Constants.IPv4) { String netAddr1 = new SubnetUtils(ipInvs1.get(0).getGateway(), ipInvs1.get(0).getNetmask()).getInfo().getNetworkAddress(); String netAddr2 = new SubnetUtils(ipInvs2.get(0).getGateway(), ipInvs2.get(0).getNetmask()).getInfo().getNetworkAddress(); return netAddr1.equals(netAddr2); } else if (l3vo1.getIpVersion() == IPv6Constants.IPv6) { return IPv6NetworkUtils.isIpv6CidrEqual(ipInvs1.get(0).getNetworkCidr(), ipInvs2.get(0).getNetworkCidr()); } return false; } private void validate(APICreateVirtualRouterOfferingMsg msg) {
private String getSyncId() { return String.format("operate-l3-%s", self.getUuid()); }
private void validate(APIAttachVmNicToVmMsg msg) { VmInstanceVO vmInstanceVO = dbf.findByUuid(msg.getVmInstanceUuid(), VmInstanceVO.class); String type = vmInstanceVO.getType(); VmInstanceState state = vmInstanceVO.getState(); if (!VmInstanceState.Running.equals(state) && !VmInstanceState.Stopped.equals(state)) { throw new ApiMessageInterceptionException(operr("unable to attach a L3 network. The vm[uuid: %s] is not Running or Stopped; the current state is %s", msg.getVmInstanceUuid(), state)); } VmNicVO vmNicVO = dbf.findByUuid(msg.getVmNicUuid(), VmNicVO.class); boolean exist = Q.New(VmNicVO.class) .eq(VmNicVO_.l3NetworkUuid, vmNicVO.getL3NetworkUuid()) .eq(VmNicVO_.vmInstanceUuid, msg.getVmInstanceUuid()) .isExists(); if (exist) { throw new ApiMessageInterceptionException(operr("unable to attach a L3 network. The L3 network[uuid:%s] is already attached to the vm[uuid: %s]", vmNicVO.getL3NetworkUuid(), msg.getVmInstanceUuid())); } L3NetworkVO l3NetworkVO = dbf.findByUuid(vmNicVO.getL3NetworkUuid(), L3NetworkVO.class); L3NetworkState l3state = l3NetworkVO.getState(); boolean system = l3NetworkVO.isSystem(); if (l3state == L3NetworkState.Disabled) { throw new ApiMessageInterceptionException(operr("unable to attach a L3 network. The L3 network[uuid:%s] is disabled", l3NetworkVO.getUuid())); } if (VmInstanceConstant.USER_VM_TYPE.equals(type) && system) { throw new ApiMessageInterceptionException(operr("unable to attach a L3 network. The L3 network[uuid:%s] is a system network and vm is a user vm", l3NetworkVO.getUuid())); } }
private void handle(APIGetAttachablePublicL3ForVRouterMsg msg) { APIGetAttachablePublicL3ForVRouterReply reply = new APIGetAttachablePublicL3ForVRouterReply(); List<L3NetworkVO> l3NetworkVOS = Q.New(L3NetworkVO.class).notEq(L3NetworkVO_.category, L3NetworkCategory.Private).list(); List<VmNicVO> vmNicVOS = Q.New(VmNicVO.class).eq(VmNicVO_.vmInstanceUuid, msg.getVmInstanceUuid()).list(); if (l3NetworkVOS == null || l3NetworkVOS.isEmpty()) { reply.setInventories(new ArrayList<L3NetworkInventory>()); bus.reply(msg, reply); return; } Set<L3NetworkVO> attachableL3NetworkVOS = new HashSet<>(l3NetworkVOS); for (L3NetworkVO l3NetworkVO : l3NetworkVOS) { for (VmNicVO vmNicVO : vmNicVOS) { if (l3NetworkVO.getIpRanges() == null || l3NetworkVO.getIpRanges().isEmpty()) { attachableL3NetworkVOS.remove(l3NetworkVO); } String vmNicCidr = NetworkUtils.getCidrFromIpMask(vmNicVO.getIp(), vmNicVO.getNetmask()); if (NetworkUtils.isCidrOverlap(l3NetworkVO.getIpRanges().stream().findFirst().get().getNetworkCidr(), vmNicCidr)) { attachableL3NetworkVOS.remove(l3NetworkVO); } attachableL3NetworkVOS.removeAll(attachableL3NetworkVOS.stream() .filter(vo -> vo.getUuid().equals(vmNicVO.getL3NetworkUuid())) .collect(Collectors.toSet())); } } reply.setInventories(L3NetworkInventory.valueOf(attachableL3NetworkVOS)); bus.reply(msg, reply); }
String type = Q.New(L2NetworkVO.class).select(L2NetworkVO_.type).eq(L2NetworkVO_.uuid, l3.getL2NetworkUuid()).findValue(); if (type.equals(VxlanNetworkConstant.VXLAN_NETWORK_TYPE)) { vxlanUuids.add(l3.getL2NetworkUuid());
private void passThrough(String l3NetworkUuid, Message msg) { L3NetworkVO vo = dbf.findByUuid(l3NetworkUuid, L3NetworkVO.class); if (vo == null && allowedMessageAfterSoftDeletion.contains(msg.getClass())) { L3NetworkEO eo = dbf.findByUuid(l3NetworkUuid, L3NetworkEO.class); vo = ObjectUtils.newAndCopy(eo, L3NetworkVO.class); } if (vo == null) { ErrorCode err = err(SysErrors.RESOURCE_NOT_FOUND, "Unable to find L3Network[uuid:%s], it may have been deleted", l3NetworkUuid); bus.replyErrorByMessageType(msg, err); return; } L3NetworkFactory factory = getL3NetworkFactory(L3NetworkType.valueOf(vo.getType())); L3Network nw = factory.getL3Network(vo); nw.handleMessage(msg); }
@Transactional(readOnly = true) private String findProviderTypeByVmNicUuid(String nicUuid) { String sql = "select l3 from L3NetworkVO l3, VmNicVO nic where nic.l3NetworkUuid = l3.uuid and nic.uuid = :uuid"; TypedQuery<L3NetworkVO> q = dbf.getEntityManager().createQuery(sql, L3NetworkVO.class); q.setParameter("uuid", nicUuid); L3NetworkVO l3 = q.getSingleResult(); for (NetworkServiceL3NetworkRefVO ref : l3.getNetworkServices()) { if (LoadBalancerConstants.LB_NETWORK_SERVICE_TYPE_STRING.equals(ref.getNetworkServiceType())) { sql = "select p.type from NetworkServiceProviderVO p where p.uuid = :uuid"; TypedQuery<String> nq = dbf.getEntityManager().createQuery(sql, String.class); nq.setParameter("uuid", ref.getNetworkServiceProviderUuid()); return nq.getSingleResult(); } } return null; }
L3NetworkVO l3NetworkVO = Q.New(L3NetworkVO.class).eq(L3NetworkVO_.uuid, vo.getL3NetworkUuid()).find(); if (l3NetworkVO.getCategory().equals(L3NetworkCategory.Private)) { vo.setMetaData(GUEST_NIC_MASK.toString());
if (l3Vo.isSystem()) { throw new ApiMessageInterceptionException(argerr("DHCP server ip [%s] can not be configured to system l3", dhcpServerIp));
private void validate(IpRangeInventory ipr) { L3NetworkVO l3Vo = Q.New(L3NetworkVO.class).eq(L3NetworkVO_.uuid, ipr.getL3NetworkUuid()).find(); if (l3Vo.getIpVersion() != IPv6Constants.IPv4) { throw new ApiMessageInterceptionException(argerr("l3 network [uuid %s: name %s] is not a ipv4 network", l3Vo.getUuid(), l3Vo.getName())); List<String> l3Uuids = Q.New(L3NetworkVO.class).eq(L3NetworkVO_.l2NetworkUuid, l3Vo.getL2NetworkUuid()).select(L3NetworkVO_.uuid).listValues(); SimpleQuery<IpRangeVO> q = dbf.createQuery(IpRangeVO.class); q.add(IpRangeVO_.l3NetworkUuid, Op.IN, l3Uuids);
.findValue(); L3NetworkVO l3Vo = Q.New(L3NetworkVO.class).eq(L3NetworkVO_.uuid, vip.getL3NetworkUuid()).find(); List<String> clusterUuids = Q.New(L2NetworkClusterRefVO.class).eq(L2NetworkClusterRefVO_.l2NetworkUuid, l3Vo.getL2NetworkUuid()) .select(L2NetworkClusterRefVO_.clusterUuid).listValues(); .param("vipL3NetworkUuid", vip.getL3NetworkUuid()) .param("clusterUuids", clusterUuids) .param("ipVersion", l3Vo.getIpVersion()) .list(); } else { .param("vipL3NetworkUuid", vip.getL3NetworkUuid()) .param("clusterUuids", clusterUuids) .param("ipVersion", l3Vo.getIpVersion()) .list(); .param("vmType", VmInstanceConstant.USER_VM_TYPE) .param("vmStates", EipConstant.attachableVmStates) .param("ipVersion", l3Vo.getIpVersion()) .param("vmInPublicL3s", vmInPublicL3s) .list(); .param("vmType", VmInstanceConstant.USER_VM_TYPE) .param("vmStates", EipConstant.attachableVmStates) .param("ipVersion", l3Vo.getIpVersion()) .list();