~ubuntu-branches/ubuntu/trusty/quantum/trusty

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
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2011 Cisco Systems, Inc.  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.
#
#    @author: Edgar Magana, Cisco Systems
#
"""
Network Library to insert services using Quantum APIs
Currently has four functionalities:
1. insert_inpath_service <tenant_id> <service_image_id>
    <management_net_name> <northbound_net_name> <southbound_net_name>
2. delete_service <tenant_id> <service_instance_id>
3. connect_vm <tenant_id> <vm_image_id> <service_instance_id>
4. disconnect_vm <vm_instance_id>
"""

import logging
import logging.handlers
from optparse import OptionParser
import os
import re
import subprocess
import sys

from quantum.common import constants
from quantum.common import utils
from quantum.plugins.cisco.common import cisco_constants as const
from quantum.plugins.cisco.db import api as db
from quantum.plugins.cisco.db import l2network_db as l2db
from quantum.plugins.cisco.db import services_db as sdb
from quantum.plugins.cisco.services import services_constants as servconts
from quantum.plugins.cisco.services import services_logistics as servlogcs
from quantumclient import Client


LOG = logging.getLogger(__name__)


def insert_inpath_service(tenant_id, service_image_id,
                          management_net_name, northbound_net_name,
                          southbound_net_name, *args):
    """Inserting a network service between two networks"""
    print ("Creating Network for Services and Servers")
    service_logic = servlogcs.ServicesLogistics()
    net_list = {}
    multiport_net_list = []
    networks_name_list = [management_net_name, northbound_net_name,
                          southbound_net_name]
    client = Client(HOST, PORT, USE_SSL, format='json', tenant=tenant_id)
    for net in networks_name_list:
        data = {servconts.NETWORK: {servconts.NAME: net}}
        net_list[net] = client.create_network(data)
        net_list[net][servconts.PORTS] = []
        LOG.debug("Network %s Created with ID: %s " % (
            net, net_list[net][servconts.NETWORK][servconts.ID]))
    print "Completed"
    print ("Creating Ports on Services and Server Networks")
    LOG.debug("Operation 'create_port' executed.")
    if not service_logic.verify_plugin(const.UCS_PLUGIN):
        for net in networks_name_list:
            net_list[net][servconts.PORTS].append
            (client.create_port
            (net_list[net][servconts.NETWORK][servconts.ID]))
        LOG.debug("Operation 'create_port' executed.")
    else:
        for net in networks_name_list:
            nets = net_list[net][servconts.NETWORK][servconts.ID]
            multiport_net_list.append(nets)
        data = create_multiport(tenant_id, multiport_net_list)
        net_idx = 0
        for net in networks_name_list:
            port_id = data[servconts.PORTS][net_idx][servconts.ID]
            net_list[net][servconts.PORTS].append(port_id)
            LOG.debug("Port UUID: %s on network: %s" %
                      (data[servconts.PORTS][net_idx][servconts.ID], net))
            net_idx = net_idx + 1
    print "Completed"
    try:
        create_vm_args = []
        create_vm_args.append(servconts.CREATE_VM_CMD)
        create_vm_args.append(service_image_id)
        print ("Creating VM with image: %s" % (service_image_id))
        process = utils.subprocess_popen(create_vm_args,
                                         stdout=subprocess.PIPE)
        result = process.stdout.readlines()
        tokens = re.search("i-[a-f0-9]*", str(result[1]))
        service_vm_name = tokens.group(0)
        print ("Image: %s instantiated successfully" % (service_vm_name))

    except Exception as exc:
        print exc

    service_logic.image_status(service_vm_name)
    print "Completed"
    print "Attaching Ports To VM Service interfaces"
    try:
        idx = 0
        for net in networks_name_list:
            network_id = net_list[net][servconts.NETWORK][servconts.ID]
            port_id = net_list[net][servconts.PORTS][idx]
            attachment = client.show_port_attachment(network_id, port_id)
            attachment = attachment[servconts.ATTACHMENT][servconts.ID][:36]
            LOG.debug(("Plugging virtual interface: %s of VM %s"
                       "into port: %s on network: %s") %
                      (attachment, service_vm_name, port_id, net))
            attach_data = {servconts.ATTACHMENT: {servconts.ID: '%s' %
                                                  attachment}}
            client.attach_resource(network_id, port_id, attach_data)
    except Exception as exc:
        print exc
    print "Completed"
    try:
        LOG.debug("Registering Service in DB")
        l2db.initialize()
        for uuid_net in db.network_id(networks_name_list[0]):
            mngnet_id = str(uuid_net.uuid)
        for uuid_net in db.network_id(networks_name_list[1]):
            nbnet_id = str(uuid_net.uuid)
        for uuid_net in db.network_id(networks_name_list[2]):
            sbnet_id = str(uuid_net.uuid)
        sdb.add_services_binding(service_vm_name, mngnet_id, nbnet_id,
                                 sbnet_id)
    except Exception as exc:
        print exc


