def test_vrrpv2_b04(self):
		""" vrrpv2_b04 """
		vrrpv2 = {
			"group": [
				{
					"group-name": "VG_1",
					"instance": [
						{
							"additional_track_interface": [
								{
									"interface": "s0e2"
								}
							],
							"instance-name": "VI_1",
							"interface": "s0e1",
							"sync-interface": "s0e1",
							"delay-gratuitous-arp": 5,
							"virtual-router-id": 1,
							"priority": 1,
							"advertisement-interval": 10,
							"ipv4_vip": [
								{
									"ipv4": "192.168.1.256",
									"netmask": "255.255.255.0",
									"interface": "s0e1"
								}
							],
							"ipv4_vr": [
								{
									"destination-ipv4": "192.168.1.1",
									"netmask": "255.255.255.0",
									"gateway": "192.168.1.1",
									"interface": "s0e1"
								}
							],
							"ipv6_vip": [
								{
									"ipv6": "2001::1",
									"prefix": 64,
									"interface": "s0e1"
								}
							],
							"ipv6_vr": [
								{
									"destination-ipv6": "2001::1",
									"prefix": 64,
									"gateway": "2001::1",
									"interface": "s0e1"
								}
							],
							"preempt": True
						}
					]
				}
			]
		}
		self.maxDiff = None
		e = ml_w_vrrpv2.set(None, vrrpv2)
		self.assertFalse(e[0], e[1])
	def test_vrrpv2_g02(self):
		""" vrrpv2_g02 """
		vrrpv2 = {
			"group": [
				{
					"group-name": "VG_1",
					"instance": [
						{
							"additional_track_interface": [
								{
									"interface": "s0e2"
								}
							],
							"instance-name": "VI_1",
							"interface": "s0e1",
							"sync-interface": "s0e1",
							"delay-gratuitous-arp": 5,
							"virtual-router-id": 1,
							"priority": 1,
							"advertisement-interval": 10,
							"ipv4_vip": [
								{
									"ipv4": "192.168.1.1",
									"netmask": "255.255.255.0",
									"interface": "s0e1"
								}
							],
							"ipv4_vr": [
								{
									"destination-ipv4": "192.168.1.1",
									"netmask": "255.255.255.0",
									"gateway": "192.168.1.1",
									"interface": "s0e1"
								}
							],
							"ipv6_vip": [
								{
									"ipv6": "2001::1",
									"prefix": 64,
									"interface": "s0e1"
								}
							],
							"ipv6_vr": [
								{
									"destination-ipv6": "2001::1",
									"prefix": 64,
									"gateway": "2001::1",
									"interface": "s0e1"
								}
							],
							"preempt": True
						}
					]
				}
			]
		}
		self.maxDiff = None
		e = ml_w_vrrpv2.set(None, vrrpv2)
		self.assertTrue(e[0], e[1])
		f = open(os.path.join("running", "vrrpv2.txt"), "r")
		e = f.readlines()
		f.close()
		if "json" == ml_system.CFG_TYPE:
			self.assertEqual(e, ['{"group": [{"group-name": "VG_1", "instance": [{"advertisement-interval": 10, "ipv4_vr": [{"interface": "s0e1", "netmask": "255.255.255.0", "destination-ipv4": "192.168.1.1", "gateway": "192.168.1.1"}], "ipv4_vip": [{"interface": "s0e1", "netmask": "255.255.255.0", "ipv4": "192.168.1.1"}], "virtual-router-id": 1, "sync-interface": "s0e1", "delay-gratuitous-arp": 5, "priority": 1, "preempt": true, "ipv6_vip": [{"interface": "s0e1", "prefix": 64, "ipv6": "2001::1"}], "interface": "s0e1", "ipv6_vr": [{"interface": "s0e1", "prefix": 64, "gateway": "2001::1", "destination-ipv6": "2001::1"}], "additional_track_interface": [{"interface": "s0e2"}], "instance-name": "VI_1"}]}]}'])
		if "jcfg" == ml_system.CFG_TYPE:
			self.assertEqual(e, ['vrrpv2 {\n', '    group-array {\n', '        group { #1\n', '            group-name VG_1\n', '            instance-array {\n', '                instance { #1\n', '                    instance-name VI_1\n', '                    additional_track_interface-array {\n', '                        additional_track_interface { #1\n', '                        }\n', '                    }\n', '                    delay-gratuitous-arp 5\n', '                    advertisement-interval 10\n', '                    ipv4_vip-array {\n', '                        ipv4_vip { #1\n', '                            ipv4 192.168.1.1\n', '                            netmask 255.255.255.0\n', '                        }\n', '                    }\n', '                    ipv4_vr-array {\n', '                        ipv4_vr { #1\n', '                            destination-ipv4 192.168.1.1\n', '                            netmask 255.255.255.0\n', '                            gateway 192.168.1.1\n', '                        }\n', '                    }\n', '                    ipv6_vip-array {\n', '                        ipv6_vip { #1\n', '                            ipv6 2001::1\n', '                        }\n', '                    }\n', '                    ipv6_vr-array {\n', '                        ipv6_vr { #1\n', '                            destination-ipv6 2001::1\n', '                            gateway 2001::1\n', '                        }\n', '                    }\n', '                }\n', '            }\n', '        }\n', '    }\n', '}\n'])
			def run(self):
				e = ml_w_vrrpv2.set(None, vrrpv2, threading.RLock())
