Beispiel #1
0
def edge_to_json_layer3(nx_edge, nx_metadata):
    """Convert a edge between A and B in a netmap layer 3 graph to JSON

    :param nx_metadata: Metadata from netmap networkx graph
    :type nx_metadata: dict
    :return edge representation in JSON
    """
    source, target = nx_edge

    metadata_collection = defaultdict(list)
    for vlan_id, edges in six.iteritems(nx_metadata['metadata']):
        for edge in edges:
            metadata_collection[vlan_id].append(edge.to_json())

    def prefixaddress(item):
        addr = item.get('prefix', {}).get('net_address')
        return IP(addr) if addr else addr

    # sorting the output based on prefix address
    for value in six.itervalues(metadata_collection):
        value.sort(key=prefixaddress)

    json = {
        'source': six.text_type(source.id),
        'target': six.text_type(target.id),
        'edges': metadata_collection
    }
    if NETMAP_CONFIG.getboolean('API_DEBUG'):
        json.update({
            'd_source_sysname': six.text_type(source.sysname),
            'd_target_sysname': six.text_type(target.sysname),
        })
    return json
Beispiel #2
0
def edge_to_json_layer2(nx_edge, metadata):
    """Convert a edge between A and B in a netmap layer2 graph to JSON

    :param nx_edge: Metadata from netmap networkx graph
    :returns: edge representation in JSON
    """
    source, target = nx_edge
    edges = metadata['metadata']
    metadata_for_edges = []
    all_vlans = set()
    for edge in edges:
        all_vlans = all_vlans | edge.vlans
        metadata_for_edges.append(edge.to_json())

    json = {
        'source': six.text_type(source.id),
        'target': six.text_type(target.id),
        'vlans': [swpv.vlan.id for swpv in all_vlans],
        'edges': metadata_for_edges
    }

    if NETMAP_CONFIG.getboolean('API_DEBUG'):
        json.update({
            'd_source_sysname': six.text_type(source.sysname),
            'd_target_sysname': six.text_type(target.sysname),
            'd_vlans': [vlan_to_json(swpv.vlan) for swpv in all_vlans]
        })
    return json
Beispiel #3
0
    def to_json(self):
        """json presentation of Group"""
        json = {
            'netbox': six.text_type(self.netbox.id),
        }
        if self.interface is not None:
            ipdevinfo_link = None
            if (self.netbox.sysname and self.interface.ifname and
                    self.interface.ifname != '?'):
                kwargs = dict(netbox_sysname=six.text_type(self.netbox.sysname),
                              port_name=six.text_type(self.interface.ifname))
                try:
                    ipdevinfo_link = reverse(
                        'ipdevinfo-interface-details-by-name',
                        kwargs=kwargs)
                except NoReverseMatch:
                    ipdevinfo_link = None

            json['interface'] = {
                'ifname': six.text_type(self.interface.ifname),
                'ipdevinfo_link': ipdevinfo_link}

        if self.gw_ip is not None:
            json['gw_ip'] = self.gw_ip
        if self.virtual is not None:
            json['virtual'] = self.virtual
        if self.vlans is not None:
            json['vlans'] = [swpv.vlan.id for swpv in self.vlans]
        if NETMAP_CONFIG.getboolean('API_DEBUG'):
            json['d_netbox_sysname'] = six.text_type(self.netbox.sysname)
            json['d_vlans'] = [vlan_to_json(swpv.vlan) for swpv in self.vlans]

        return json
Beispiel #4
0
def edge_to_json_layer3(nx_edge, nx_metadata):
    """Convert a edge between A and B in a netmap layer 3 graph to JSON

    :param nx_metadata: Metadata from netmap networkx graph
    :type nx_metadata: dict
    :return edge representation in JSON
    """
    source, target = nx_edge

    # todo: fix sorted list keyed on prefix :-))
    metadata_collection = defaultdict(list)
    for vlan_id, edges in nx_metadata['metadata'].iteritems():
        for edge in edges:
            metadata_collection[vlan_id].append(edge.to_json())

    for key, value in metadata_collection.iteritems():
        value = sorted(value, key=operator.itemgetter('prefix'))

    json = {
        'source': unicode(source.id),
        'target': unicode(target.id),
        'edges': metadata_collection
    }
    if NETMAP_CONFIG.getboolean('API_DEBUG'):
        json.update({
            'd_source_sysname': unicode(source.sysname),
            'd_target_sysname': unicode(target.sysname),
        })
    return json
Beispiel #5
0
    def to_json(self):
        """json presentation of Group"""
        json = {
            'netbox': unicode(self.netbox.id),
        }
        if self.interface is not None:
            ipdevinfo_link = None
            if self.interface.ifname and self.interface.ifname != '?':
                ipdevinfo_link = reverse('ipdevinfo-interface-details-by-name',
                                         kwargs={
                                             'netbox_sysname':
                                             unicode(self.netbox.sysname),
                                             'port_name':
                                             unicode(self.interface.ifname)
                                         })
            json.update({
                'interface': {
                    'ifname': unicode(self.interface.ifname),
                    'ipdevinfo_link': ipdevinfo_link
                }
            })

        if self.gw_ip is not None:
            json.update({'gw_ip': self.gw_ip})
        if self.virtual is not None:
            json.update({'virtual': self.virtual})
        if self.vlans is not None:
            json.update({'vlans': [swpv.vlan.id for swpv in self.vlans]})
        if NETMAP_CONFIG.getboolean('API_DEBUG'):
            json.update({'d_netbox_sysname': unicode(self.netbox.sysname)})
            json.update(
                {'d_vlans': [vlan_to_json(swpv.vlan) for swpv in self.vlans]})

        return json
Beispiel #6
0
    def to_json(self):
        """json presentation of Node"""
        json = {}

        if self.metadata:
            if 'position' in self.metadata:
                json.update({
                    'position': {
                        'x': self.metadata['position'].x,
                        'y': self.metadata['position'].y
                    }})
            if 'vlans' in self.metadata:  # Layer2 metadata

                json.update({
                    'vlans': [nav_vlan_id for nav_vlan_id, _ in
                              self.metadata['vlans']]
                })
                if NETMAP_CONFIG.getboolean('API_DEBUG'):
                    json.update({
                        'd_vlans': [vlan_to_json(swpv.vlan) for _, swpv in
                                    self.metadata['vlans']]
                    })

        if isinstance(self.node, stubs.Netbox):
            json.update({
                'id': str(self.node.id),
                'sysname': self.node.sysname,
                'category': str(self.node.category_id),
                'is_elink_node': True
            })
        else:
            try:
                location = self.node.room.location
                locationid = location.id
                location_descr = location.description
            except AttributeError:
                locationid = ''
                location_descr = ''
            json.update({
                'id': str(self.node.id),
                'sysname': str(self.node.sysname),
                'category': str(self.node.category_id),
                'ip': self.node.ip,
                'ipdevinfo_link': reverse('ipdevinfo-details-by-name',
                                          args=[self.node.sysname]),
                'up': str(self.node.up),
                'up_image': get_status_image_link(self.node.up),
                'roomid': self.node.room.id,
                'locationid': six.text_type(locationid),
                'location': six.text_type(location_descr),
                'room': six.text_type(self.node.room),
                'is_elink_node': False,
            })
        return {six.text_type(self.node.id): json}