File: operator.py

package info (click to toggle)
python-os-ken 3.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,320 kB
  • sloc: python: 100,703; erlang: 14,517; ansic: 594; sh: 338; makefile: 136
file content (76 lines) | stat: -rw-r--r-- 2,451 bytes parent folder | download | duplicates (4)
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
# Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
 Api for operator. Mainly commands to build CLI and
 operator interface around them.
"""
import logging

from os_ken.services.protocols.bgp.api.base import ApiException
from os_ken.services.protocols.bgp.api.base import register
from os_ken.services.protocols.bgp.api.rpc_log_handler import RpcLogHandler
from os_ken.services.protocols.bgp.operator.command import Command
from os_ken.services.protocols.bgp.operator.command import STATUS_ERROR
from os_ken.services.protocols.bgp.operator.commands.clear import ClearCmd
from os_ken.services.protocols.bgp.operator.commands.set import SetCmd
from os_ken.services.protocols.bgp.operator.commands.show import ShowCmd
from os_ken.services.protocols.bgp.operator.internal_api import InternalApi

LOG = logging.getLogger('bgpspeaker.api.rtconf')

DEFAULT_LOG_FORMAT = '%(asctime)s %(levelname)s %(message)s'


def _init_log_handler():
    log_handler = RpcLogHandler()
    log_handler.setLevel(logging.ERROR)
    log_handler.formatter = logging.Formatter(DEFAULT_LOG_FORMAT)
    return log_handler


INTERNAL_API = InternalApi(_init_log_handler())


class RootCmd(Command):
    subcommands = {
        'show': ShowCmd,
        'set': SetCmd,
        'clear': ClearCmd}


def operator_run(cmd, **kwargs):
    params = kwargs.get('params', [])
    fmt = kwargs.get('format', 'json')
    root = RootCmd(api=INTERNAL_API, resp_formatter_name=fmt)
    ret, _ = root([cmd] + params)
    if ret.status == STATUS_ERROR:
        raise ApiException(str(ret.value))
    return ret.value


@register(name="operator.show")
def operator_show(**kwargs):
    return operator_run('show', **kwargs)


@register(name="operator.set")
def operator_set(**kwargs):
    return operator_run('set', **kwargs)


@register(name="operator.clear")
def operator_clear(**kwargs):
    return operator_run('clear', **kwargs)