summaryrefslogtreecommitdiffstats
path: root/presence/pfpgen.py
blob: 1e22ac781ec7d7049804f9b33938b5764f31bb8b (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env python

'''
Phosphor Fan Presence (PFP) YAML parser and code generator.

Parse the provided PFP configuration file and generate C++ code.

The parser workflow is broken down as follows:
  1 - Import the YAML configuration file as native python type(s)
        instance(s).
  2 - Create an instance of the Everything class from the
        native python type instance(s) with the Everything.load
        method.
  3 - The Everything class constructor orchestrates conversion of the
        native python type(s) instances(s) to render helper types.
        Each render helper type constructor imports its attributes
        from the native python type(s) instances(s).
  4 - Present the converted YAML to the command processing method
        requested by the script user.
'''

import os
import sys
import yaml
from argparse import ArgumentParser
import mako.lookup
from sdbusplus.renderer import Renderer
from sdbusplus.namedelement import NamedElement


class InvalidConfigError(BaseException):
    '''General purpose config file parsing error.'''

    def __init__(self, path, msg):
        '''Display configuration file with the syntax
        error and the error message.'''

        self.config = path
        self.msg = msg


class NotUniqueError(InvalidConfigError):
    '''Within a config file names must be unique.
    Display the duplicate item.'''

    def __init__(self, path, cls, *names):
        fmt = 'Duplicate {0}: "{1}"'
        super(NotUniqueError, self).__init__(
            path, fmt.format(cls, ' '.join(names)))


def get_index(objs, cls, name):
    '''Items are usually rendered as C++ arrays and as
    such are stored in python lists.  Given an item name
    its class, find the item index.'''

    for i, x in enumerate(objs.get(cls, [])):
        if x.name != name:
            continue

        return i
    raise InvalidConfigError('Could not find name: "{0}"'.format(name))


def exists(objs, cls, name):
    '''Check to see if an item already exists in a list given
    the item name.'''

    try:
        get_index(objs, cls, name)
    except:
        return False

    return True


def add_unique(obj, *a, **kw):
    '''Add an item to one or more lists unless already present.'''

    for container in a:
        if not exists(container, obj.cls, obj.name):
            container.setdefault(obj.cls, []).append(obj)


class Indent(object):
    '''Help templates be depth agnostic.'''

    def __init__(self, depth=0):
        self.depth = depth

    def __add__(self, depth):
        return Indent(self.depth + depth)

    def __call__(self, depth):
        '''Render an indent at the current depth plus depth.'''
        return 4*' '*(depth + self.depth)


class ConfigEntry(NamedElement):
    '''Base interface for rendered items.'''

    def __init__(self, *a, **kw):
        '''Pop the class keyword.'''

        self.cls = kw.pop('class')
        super(ConfigEntry, self).__init__(**kw)

    def factory(self, objs):
        ''' Optional factory interface for subclasses to add
        additional items to be rendered.'''

        pass

    def setup(self, objs):
        ''' Optional setup interface for subclasses, invoked
        after all factory methods have been run.'''

        pass


class Sensor(ConfigEntry):
    '''Convenience type for config file method:type handlers.'''

    def __init__(self, *a, **kw):
        kw['class'] = 'sensor'
        kw.pop('type')
        self.policy = kw.pop('policy')
        super(Sensor, self).__init__(**kw)

    def setup(self, objs):
        '''All sensors have an associated policy.  Get the policy index.'''

        self.policy = get_index(objs, 'policy', self.policy)


class Gpio(Sensor, Renderer):
    '''Handler for method:type:gpio.'''

    def __init__(self, *a, **kw):
        self.key = kw.pop('key')
        self.physpath = kw.pop('physpath')
        kw['name'] = 'gpio-{}'.format(self.key)
        super(Gpio, self).__init__(**kw)

    def construct(self, loader, indent):
        return self.render(
            loader,
            'gpio.mako.hpp',
            g=self,
            indent=indent)

    def setup(self, objs):
        super(Gpio, self).setup(objs)


class Tach(Sensor, Renderer):
    '''Handler for method:type:tach.'''

    def __init__(self, *a, **kw):
        self.sensors = kw.pop('sensors')
        kw['name'] = 'tach-{}'.format('-'.join(self.sensors))
        super(Tach, self).__init__(**kw)

    def construct(self, loader, indent):
        return self.render(
            loader,
            'tach.mako.hpp',
            t=self,
            indent=indent)

    def setup(self, objs):
        super(Tach, self).setup(objs)


class Rpolicy(ConfigEntry):
    '''Convenience type for config file rpolicy:type handlers.'''

    def __init__(self, *a, **kw):
        kw.pop('type', None)
        self.fan = kw.pop('fan')
        self.sensors = []
        kw['class'] = 'policy'
        super(Rpolicy, self).__init__(**kw)

    def setup(self, objs):
        '''All policies have an associated fan and methods.
        Resolve the indicies.'''

        sensors = []
        for s in self.sensors:
            sensors.append(get_index(objs, 'sensor', s))

        self.sensors = sensors
        self.fan = get_index(objs, 'fan', self.fan)


class Fallback(Rpolicy, Renderer):
    '''Default policy handler (policy:type:fallback).'''

    def __init__(self, *a, **kw):
        kw['name'] = 'fallback-{}'.format(kw['fan'])
        super(Fallback, self).__init__(**kw)

    def setup(self, objs):
        super(Fallback, self).setup(objs)

    def construct(self, loader, indent):
        return self.render(
            loader,
            'fallback.mako.hpp',
            f=self,
            indent=indent)


class Fan(ConfigEntry):
    '''Fan directive handler.  Fans entries consist of an inventory path,
    optional redundancy policy and associated sensors.'''

    def __init__(self, *a, **kw):
        self.path = kw.pop('path')
        self.methods = kw.pop('methods')
        self.rpolicy = kw.pop('rpolicy', None)
        super(Fan, self).__init__(**kw)

    def factory(self, objs):
        ''' Create rpolicy and sensor(s) objects.'''

        if self.rpolicy:
            self.rpolicy['fan'] = self.name
            factory = Everything.classmap(self.rpolicy['type'])
            rpolicy = factory(**self.rpolicy)
        else:
            rpolicy = Fallback(fan=self.name)

        for m in self.methods:
            m['policy'] = rpolicy.name
            factory = Everything.classmap(m['type'])
            sensor = factory(**m)
            rpolicy.sensors.append(sensor.name)
            add_unique(sensor, objs)

        add_unique(rpolicy, objs)
        super(Fan, self).factory(objs)


class Everything(Renderer):
    '''Parse/render entry point.'''

    @staticmethod
    def classmap(cls):
        '''Map render item class entries to the appropriate
        handler methods.'''

        class_map = {
            'fan': Fan,
            'fallback': Fallback,
            'gpio': Gpio,
            'tach': Tach,
        }

        if cls not in class_map:
            raise NotImplementedError('Unknown class: "{0}"'.format(cls))

        return class_map[cls]

    @staticmethod
    def load(args):
        '''Load the configuration file.  Parsing occurs in three phases.
        In the first phase a factory method associated with each
        configuration file directive is invoked.  These factory
        methods generate more factory methods.  In the second
        phase the factory methods created in the first phase
        are invoked.  In the last phase a callback is invoked on
        each object created in phase two.  Typically the callback
        resolves references to other configuration file directives.'''

        factory_objs = {}
        objs = {}
        with open(args.input, 'r') as fd:
            for x in yaml.safe_load(fd.read()) or {}:

                # The top level elements all represent fans.
                x['class'] = 'fan'
                # Create factory object for this config file directive.
                factory = Everything.classmap(x['class'])
                obj = factory(**x)

                # For a given class of directive, validate the file
                # doesn't have any duplicate names.
                if exists(factory_objs, obj.cls, obj.name):
                    raise NotUniqueError(args.input, 'fan', obj.name)

                factory_objs.setdefault('fan', []).append(obj)
                objs.setdefault('fan', []).append(obj)

            for cls, items in factory_objs.items():
                for obj in items:
                    # Add objects for template consumption.
                    obj.factory(objs)

            # Configuration file directives reference each other via
            # the name attribute; however, when rendered the reference
            # is just an array index.
            #
            # At this point all objects have been created but references
            # have not been resolved to array indicies.  Instruct objects
            # to do that now.
            for cls, items in objs.items():
                for obj in items:
                    obj.setup(objs)

        return Everything(**objs)

    def __init__(self, *a, **kw):
        self.fans = kw.pop('fan', [])
        self.policies = kw.pop('policy', [])
        self.sensors = kw.pop('sensor', [])
        super(Everything, self).__init__(**kw)

    def generate_cpp(self, loader):
        '''Render the template with the provided data.'''
        sys.stdout.write(
            self.render(
                loader,
                args.template,
                fans=self.fans,
                sensors=self.sensors,
                policies=self.policies,
                indent=Indent()))

if __name__ == '__main__':
    script_dir = os.path.dirname(os.path.realpath(__file__))
    valid_commands = {
        'generate-cpp': 'generate_cpp',
    }

    parser = ArgumentParser(
        description='Phosphor Fan Presence (PFP) YAML '
        'scanner and code generator.')

    parser.add_argument(
        '-i', '--input', dest='input',
        default=os.path.join(script_dir, 'example', 'example.yaml'),
        help='Location of config file to process.')
    parser.add_argument(
        '-t', '--template', dest='template',
        default='generated.mako.hpp',
        help='The top level template to render.')
    parser.add_argument(
        '-p', '--template-path', dest='template_search',
        default=os.path.join(script_dir, 'templates'),
        help='The space delimited mako template search path.')
    parser.add_argument(
        'command', metavar='COMMAND', type=str,
        choices=valid_commands.keys(),
        help='%s.' % ' | '.join(valid_commands.keys()))

    args = parser.parse_args()

    if sys.version_info < (3, 0):
        lookup = mako.lookup.TemplateLookup(
            directories=args.template_search.split(),
            disable_unicode=True)
    else:
        lookup = mako.lookup.TemplateLookup(
            directories=args.template_search.split())
    try:
        function = getattr(
            Everything.load(args),
            valid_commands[args.command])
        function(lookup)
    except InvalidConfigError as e:
        sys.stderr.write('{0}: {1}\n\n'.format(e.config, e.msg))
        raise
OpenPOWER on IntegriCloud