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