aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/openstack/neutron/plugin/networking-onos/mech_driver.py
blob: 207dc6997fd6485699e453ccfda23cefc16ea790 (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
# Copyright (c) 2015 Huawei Technologies India Pvt Ltd
# All Rights Reserved.
#
#    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.

import requests

from oslo_config import cfg
#from oslo_log import helpers as log_helpers
from neutron.openstack.common import log as logging
from neutron.openstack.common import jsonutils

from neutron.plugins.ml2 import driver_api as api

LOG = logging.getLogger(__name__)

ONOS_DRIVER_OPTS = [
    cfg.StrOpt('path',
               default='',
               help=_('ONOS ReST interface URL')),
    cfg.StrOpt('username',
               default='',
               help=_('Username for authentication.')),
    cfg.StrOpt('password',
               default='',
               secret=True,  # do not expose value in the logs
               help=_('Password for authentication.'))
]

cfg.CONF.register_opts(ONOS_DRIVER_OPTS, "ml2_onos")


def send_msg(onos_path, onos_auth, msg_type, entity_path, entity=None):
    """Send message to the ONOS controller."""

    path = '/'.join([onos_path, entity_path])
    LOG.debug("Sending MSG (%(msg)) URL (%(path)s) JSON (%(entity)s)",
              {'msg': msg_type, 'path': path, 'entity': entity})
    hdr = {'Content-Type': 'application/json'}
    body = jsonutils.dumps(entity, indent=2) if entity else None
    req = requests.request(method=msg_type, url=path,
                           headers=hdr, data=body,
                           auth=onos_auth)
    # Let's raise voice for an error
    req.raise_for_status()


class ONOSMechanismDriver(api.MechanismDriver):

    """Open Networking Operating System ML2 Driver for Neutron.

    Code which makes communication between ONOS and OpenStack Neutron
    possible.
    """
    def __init__(self):
        conf = cfg.CONF.ml2_onos
        self.onos_path = conf.url_path
        self.onos_auth = (conf.username, conf.password)

    def initialize(self):
        # No action required as of now. Can be extended in
        # the future if required.
        pass

    #@log_helpers.log_method_call
    def create_network_postcommit(self, context):
        entity_path = 'networks/' + context.current['id']
        resource = context.current.copy()
        send_msg(self.onos_path, self.onos_auth, 'post',
                 entity_path, {'network': resource})

    #@log_helpers.log_method_call
    def update_network_postcommit(self, context):
        entity_path = 'networks/' + context.current['id']
        resource = context.current.copy()
        send_msg(self.onos_path, self.onos_auth, 'put',
                 entity_path, {'network': resource})

    #@log_helpers.log_method_call
    def delete_network_postcommit(self, context):
        entity_path = 'networks/' + context.current['id']
        send_msg(self.onos_path, self.onos_auth, 'delete',
                 entity_path)

    #@log_helpers.log_method_call
    def create_subnet_postcommit(self, context):
        entity_path = 'subnets/' + context.current['id']
        resource = context.current.copy()
        send_msg(self.onos_path, self.onos_auth, 'post',
                 entity_path, {'subnet': resource})

    #@log_helpers.log_method_call
    def update_subnet_postcommit(self, context):
        entity_path = 'subnets/' + context.current['id']
        resource = context.current.copy()
        send_msg(self.onos_path, self.onos_auth, 'put',
                 entity_path, {'subnet': resource})

    #@log_helpers.log_method_call
    def delete_subnet_postcommit(self, context):
        entity_path = 'subnets/' + context.current['id']
        send_msg(self.onos_path, self.onos_auth, 'delete',
                 entity_path)

    #@log_helpers.log_method_call
    def create_port_postcommit(self, context):
        entity_path = 'ports/' + context.current['id']
        resource = context.current.copy()
        send_msg(self.onos_path, self.onos_auth, 'post',
                 entity_path, {'port': resource})

    #@log_helpers.log_method_call
    def update_port_postcommit(self, context):
        entity_path = 'ports/' + context.current['id']
        resource = context.current.copy()
        send_msg(self.onos_path, self.onos_auth, 'put',
                 entity_path, {'port': resource})

    #@log_helpers.log_method_call
    def delete_port_postcommit(self, context):
        entity_path = 'ports/' + context.current['id']
        send_msg(self.onos_path, self.onos_auth, 'delete',
                 entity_path)