Beispiel #1
0
    def _interfaces_from_name(self, name):
        """Tries to find an Interface in NAV's database for the already
        identified netbox.

        The ifName, ifDescr, ifAlias, and optionally, baseport attributes are
        searched for name.

        :returns: A shadows.Interface object representing the interface, or None
                  if no corresponding interface was found.

        """
        if not (self.netbox and name):
            return

        if is_invalid_utf8(name):
            self._logger.warning(
                "cannot search database for malformed "
                "neighboring port name %r", name)
            return

        queries = [Q(ifdescr=name), Q(ifname=name), Q(ifalias=name)]
        if name.isdigit():
            queries.append(Q(baseport=int(name)))

        for query in queries:
            ifc = self._interface_query(query)
            if ifc:
                return ifc
Beispiel #2
0
    def _interface_from_name(self, name):
        """Tries to find an Interface in NAV's database for the already
        identified netbox.

        The ifName, ifDescr, ifAlias, and optionally, baseport attributes are
        searched for name.

        :returns: A shadows.Interface object representing the interface, or None
                  if no corresponding interface was found.

        """
        if not (self.netbox and name):
            return

        if is_invalid_utf8(name):
            self._logger.warn("cannot search database for malformed "
                              "neighboring port name %r", name)
            return

        queries = [Q(ifdescr=name), Q(ifname=name), Q(ifalias=name)]
        if name.isdigit():
            queries.append(Q(baseport=int(name)))

        netbox = Q(netbox__id=self.netbox.id)
        for query in queries:
            ifc = self._interface_query(netbox & query)
            if ifc:
                return ifc
Beispiel #3
0
    def _fix_binary_garbage(self):
        """Fixes version strings that appear as binary garbage."""

        for attr in ('hardware_version', 'software_version',
                     'firmware_version', 'serial'):
            value = getattr(self, attr)
            if utils.is_invalid_utf8(value):
                self._logger.warning("Invalid value for %s: %r", attr, value)
                setattr(self, attr, repr(value))
        self.clear_cached_objects()
Beispiel #4
0
 def prepare(self, _=None):
     for attr in ('remote_id', 'remote_name'):
         if getattr(self, attr) and is_invalid_utf8(getattr(self, attr)):
             self._logger.debug("converting invalid %s: %r", attr,
                                getattr(self, attr))
             setattr(self, attr, repr(getattr(self, attr)))
         elif not getattr(self, attr):
             setattr(self, attr, '')
         elif not isinstance(getattr(self, attr), six.text_type):
             value = getattr(self, attr)
             setattr(self, attr, value.decode('utf-8'))
Beispiel #5
0
    def _fix_binary_garbage(self):
        """Fixes version strings that appear as binary garbage."""

        for attr in ('hardware_version',
                     'software_version',
                     'firmware_version',
                     'serial'):
            value = getattr(self, attr)
            if utils.is_invalid_utf8(value):
                self._logger.warn("Invalid value for %s: %r",
                                  attr, value)
                setattr(self, attr, repr(value))
        self.clear_cached_objects()
Beispiel #6
0
 def test_valid_utf8(self):
     self.assertFalse(is_invalid_utf8("ABC-123"))
     self.assertFalse(is_invalid_utf8('\xc3\x86\xc3\x98\xc3\x85'))
Beispiel #7
0
 def test_invalid_utf8(self):
     self.assertTrue(is_invalid_utf8('P%\xe4\xb8D\xb6\x108B\x1d'))
Beispiel #8
0
    def _fix_binary_garbage(self):
        """Fixes string attributes that appear as binary garbage."""

        if utils.is_invalid_utf8(self.model):
            self._logger.warn("Invalid value for model: %r", self.model)
            self.model = repr(self.model)
Beispiel #9
0
    def _fix_binary_garbage(self):
        """Fixes string attributes that appear as binary garbage."""

        if utils.is_invalid_utf8(self.model):
            self._logger.warning("Invalid value for model: %r", self.model)
            self.model = repr(self.model)
Beispiel #10
0
 def test_valid_utf8(self):
     self.assertFalse(is_invalid_utf8("ABC-123"))
     self.assertFalse(is_invalid_utf8('\xc3\x86\xc3\x98\xc3\x85'))
Beispiel #11
0
 def test_invalid_utf8(self):
     self.assertTrue(is_invalid_utf8('P%\xe4\xb8D\xb6\x108B\x1d'))