package org.zstack.test.network.flat;

import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.zstack.core.cloudbus.CloudBus;
import org.zstack.core.componentloader.ComponentLoader;
import org.zstack.core.db.DatabaseFacade;
import org.zstack.header.host.HostInventory;
import org.zstack.header.identity.SessionInventory;
import org.zstack.header.network.l3.L3NetworkInventory;
import org.zstack.network.service.flat.*;
import org.zstack.network.service.vip.VipInventory;
import org.zstack.network.service.vip.VipVO;
import org.zstack.storage.primary.local.LocalStorageSimulatorConfig;
import org.zstack.storage.primary.local.LocalStorageSimulatorConfig.Capacity;
import org.zstack.test.Api;
import org.zstack.test.ApiSenderException;
import org.zstack.test.DBUtil;
import org.zstack.test.WebBeanConstructor;
import org.zstack.test.deployer.Deployer;
import org.zstack.utils.Utils;
import org.zstack.utils.data.SizeUnit;
import org.zstack.utils.logging.CLogger;

import java.util.concurrent.TimeUnit;

/**
 * 1. delete a flat network
 *
 * confirm the namespace is deleted
 */
public class TestFlatNetwork5 {
    CLogger logger = Utils.getLogger(TestFlatNetwork5.class);
    Deployer deployer;
    Api api;
    ComponentLoader loader;
    CloudBus bus;
    DatabaseFacade dbf;
    SessionInventory session;
    LocalStorageSimulatorConfig config;
    FlatNetworkServiceSimulatorConfig fconfig;
    FlatDhcpUpgradeExtension dhcpUpgradeExtension;
    long totalSize = SizeUnit.GIGABYTE.toByte(100);

    @Before
    public void setUp() throws Exception {
        DBUtil.reDeployDB();
        WebBeanConstructor con = new WebBeanConstructor();
        deployer = new Deployer("deployerXml/flatnetwork/TestFlatNetwork2.xml", con);
        deployer.addSpringConfig("KVMRelated.xml");
        deployer.addSpringConfig("localStorageSimulator.xml");
        deployer.addSpringConfig("localStorage.xml");
        deployer.addSpringConfig("flatNetworkServiceSimulator.xml");
        deployer.addSpringConfig("flatNetworkProvider.xml");
        deployer.load();

        loader = deployer.getComponentLoader();
        bus = loader.getComponent(CloudBus.class);
        dbf = loader.getComponent(DatabaseFacade.class);
        config = loader.getComponent(LocalStorageSimulatorConfig.class);
        fconfig = loader.getComponent(FlatNetworkServiceSimulatorConfig.class);
        dhcpUpgradeExtension = loader.getComponent(FlatDhcpUpgradeExtension.class);

        Capacity c = new Capacity();
        c.total = totalSize;
        c.avail = totalSize;

        config.capacityMap.put("host1", c);
        config.capacityMap.put("host2", c);

        deployer.build();
        api = deployer.getApi();
        session = api.loginAsAdmin();
    }

	@Test
	public void test() throws ApiSenderException, InterruptedException {
        FlatNetworkGlobalProperty.DELETE_DEPRECATED_DHCP_NAME_SPACE = true;
        dhcpUpgradeExtension.start();

        HostInventory host = deployer.hosts.get("host1");
        api.reconnectHost(host.getUuid());

        TimeUnit.SECONDS.sleep(2);

        Assert.assertEquals(1, fconfig.deleteNamespaceCmds.size());


        L3NetworkInventory l3nw = deployer.l3Networks.get("TestL3Network1");

        APIGetL3NetworkDhcpIpAddressMsg msg = new APIGetL3NetworkDhcpIpAddressMsg();
        msg.setL3NetworkUuid(l3nw.getUuid());
        bus.makeTargetServiceIdByResourceUuid(msg, FlatNetworkServiceConstant.SERVICE_ID, l3nw.getUuid());
        msg.setTimeout(TimeUnit.SECONDS.toMillis(15));
        APIGetL3NetworkDhcpIpAddressReply reply = (APIGetL3NetworkDhcpIpAddressReply) bus.call(msg);
        logger.info("!!!!!!APIGetL3NetworkDhcpIpAddressMsg!!!!" + reply.getIp());
    }
}

@@ -255,24 +255,142 @@
 aseFacade;%0D%0A
+import org.zstack.core.scheduler.APIUpdateSchedulerEvent;%0D%0Aimport org.zstack.header.apimediator.ApiMediatorConstant;%0D%0A
 import org.z
@@ -415,32 +415,32 @@
 HostInventory;%0D%0A
-
 import org.zstac
@@ -851,131 +851,9 @@
 est.
-Api;%0D%0Aimport org.zstack.test.ApiSenderException;%0D%0Aimport org.zstack.test.DBUtil;%0D%0Aimport org.zstack.test.WebBeanConstructor
+*
 ;%0D%0Ai
@@ -1087,16 +1087,20 @@
 work%0D%0A *
+ %3Cp%3E
 %0D%0A * con
@@ -2841,17 +2841,23 @@
 %0D%0A%0D%0A
-%09
+    
 @Test%0D%0A
-%09
+    
 publ
@@ -3254,18 +3254,20 @@
 ());%0D%0A%0D%0A
+//
 %0D%0A
-
         
@@ -3420,24 +3420,58 @@
 ressMsg();%0D%0A
+        msg.setSession(session);%0D%0A
         msg.
@@ -3517,104 +3517,100 @@
     
-bus.makeTargetServiceIdByResourceUuid(msg, FlatNetworkServiceConstant.SERVICE_ID, l3nw.getUuid()
+msg.setServiceId(ApiMediatorConstant.SERVICE_ID);%0D%0A        ApiSender sender = new ApiSender(
 );%0D%0A
@@ -3613,27 +3613,30 @@
 );%0D%0A        
-msg
+sender
 .setTimeout(
@@ -3639,37 +3639,13 @@
 out(
-TimeUnit.SECONDS.toMillis(15)
+15000
 );%0D%0A
@@ -3692,15 +3692,28 @@
 y re
-ply = (
+ = sender.call(msg, 
 APIG
@@ -3745,24 +3745,18 @@
 eply
-) bus.call(msg);
+.class);%0D%0A
 %0D%0A  
@@ -3821,19 +3821,16 @@
 !!%22 + re
-ply
 .getIp()

modify unit test for APIGetL3NetworkDhcpIpAddressMsg