Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / 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
19     def __init__(self, path="/etc/vstf/perf/",
20                  filename="sw_perf.flownodes-settings",
21                  mode=sets.SETS_SINGLE):
22         self._check_actors = {'namespaces', 'senders', 'receivers', 'watchers'}
23         self._nocheck_actors = {"cpu_listens"}
24         super(FlowsSettings, self).__init__(path, filename, mode)
25
26     def _register_func(self):
27         super(FlowsSettings, self)._register_func()
28         for actor in self._check_actors:
29             actor = actor.encode()
30             func_name = "add_%s" % actor
31             setattr(
32                 self,
33                 func_name,
34                 self._adding_file(
35                     func_name,
36                     self._mset,
37                     self._fset,
38                     actor,
39                     self._check_add))
40             func_name = "madd_%s" % actor
41             setattr(
42                 self,
43                 func_name,
44                 self._adding_memory(
45                     func_name,
46                     self._mset,
47                     actor,
48                     self._check_add))
49
50         for actor in self._nocheck_actors:
51             actor = actor.encode()
52             func_name = "add_%s" % actor
53             setattr(
54                 self,
55                 func_name,
56                 self._adding_file(
57                     func_name,
58                     self._mset,
59                     self._fset,
60                     actor))
61             func_name = "madd_%s" % actor
62             setattr(
63                 self,
64                 func_name,
65                 self._adding_memory(
66                     func_name,
67                     self._mset,
68                     actor))
69
70         LOG.debug(self.__dict__.keys())
71
72     def clear_all(self):
73         actors = self._check_actors | self._nocheck_actors
74         for actor in actors:
75             func_name = "set_%s" % actor
76             func = getattr(self, func_name)
77             func([])
78
79     def mclear_all(self):
80         actors = self._check_actors | self._nocheck_actors
81         for actor in actors:
82             func_name = "mset_%s" % actor
83             func = getattr(self, func_name)
84             func([])
85
86     def _check_add(self, value):
87         flows = ['agent', 'dev']
88         if not isinstance(value, dict):
89             raise Exception("type is error: %s" % (str(value)))
90         for flow in flows:
91             if flow not in value.keys():
92                 raise Exception("keys[%s] is missing: %s" % (flow, str(value)))
93
94         items = ["ip", "namespace", "mac", "iface", "bdf"]
95         for item in items:
96             if item not in value['dev'].keys():
97                 raise Exception("keys[%s] is error: %s" % (item, str(value)))
98
99
100 def unit_test():
101     from vstf.common.log import setup_logging
102     setup_logging(
103         level=logging.DEBUG,
104         log_file="/var/log/vstf/vstf-flows-settings.log",
105         clevel=logging.INFO)
106
107     flows_settings = FlowsSettings()
108     LOG.info(flows_settings.settings)
109
110     flows_settings.clear_all()
111     flows_settings.set_flows(2)
112     LOG.info(flows_settings.settings)
113
114     flow_1 = {
115         "agent": "192.168.188.14",
116         "dev": {
117             "ip": "192.168.1.100",
118             "namespace": "vstf-space-1",
119             "mac": "90:e2:ba:20:1f:d8",
120             "iface": "eth4",
121             "bdf": "04:00.0"
122         }
123     }
124     flow_2 = {
125         "agent": "192.168.188.14",
126         "dev": {
127             "ip": "192.168.1.101",
128             "namespace": "vstf-space-2",
129             "mac": "90:e2:ba:20:1f:d9",
130             "iface": "p57p2",
131             "bdf": "04:00.1"
132         }
133     }
134
135     flows_settings.add_senders(flow_1)
136     flows_settings.add_senders(flow_2)
137     flows_settings.add_receivers(flow_2)
138     flows_settings.add_receivers(flow_1)
139
140     flows_settings.add_watchers(flow_1)
141     flows_settings.add_watchers(flow_2)
142
143     flows_settings.add_namespaces(flow_1)
144     flows_settings.add_namespaces(flow_2)
145
146     cpu = {
147         "agent": "192.168.188.16",
148         "affctl": {
149             "policy": 2
150         }
151     }
152     flows_settings.add_cpu_listens(cpu)
153     LOG.info(flows_settings.settings)
154
155
156 if __name__ == '__main__':
157     unit_test()