bunlongheng
5/2/2018 - 9:32 PM

policy-factory

const Account = require('../services').Account;
const FortinetService = require("../services").Fortinet;
const SSCService = require("../services").SSC;
const kue = require('kue');

const dotenv = require('dotenv').config();
const queue = kue.createQueue({redis: process.env.REDIS_URL});

module.exports = (config) => {

    let policyFactory = {};

    /*=================================
    =            updateCpe            =
    =================================*/
    policyFactory.updateCpe = (req) => {
        return new Promise(async (resolve, reject) => {
            try {
                let cpeMac         = req.params.cpeMac;
                let vcpeExt        = await SSCService.vcpeExt(cpeMac);
                let serviceType    = req.body.service_type;

                let privateVlan    = config.privateVlan;
                let guestVlan      = config.guestVlan;

                let login          = await FortinetService.login();
                let vcpe           = await SSCService.vcpe(cpeMac);
                let gVlanId        = await SSCService.hns(cpeMac,guestVlan);
                let pVlanId        = await SSCService.hns(cpeMac,privateVlan);

                let pHnsId         = pVlanId.hns_id;
                let gHnsId         = gVlanId.hns_id;
                let accountId      = vcpe.account_id;
                let session        = JSON.parse(login).session;
                let pTag           = `${accountId}-${pHnsId}`;
                let gTag           = `${accountId}-${gHnsId}`;

                let checkIfPrivatePolicyExist = await FortinetService.getPolicyBaseOnName(session,`fp-${accountId}-${pHnsId}`,'policy','inline');
                let checkIfGuestPolicyExist   = await FortinetService.getPolicyBaseOnName(session,`fg-${accountId}-${gHnsId}`,'policy','inline');

                // console.log(JSON.parse(checkIfPrivatePolicyExist).result[0].data.length);
                // console.log(JSON.parse(checkIfGuestPolicyExist).result[0].data.length);

                if (JSON.parse(checkIfPrivatePolicyExist).result[0].data.length > 0) {
                    var privatePolicyExist = true;
                } else {
                    var privatePolicyExist = false;
                }

                // console.log('-----------------------');
                // console.log(checkIfPrivatePolicyExist);
                // console.log('-----------------------');
                // console.log(checkIfGuestPolicyExist);
                // console.log('-----------------------');

                if (JSON.parse(checkIfGuestPolicyExist).result[0].data.length > 0) {
                    var guestPolicyExist = true;
                } else {
                    var guestPolicyExist = false;
                }

                // console.log('privatePolicyExist / guestPolicyExist = ',  privatePolicyExist , '/' , guestPolicyExist );

                if (privatePolicyExist == true && guestPolicyExist == true ) {
                    var oldServiceType = 3;
                } else if (privatePolicyExist == true && guestPolicyExist == false ) {
                    var oldServiceType = 2;
                } else if (privatePolicyExist == false && guestPolicyExist == true ) {
                    console.log('Invalid Case');
                } else {
                    var oldServiceType = 1;
                }

                console.log('oldServiceType = ', oldServiceType, '| serviceType = ', serviceType );

                //Check for same
                if (serviceType == oldServiceType ) {
                    console.log('Same do nothing');
                }

                let data = {
                    title: `CPE PUT: ${cpeMac}`,
                    cpeMac: cpeMac
                };


                if (oldServiceType == 1 && serviceType == 2 ) {

                    data.vlanId = config.privateVlan;

                    queue.create(`createVlan`, data)
                    .attempts(3) // if job fails retry 3 times
                    .backoff({delay: 60 * 1000}) // wait 60s before retry;
                    .save();

                    console.log('-- createVlan : privateVlan --');

                } else if(oldServiceType == 1 && serviceType == 3 ) {

                    queue.create(`createVlan`, {
                            title: `CPE PUT: ${cpeMac}`,
                            cpeMac: cpeMac,
                            vlanId: config.privateVlan
                        }
                    )
                    .attempts(3) // if job fails retry 3 times
                    .backoff({delay: 60 * 1000}) // wait 60s before retry;
                    .save();

                    queue.create(`createVlan`, {
                            title: `CPE PUT: ${cpeMac}`,
                            cpeMac: cpeMac,
                            vlanId: config.guestVlan
                        }
                    )
                    .attempts(3) // if job fails retry 3 times
                    .backoff({delay: 60 * 1000}) // wait 60s before retry;
                    .save();

                    console.log('-- createVlan : both --');

                } else if(oldServiceType == 2 && serviceType == 1 ) {

                    data.vlanId = config.privateVlan;

                    queue.create(`deleteVlan`, data)
                    .attempts(3) // if job fails retry 3 times
                    .backoff({delay: 60 * 1000}) // wait 60s before retry;
                    .save();

                    console.log('-- deleteVlan : privateVlan --');

                } else if(oldServiceType == 2 && serviceType == 3 ) {

                    data.vlanId = config.guestVlan;

                    queue.create(`createVlan`, data)
                    .attempts(3) // if job fails retry 3 times
                    .backoff({delay: 60 * 1000}) // wait 60s before retry;
                    .save();

                    console.log('-- createVlan : guestVlan  --');

                } else if(oldServiceType == 3 && serviceType == 1 ) {

                    queue.create(`deleteVlan`, {
                            title: `CPE PUT: ${cpeMac}`,
                            cpeMac: cpeMac,
                            vlanId: config.privateVlan
                        }
                    )
                    .attempts(3) // if job fails retry 3 times
                    .backoff({delay: 60 * 1000}) // wait 60s before retry;
                    .save();

                    queue.create(`deleteVlan`, {
                            title: `CPE PUT: ${cpeMac}`,
                            cpeMac: cpeMac,
                            vlanId: config.guestVlan
                        }
                    )
                    .attempts(3) // if job fails retry 3 times
                    .backoff({delay: 60 * 1000}) // wait 60s before retry;
                    .save();

                    console.log('-- deleteVlan : both --');

                } else if(oldServiceType == 3 && serviceType == 2 ) {

                    data.vlanId = config.guestVlan;

                    queue.create(`deleteVlan`, data)
                    .attempts(3) // if job fails retry 3 times
                    .backoff({delay: 60 * 1000}) // wait 60s before retry;
                    .save();

                    console.log('-- deleteVlan : guestVlan --');

                }else {

                    console.log('-- not match any cases, bugs !!! --');

                }

                resolve();

            } catch (error) {
                console.log(error)
                reject(error);
            }
        });
    };
    /*=================================
    =            deleteCpe            =
    =================================*/
    policyFactory.deleteCpe = () => {
        return new Promise(async (resolve,reject) => {
            try {
                resolve();
            } catch (error) {
                reject(error);
            }
        });
    };
    /*==================================
    =            updateVlan            =
    ==================================*/
    policyFactory.updateVlan = (cpeMac, vlanId) => {
        return new Promise(async(resolve, reject) => {
            try {
                let vcpeExt     = await SSCService.vcpeExt(cpeMac);
                let serviceType = vcpeExt.service_type;

                if (serviceType == 0 || serviceType == 1 || serviceType == '' || serviceType == null ) {
                    console.error('\x1b[41m%s\x1b[0m', 'No security policy');
                    return;
                }

                let data = {
                    title: `CPE: ${cpeMac}`,
                    vlanId: vlanId,
                    cpeMac: cpeMac
                };

                if (vlanId == config.guestVlan && serviceType == 3) {
                    data.vlanId = config.guestVlan;
                }

                queue.create(`createVlan`, data)
                    .attempts(3) // if job fails retry 3 times
                    .backoff({delay: 60 * 1000}) // wait 60s before retry;
                    .save();

                resolve();
            } catch (error) {
                reject(error);
            }
        });
    };
    /*==================================
    =            deleteVlan            =
    ==================================*/
    policyFactory.deleteVlan = (cpeMac, vlanId) => {
        return new Promise(async(resolve, reject) => {
            try {
                let vcpeExt     = await SSCService.vcpeExt(cpeMac);
                let serviceType = vcpeExt.service_type;

                if (serviceType == 0 || serviceType == 1 || serviceType == '' || serviceType == null ) {
                    console.error('\x1b[41m%s\x1b[0m', 'No security policy');
                    return;
                }

                let data = {
                    title: `CPE: ${cpeMac}`,
                    vlanId: vlanId,
                    cpeMac: cpeMac
                };

                if (vlanId == config.guestVlan && serviceType == 3) {
                    data.vlanId = config.guestVlan;
                }

                queue.create(`deleteVlan`, data)
                    .attempts(3) // if job fails retry 3 times
                    .backoff({delay: 60 * 1000}) // wait 60s before retry;
                    .save();

                resolve();
            } catch (error) {
                reject(error);
            }
        });
    };
    /*========================================
    =            updateVlan Process          =
    ========================================*/
    queue.process(`createVlan`, async(job, done) => {
        let cpeMac      = job.data.cpeMac;
        let vlanId      = job.data.vlanId;
        let vcpeExt     = await SSCService.vcpeExt(cpeMac);
        let serviceType = vcpeExt.service_type;
        let cluster     = `process.env.${vcpeExt.security_cluster}`;
        let poolName    = `${vcpeExt.security_cluster}_POOL`;
        let clusterDevises = eval(cluster);
        let clusterDevisesArray = clusterDevises.split(' ');
        let privateVlan = config.privateVlan;
        let guestVlan   = config.guestVlan;

        if(serviceType == 0 || serviceType == 1 || serviceType == '' || serviceType == null) {
            console.error('\x1b[41m%s\x1b[0m', 'No security policy');
            return;
        }

        console.log('Inside createVlan for vlandId ', vlanId, 'pVlan ', privateVlan, 'gVlan ', guestVlan);
        let login       = await FortinetService.login();
        let vcpe        = await SSCService.vcpe(cpeMac);
        let accountId   = vcpe.account_id;
        let session     = JSON.parse(login).session;

        let data = {};
            job.data.accountId   = accountId;
            job.data.cpeMac      = cpeMac;


        if(vlanId == privateVlan) {

            var pVlanId     = await SSCService.hns(cpeMac,privateVlan);
            var pHnsId      = pVlanId.hns_id;
            var pTag        = `${accountId}-${pHnsId}`;

            job.data.privateVlan = privateVlan;
            job.data.pHnsId      = pHnsId;
            job.data.pTag        = pTag;

            await FortinetService.vxLan('add',session,clusterDevisesArray[0],'ingress',privateVlan,pHnsId,`ip-${pTag}`);
            await FortinetService.vxLan('add',session,clusterDevisesArray[0],'egress',privateVlan,pHnsId,`ep-${pTag}`);
            await FortinetService.switch('add',session,clusterDevisesArray[0],`ip-${pTag}`,`ep-${pTag}`,`sp-${pTag}`);
            await FortinetService.vxLan('add',session,clusterDevisesArray[2],'ingress',privateVlan,pHnsId,`ip-${pTag}`);
            await FortinetService.vxLan('add',session,clusterDevisesArray[2],'egress',privateVlan,pHnsId,`ep-${pTag}`);
            await FortinetService.switch('add',session,clusterDevisesArray[2],`ip-${pTag}`,`ep-${pTag}`,`sp-${pTag}`);

        } else {

            var gVlanId     = await SSCService.hns(cpeMac,guestVlan);
            var gHnsId      = gVlanId.hns_id;
            var gTag        = `${accountId}-${gHnsId}`;

            job.data.guestVlan   = guestVlan;
            job.data.gHnsId      = gHnsId;
            job.data.gTag        = gTag;

            await FortinetService.vxLan('add',session,clusterDevisesArray[0],'ingress',guestVlan,gHnsId,`ig-${gTag}`);
            await FortinetService.vxLan('add',session,clusterDevisesArray[0],'egress',guestVlan,gHnsId,`eg-${gTag}`);
            await FortinetService.switch('add',session,clusterDevisesArray[0],`ig-${gTag}`,`eg-${gTag}`,`sg-${gTag}`);
            await FortinetService.vxLan('add',session,clusterDevisesArray[2],'ingress',guestVlan,gHnsId,`ig-${gTag}`);
            await FortinetService.vxLan('add',session,clusterDevisesArray[2],'egress',guestVlan,gHnsId,`eg-${gTag}`);
            await FortinetService.switch('add',session,clusterDevisesArray[2],`ig-${gTag}`,`eg-${gTag}`,`sg-${gTag}`);

        }

        let installDevice = await FortinetService.installDevice(session, clusterDevisesArray);
        let taskId = JSON.parse(installDevice).result[0].data.task;
        let monitor = await FortinetService.monitor(session,taskId);
        let percent = JSON.parse(monitor).result[0].data.percent;

        var installDeviceProcess = setInterval(async function(){ startMonitorInstallDevice() }, 5000);

        async function startMonitorInstallDevice() {

            let monitor = await FortinetService.monitor(session,taskId);
            let percent = JSON.parse(monitor).result[0].data.percent;

            if(percent >= 100) {
                stopMonitorInstallDevice();
            }
        }

        async function stopMonitorInstallDevice() {

            clearInterval(installDeviceProcess);

            if(vlanId == privateVlan) {

                await FortinetService.dynamicInterface('add',session,`ip-${pTag}`, clusterDevisesArray);
                await FortinetService.dynamicInterface('add',session,`ep-${pTag}`, clusterDevisesArray);

                await FortinetService.policyFirewall('add',session,'v4','inline',privateVlan,job.data, 0, poolName);
                await FortinetService.policyFirewall('add',session,'v4','inline',privateVlan,job.data,1, poolName);
                await FortinetService.policyFirewall('add',session,'v6','inline',privateVlan,job.data, 0, poolName);
                await FortinetService.policyFirewall('add',session,'v6','inline',privateVlan,job.data,1, poolName);

                await FortinetService.policyFirewall('add',session,'v4','vpn',privateVlan,job.data, 0, poolName);
                await FortinetService.policyFirewall('add',session,'v6','vpn',privateVlan,job.data, 0, poolName);

            }else {

                await FortinetService.dynamicInterface('add',session,`ig-${gTag}`, clusterDevisesArray);
                await FortinetService.dynamicInterface('add',session,`eg-${gTag}`, clusterDevisesArray);

                await FortinetService.policyFirewall('add',session,'v4','inline',guestVlan,job.data, 0 , poolName);
                await FortinetService.policyFirewall('add',session,'v6','inline',guestVlan,job.data, 0, poolName);

            }


            //installPackageInline
            var installPackageInline        = await FortinetService.installPackage(session,'inline', clusterDevisesArray);
            var taskId                      = JSON.parse(installPackageInline).result[0].data.task;
            var monitor                     = await FortinetService.monitor(session,taskId);
            var percent                     = JSON.parse(monitor).result[0].data.percent;
            var installPackageInlineProcess = setInterval(async function(){ startMonitorInstallPackageInline() }, 5000);

            async function startMonitorInstallPackageInline() {

                let monitor = await FortinetService.monitor(session,taskId);
                let percent = JSON.parse(monitor).result[0].data.percent;

                if(percent >= 100) {
                    stopMonitorInstallPackageInline();
                }
            }

            async function stopMonitorInstallPackageInline() {

                clearInterval(installPackageInlineProcess);

                if(vlanId == privateVlan) {

                    //installPackageVpn
                    var installPackageVpn        = await FortinetService.installPackage(session,'vpn', clusterDevisesArray);
                    var taskId                      = JSON.parse(installPackageVpn).result[0].data.task;
                    var monitor                     = await FortinetService.monitor(session,taskId);
                    var percent                     = JSON.parse(monitor).result[0].data.percent;
                    var installPackageVpnProcess = setInterval(async function(){ startMonitorInstallPackageVpn() }, 5000);

                    async function startMonitorInstallPackageVpn() {

                        let monitor = await FortinetService.monitor(session,taskId);
                        let percent = JSON.parse(monitor).result[0].data.percent;

                        if(percent >= 100) {
                            stopMonitorInstallPackageVpn();
                        }
                    }

                    async function stopMonitorInstallPackageVpn() {
                        clearInterval(installPackageVpnProcess);
                        await FortinetService.logout(session);
                        done();
                    }

                } else  {
                    await FortinetService.logout(session);
                    done();
                }

            }


        }
    });
    /*==================================
    =            deleteVlan Process    =
    ==================================*/
    queue.process(`deleteVlan`, async(job, done) => {
        let cpeMac      = job.data.cpeMac;
        let vlanId      = job.data.vlanId;
        let privateVlan = config.privateVlan;
        let guestVlan   = config.guestVlan;
        let vcpeExt     = await SSCService.vcpeExt(cpeMac);
        let serviceType = vcpeExt.service_type;
        let cluster     = `process.env.${vcpeExt.security_cluster}`;
        let clusterDevises = eval(cluster);
        let clusterDevisesArray = clusterDevises.split(' ');

        let login       = await FortinetService.login();
        let vcpe        = await SSCService.vcpe(cpeMac);
        let vlanDetails     = await SSCService.hns(cpeMac, vlanId);


        let hnsId      = vlanDetails.hns_id;
        let accountId   = vcpe.account_id;

        let session     = JSON.parse(login).session;
        let tag        = `${accountId}-${hnsId}`;

        let data = {};
        data.hnsId      = hnsId;
        data.tag        = tag;
        data.accountId   = accountId;
        data.cpeMac      = cpeMac;

        function checkIfDataExist(array){
            if(JSON.parse(array).result[0].data.length > 0) {
                return JSON.parse(array).result[0].data[0].policyid;
            }else{
                // console.log(JSON.parse(array));
                return null;
            }

        }

        if(vlanId == privateVlan) {

            var inlinePvtV4Resp    = await FortinetService.getPolicyId(session,`fp-${tag}`,'policy');
            var inlinePvtV4NtwResp = await FortinetService.getPolicyId(session,`fn-${tag}`,'policy');
            var inlinePvtV6Resp    = await FortinetService.getPolicyId(session,`fp6-${tag}`,'policy6');
            var inlinePvtV6NtwResp = await FortinetService.getPolicyId(session,`fn6-${tag}`,'policy6');
            var vpnPvtV4Resp       = await FortinetService.getPolicyId(session,`fv-${tag}`,'policy','vpn');
            var defaultPolicy      = await FortinetService.getPolicyId(session,`${vcpeExt.security_cluster}`,'policy','vpn');
            var defaultPolicyV6    = await FortinetService.getPolicyId(session,`${vcpeExt.security_cluster}_6`,'policy6','vpn');
            var vpnPvtV6Resp       = await FortinetService.getPolicyId(session,`fv6-${tag}`,'policy6','vpn');

            var inlinePvtV4Id    = checkIfDataExist(inlinePvtV4Resp);
            var inlinePvtV4NtwId = checkIfDataExist(inlinePvtV4NtwResp);
            var inlinePvtV6Id    = checkIfDataExist(inlinePvtV6Resp);
            var inlinePvtV6NtwId = checkIfDataExist(inlinePvtV6NtwResp);
            var vpnPvtV4Id       = checkIfDataExist(vpnPvtV4Resp);
            var vpnPvtV6Id       = checkIfDataExist(vpnPvtV6Resp);
            var defaultPolicyId  = checkIfDataExist(defaultPolicy);
            var defaultPolicyIdV6  = checkIfDataExist(defaultPolicyV6);

            console.log ('inside deleteVlan  vpn v4policy id ', vpnPvtV4Id, 'vpn v6policy id', vpnPvtV6Id);

            var policyUsers    = '';
            if (JSON.parse(vpnPvtV4Resp).result[0].data[0].users != '' && JSON.parse(vpnPvtV4Resp).result[0].data[0].users != undefined) {
                var policyUsers    = JSON.parse(vpnPvtV4Resp).result[0].data[0].users;
            } 
            var defaultPolicyUsers    = [];
            if (JSON.parse(defaultPolicy).result[0].data[0].users != '' && JSON.parse(defaultPolicy).result[0].data[0].users != undefined) {
                var defaultPolicyUsers    = JSON.parse(defaultPolicy).result[0].data[0].users;
            }

            var policyUsersFinal = defaultPolicyUsers.filter(val => !policyUsers.includes(val));

            var policyUsersV6    = '';
            if (JSON.parse(vpnPvtV6Resp).result[0].data[0].users != '' && JSON.parse(vpnPvtV6Resp).result[0].data[0].users != undefined) {
                var policyUsersV6    = JSON.parse(vpnPvtV6Resp).result[0].data[0].users;
            } 
            var defaultPolicyUsersV6    = [];
            if (JSON.parse(defaultPolicyV6).result[0].data[0].users != '' && JSON.parse(defaultPolicyV6).result[0].data[0].users != undefined) {
                var defaultPolicyUsersV6    = JSON.parse(defaultPolicyV6).result[0].data[0].users;
            }

            var policyUsersFinalV6 = defaultPolicyUsersV6.filter(val => !policyUsersV6.includes(val));
            
            await FortinetService.deletePolicyFirewall(session,inlinePvtV4Id,'policy');
            await FortinetService.deletePolicyFirewall(session,inlinePvtV4NtwId,'policy');
            await FortinetService.deletePolicyFirewall(session,inlinePvtV6Id,'policy6');
            await FortinetService.deletePolicyFirewall(session,inlinePvtV6NtwId,'policy6');
            await FortinetService.deletePolicyFirewall(session,vpnPvtV4Id,'policy', 'vpn');
            await FortinetService.updatePolicyFirewall(session, policyUsersFinal, defaultPolicyId, 'policy');
            await FortinetService.updatePolicyFirewall(session, policyUsersFinalV6, defaultPolicyIdV6, 'policy6');
            
            if (policyUsers.length > 0) {
                policyUsers.forEach((email) => {
                    FortinetService.removeUser(session, email);
                });
            }
            await FortinetService.deletePolicyFirewall(session,vpnPvtV6Id,'policy6', 'vpn');
            if (policyUsersV6.length > 0) {
                policyUsersV6.forEach((email) => {
                    FortinetService.removeUser(session, email);
                });
            }
            await FortinetService.dynamicInterface('delete',session,`ip-${tag}`, clusterDevisesArray);
            await FortinetService.dynamicInterface('delete',session,`ep-${tag}`, clusterDevisesArray);

        } else {

            var inlineGstV4Resp    = await FortinetService.getPolicyId(session,`fg-${tag}`,'policy');
            var inlineGstV6Resp    = await FortinetService.getPolicyId(session,`fg6-${tag}`,'policy6');

            var inlineGstV4Id    = checkIfDataExist(inlineGstV4Resp);
            var inlineGstV6Id    = checkIfDataExist(inlineGstV6Resp);

            await FortinetService.deletePolicyFirewall(session,inlineGstV4Id,'policy');
            await FortinetService.deletePolicyFirewall(session,inlineGstV6Id,'policy6');

            await FortinetService.dynamicInterface('delete',session,`ig-${tag}`, clusterDevisesArray);
            await FortinetService.dynamicInterface('delete',session,`eg-${tag}`, clusterDevisesArray);

        }

            //installPackageInline
        var installPackageInline        = await FortinetService.installPackage(session,'inline', clusterDevisesArray);
        var taskId                      = JSON.parse(installPackageInline).result[0].data.task;
        var monitor                     = await FortinetService.monitor(session,taskId);
        var percent                     = JSON.parse(monitor).result[0].data.percent;
        var installPackageInlineProcess = setInterval(async function(){ startMonitorInstallPackageInline() }, 5000);

        async function startMonitorInstallPackageInline() {

            let monitor = await FortinetService.monitor(session,taskId);
            let percent = JSON.parse(monitor).result[0].data.percent;

            if(percent >= 100) {
                stopMonitorInstallPackageInline();
            }
        }

        async function stopMonitorInstallPackageInline() {

        clearInterval(installPackageInlineProcess);

        if (vlanId == privateVlan) {

              //installPackageVpn
              var installPackageVpn           = await FortinetService.installPackage(session,'vpn', clusterDevisesArray);
              var taskId                      = JSON.parse(installPackageVpn).result[0].data.task;
              var monitor                     = await FortinetService.monitor(session,taskId);
              var percent                     = JSON.parse(monitor).result[0].data.percent;
              var installPackageVpnProcess    = setInterval(async function(){ startMonitorInstallPackageVpn() }, 5000);

              async function startMonitorInstallPackageVpn() {

                let monitor = await FortinetService.monitor(session,taskId);
                let percent = JSON.parse(monitor).result[0].data.percent;

                if(percent >= 100) {
                    stopMonitorInstallPackageVpn();
                }
              }

              async function stopMonitorInstallPackageVpn() {

                clearInterval(installPackageVpnProcess);
                await FortinetService.switch('delete',session,clusterDevisesArray[2],`ip-${tag}`,`ep-${tag}`,`sp-${tag}`);
                await FortinetService.vxLan('delete',session,clusterDevisesArray[2],'ingress',privateVlan,hnsId,`ip-${tag}`);
                await FortinetService.vxLan('delete',session,clusterDevisesArray[2],'egress',privateVlan,hnsId,`ep-${tag}`);
                await FortinetService.switch('delete',session,clusterDevisesArray[0],`ip-${tag}`,`ep-${tag}`,`sp-${tag}`);
                await FortinetService.vxLan('delete',session,clusterDevisesArray[0],'ingress',privateVlan,hnsId,`ip-${tag}`);
                await FortinetService.vxLan('delete',session,clusterDevisesArray[0],'egress',privateVlan,hnsId,`ep-${tag}`);

                let installDevice = await FortinetService.installDevice(session, clusterDevisesArray);
                let taskId = JSON.parse(installDevice).result[0].data.task;
                let monitor = await FortinetService.monitor(session,taskId);
                let percent = JSON.parse(monitor).result[0].data.percent;

                var installDeviceProcess = setInterval(async function(){ startMonitorInstallDevice() }, 5000);

                async function startMonitorInstallDevice() {

                    let monitor = await FortinetService.monitor(session,taskId);
                    let percent = JSON.parse(monitor).result[0].data.percent;

                    if(percent >= 100) {
                        stopMonitorInstallDevice();
                    }
                }

                async function stopMonitorInstallDevice() {
                    clearInterval(installDeviceProcess);
                    await FortinetService.logout(session);
                    done();
                }
            }
        } // privateVlan
        else {

            await FortinetService.switch('delete',session,clusterDevisesArray[0],`ig-${tag}`,`eg-${tag}`,`sg-${tag}`);
            await FortinetService.vxLan('delete',session,clusterDevisesArray[0],'ingress',guestVlan,hnsId,`ig-${tag}`);
            await FortinetService.vxLan('delete',session,clusterDevisesArray[0],'egress',guestVlan,hnsId,`eg-${tag}`);
            await FortinetService.switch('delete',session,clusterDevisesArray[2],`ig-${tag}`,`eg-${tag}`,`sg-${tag}`);
            await FortinetService.vxLan('delete',session,clusterDevisesArray[2],'ingress',guestVlan,hnsId,`ig-${tag}`);
            await FortinetService.vxLan('delete',session,clusterDevisesArray[2],'egress',guestVlan,hnsId,`eg-${tag}`);

            let installDevice = await FortinetService.installDevice(session, clusterDevisesArray);
            let taskId = JSON.parse(installDevice).result[0].data.task;
            let monitor = await FortinetService.monitor(session,taskId);
            let percent = JSON.parse(monitor).result[0].data.percent;

            var installDeviceProcess = setInterval(async function(){ startMonitorInstallDevice() }, 5000);

            async function startMonitorInstallDevice() {

                let monitor = await FortinetService.monitor(session,taskId);
                let percent = JSON.parse(monitor).result[0].data.percent;

                if(percent >= 100) {
                    stopMonitorInstallDevice();
                }
            }

            async function stopMonitorInstallDevice() {
                clearInterval(installDeviceProcess);
                await FortinetService.logout(session);
                done();
            }
        } // else
        } //stopMonitorInstallPackageInline
    });

    return policyFactory;
}