Upload the contribution of vstf as bottleneck network framework.
[bottlenecks.git] / vstf / vstf / controller / settings / flows_settings.py
1 #!/usr/bin/env python
2 # -*- coding: utf8 -*-
3 # author: wly
4 # date: 2015-09-18
5 # see license for license details
6
7 import logging
8
9 import vstf.controller.settings.settings as sets
10
11 LOG = logging.getLogger(__name__)
12
13
14 class FlowsSettings(sets.Settings):
15     def __init__(self, path="/etc/vstf/perf/",
16                  filename="sw_perf.flownodes-settings",
17                  mode=sets.SETS_SINGLE):
18         self._check_actors = {'namespaces', 'senders', 'receivers', 'watchers'}
19         self._nocheck_actors = {"cpu_listens"}
20         super(FlowsSettings, self).__init__(path, filename, mode)
21
22     def _register_func(self):
23         super(FlowsSettings, self)._register_func()
24         for actor in self._check_actors:
25             actor = actor.encode()
26             func_name = "add_%s" % actor
27             setattr(self, func_name, self._adding_file(func_name, self._mset, self._fset, actor, self._check_add))
28             func_name = "madd_%s" % actor
29             setattr(self, func_name, self._adding_memory(func_name, self._mset, actor, self._check_add))
30
31         for actor in self._nocheck_actors:
32             actor = actor.encode()
33             func_name = "add_%s" % actor
34             setattr(self, func_name, self._adding_file(func_name, self._mset, self._fset, actor))
35             func_name = "madd_%s" % actor
36             setattr(self, func_name, self._adding_memory(func_name, self._mset, actor))
37
38         LOG.debug(self.__dict__.keys())
39
40     def clear_all(self):
41         actors = self._check_actors | self._nocheck_actors
42         for actor in actors:
43             func_name = "set_%s" % actor
44             func = getattr(self, func_name)
45             func([])
46
47     def mclear_all(self):
48         actors = self._check_actors | self._nocheck_actors
49         for actor in actors:
50             func_name = "mset_%s" % actor
51             func = getattr(self, func_name)
52             func([])
53
54     def _check_add(self, value):
55         flows = ['agent', 'dev']
56         if not isinstance(value, dict):
57             raise Exception("type is error: %s" % (str(value)))
58         for flow in flows:
59             if flow not in value.keys():
60                 raise Exception("keys[%s] is missing: %s" % (flow, str(value)))
61
62         items = ["ip", "namespace", "mac", "iface", "bdf"]
63         for item in items:
64             if item not in value['dev'].keys():
65                 raise Exception("keys[%s] is error: %s" % (item, str(value)))
66
67
68 def unit_test():
69     from vstf.common.log import setup_logging
70     setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/vstf-flows-settings.log", clevel=logging.INFO)
71
72     flows_settings = FlowsSettings()
73     LOG.info(flows_settings.settings)
74
75     flows_settings.clear_all()
76     flows_settings.set_flows(2)
77     LOG.info(flows_settings.settings)
78
79     flow_1 = {
80         "agent": "192.168.188.14",
81         "dev": {
82             "ip": "192.168.1.100",
83             "namespace": "vstf-space-1",
84             "mac": "90:e2:ba:20:1f:d8",
85             "iface": "eth4",
86             "bdf": "04:00.0"
87         }
88     }
89     flow_2 = {
90         "agent": "192.168.188.14",
91         "dev": {
92             "ip": "192.168.1.101",
93             "namespace": "vstf-space-2",
94             "mac": "90:e2:ba:20:1f:d9",
95             "iface": "p57p2",
96             "bdf": "04:00.1"
97         }
98     }
99
100     flows_settings.add_senders(flow_1)
101     flows_settings.add_senders(flow_2)
102     flows_settings.add_receivers(flow_2)
103     flows_settings.add_receivers(flow_1)
104
105     flows_settings.add_watchers(flow_1)
106     flows_settings.add_watchers(flow_2)
107
108     flows_settings.add_namespaces(flow_1)
109     flows_settings.add_namespaces(flow_2)
110
111     cpu = {
112         "agent": "192.168.188.16",
113         "affctl":{
114             "policy": 2
115         }
116     }
117     flows_settings.add_cpu_listens(cpu)
118     LOG.info(flows_settings.settings)
119
120
121 if __name__ == '__main__':
122     unit_test()