aboutsummaryrefslogtreecommitdiffstats
path: root/functest/energy/energy.py
blob: 71b7123930ed78df34f3db3203b1bbee2fd56f3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Copyright (c) 2017 Orange and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

"""This module manages calls to Energy recording API."""

import json
import logging
import urllib
import requests

import functest.utils.functest_utils as ft_utils


def enable_recording(method):
    """
    Record energy during method execution.

    Decorator to record energy during "method" exection.

        param method: Method to suround with start and stop
        :type method: function

        .. note:: "method" should belong to a class having a "case_name"
                  attribute
    """
    def wrapper(*args):
        """
        Record energy during method execution (implementation).

        Wrapper for decorator to handle method arguments.
        """
        current_scenario = EnergyRecorder.get_current_scenario()
        EnergyRecorder.start(args[0].case_name)
        return_value = method(*args)
        if current_scenario is None:
            EnergyRecorder.stop()
        else:
            EnergyRecorder.submit_scenario(
                current_scenario["scenario"],
                current_scenario["step"]
            )
        return return_value
    return wrapper


# Class to manage energy recording sessions
class EnergyRecorder(object):
    """Manage Energy recording session."""

    logger = logging.getLogger(__name__)
    # Energy recording API connectivity settings
    # see load_config method
    energy_recorder_api = None

    # Default initial step
    INITIAL_STEP = "running"

    @staticmethod
    def load_config():
        """
        Load connectivity settings from yaml.

        Load connectivity settings to Energy recording API
        Use functest global config yaml file
        (see functest_utils.get_functest_config)
        """
        # Singleton pattern for energy_recorder_api static member
        # Load only if not previouly done
        if EnergyRecorder.energy_recorder_api is None:
            environment = ft_utils.get_pod_name()

            # API URL
            energy_recorder_uri = ft_utils.get_functest_config(
                "energy_recorder.api_url")
            assert energy_recorder_uri
            assert environment

            energy_recorder_uri += "/recorders/environment/"
            energy_recorder_uri += urllib.quote_plus(environment)
            EnergyRecorder.logger.debug(
                "API recorder at: " + energy_recorder_uri)

            # Creds
            user = ft_utils.get_functest_config(
                "energy_recorder.api_user")
            password = ft_utils.get_functest_config(
                "energy_recorder.api_password")

            if user != "" and password != "":
                energy_recorder_api_auth = (user, password)
            else:
                energy_recorder_api_auth = None

            # Final config
            EnergyRecorder.energy_recorder_api = {
                "uri": energy_recorder_uri,
                "auth": energy_recorder_api_auth
            }

    @staticmethod
    def submit_scenario(scenario, step):
        """
        Submit a complet scenario definition to Energy recorder API.

            param scenario: Scenario name
            :type scenario: string
            param step: Step name
            :type step: string
        """
        return_status = True
        try:
            EnergyRecorder.logger.debug("Submitting scenario")
            # Ensure that connectyvity settings are loaded
            EnergyRecorder.load_config()

            # Create API payload
            payload = {
                "step": step,
                "scenario": scenario
            }
            # Call API to start energy recording
            response = requests.post(
                EnergyRecorder.energy_recorder_api["uri"],
                data=json.dumps(payload),
                auth=EnergyRecorder.energy_recorder_api["auth"],
                headers={
                    'content-type': 'application/json'
                }
            )
            if response.status_code != 200:
                log_msg = "Error while submitting scenario\n{}"
                log_msg = log_msg.format(response.text)
                EnergyRecorder.logger.info(log_msg)
                return_status = False
        except Exception:  # pylint: disable=broad-except
            # Default exception handler to ensure that method
            # is safe for caller
            EnergyRecorder.logger.exception(
                "Error while submitting scenarion to energy recorder API"
            )
            return_status = False
        return return_status

    @staticmethod
    def start(scenario):
        """
        Start a recording session for scenario.

            param scenario: Starting scenario
            :type scenario: string
        """
        return_status = True
        try:
            EnergyRecorder.logger.debug("Starting recording")
            return_status = EnergyRecorder.submit_scenario(
                scenario,
                EnergyRecorder.INITIAL_STEP
            )

        except Exception:  # pylint: disable=broad-except
            # Default exception handler to ensure that method
            # is safe for caller
            EnergyRecorder.logger.exception(
                "Error while starting energy recorder API"
            )
            return_status = False
        return return_status

    @staticmethod
    def stop():
        """Stop current recording session."""
        EnergyRecorder.logger.debug("Stopping recording")
        return_status = True
        try:
            # Ensure that connectyvity settings are loaded
            EnergyRecorder.load_config()

            # Call API to stop energy recording
            response = requests.delete(
                EnergyRecorder.energy_recorder_api["uri"],
                auth=EnergyRecorder.energy_recorder_api["auth"],
                headers={
                    'content-type': 'application/json'
                }
            )
            if response.status_code != 200:
                log_msg = "Error while stating energy recording session\n{}"
                log_msg = log_msg.format(response.text)
                EnergyRecorder.logger.error(log_msg)
                return_status = False
        except Exception:  # pylint: disable=broad-except
            # Default exception handler to ensure that method
            # is safe for caller
            EnergyRecorder.logger.exception(
                "Error while stoping energy recorder API"
            )
            return_status = False
        return return_status

    @staticmethod
    def set_step(step):
        """Notify energy recording service of current step of the testcase."""
        EnergyRecorder.logger.debug("Setting step")
        return_status = True
        try:
            # Ensure that connectyvity settings are loaded
            EnergyRecorder.load_config()

            # Create API payload
            payload = {
                "step": step,
            }

            # Call API to define step
            response = requests.post(
                EnergyRecorder.energy_recorder_api["uri"] + "/step",
                data=json.dumps(payload),
                auth=EnergyRecorder.energy_recorder_api["auth"],
                headers={
                    'content-type': 'application/json'
                }
            )
            if response.status_code != 200:
                log_msg = "Error while setting current step of testcase\n{}"
                log_msg = log_msg.format(response.text)
                EnergyRecorder.logger.error(log_msg)
                return_status = False
        except Exception:  # pylint: disable=broad-except
            # Default exception handler to ensure that method
            # is safe for caller
            EnergyRecorder.logger.exception(
                "Error while setting step on energy recorder API"
            )
            return_status = False
        return return_status

    @staticmethod
    def get_current_scenario():
        """Get current running scenario (if any, None else)."""
        EnergyRecorder.logger.debug("Getting current scenario")
        return_value = None
        print "In get current"
        try:
            # Ensure that connectyvity settings are loaded
            EnergyRecorder.load_config()

            # Call API get running scenario
            response = requests.get(
                EnergyRecorder.energy_recorder_api["uri"],
                auth=EnergyRecorder.energy_recorder_api["auth"]
            )
            if response.status_code == 200:
                return_value = json.loads(response.text)
            elif response.status_code == 404:
                log_msg = "No current running scenario at {}"
                log_msg = log_msg.format(
                    EnergyRecorder.energy_recorder_api["uri"])
                EnergyRecorder.logger.error(log_msg)
                print log_msg
                return_value = None
            else:
                log_msg = "Error while getting current scenario\n{}"
                log_msg = log_msg.format(response.text)
                EnergyRecorder.logger.error(log_msg)
                print log_msg
                return_value = None
        except Exception:  # pylint: disable=broad-except
            # Default exception handler to ensure that method
            # is safe for caller
            EnergyRecorder.logger.exception(
                "Error while getting current scenario from energy recorder API"
            )
            return_value = None
        return return_value