def _get_template_undeploy_name(self):

        ip_version = IPAddress(self.neighbor.get('remote_ip')).version

        if ip_version == 4:
            return self.TEMPLATE_NEIGHBOR_V4_REMOVE
        return self.TEMPLATE_NEIGHBOR_V6_REMOVE
Beispiel #2
0
    def handle_get(self, request, user, *args, **kwargs):
        """Treat requests GET to verify that the IP belongs to environment.

        URLs:  /ip/x1.x2.x3.x4/ambiente/<id_amb>
        URLs:  /ip/<ip>/ambiente/<id_amb>
        """

        self.log.info("GET to verify that the IP belongs to environment")

        try:

            # User permission
            if not has_perm(user, AdminPermission.IPS,
                            AdminPermission.READ_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

            environment_id = kwargs.get('id_amb')

            # Valid Environment ID
            if not is_valid_int_greater_zero_param(environment_id):
                self.log.error(
                    u'The id_environment parameter is not a valid value: %s.',
                    environment_id)
                raise InvalidValueError(None, 'id_environment', environment_id)

            ip = kwargs.get('ip')

            # Valid IP
            if not is_valid_ipv4(ip):
                self.log.error(u'Parameter ip is invalid. Value: %s.', ip)
                raise InvalidValueError(None, 'ip', ip)

            # Find Environment by ID to check if it exist
            Ambiente.get_by_pk(environment_id)

            # Existing IP
            octs = str(IPAddress(ip, 4).exploded).split('.')
            ip = Ip.get_by_octs_and_environment(octs[0], octs[1], octs[2],
                                                octs[3], environment_id)

            # Build dictionary return
            ip_map = dict()
            ip_map['id'] = ip.id
            ip_map['id_vlan'] = ip.networkipv4.vlan.id
            ip_map['oct4'] = ip.oct4
            ip_map['oct3'] = ip.oct3
            ip_map['oct2'] = ip.oct2
            ip_map['oct1'] = ip.oct1
            ip_map['descricao'] = ip.descricao

            return self.response(dumps_networkapi({'ip': ip_map}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
Beispiel #3
0
def get_equipment(id_, remote_ip):

    version_ip = IPAddress(remote_ip).version

    if version_ip == 4:
        return Equipamento.objects.get(
            ipequipamento__virtual_interface__neighbor=id_)
    else:
        return Equipamento.objects.get(
            ipv6equipament__virtual_interface__neighbor=id_)
Beispiel #4
0
    def undeploy_neighbor(self, neighbor):
        """Undeploy neighbor"""

        ip_version = IPAddress(str(neighbor.remote_ip)).version

        self._undeploy_pre_req(neighbor, ip_version)

        template_type = self.TEMPLATE_NEIGHBOR_V4_REMOVE \
            if ip_version == 4 else self.TEMPLATE_NEIGHBOR_V6_REMOVE

        config = self._generate_template_dict_neighbor(neighbor)
        self._operate_equipment('neighbor', template_type, config)
Beispiel #5
0
    def deploy_neighbor(self, neighbor):
        """Deploy neighbor"""

        self._deploy_pre_req(neighbor)

        ip_version = IPAddress(str(neighbor.remote_ip)).version

        template_type = self.TEMPLATE_NEIGHBOR_V4_ADD if ip_version == 4 else \
            self.TEMPLATE_NEIGHBOR_V6_ADD

        config = self._generate_template_dict_neighbor(neighbor)
        self._operate_equipment('neighbor', template_type, config)
Beispiel #6
0
def is_valid_ip_ipaddr(param):
    """Checks if the parameter is a valid ip is ipv4 or ipv6.

    @param param: Value to be validated.

    @return True if the parameter has a valid ipv6 or ipv4 value, or False otherwise.
    """
    try:
        IPAddress(param)
        return True
    except ValueError:
        return False
Beispiel #7
0
def is_valid_ipv6(param):
    """Checks if the parameter is a valid ipv6.

    @param param: Value to be validated.

    @return True if the parameter has a valid ipv6 value, or False otherwise.
    """
    try:
        IPAddress(param, 6)
        return True
    except AddressValueError:
        return False
    def handle_post(self, request, user, *args, **kwargs):
        """Handles POST requests to find all Equipments by search parameters.

        URLs: /equipment/find/
        """

        self.log.info('Find all Equipments')

        try:

            # Commons Validations

            # User permission
            if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.READ_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

            # Business Validations

            # Load XML data
            xml_map, attrs_map = loads(
                request.raw_post_data, ['searchable_columns', 'asorting_cols'])

            # XML data format
            networkapi_map = xml_map.get('networkapi')
            if networkapi_map is None:
                msg = u'There is no value to the networkapi tag of XML request.'
                self.log.error(msg)
                return self.response_error(3, msg)
            equipment_map = networkapi_map.get('equipamento')
            if equipment_map is None:
                msg = u'There is no value to the equipment tag of XML request.'
                self.log.error(msg)
                return self.response_error(3, msg)

            # Get XML data
            start_record = equipment_map.get('start_record')
            end_record = equipment_map.get('end_record')
            asorting_cols = equipment_map.get('asorting_cols')
            searchable_columns = equipment_map.get('searchable_columns')
            custom_search = equipment_map.get('custom_search')

            name = equipment_map.get('nome')
            iexact = equipment_map.get('exato')
            environment = equipment_map.get('ambiente')
            equip_type = equipment_map.get('tipo_equipamento')
            group = equipment_map.get('grupo')
            ip = equipment_map.get('ip')

            # Business Rules

            # Start with alls
            equip = Equipamento.objects.all()

            if name is not None:
                # If name is valid, add to filter
                if not is_valid_string_minsize(name, 3, False):
                    raise InvalidValueError(None, 'nome', name)
                else:
                    # Iexact must be valid to add name to filter
                    if not is_valid_boolean_param(iexact, False):
                        raise InvalidValueError(None, 'exato', iexact)
                    else:
                        if (iexact is None) or (iexact == 'False') or (iexact == '0'):
                            iexact = False

                        if iexact:
                            equip = equip.filter(nome=name)
                        else:
                            equip = equip.filter(nome__icontains=name)

            # If environment is valid, add to filter
            if environment is not None:
                if not is_valid_int_greater_zero_param(environment, False):
                    raise InvalidValueError(None, 'ambiente', environment)
                else:
                    equip = equip.filter(
                        equipamentoambiente__ambiente__pk=environment)

            if equip_type is not None:
                # If equip_type is valid, add to filter
                if not is_valid_int_greater_zero_param(equip_type, False):
                    raise InvalidValueError(
                        None, 'tipo_equipamento', equip_type)
                else:
                    equip = equip.filter(tipo_equipamento__pk=equip_type)

            if group is not None:
                # If equip_type is valid, add to filter
                if not is_valid_int_greater_zero_param(group, False):
                    raise InvalidValueError(None, 'grupo', group)
                else:
                    equip = equip.filter(grupos__pk=group)

            if ip is not None:
                # If ip is valid
                if not is_valid_string_minsize(ip, 1, False):
                    raise InvalidValueError(None, 'ip', ip)
                else:
                    blocks, ip, version = break_ip(ip)
                    try:
                        IPAddress(ip)
                    except ValueError, e:
                        raise InvalidValueError(None, 'ip', ip)

                    # Filter octs
                    if version == IP_VERSION.IPv4[0]:
                        # IP v4
                        oct1 = oct2 = oct3 = oct4 = Q()

                        if len(blocks[0]) != 0:
                            oct1 = Q(ipequipamento__ip__oct1=blocks[0])
                        if len(blocks[1]) != 0:
                            oct2 = Q(ipequipamento__ip__oct2=blocks[1])
                        if len(blocks[2]) != 0:
                            oct3 = Q(ipequipamento__ip__oct3=blocks[2])
                        if len(blocks[3]) != 0:
                            oct4 = Q(ipequipamento__ip__oct4=blocks[3])

                        equip = equip.filter(oct1 & oct2 & oct3 & oct4)
                    else:
                        # IP v6
                        oct1 = oct2 = oct3 = oct4 = oct5 = oct6 = oct7 = oct8 = Q()

                        if len(blocks[0]) != 0:
                            oct1 = Q(
                                ipv6equipament__ip__block1__iexact=blocks[0])
                        if len(blocks[1]) != 0:
                            oct2 = Q(
                                ipv6equipament__ip__block2__iexact=blocks[1])
                        if len(blocks[2]) != 0:
                            oct3 = Q(
                                ipv6equipament__ip__block3__iexact=blocks[2])
                        if len(blocks[3]) != 0:
                            oct4 = Q(
                                ipv6equipament__ip__block4__iexact=blocks[3])
                        if len(blocks[4]) != 0:
                            oct5 = Q(
                                ipv6equipament__ip__block5__iexact=blocks[4])
                        if len(blocks[5]) != 0:
                            oct6 = Q(
                                ipv6equipament__ip__block6__iexact=blocks[5])
                        if len(blocks[6]) != 0:
                            oct7 = Q(
                                ipv6equipament__ip__block7__iexact=blocks[6])
                        if len(blocks[7]) != 0:
                            oct8 = Q(
                                ipv6equipament__ip__block8__iexact=blocks[7])

                        equip = equip.filter(
                            oct1 & oct2 & oct3 & oct4 & oct5 & oct6 & oct7 & oct8)

            equip = equip.distinct()

            # Datatable paginator
            equip, total = build_query_to_datatable(
                equip, asorting_cols, custom_search, searchable_columns, start_record, end_record)

            itens = get_equips(equip)

            equipment_map = dict()
            equipment_map['equipamento'] = itens
            equipment_map['total'] = total

            return self.response(dumps_networkapi(equipment_map))
    def handle_post(self, request, user, *args, **kwargs):
        """Handles POST requests to find all Vip Requests by search parameters.

        URLs: /requestvip/get_by_ip_id/
        """

        self.log.info('Find all Vip Requests')

        try:

            # Commons Validations
            # User permission
            if not has_perm(user, AdminPermission.VIPS_REQUEST,
                            AdminPermission.READ_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

            # Business Validations

            # Load XML data
            xml_map, attrs_map = loads(request.raw_post_data,
                                       ["searchable_columns", "asorting_cols"])

            # XML data format
            networkapi_map = xml_map.get('networkapi')
            if networkapi_map is None:
                msg = u'There is no value to the networkapi tag of XML request.'
                self.log.error(msg)
                return self.response_error(3, msg)

            vip_map = networkapi_map.get('vip')
            if vip_map is None:
                msg = u'There is no value to the vip tag of XML request.'
                self.log.error(msg)
                return self.response_error(3, msg)

            # Get XML data
            start_record = vip_map.get("start_record")
            end_record = vip_map.get("end_record")
            asorting_cols = vip_map.get("asorting_cols")
            searchable_columns = vip_map.get("searchable_columns")
            custom_search = vip_map.get("custom_search")

            id_vip = vip_map.get("id_vip")
            ip = vip_map.get("ip")
            created_vip = vip_map.get("create")
            if created_vip == 'True':
                create = True
            elif created_vip == 'False':
                create = None
            else:
                create = created_vip

            # Business Rules

            # Start with all

            vip = RequisicaoVips.objects.all()

            if id_vip is not None and ip is not None:
                raise InvalidValueError(None, 'id_vip - ip',
                                        "%s - %s" % (id_vip, ip))

            if id_vip is not None:
                # If id_vip is valid, add to filter
                if not is_valid_int_greater_zero_param(id_vip, False):
                    raise InvalidValueError(None, 'id_vip', id_vip)
                else:
                    vip = vip.filter(id=id_vip)

            if create is not None:
                # if create is valid, add to filter
                if not is_valid_boolean_param(create, False):
                    raise InvalidValueError(None, 'vip_criado', create)
                else:
                    vip = vip.filter(vip_criado=create)

            if ip is not None:
                # If ip is valid
                if not is_valid_string_minsize(ip, 1, False):
                    raise InvalidValueError(None, 'ip', ip)
                else:
                    blocks, ip, version = break_ip(ip)
                    try:
                        IPAddress(ip)
                    except ValueError, e:
                        raise InvalidValueError(None, 'ip', ip)

                    # Filter octs
                    if version == IP_VERSION.IPv4[0]:
                        # IP v4
                        oct1 = oct2 = oct3 = oct4 = Q()

                        if len(blocks[0]) != 0:
                            oct1 = Q(ip__oct1=blocks[0])
                        if len(blocks[1]) != 0:
                            oct2 = Q(ip__oct2=blocks[1])
                        if len(blocks[2]) != 0:
                            oct3 = Q(ip__oct3=blocks[2])
                        if len(blocks[3]) != 0:
                            oct4 = Q(ip__oct4=blocks[3])

                        vip = vip.filter(oct1 & oct2 & oct3 & oct4)
                    else:
                        # IP v6
                        oct1 = oct2 = oct3 = oct4 = oct5 = oct6 = oct7 = oct8 = Q(
                        )

                        if len(blocks[0]) != 0:
                            oct1 = Q(ipv6__block1__iexact=blocks[0])
                        if len(blocks[1]) != 0:
                            oct2 = Q(ipv6__block2__iexact=blocks[1])
                        if len(blocks[2]) != 0:
                            oct3 = Q(ipv6__block3__iexact=blocks[2])
                        if len(blocks[3]) != 0:
                            oct4 = Q(ipv6__block4__iexact=blocks[3])
                        if len(blocks[4]) != 0:
                            oct5 = Q(ipv6__block5__iexact=blocks[4])
                        if len(blocks[5]) != 0:
                            oct6 = Q(ipv6__block6__iexact=blocks[5])
                        if len(blocks[6]) != 0:
                            oct7 = Q(ipv6__block7__iexact=blocks[6])
                        if len(blocks[7]) != 0:
                            oct8 = Q(ipv6__block8__iexact=blocks[7])

                        vip = vip.filter(oct1 & oct2 & oct3 & oct4 & oct5
                                         & oct6 & oct7 & oct8)

            vip = vip.distinct()

            vip = vip.order_by("-pk")

            # Datatable paginator
            vip, total = build_query_to_datatable(vip, asorting_cols,
                                                  custom_search,
                                                  searchable_columns,
                                                  start_record, end_record)

            itens = get_vips(vip)

            vip_map = dict()
            vip_map["vips"] = itens
            vip_map["total"] = total

            return self.response(dumps_networkapi(vip_map))