def delete_asn_equipment(as_ids): """Delete ASNEquipment.""" try: asn_equipment = AsnEquipment() obj = asn_equipment.get_by_pk(ids=as_ids) obj.delete_v4() except exceptions.AsnDoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except exceptions.AsnAssociatedToEquipmentError as e: raise ValidationAPIException(str(e)) except AsnError as e: raise NetworkAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e))
def delete_neighbor_v6(obj_ids): """Delete NeighborV6.""" for obj_id in obj_ids: try: obj_to_delete = get_neighbor_v6_by_id(obj_id) obj_to_delete.delete_v4() except NeighborV6DoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except NeighborV6IsDeployed as e: raise ValidationAPIException(str(e)) except NeighborV6Error as e: raise NetworkAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e))
def delete_as(as_ids): """Delete AS.""" for as_id in as_ids: try: as_obj = get_as_by_id(as_id) as_obj.delete_v4() except exceptions.AsnDoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except exceptions.AsnAssociatedToEquipmentError as e: raise ValidationAPIException(str(e)) except AsnError as e: raise NetworkAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e))
def delete_route_map_entry(obj_ids): """Delete RouteMapEntry.""" for obj_id in obj_ids: try: obj_to_delete = get_route_map_entry_by_id(obj_id) obj_to_delete.delete_v4() except RouteMapEntryDoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except RouteMapEntryWithDeployedRouteMapException as e: raise ValidationAPIException(str(e)) except RouteMapEntryError as e: raise NetworkAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e))
def delete_peer_group(obj_ids): """Delete PeerGroup.""" for obj_id in obj_ids: try: obj_to_delete = get_peer_group_by_id(obj_id) obj_to_delete.delete_v4() except PeerGroupDoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except PeerGroupIsAssociatedWithNeighborsException as e: raise ValidationAPIException(str(e)) except PeerGroupError as e: raise NetworkAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e))
def update_neighbor_v4(obj, user): """Update NeighborV4.""" try: obj_to_update = get_neighbor_v4_by_id(obj.get('id')) obj_to_update.update_v4(obj, user) except NeighborV4Error as e: raise ValidationAPIException(str(e)) except DontHavePermissionForPeerGroupException as e: raise ValidationAPIException(str(e)) except LocalIpAndRemoteIpAreInDifferentVrfsException as e: raise ValidationAPIException(str(e)) except LocalIpAndLocalAsnAtDifferentEquipmentsException as e: raise ValidationAPIException(str(e)) except RemoteIpAndRemoteAsnAtDifferentEquipmentsException as e: raise ValidationAPIException(str(e)) except LocalIpAndPeerGroupAtDifferentEnvironmentsException as e: raise ValidationAPIException(str(e)) except NeighborDuplicatedException as e: raise ValidationAPIException(str(e)) except NeighborV4IsDeployed as e: raise ValidationAPIException(str(e)) except ValidationAPIException as e: raise ValidationAPIException(str(e)) except NeighborV4DoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) return obj_to_update
def deploy_neighbor_v4(neighbor_id): neighbor = NeighborV4.objects.get(id=neighbor_id) if neighbor.created: raise NeighborV4IsDeployed(neighbor) locks_list = lock_resources_used_by_neighbor_v4(neighbor) try: depl_v4 = get_created_neighbors_v4_shares_same_eqpt_and_peer(neighbor) depl_v6 = get_created_neighbors_v6_shares_same_eqpt_and_peer(neighbor) eqpt = get_v4_equipment(neighbor) plugin = PluginFactory.factory(eqpt) plugin.bgp().deploy_neighbor(neighbor) neighbor.deploy() except Exception as e: raise NetworkAPIException(str(e)) finally: destroy_lock(locks_list)
def create_neighbor_v6(obj, user): """Create NeighborV6.""" try: obj_to_create = NeighborV6() obj_to_create.create_v4(obj, user) except NeighborV6Error as e: raise ValidationAPIException(str(e)) except DontHavePermissionForPeerGroupException as e: raise ValidationAPIException(str(e)) except LocalIpAndRemoteIpAreInDifferentVrfsException as e: raise ValidationAPIException(str(e)) except LocalIpAndLocalAsnAtDifferentEquipmentsException as e: raise ValidationAPIException(str(e)) except RemoteIpAndRemoteAsnAtDifferentEquipmentsException as e: raise ValidationAPIException(str(e)) except LocalIpAndPeerGroupAtDifferentEnvironmentsException as e: raise ValidationAPIException(str(e)) except NeighborDuplicatedException as e: raise ValidationAPIException(str(e)) except ValidationAPIException as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) return obj_to_create
def create_environment_vip(environment_vip): try: env = EnvironmentVip() env.create_v3(environment_vip) except Exception, e: raise NetworkAPIException(str(e))
def delete_asn_equipment_by_asn(asn_id): """Delete ASNEquipment.""" try: asn_obj = AsnEquipment() asn_equipment = asn_obj.get_by_pk(asn=asn_id) for obj in asn_equipment: obj.delete_v4() except exceptions.AsnDoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except exceptions.AsnAssociatedToEquipmentError as e: raise ValidationAPIException(str(e)) except AsnError as e: raise NetworkAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e))
def delete_list_config_bgp(obj_ids): """Delete ListConfigBGP.""" for obj_id in obj_ids: try: obj_to_delete = get_list_config_bgp_by_id(obj_id) obj_to_delete.delete_v4() except ListConfigBGPDoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except ListConfigBGPAssociatedToRouteMapEntryException as e: raise ValidationAPIException(str(e)) except ListConfigBGPIsDeployedException as e: raise ValidationAPIException(str(e)) except ListConfigBGPError as e: raise NetworkAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e))
def delete_route_map(obj_ids): """Delete RouteMap.""" for obj_id in obj_ids: try: obj_to_delete = get_route_map_by_id(obj_id) obj_to_delete.delete_v4() except RouteMapDoesNotExistException as e: raise ObjectDoesNotExistException(str(e)) except RouteMapIsDeployedException as e: raise ValidationAPIException(str(e)) except RouteMapAssociatedToRouteMapEntryException as e: raise ValidationAPIException(str(e)) except RouteMapAssociatedToPeerGroupException as e: raise ValidationAPIException(str(e)) except RouteMapError as e: raise NetworkAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e))
def get_equipment_by_ids(equipment_ids): """Get equipment by ids""" eqpt_ids = list() for equipment_id in equipment_ids: try: equipment = get_equipment_by_id(equipment_id) except ObjectDoesNotExistException, e: raise ObjectDoesNotExistException(e.detail) except Exception, e: raise NetworkAPIException(str(e))
def get_vip_request_by_search(search=dict()): try: vip_requests = VipRequest.objects.filter() vip_map = build_query_to_datatable_v3(vip_requests, search) except FieldError as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) else: return vip_map
def get_pool_by_search(search=dict()): try: pools = ServerPool.objects.filter() pool_map = build_query_to_datatable_v3(pools, search) except FieldError as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) else: return pool_map
def flush_flows(env_id): """ Flushes flow from a environment without restore it """ eqpts = get_controller_by_envid(env_id) for eqpt in eqpts: plugin = PluginFactory.factory(eqpt, env_id=env_id) try: plugin.flush_flows() except Exception as e: log.error(e) raise NetworkAPIException('Failed to flush Controller ' 'plugin. %s' % e)
def delete_neighbor(neighbor_ids): """Delete Neighbor.""" for neighbor_id in neighbor_ids: try: neighbor_obj = get_neighbor_by_id(neighbor_id) neighbor_obj.delete_v4() except exceptions.NeighborDoesNotExistException, e: raise ObjectDoesNotExistException(str(e)) except NeighborError, e: raise NetworkAPIException(str(e))
def get_environmentvip_by_ids(environment_vip_ids): envvips = list() for environment_vip_id in environment_vip_ids: try: envvip = get_environmentvip_by_id(environment_vip_id).id envvips.append(envvip) except ObjectDoesNotExistException, e: raise ObjectDoesNotExistException(str(e)) except Exception, e: raise NetworkAPIException(str(e))
def delete_flow(env_id, flow_id): eqpts = get_controller_by_envid(env_id) for eqpt in eqpts: plugin = PluginFactory.factory(eqpt, env_id=env_id) try: plugin.del_flow(flow_id=flow_id) except Exception as e: log.error(e) raise NetworkAPIException('Failed to delete flow ' 'plugin. %s' % e)
def insert_flow(env_id, data, user_id): eqpts = get_controller_by_envid(env_id) plugins = [] for eqpt in eqpts: plugins.append(PluginFactory.factory(eqpt, env_id=env_id)) try: return async_add_flow.apply_async(args=[plugins, user_id, data], queue='napi.odl_flow') except Exception as e: log.error(e) raise NetworkAPIException('Failed to insert flow(s) ' 'plugin. %s' % e)
def post_cidr(obj): from netaddr import IPNetwork try: cidr = EnvCIDR() data = dict() data['id'] = obj.get('id') data['ip_version'] = obj.get('ip_version') data['subnet_mask'] = obj.get('subnet_mask') data['network_type'] = obj.get('network_type') data['environment'] = obj.get('environment') data['network'] = obj.get('network') try: network = IPNetwork(obj.get('network')) except Exception as e: raise ValidationAPIException(str(e)) environment = Ambiente().get_by_pk(int(obj.get('environment'))) msg = list() if not cidr.check_cidr(environment, obj.get('network')): message = "The network is not a subnet of the father environment." msg.append( dict(message=message, environment_id=obj.get('environment'))) log.info(message) duplicated_cidr = cidr.check_duplicated_cidr(environment, obj.get('network')) duplicated_ids = [ids.id_env.id for ids in duplicated_cidr] if duplicated_cidr: message = "CIDR %s overlaps with networks from environments: %s" % \ (obj.get('network'), duplicated_ids) msg.append( dict(message=message, environment_id=obj.get('environment'))) log.info(message) data['network_first_ip'] = int(network.ip) data['network_last_ip'] = int(network.broadcast) data['network_mask'] = network.prefixlen response = cidr.post(data) except CIDRErrorV3 as e: raise ValidationAPIException(str(e)) except ValidationAPIException as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) return response, msg
def get_vlan_by_search(search=dict()): """Get vlans by search.""" try: vlans = Vlan.objects.filter() vlan_map = build_query_to_datatable_v3(vlans, search) except FieldError as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) else: return vlan_map
def get_cidr_by_search(search=dict()): """Return a list of dc environments by dict.""" try: cidrs = EnvCIDR.objects.filter() cidrs_map = build_query_to_datatable_v3(cidrs, search) except FieldError as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) else: return cidrs_map
def get_route_map_by_search(search=dict()): """Return a list of RouteMap's by dict.""" try: objects = RouteMap.objects.filter() object_map = build_query_to_datatable_v3(objects, search) except FieldError as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) else: return object_map
def get_neighbor_by_search(search=dict()): """Return a list of Neighbor's by dict.""" try: neighbors = Neighbor.objects.filter() neighbor_map = build_query_to_datatable_v3(neighbors, search) except FieldError as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) else: return neighbor_map
def get_ipv4_by_search(search=dict()): """Get List of Ipv4 by Search.""" try: networks = Ip.objects.all() net_map = build_query_to_datatable_v3(networks, search) except FieldError as e: raise ValidationAPIException(e.message) except Exception as e: raise NetworkAPIException(str(e)) else: return net_map
def remove_link(interfaces): interface_list = list() for i in interfaces: try: interface_obj = Interface.objects.get(id=int(i)) interface_list.append(interface_obj) except ObjectDoesNotExistException, e: raise ObjectDoesNotExistException(e.detail) except Exception, e: raise NetworkAPIException(str(e))
def get_interface_by_search(search=dict()): """Return a list of interface by dict.""" try: interfaces = Interface.objects.filter() interface_map = build_query_to_datatable_v3(interfaces, search) except FieldError as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) else: return interface_map
def get_logic_environment_by_search(search=dict()): """Return a list of dc environments by dict.""" try: environments = AmbienteLogico.objects.filter() env_map = build_query_to_datatable_v3(environments, search) except FieldError as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) else: return env_map
def get_as_by_search(search=dict()): """Return a list of AS's by dict.""" try: as_s = As.objects.filter() as_map = build_query_to_datatable_v3(as_s, search) except FieldError as e: raise ValidationAPIException(str(e)) except Exception as e: raise NetworkAPIException(str(e)) else: return as_map