<commit_before>#!/usr/bin/python
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

import sys
import argparse
import ConfigParser

from vnc_api.vnc_api import *
from vnc_api.gen.resource_xsd import RouteType
from vnc_api.gen.resource_xsd import RouteTableType
from vnc_api.gen.resource_client import InterfaceRouteTable


class StaticRouteProvisioner(object):

    def __init__(self, args_str=None):
        self._args = None
        if not args_str:
            args_str = ' '.join(sys.argv[1:])
        self._parse_args(args_str)

        self._vnc_lib = VncApi(
            self._args.user, self._args.password,
            self._args.tenant_name,
            self._args.api_server_ip,
            self._args.api_server_port, '/')
        
        prefix = self._args.prefix
        vmi_id_got = self._args.virtual_machine_interface_id
        route_table_name = self._args.route_table_name
        
        project_fq_name_str = 'default-domain:'+ self._args.tenant_name
        project_fq_name = project_fq_name_str.split(':')
        project_obj = self._vnc_lib.project_read(fq_name=project_fq_name)
        
        route_table = RouteTableType(route_table_name)
        route_table.set_route([])
        intf_route_table = InterfaceRouteTable(
                                interface_route_table_routes = route_table,
                                parent_obj=project_obj, 
                                name=route_table_name)
        try:
            route_table_obj = self._vnc_lib.interface_route_table_read(
                                    fq_name = intf_route_table.get_fq_name())
            intf_route_table_id = route_table_obj.uuid
        except NoIdError:
            if self._args.oper == 'del':
                print "Route table %s does not exist" %(route_table_name)
                sys.exit(1)
            print "Creating Route table"
            intf_route_table_id = self._vnc_lib.interface_route_table_create(
                                    intf_route_table)
        intf_route_table_obj = self._vnc_lib.interface_route_table_read(
                                    id = intf_route_table_id) 
        if self._args.oper == 'add':
            intf_route_table_obj = self.add_route(intf_route_table_obj, prefix)
        elif self._args.oper == 'del':
            intf_route_table_obj = self.del_route(intf_route_table_obj, prefix)
        self._vnc_lib.interface_route_table_update(intf_route_table_obj)
        
        #Update the VMI Object now
        vmi_obj = self._vnc_lib.virtual_machine_interface_read(id = vmi_id_got)
        if self._args.oper == 'add':
            vmi_obj.set_interface_route_table(intf_route_table_obj)
        elif self._args.oper == 'del':
            if self.is_route_table_empty(intf_route_table_obj):
                vmi_obj.del_interface_route_table(intf_route_table_obj)
        self._vnc_lib.virtual_machine_interface_update(vmi_obj)

    # end __init__
    
    def add_route(self, intf_route_table_obj, prefix):
        rt_routes = intf_route_table_obj.get_interface_route_table_routes()
        routes = rt_routes.get_route()
        found = False
        for route in routes:
            if route.prefix == prefix:
                print "Prefix already present in Interface Route Table, not adding"
                found = True
                sys.exit(0) 
        if not found:
   	    rt1 = RouteType(prefix = prefix)
            routes.append(rt1)
        return intf_route_table_obj
    #end add_route 
     
    def del_route(self, intf_route_table_obj, prefix):