def delete_service(tenant_id, service_instance_id, *args):
    """
    Removes a service and all the network configuration
    """
    l2db.initialize()
    print ("Terminating Service VM")
    service_logic = servlogcs.ServicesLogistics()
    vms_list = []
    vms_list.append(servconts.DELETE_VM_CMD)
    vms_list.append(service_instance_id)

    if not service_logic.image_exist(service_instance_id):
        print ("Service VM does not exist")
        sys.exit()

    result = subprocess.call(vms_list)
    service_logic.image_shutdown_verification(service_instance_id)

    client = Client(HOST, PORT, USE_SSL, format='json', tenant=tenant_id)
    service_nets = sdb.get_service_bindings(service_instance_id)
    print ("Terminating Ports and Networks")
    network_name = db.network_get(service_nets.mngnet_id)
    port_id_net = db.port_list(service_nets.mngnet_id)
    for ports_uuid in port_id_net:
        client.delete_port(service_nets.mngnet_id, ports_uuid.uuid)
    client.delete_network(service_nets.mngnet_id)
    network_name = db.network_get(service_nets.nbnet_id)
    port_id_net = db.port_list(service_nets.nbnet_id)
    for ports_uuid in port_id_net:
        client.delete_port(service_nets.nbnet_id, ports_uuid.uuid)
    client.delete_network(service_nets.nbnet_id)
    network_name = db.network_get(service_nets.sbnet_id)
    port_id_net = db.port_list(service_nets.sbnet_id)
    for ports_uuid in port_id_net:
        client.delete_port(service_nets.sbnet_id, ports_uuid.uuid)
    client.delete_network(service_nets.sbnet_id)
    service_list = sdb.remove_services_binding(service_instance_id)
    print ("Configuration Removed Successfully")


def disconnect_vm(vm_instance_id, *args):
    """
    Deletes VMs and Port connection
    """
    l2db.initialize()
    print ("Terminating Service VM")
    service_logic = servlogcs.ServicesLogistics()
    vms_list = []
    vms_list.append(servconts.DELETE_VM_CMD)
    vms_list.append(vm_instance_id)
    result = subprocess.call(vms_list)
    service_logic.image_shutdown_verification(vm_instance_id)
    print ("VM Server Off")


def connect_vm(tenant_id, vm_image_id, service_instance_id, *args):
    """
    Starts a VMs and is connected to southbound network
    """
    l2db.initialize()
    client = Client(HOST, PORT, USE_SSL, format='json', tenant=tenant_id)
    print ("Connecting %s to Service %s " % (vm_image_id, service_instance_id))
    service_logic = servlogcs.ServicesLogistics()
    service_nets = sdb.get_service_bindings(service_instance_id)
    client.create_port(service_nets.mngnet_id)
    client.create_port(service_nets.nbnet_id)
    sb_port_id = client.create_port(service_nets.sbnet_id)
    LOG.debug("Operation 'create_port' executed.")
    new_port_id = sb_port_id[servconts.PORT][servconts.ID]
    try:
        create_vm_args = []
        create_vm_args.append(servconts.CREATE_VM_CMD)
        create_vm_args.append(vm_image_id)
        print ("Creating VM with image: %s" % (vm_image_id))
        process = utils.subprocess_popen(create_vm_args,
                                         stdout=subprocess.PIPE)
        result = process.stdout.readlines()
        tokens = re.search("i-[a-f0-9]*", str(result[1]))
        vm_name = tokens.group(0)
        print ("Image: %s instantiated successfully" % (vm_name))
    except Exception as exc:
        print exc

    service_logic.image_status(vm_name)
    print "Completed"
    print "Attaching Ports To VM Service interfaces"
    south_net = service_nets.sbnet_id
    attachment = client.show_port_attachment(south_net, new_port_id)
    attachment = attachment[servconts.ATTACHMENT][servconts.ID][:36]
    LOG.debug(("Plugging virtual interface: %s of VM %s "
               "into port: %s on network: %s") %
              (attachment, vm_name, new_port_id, service_nets.sbnet_id))
    attach_data = {servconts.ATTACHMENT: {servconts.ID: '%s' % attachment}}
    client.attach_resource(service_nets.sbnet_id, new_port_id, attach_data)
    print ("Connect VM Ended")


