File: core.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 (95 lines) | stat: -rw-r--r-- 3,297 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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.

"""
 Defines APIs related to Core/CoreManager.
"""
from os_ken.lib import hub

from os_ken.services.protocols.bgp.api.base import register
from os_ken.services.protocols.bgp.core_manager import CORE_MANAGER
from os_ken.services.protocols.bgp.rtconf.base import RuntimeConfigError
from os_ken.services.protocols.bgp.rtconf.common import CommonConf


NEIGHBOR_RESET_WAIT_TIME = 3


@register(name='core.start')
def start(**kwargs):
    """Starts new context using provided configuration.

    Raises RuntimeConfigError if a context is already active.
    """
    if CORE_MANAGER.started:
        raise RuntimeConfigError('Current context has to be stopped to start '
                                 'a new context.')

    try:
        waiter = kwargs.pop('waiter')
    except KeyError:
        waiter = hub.Event()
    common_config = CommonConf(**kwargs)
    hub.spawn(CORE_MANAGER.start, *[], **{'common_conf': common_config,
                                          'waiter': waiter})
    return True


@register(name='core.stop')
def stop(**kwargs):
    """Stops current context is one is active.

    Raises RuntimeConfigError if runtime is not active or initialized yet.
    """
    if not CORE_MANAGER.started:
        raise RuntimeConfigError('No runtime is active. Call start to create '
                                 'a runtime')
    CORE_MANAGER.stop()
    return True


@register(name='core.reset_neighbor')
def reset_neighbor(ip_address):
    neighs_conf = CORE_MANAGER.neighbors_conf
    neigh_conf = neighs_conf.get_neighbor_conf(ip_address)
    # Check if we have neighbor with given IP.
    if not neigh_conf:
        raise RuntimeConfigError('No neighbor configuration found for given'
                                 ' IP: %s' % ip_address)
    # If neighbor is enabled, we disable it.
    if neigh_conf.enabled:
        # Disable neighbor to close existing session.
        neigh_conf.enabled = False
        # Enable neighbor after NEIGHBOR_RESET_WAIT_TIME
        # this API works asynchronously
        # it's recommended to check it really reset neighbor later

        def up():
            neigh_conf.enabled = True
        hub.spawn_after(NEIGHBOR_RESET_WAIT_TIME, up)
    else:
        raise RuntimeConfigError('Neighbor %s is not enabled, hence cannot'
                                 ' reset.' % ip_address)
    return True


# =============================================================================
# Common configuration related APIs
# =============================================================================

@register(name='comm_conf.get')
def get_common_conf():
    comm_conf = CORE_MANAGER.common_conf
    return comm_conf.settings