link.py 2.26 KB
Newer Older
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
1
2
3
4
5
6
# ---------------------------------------------------------------------
# Link's dynamic dashboard
# ---------------------------------------------------------------------
# Copyright (C) 2007-2016 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
Andrey Vertiprahov's avatar
Andrey Vertiprahov committed
7

8
# Third-Party modules
9
import demjson
10
from jinja2 import Environment, FileSystemLoader
Dmitry Volodin's avatar
Dmitry Volodin committed
11

12
13
# NOC modules
from .base import BaseDashboard
14
15
16
from noc.config import config
from noc.inv.models.link import Link

Andrey Vertiprahov's avatar
Andrey Vertiprahov committed
17
18
19
20
21
22
23
24
25
26
27

class LinkDashboard(BaseDashboard):
    name = "link"

    def resolve_object(self, object):
        try:
            return Link.objects.get(id=object)
        except Link.DoesNotExist:
            raise self.NotFound()

    def render(self):
28
        mos = self.object
29
        if mos.interfaces[0].description:
30
            mos.interfaces[0].description = self.str_cleanup(mos.interfaces[0].description)
31
        if mos.interfaces[1].description:
32
            mos.interfaces[1].description = self.str_cleanup(mos.interfaces[1].description)
33
        context = {
Dmitry Volodin's avatar
Dmitry Volodin committed
34
35
            "device_a": mos.interfaces[0].managed_object.name.replace('"', ""),
            "device_b": mos.interfaces[1].managed_object.name.replace('"', ""),
36
37
            "bi_id_a": mos.interfaces[0].managed_object.bi_id,
            "bi_id_b": mos.interfaces[0].managed_object.bi_id,
38
39
            "interface_a": {
                "name": mos.interfaces[0].name,
Dmitry Volodin's avatar
Dmitry Volodin committed
40
41
                "descr": mos.interfaces[0].description or mos.interfaces[0].name,
            },
42
43
            "interface_b": {
                "name": mos.interfaces[1].name,
Dmitry Volodin's avatar
Dmitry Volodin committed
44
45
                "descr": mos.interfaces[1].description or mos.interfaces[1].name,
            },
46
47
            "segment": mos.managed_objects[0].segment.id,
            "device_a_id": mos.managed_objects[0].id,
Aleksey Shirokih's avatar
Aleksey Shirokih committed
48
            "device_b_id": mos.managed_objects[1].id,
Aleksey Shirokih's avatar
Aleksey Shirokih committed
49
            "pool": mos.managed_objects[0].pool.name,
Dmitry Volodin's avatar
Dmitry Volodin committed
50
            "link_id": mos.id,
51
52
        }
        self.logger.info("Context with data: %s" % context)
53
        j2_env = Environment(loader=FileSystemLoader(config.path.pm_templates))
54
55
56
        tmpl = j2_env.get_template("dash_link.j2")
        data = tmpl.render(context)

57
        render = demjson.decode(data)
58
        return render