def get_interface_subjects(self): netdev = self._proc.open('net', 'dev') faces = [] subjects = {} type_map = {'ipv4': netifaces.AF_INET, 'ipv6': netifaces.AF_INET6, 'mac': netifaces.AF_LINK} for line in netdev: line = line.split() if line[0].count(":"): faces.append(line[0][:line[0].index(":")]) unis = UNISInstance(unis_url=self.unis_url) for face in faces: post_dict = {} try: capacity = ethtool.get_speed(face) except OSError: capacity = 0 # assume each port is a layer2 port for the main 'address' try: l2_addr = netifaces.ifaddresses(face)[type_map['mac']] if len(l2_addr): addr = {"type": "mac", "address": l2_addr[0]['addr']} post_dict['address'] = addr except: pass # add all the other address info we can find post_dict['properties'] = {} for t in type_map: try: addrs = netifaces.ifaddresses(face)[type_map[t]] for a in addrs: addr = {"type": t, "address": a['addr']} post_dict['properties'][t] = addr except: pass ### some sort of verification here that capacity is right post_dict['name'] = face post_dict['capacity'] = capacity # hack in a 'nodeRef' so we can find port from rspec post_dict['nodeRef'] = settings.URN_STRING[:-1] resp = unis.post_port(post_dict) if isinstance(resp, dict): subjects[face]=resp['selfRef'] return subjects
def _build_port_dict(self, port_name): type_map = {'ipv4': netifaces.AF_INET, 'ipv6': netifaces.AF_INET6, 'mac': netifaces.AF_LINK} post_dict = {} try: capacity = ethtool.get_speed(port_name) except OSError: capacity = 0 # assume each port is a layer2 port for the main 'address' try: l2_addr = netifaces.ifaddresses(port_name)[type_map['mac']] if len(l2_addr): addr = {"type": "mac", "address": l2_addr[0]['addr']} post_dict['address'] = addr.strip().replace(':', '').lower() except: pass # add all the other address info we can find post_dict['properties'] = {} for t in type_map: try: addrs = netifaces.ifaddresses(port_name)[type_map[t]] for a in addrs: addr = {"type": t, "address": a['addr']} post_dict['properties'][t] = addr except Exception as e: logger.exc('get_interface_subjects', e) # TODO some sort of verification here that capacity is right post_dict['name'] = port_name post_dict['capacity'] = capacity # hack in a 'nodeRef' so we can find port from rspec post_dict['nodeRef'] = settings.HOST_URN[:-1] return post_dict