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