Last active
August 9, 2016 10:16
-
-
Save kahou82/23b1cd9185fb82a980c161128dc52f9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid | |
from datetime import datetime | |
from sqlalchemy import create_engine | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy import Column, Integer, String, Boolean, Enum, ForeignKey, DateTime | |
TEMPLATE_ID = '00000000-0000-0000-0000-000000000003' | |
CREDENTIAL_ID = '00000000-0000-0000-0000-000000000001' | |
ACTIVE_HOSTING_DEVICE_ID = '00000000-0000-0000-0000-000000000003' #TODO: Change to 0 in fusion | |
STANDBY_HOSTING_DEVICE_ID = '00000000-0000-0000-0000-000000000004' #TODO: Change to 1 in fusion | |
ROUTER_TYPE_ID = '00000000-0000-0000-0000-000000000003' | |
CISCO_CFG_AGENT_TOPIC = 'cisco_cfg_agent' | |
Base = declarative_base() | |
class Router(Base): | |
__tablename__ = "routers" | |
id = Column(String, primary_key=True) | |
tenant_id = Column(String) | |
name = Column(String(255)) | |
status = Column(String) | |
admin_state_up = Column(Boolean) | |
gw_port_id = Column(String(36), ForeignKey('ports.id')) | |
enable_snat = Column(Boolean) | |
class Port(Base): | |
__tablename__ = "ports" | |
id = Column(String, primary_key=True) | |
name = Column(String) | |
network_id = Column(String) | |
admin_state_up = Column(Boolean) | |
device_id = Column(String) | |
device_owner = Column(String) | |
class CiscoRouterRedundancyBinding(Base): | |
__tablename__ = 'cisco_router_redundancy_bindings' | |
redundancy_router_id = Column(String, primary_key=True) | |
user_router_id = Column(String) | |
priority = Column(Integer) | |
state = Column(String) | |
class CiscoSlotAllocation(Base): | |
__tablename__ = 'cisco_slot_allocations' | |
template_id = Column(String) | |
hosting_device_id = Column(String) | |
logical_resource_type = Column(String) | |
logical_resource_service = Column(String) | |
logical_resource_id = Column(String, primary_key=True) | |
logical_resource_owner = Column(String) | |
num_allocated = Column(Integer) | |
tenant_bound = Column(String) | |
class CiscoPortMappings(Base): | |
__tablename__ = 'cisco_port_mappings' | |
logical_resource_id = Column(String, primary_key=True) | |
logical_port_id = Column(String) | |
port_type = Column(String) | |
network_type = Column(String) | |
hosting_port_id = Column(String) | |
segmentation_id = Column(String) | |
class CiscoRouterMappings(Base): | |
__tablename__ = 'cisco_router_mappings' | |
router_id = Column(String, primary_key=True) | |
auto_schedule = Column(Boolean) | |
hosting_device_id = Column(String) | |
role = Column(String) | |
router_type_id = Column(String) | |
inflated_slot_need = Column(Boolean) | |
share_hosting_device = Column(Boolean) | |
class CiscoRouterHaGroups(Base): | |
__tablename__ = 'cisco_router_ha_groups' | |
ha_type = Column(String) | |
ha_port_id = Column(String, primary_key=True) | |
extra_port_id = Column(String) | |
subnet_id = Column(String) | |
user_router_id = Column(String) | |
timers_config = Column(String) | |
tracking_config = Column(String) | |
other_config = Column(String) | |
group_identity = Column(String) | |
id = Column(String) | |
class Subnet(Base): | |
__tablename__ = 'subnets' | |
tenant_id = Column(String) | |
id = Column(String, primary_key=True) | |
name = Column(String) | |
network_id = Column(String) | |
class NetworkSegment(Base): | |
__tablename__ = 'ml2_network_segments' | |
id = Column(String, primary_key=True) | |
network_id = Column(String) | |
network_type = Column(String) | |
physical_network = Column(String) | |
segmentation_id = Column(String) | |
is_dynamic = Column(Boolean) | |
class CiscoHostingDevice(Base): | |
__tablename__ = 'cisco_hosting_devices' | |
id = Column(String, primary_key=True) | |
tenant_id = Column(String) | |
complementary_id = Column(String) | |
device_id = Column(String) | |
admin_state_up = Column(Boolean) | |
management_port_id = Column(String) | |
protocol_port = Column(Integer) | |
cfg_agent_id = Column(Integer) | |
template_id = Column(String) | |
credentials_id = Column(String) | |
management_ip_address = Column(String) | |
auto_delete = Column(Boolean) | |
created_at = Column(DateTime) | |
class Agent(Base): | |
__tablename__ = 'agents' | |
id = Column(String, primary_key=True) | |
topic = Column(String) | |
class ASRDataMigration(object): | |
def __init__(self, engine): | |
Session = sessionmaker(bind=engine) | |
self._session = Session() | |
self._user_routers = self._get_user_created_routers() | |
def migrate(self): | |
self._get_user_created_routers() | |
#self._create_hosting_devices_by_agent() | |
self._create_backup_router_and_setup_binding() | |
self._handle_cisco_slot_allocations() | |
self._handle_cisco_port_mappings() | |
self._handle_cisco_router_mappings() | |
self._handle_cisco_router_ha_group_for_gw_port() | |
def _get_user_created_routers(self): | |
return self._session.query(Router).filter(Router.name!='PHYSICAL_GLOBAL_ROUTER_ID', Router.tenant_id!='').all() | |
def _get_all_routers(self): | |
return self._session.query(Router).filter(Router.name!='PHYSICAL_GLOBAL_ROUTER_ID') | |
def _get_all_ports(self): | |
return self._session.query(Port) | |
def _get_subnet_from_network(self, network_id): | |
return self._session.query(Subnet).filter(Subnet.network_id==network_id).first() | |
def _get_network_segment_from_network(self, network_id): | |
return self._session.query(NetworkSegment).filter(NetworkSegment.network_id==network_id).first() | |
def _create_hosting_devices(self, management_ip_address, hosting_device_id, agent_id): | |
hosting_dev = CiscoHostingDevice() | |
hosting_dev.tenant_id = '1b90690ddce74a7abd7ad332cc7bf5de' # TODO fix it with query (or it is the default domain) | |
hosting_dev.id = hosting_device_id | |
hosting_dev.device_id = 'SN:abcd1234efgh' | |
hosting_dev.admin_state_up = True | |
hosting_devport = 22 | |
hosting_dev.cfg_agent_id = agent_id | |
hosting_dev.template_id = TEMPLATE_ID | |
hosting_dev.credential_id = CREDENTIAL_ID | |
hosting_dev.management_ip_address = management_ip_address | |
hosting_dev.created_at = datetime.now() | |
hosting_dev.auto_delete = False | |
self._session.add(hosting_dev) | |
self._session.commit() | |
def _create_hosting_devices_by_agent(self): | |
print "Create hosting devices entry" | |
agent = self._session.query(Agent).filter(Agent.topic==CISCO_CFG_AGENT_TOPIC).first() | |
self._create_hosting_devices('192.168.59.111', ACTIVE_HOSTING_DEVICE_ID, agent.id) | |
self._create_hosting_devices('192.168.59.112', STANDBY_HOSTING_DEVICE_ID, agent.id) | |
def _create_backup_router_and_setup_binding(self): | |
print "Create backup router and setup binding" | |
router_binding_map = list() | |
for router in self._user_routers: | |
backup_router = Router() | |
backup_router.id = str(uuid.uuid4()) | |
backup_router.name = "{0}_HA_backup_1".format(router.name) | |
backup_router.status = router.status | |
backup_router.admin_state_up = router.admin_state_up | |
backup_router.gw_port_id = None | |
backup_router.enable_snat = router.enable_snat | |
backup_router.tenant_id = '' | |
self._session.add(backup_router) | |
router_binding_map.append((router, backup_router)) | |
self._session.commit() | |
for router_binding in router_binding_map: | |
user_router = router_binding[0] | |
backup_router = router_binding[1] | |
binding = CiscoRouterRedundancyBinding() | |
binding.redundancy_router_id = backup_router.id | |
binding.user_router_id = user_router.id | |
binding.priority = 20 | |
binding.state = 'STANDBY' | |
self._session.add(binding) | |
self._session.commit() | |
def _handle_cisco_slot_allocations(self): | |
print "Create cisco slot allocations" | |
for router in self._get_all_routers(): | |
slot_allocation = CiscoSlotAllocation() | |
slot_allocation.template_id = TEMPLATE_ID | |
slot_allocation.logical_resource_type = 'router' | |
slot_allocation.logical_resource_service = 'L3_ROUTER_NAT' | |
slot_allocation.logical_resource_id = router.id | |
slot_allocation.num_allocated = 1 | |
slot_allocation.tenant_bound = None | |
if router.tenant_id != '': | |
# Handle user create router | |
slot_allocation.hosting_device_id = ACTIVE_HOSTING_DEVICE_ID | |
slot_allocation.logical_resource_owner = router.tenant_id | |
else: | |
# Handle backup router | |
slot_allocation.hosting_device_id = STANDBY_HOSTING_DEVICE_ID | |
slot_allocation.logical_resource_owner = '' | |
self._session.add(slot_allocation) | |
self._session.commit() | |
def _handle_cisco_port_mappings(self): | |
print "Create cisco port mappings" | |
res = self._session.query(Port).join(Router).filter(Router.tenant_id != '').all() | |
for r in res: | |
mapping = CiscoPortMappings() | |
mapping.logical_resource_id = r.device_id | |
mapping.logical_port_id = r.id | |
mapping.port_type = None | |
mapping.network_type = 'vlan' | |
mapping.hosting_port_id = r.id | |
mapping.segmentation_id = self._get_network_segment_from_network(r.network_id).segmentation_id | |
self._session.add(mapping) | |
self._session.commit() | |
def _handle_cisco_router_mappings(self): | |
print "Create cisco router mappings" | |
for router in self._get_all_routers(): | |
router_map = CiscoRouterMappings() | |
router_map.router_id = router.id | |
router_map.auto_schedule = 1 | |
router_map.router_type_id = ROUTER_TYPE_ID | |
router_map.inflated_slot_need = 0 | |
router_map.share_hosting_device = True | |
if router.tenant_id == '': | |
router_map.hosting_device_id = STANDBY_HOSTING_DEVICE_ID | |
router_map.role = 'HA-Redundancy' | |
else: | |
router_map.hosting_device_id = ACTIVE_HOSTING_DEVICE_ID | |
router_map.role = None | |
self._session.add(router_map) | |
self._session.commit() | |
def _handle_cisco_router_ha_group_for_gw_port(self): | |
print "Create cisco_router_ha_group for gateway port" | |
res = self._session.query(Port).join(Router).filter(Router.tenant_id != '').all() | |
for r in res: | |
ha_group = CiscoRouterHaGroups() | |
ha_group.tenant_id = '' | |
ha_group.id = str(uuid.uuid4()) | |
ha_group.ha_type = 'HSRP' | |
ha_group.group_identity = 1064 | |
ha_group.ha_port_id = r.id | |
ha_group.extra_port_id = None | |
ha_group.subnet_id = self._get_subnet_from_network(r.network_id).id | |
ha_group.user_router_id = r.device_id | |
ha_group.timer_config = '' | |
ha_group.tracking_config = '' | |
ha_group.other_config = '' | |
self._session.add(ha_group) | |
self._session.commit() | |
engine = create_engine('mysql+pymysql://root:secrete@127.0.0.1/neutron_ml2?charset=utf8') | |
migration = ASRDataMigration(engine) | |
migration.migrate() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment