Re-factor Node.py to use better python inbuilt functions
[yardstick.git] / yardstick / benchmark / contexts / node.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 from __future__ import absolute_import
11 import logging
12 import errno
13 import os
14 import collections
15 import yaml
16
17 from yardstick.benchmark.contexts.base import Context
18 from yardstick.definitions import YARDSTICK_ROOT_PATH
19
20 LOG = logging.getLogger(__name__)
21
22
23 class NodeContext(Context):
24     """Class that handle nodes info"""
25
26     __context_type__ = "Node"
27
28     def __init__(self):
29         self.name = None
30         self.file_path = None
31         self.nodes = []
32         self.controllers = []
33         self.computes = []
34         self.baremetals = []
35         super(self.__class__, self).__init__()
36
37     def read_config_file(self):
38         """Read from config file"""
39
40         with open(self.file_path) as stream:
41             LOG.info("Parsing pod file: %s", self.file_path)
42             cfg = yaml.load(stream)
43         return cfg
44
45     def init(self, attrs):
46         """initializes itself from the supplied arguments"""
47         self.name = attrs["name"]
48         self.file_path = attrs.get("file", "pod.yaml")
49
50         try:
51             cfg = self.read_config_file()
52         except IOError as ioerror:
53             if ioerror.errno == errno.ENOENT:
54                 self.file_path = \
55                     os.path.join(YARDSTICK_ROOT_PATH, self.file_path)
56                 cfg = self.read_config_file()
57             else:
58                 raise
59
60         self.nodes.extend(cfg["nodes"])
61         self.controllers.extend([node for node in cfg["nodes"]
62                                  if node["role"] == "Controller"])
63         self.computes.extend([node for node in cfg["nodes"]
64                               if node["role"] == "Compute"])
65         self.baremetals.extend([node for node in cfg["nodes"]
66                                 if node["role"] == "Baremetal"])
67         LOG.debug("Nodes: %r", self.nodes)
68         LOG.debug("Controllers: %r", self.controllers)
69         LOG.debug("Computes: %r", self.computes)
70         LOG.debug("BareMetals: %r", self.baremetals)
71
72     def deploy(self):
73         """don't need to deploy"""
74         pass
75
76     def undeploy(self):
77         """don't need to undeploy"""
78         pass
79
80     def _get_server(self, attr_name):
81         """lookup server info by name from context
82         attr_name: a name for a server listed in nodes config file
83         """
84         if isinstance(attr_name, collections.Mapping):
85             return None
86
87         if self.name != attr_name.split(".")[1]:
88             return None
89         node_name = attr_name.split(".")[0]
90         matching_nodes = (n for n in self.nodes if n["name"] == node_name)
91
92         try:
93             # A clone is created in order to avoid affecting the
94             # original one.
95             node = dict(next(matching_nodes))
96         except StopIteration:
97             return None
98
99         try:
100             duplicate = next(matching_nodes)
101         except StopIteration:
102             pass
103         else:
104             raise ValueError("Duplicate nodes!!! Nodes: %s %s",
105                              (matching_nodes, duplicate))
106
107         node["name"] = attr_name
108         return node