#        routes = intf_route_table_obj['interface_route_table_routes']['route']
        rt_routes = intf_route_table_obj.get_interface_route_table_routes()
        routes = rt_routes.get_route()
        found = False
        for route in routes:
            if route.prefix == prefix:
                found = True
                routes.remove(route)
        if not found : 
            print "Prefix %s not found in Route table %s!" %( prefix, intf_route_table_obj.name)
            sys.exit(1)
        return intf_route_table_obj
    
    def is_route_table_empty(self, intf_route_table_obj):
        rt_routes = intf_route_table_obj.get_interface_route_table_routes()
        if len(rt_routes.get_route()) == 0 :
            return True
        else:
            return False
    #end is_route_table_empty

    def _parse_args(self, args_str):
        '''
        Eg. python provision_static_route.py 
                                        --api_server_ip 127.0.0.1
                                        --api_server_port 8082
                                        --prefix 172.16.17.32/24<commit_msg>Accomodate change in API behavior<commit_after>#!/usr/bin/python
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

import sys
import argparse
import ConfigParser

from vnc_api.vnc_api import *
from vnc_api.gen.resource_xsd import RouteType
from vnc_api.gen.resource_xsd import RouteTableType
from vnc_api.gen.resource_client import InterfaceRouteTable


class StaticRouteProvisioner(object):

    def __init__(self, args_str=None):
        self._args = None
        if not args_str:
            args_str = ' '.join(sys.argv[1:])
        self._parse_args(args_str)

        self._vnc_lib = VncApi(
            self._args.user, self._args.password,
            self._args.tenant_name,
            self._args.api_server_ip,
            self._args.api_server_port, '/')
        
        prefix = self._args.prefix
        vmi_id_got = self._args.virtual_machine_interface_id
        route_table_name = self._args.route_table_name
        
        project_fq_name_str = 'default-domain:'+ self._args.tenant_name
        project_fq_name = project_fq_name_str.split(':')
        project_obj = self._vnc_lib.project_read(fq_name=project_fq_name)
        
        route_table = RouteTableType(route_table_name)
        route_table.set_route([])
        intf_route_table = InterfaceRouteTable(
                                interface_route_table_routes = route_table,
                                parent_obj=project_obj, 
                                name=route_table_name)
        try:
            route_table_obj = self._vnc_lib.interface_route_table_read(
                                    fq_name = intf_route_table.get_fq_name())
            intf_route_table_id = route_table_obj.uuid
        except NoIdError:
            if self._args.oper == 'del':
                print "Route table %s does not exist" %(route_table_name)
                sys.exit(1)
            print "Creating Route table"
            intf_route_table_id = self._vnc_lib.interface_route_table_create(
                                    intf_route_table)
        intf_route_table_obj = self._vnc_lib.interface_route_table_read(
                                    id = intf_route_table_id) 
        if self._args.oper == 'add':
            intf_route_table_obj = self.add_route(intf_route_table_obj, prefix)
        elif self._args.oper == 'del':
            intf_route_table_obj = self.del_route(intf_route_table_obj, prefix)
        self._vnc_lib.interface_route_table_update(intf_route_table_obj)
        
        #Update the VMI Object now
        vmi_obj = self._vnc_lib.virtual_machine_interface_read(id = vmi_id_got)
        if self._args.oper == 'add':
            vmi_obj.set_interface_route_table(intf_route_table_obj)
        elif self._args.oper == 'del':
            if self.is_route_table_empty(intf_route_table_obj):
                vmi_obj.del_interface_route_table(intf_route_table_obj)
        self._vnc_lib.virtual_machine_interface_update(vmi_obj)

    # end __init__
    
    def add_route(self, intf_route_table_obj, prefix):
        rt_routes = intf_route_table_obj.get_interface_route_table_routes()
        routes = rt_routes.get_route()
        found = False
        for route in routes:
            if route.prefix == prefix:
                print "Prefix already present in Interface Route Table, not adding"
                found = True
                sys.exit(0) 
        if not found:
   	    rt1 = RouteType(prefix = prefix)
        routes.append(rt1)
        intf_route_table_obj.set_interface_route_table_routes(rt_routes)
        return intf_route_table_obj
    #end add_route 
     
    def del_route(self, intf_route_table_obj, prefix):
#        routes = intf_route_table_obj['interface_route_table_routes']['route']
        rt_routes = intf_route_table_obj.get_interface_route_table_routes()
        routes = rt_routes.get_route()
        found = False
        for route in routes:
            if route.prefix == prefix:
                found = True
                routes.remove(route)
        if not found : 
            print "Prefix %s not found in Route table %s!" %( prefix, intf_route_table_obj.name)
            sys.exit(1)
        intf_route_table_obj.set_interface_route_table_routes(rt_routes)
        return intf_route_table_obj
    
    def is_route_table_empty(self, intf_route_table_obj):
        rt_routes = intf_route_table_obj.get_interface_route_table_routes()
        if len(rt_routes.get_route()) == 0 :
            return True
        else:
            return False
    #end is_route_table_empty

    def _parse_args(self, args_str):
        '''
        Eg. python provision_static_route.py 
                                        --api_server_ip 127.0.0.1
                                        --api_server_port 8082
                                        --prefix 172.16.17.32/24
{'fraction_non_alphanumeric': 0.07286854262914742, 'fraction_numerical': 0.006719865602687947, 'mean_word_length': 2.0656581911812038, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '921986', 'n_tokens_mistral': 2948, 'n_tokens_neox': 2856, 'n_words': 497}