Ejemplo n.º 4
0
    def vrrp(self, **kwargs):
        '''
            VRRPv2 getter and setter
            format:
            {'group': [{'group-name': 'VG_1',
                     'instance': [{'additional_track_interface': [{'interface': 's0e2'}],
                                   'advertisement-interval': 10,
                                   'delay-gratuitous-arp': 5,
                                   'instance-name': 'VI_1',
                                   'interface': 's0e1',
                                   'ipv4_vip': [{'interface': 's0e1',
                                                 'ipv4': '192.168.1.1',
                                                 'netmask': '255.255.255.0'}],
                                   'ipv4_vr': [{'destination-ipv4': '192.168.1.1',
                                                'gateway': '192.168.1.1',
                                                'interface': 's0e1',
                                                'netmask': '255.255.255.0'}],
                                   'ipv6_vip': [{'interface': 's0e1',
                                                 'ipv6': '2001::1',
                                                 'prefix': 64}],
                                   'ipv6_vr': [{'destination-ipv6': '2001::1',
                                                'gateway': '2001::1',
                                                'interface': 's0e1',
                                                'prefix': 64}],
                                   'preempt': True,
                                   'priority': 1,
                                   'sync-interface': 's0e1',
                                   'virtual-router-id': 1
                               }]
                       }]
               }
        '''
        import libs.login
        if False == libs.login.cklogin():
            raise _.HTTPRedirect('/')

        import ml_w_vrrpv2 as wvr
        import json
        import libs.tools

        _.response.headers["Content-Type"] = "application/json"
        if 'group' in kwargs:
            # setter

            dat = kwargs

            res = []
            opt = []
            goptTmp = []
            nopt = {}

            for k in dat:
                if "@@" in k:
                    kk = k.split("@@")
                    if "list" == type(kk).__name__ and len(kk) > 1:
                        libs.tools.v(dat)
                        goptTmp = self._unstructDict(dat, "@@")

            gopt = []
            for k in goptTmp:
                if not "vrrpv2" == goptTmp[k]:
                    gopt.append(goptTmp[k])


            prefix = "instance@@"
            for k in range(0, len(gopt)):
                dat = self._unstructDict(gopt[k], "@@")
                iopt = []
                ioptTmp = self._unstructDict(dat["instance"], "@@")

                for kk in ioptTmp:
                    if "list" == type(ioptTmp[kk]["instance-name"]).__name__:
                        ov = ioptTmp[kk]["instance-name"][0]
                        ioptTmp[kk]["instance-name"] = ov

                    data = self._unstructDict(ioptTmp[kk], "@@")
                    if "preempt" in data.keys():
                        if "true" == data["preempt"]:
                            data["preempt"] = True
                        else:
                            data["preempt"] = False
                    if "ipv4_vip" in data.keys():
                        ov = []
                        da = self._unstructDict(data["ipv4_vip"], "@@")
                        for kt in da:
                            ov.append(da[kt])
                        data["ipv4_vip"] = ov
                    if "ipv4_vr" in data.keys():
                        ov = []
                        da = self._unstructDict(data["ipv4_vr"], "@@")
                        for kt in da:
                            ov.append(da[kt])
                        data["ipv4_vr"] = ov
                    if "ipv6_vip" in data.keys():
                        ov = []
                        da = self._unstructDict(data["ipv6_vip"], "@@")
                        for kt in da:
                            ov.append(da[kt])
                        data["ipv6_vip"] = ov
                    if "ipv6_vr" in data.keys():
                        ov = []
                        da = self._unstructDict(data["ipv6_vr"], "@@")
                        for kt in da:
                            ov.append(da[kt])
                        data["ipv6_vr"] = ov
                    if "additional_track_interface" in data.keys():
                        ov = []
                        da = self._unstructDict(data["additional_track_interface"], "@@")
                        for kt in da:
                            ov.append(da[kt])
                        data["additional_track_interface"] = ov
                    iopt.append(data)

                res.append({"group-name": dat["group-name"], "instance": iopt})

            res = {"group": res}

            res = wvr.set(user = self.getUser(), cfg = res)
            if False == res[0]:
                return json.dumps([res[0], libs.tools.translateMessage(res[1])])
            else:
                return json.dumps(res)
        else:
            # getter
            return json.dumps(wvr.get())