def create_multiport(tenant_id, networks_list, *args):
    """Creates ports on a single host"""
    ports_info = {'multiport':
                  {'status': constants.PORT_STATUS_ACTIVE,
                   'net_id_list': networks_list,
                   'ports_desc': {'key': 'value'}}}
    request_url = "/multiport"
    client = Client(HOST, PORT, USE_SSL, format='json', tenant=tenant_id,
                    action_prefix=servconts.ACTION_PREFIX_CSCO)
    data = client.do_request('POST', request_url, body=ports_info)
    return data


def build_args(cmd, cmdargs, arglist):
    """Building the list of args for a particular CLI"""
    args = []
    orig_arglist = arglist[:]
    try:
        for cmdarg in cmdargs:
            args.append(arglist[0])
            del arglist[0]
    except:
        LOG.debug("Not enough arguments for \"%s\" (expected: %d, got: %d)" %
                  (cmd, len(cmdargs), len(orig_arglist)))
        print "Service Insertion Usage:\n    %s %s" % (
            cmd, " ".join(["<%s>" % y for y in SERVICE_COMMANDS[cmd]["args"]]))
        sys.exit()
    if len(arglist) > 0:
        LOG.debug("Too many arguments for \"%s\" (expected: %d, got: %d)"
                  % (cmd, len(cmdargs), len(orig_arglist)))
        print "Service Insertion Usage:\n    %s %s" % (
            cmd, " ".join(["<%s>" % y for y in SERVICE_COMMANDS[cmd]["args"]]))
        sys.exit()
    return args


SERVICE_COMMANDS = {
    "insert_inpath_service": {
        "func": insert_inpath_service,
        "args": ["tenant_id", "service_image_id", "management_net_name",
                 "northbound_net_name", "southbound_net_name"],
    },
    "delete_service": {
        "func": delete_service,
        "args": ["tenant_id", "service_instance_id"],
    },
    "connect_vm": {
        "func": connect_vm,
        "args": ["tenant_id", "vm_image_id", "service_instance_id"],
    },
    "disconnect_vm": {
        "func": disconnect_vm,
        "args": ["vm_instance_id"],
    },
}


if __name__ == "__main__":
    os.system("clear")
    usagestr = "Usage: %prog [OPTIONS] <command> [args]"
    PARSER = OptionParser(usage=usagestr)
    PARSER.add_option("-H", "--host", dest="host",
                      type="string", default="127.0.0.1",
                      help="ip address of api host")
    PARSER.add_option("-p", "--port", dest="port",
                      type="int", default=9696, help="api port")
    PARSER.add_option("-s", "--ssl", dest="ssl",
                      action="store_true", default=False, help="use ssl")
    PARSER.add_option("-v", "--verbose", dest="verbose",
                      action="store_true", default=False,
                      help="turn on verbose logging")
    PARSER.add_option("-f", "--logfile", dest="logfile",
                      type="string", default="syslog", help="log file path")
    options, args = PARSER.parse_args()
    if options.verbose:
        LOG.setLevel(logging.DEBUG)
    else:
        LOG.setLevel(logging.WARN)
    if options.logfile == "syslog":
        LOG.addHandler(logging.handlers.SysLogHandler(address='/dev/log'))
    else:
        LOG.addHandler(logging.handlers.WatchedFileHandler(options.logfile))
        os.chmod(options.logfile, 0644)
    service_logic = servlogcs.ServicesLogistics()
    HOST = options.host
    PORT = options.port
    USE_SSL = options.ssl
    CMD = args[0]
    args = build_args(CMD, SERVICE_COMMANDS[CMD]["args"], args[1:])
    SERVICE_COMMANDS[CMD]["func"](*args)
    sys.exit(0)