@Test public void test() throws ApiSenderException { EipInventory eip = deployer.eips.get("eip"); VmInstanceInventory vm = deployer.vms.get("TestVm"); VmNicInventory nic = vm.getVmNics().get(0); IpRangeInventory ipr = deployer.ipRanges.get("GuestIpRange"); String newIp = null; long s = NetworkUtils.ipv4StringToLong(ipr.getStartIp()); long e = NetworkUtils.ipv4StringToLong(ipr.getEndIp()); for (long l = s; s < e; s++) { String ip = NetworkUtils.longToIpv4String(l); if (!ip.equals(nic.getIp())) { newIp = ip; break; } } Assert.assertNotNull(newIp); api.stopVmInstance(vm.getUuid()); api.setStaticIp(vm.getUuid(), nic.getL3NetworkUuid(), newIp); EipVO eipvo = dbf.findByUuid(eip.getUuid(), EipVO.class); Assert.assertEquals(newIp, eipvo.getGuestIp()); }
@Transactional private List<PortForwardingRuleVO> findRulesForThisRouter( VirtualRouterVmInventory vr, Map<String, Object> data, boolean isNewCreated) { if (!isNewCreated) { String sql = "select rule from PortForwardingRuleVO rule, VirtualRouterPortForwardingRuleRefVO ref, VmNicVO nic, VmInstanceVO vm where vm.state = :vmState and nic.vmInstanceUuid = vm.uuid and rule.vmNicUuid = nic.uuid and rule.uuid = ref.uuid and ref.virtualRouterVmUuid = :vrUuid"; TypedQuery<PortForwardingRuleVO> q = dbf.getEntityManager().createQuery(sql, PortForwardingRuleVO.class); q.setParameter("vrUuid", vr.getUuid()); q.setParameter("vmState", VmInstanceState.Running); return q.getResultList(); } else { VmNicInventory publicNic = vr.getPublicNic(); VmNicInventory guestNic = vr.getGuestNic(); String sql = "select rule from PortForwardingRuleVO rule, VipVO vip, VmNicVO nic, VmInstanceVO vm where vm.uuid = nic.vmInstanceUuid and vm.state = :vmState and rule.vipUuid = vip.uuid and rule.vmNicUuid = nic.uuid and vip.l3NetworkUuid = :vipL3Uuid and nic.l3NetworkUuid = :guestL3Uuid"; TypedQuery<PortForwardingRuleVO> q = dbf.getEntityManager().createQuery(sql, PortForwardingRuleVO.class); q.setParameter("vipL3Uuid", publicNic.getL3NetworkUuid()); q.setParameter("guestL3Uuid", guestNic.getL3NetworkUuid()); q.setParameter("vmState", VmInstanceState.Running); List<PortForwardingRuleVO> rules = q.getResultList(); if (!rules.isEmpty()) { List<VirtualRouterPortForwardingRuleRefVO> refs = new ArrayList<VirtualRouterPortForwardingRuleRefVO>(); for (PortForwardingRuleVO rule : rules) { VirtualRouterPortForwardingRuleRefVO ref = new VirtualRouterPortForwardingRuleRefVO(); ref.setVirtualRouterVmUuid(vr.getUuid()); ref.setVipUuid(rule.getVipUuid()); ref.setUuid(rule.getUuid()); dbf.getEntityManager().persist(ref); refs.add(ref); } data.put(VirtualRouterSyncPortForwardingRulesOnStartFlow.class.getName(), refs); } return rules; } }
@Test public void test() throws ApiSenderException { final L3NetworkInventory l3 = deployer.l3Networks.get("TestL3Network4"); VmInstanceInventory vm = deployer.vms.get("TestVm"); vm = api.attachNic(vm.getUuid(), l3.getUuid()); Assert.assertEquals(4, vm.getVmNics().size()); VmNicInventory nic = vm.getVmNics().get(0); vm = api.detachNic(nic.getUuid()); Assert.assertEquals(3, vm.getVmNics().size()); Assert.assertFalse(config.detachNicCommands.isEmpty()); String l3Uuid = nic.getL3NetworkUuid(); nic = vm.findNic(l3Uuid); Assert.assertNull(nic); api.stopVmInstance(vm.getUuid()); vm = api.startVmInstance(vm.getUuid()); Assert.assertEquals(3, vm.getVmNics().size()); nic = vm.findNic(l3Uuid); Assert.assertNull(nic); }
@Test public void test() throws ApiSenderException, InterruptedException { VmInstanceInventory vm = deployer.vms.get("TestVm"); final L3NetworkInventory l31 = deployer.l3Networks.get("TestL3Network1"); final L3NetworkInventory l32 = deployer.l3Networks.get("TestL3Network2"); VmNicInventory nic1 = CollectionUtils.find( vm.getVmNics(), new Function<VmNicInventory, VmNicInventory>() { @Override public VmNicInventory call(VmNicInventory arg) { return arg.getL3NetworkUuid().equals(l31.getUuid()) ? arg : null; } }); VmNicInventory nic2 = CollectionUtils.find( vm.getVmNics(), new Function<VmNicInventory, VmNicInventory>() { @Override public VmNicInventory call(VmNicInventory arg) { return arg.getL3NetworkUuid().equals(l32.getUuid()) ? arg : null; } }); VmInstanceInventory update = new VmInstanceInventory(); update.setUuid(vm.getUuid()); update.setDefaultL3NetworkUuid(l32.getUuid()); api.updateVm(update); TimeUnit.SECONDS.sleep(2); Assert.assertEquals(1, fconfig.resetDefaultGatewayCmds.size()); ResetDefaultGatewayCmd cmd = fconfig.resetDefaultGatewayCmds.get(0); Assert.assertEquals(nic1.getMac(), cmd.macOfGatewayToRemove); Assert.assertEquals(nic1.getGateway(), cmd.gatewayToRemove); Assert.assertEquals( new BridgeNameFinder().findByL3Uuid(l31.getUuid()), cmd.bridgeNameOfGatewayToRemove); Assert.assertEquals(nic2.getMac(), cmd.macOfGatewayToAdd); Assert.assertEquals(nic2.getGateway(), cmd.gatewayToAdd); Assert.assertEquals( new BridgeNameFinder().findByL3Uuid(l32.getUuid()), cmd.bridgeNameOfGatewayToAdd); }
@Override public void run(final FlowTrigger chain, Map data) { final VirtualRouterVmInventory vr = (VirtualRouterVmInventory) data.get(VirtualRouterConstant.Param.VR.toString()); VmNicInventory guestNic = vr.getGuestNic(); if (!vrMgr.isL3NetworkNeedingNetworkServiceByVirtualRouter( guestNic.getL3NetworkUuid(), PortForwardingConstant.PORTFORWARDING_NETWORK_SERVICE_TYPE)) { chain.next(); return; } boolean isNewCreated = data.containsKey(Param.IS_NEW_CREATED.toString()); List<PortForwardingRuleVO> ruleVOs = findRulesForThisRouter(vr, data, isNewCreated); if (ruleVOs.isEmpty()) { chain.next(); return; } Map<String, PortForwardingRuleVO> ruleMap = new HashMap<String, PortForwardingRuleVO>(ruleVOs.size()); for (PortForwardingRuleVO rvo : ruleVOs) { ruleMap.put(rvo.getUuid(), rvo); } Collection<PortForwardingRuleTO> tos = calculateAllRules(ruleMap, vr.getUuid()); List<PortForwardingRuleTO> toList = new ArrayList<PortForwardingRuleTO>(tos.size()); toList.addAll(tos); SyncPortForwardingRuleCmd cmd = new SyncPortForwardingRuleCmd(); cmd.setRules(toList); VirtualRouterAsyncHttpCallMsg msg = new VirtualRouterAsyncHttpCallMsg(); msg.setCommand(cmd); msg.setPath(VirtualRouterConstant.VR_SYNC_PORT_FORWARDING); msg.setVmInstanceUuid(vr.getUuid()); bus.makeTargetServiceIdByResourceUuid(msg, VmInstanceConstant.SERVICE_ID, vr.getUuid()); bus.send( msg, new CloudBusCallBack(chain) { @Override public void run(MessageReply reply) { if (!reply.isSuccess()) { chain.fail(reply.getError()); return; } VirtualRouterAsyncHttpCallReply re = reply.castReply(); SyncPortForwardingRuleRsp ret = re.toResponse(SyncPortForwardingRuleRsp.class); if (ret.isSuccess()) { String info = String.format( "successfully sync port forwarding rules served by virtual router[name: %s uuid: %s]", vr.getName(), vr.getUuid()); logger.debug(info); chain.next(); } else { String err = String.format( "failed to sync port forwarding rules served by virtual router[name: %s, uuid: %s], because %s", vr.getName(), vr.getUuid(), ret.getError()); logger.warn(err); chain.fail(errf.stringToOperationError(err)); } } }); }