JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / controller / sw_perf / flow_producer.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
11 import logging
12
13 from vstf.controller.settings.device_settings import DeviceSettings
14 from vstf.controller.settings.forwarding_settings import ForwardingSettings
15 from vstf.controller.settings.cpu_settings import CpuSettings
16 from vstf.controller.fabricant import Fabricant
17 from vstf.controller.settings.flows_settings import FlowsSettings
18 import vstf.common.constants as cst
19
20 LOG = logging.getLogger(__name__)
21
22
23 class FlowsProducer(object):
24     def __init__(self, conn, flows_settings):
25         self._perf = flows_settings
26         self._forwarding = ForwardingSettings().settings
27         self._device = DeviceSettings().settings
28         self._cpu = CpuSettings().settings
29         self._conn = conn
30         self._devs_map = {}
31
32     def get_dev(self, item):
33         agent = self._device[item[0]]["agent"]
34         devs = self._device[item[0]]["devs"][item[1]]
35
36         keys = ["bdf", "iface", "mac"]
37
38         key = devs.keys()[0]
39
40         if key in keys:
41             name = devs[key]
42         else:
43             raise Exception("error devs :%s", devs)
44         LOG.info(agent)
45         LOG.info(name)
46         if not self._devs_map.has_key((agent, name)):
47             query = Fabricant(agent, self._conn)
48             query.clean_all_namespace()
49             dev_info = query.get_device_verbose(identity=name)
50             if not isinstance(dev_info, dict):
51                 err = "get device detail failed, agent:%s net:%s" % (agent, name)
52                 raise Exception(err)
53             dev = {
54                 "agent": agent,
55                 "dev": {
56                     "bdf": dev_info["bdf"],
57                     "iface": dev_info["iface"],
58                     "mac": dev_info["mac"],
59                     "ip": None,
60                     "namespace": None
61                 }
62             }
63
64             self._devs_map[(agent, name)] = dev
65             LOG.info(dev)
66
67         return self._devs_map[(agent, name)]
68
69     def get_host(self):
70         result = {
71             "agent": self._device["host"]["agent"],
72             "affctl": self._cpu["affctl"]
73         }
74         return result
75
76     def create(self, scenario, case):
77         self._devs_map = {}
78         flows_indexes = self._forwarding[scenario]["flows"]
79         flows_infos = []
80         for index in flows_indexes:
81             if not index:
82                 raise Exception("error flows %s" % flows_indexes)
83             dev = self.get_dev(index)
84             flows_infos.append(dev)
85
86         flows_infos[0]['dev'].update(self._forwarding["head"])
87         flows_infos[-1]['dev'].update(self._forwarding["tail"])
88
89         LOG.info(flows_infos)
90
91         actor_info = cst.CASE_ACTOR_MAP[case]
92
93         self._perf.clear_all()
94         senders = actor_info["senders"]
95         LOG.info(senders)
96         for sender in senders:
97             dev = flows_infos[sender]
98             if dev:
99                 self._perf.add_senders(dev)
100
101         receivers = actor_info["receivers"]
102         for receiver in receivers:
103             dev = flows_infos[receiver]
104             if dev:
105                 self._perf.add_receivers(dev)
106
107         watchers = self._forwarding[scenario]["watchers"]
108         for watcher in watchers:
109             dev = flows_infos[watcher]
110             if dev:
111                 self._perf.add_watchers(dev)
112
113         namespaces = [0, -1]
114         for namespace in namespaces:
115             dev = flows_infos[namespace]
116             if dev:
117                 self._perf.add_namespaces(dev)
118
119         host = self.get_host()
120         if host:
121             self._perf.add_cpu_listens(host)
122
123         self._perf.set_flows(actor_info["flows"])
124         return True
125
126
127 def unit_test():
128     from vstf.rpc_frame_work.rpc_producer import Server
129     from vstf.common.log import setup_logging
130     setup_logging(level=logging.INFO, log_file="/var/log/vstf/vstf-producer.log", clevel=logging.INFO)
131
132     conn = Server("192.168.188.10")
133     flow_settings = FlowsSettings()
134     flow_producer = FlowsProducer(conn, flow_settings)
135     scenario = "Tn"
136     case = "Tn-1"
137     flow_producer.create(scenario, case)
138
139
140 if __name__ == '__main__':
141     unit_test()