Test Failed
Pull Request — master (#84)
by Vinicius
07:12
created

build.db.models   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 94
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A SwitchDoc.projection() 0 27 1
A SwitchDoc.preset_interfaces() 0 6 2
A LinkDoc.projection() 0 13 1
A DocumentBaseModel.dict() 0 7 5
1
"""DB models."""
2
3
from typing import List, Optional
4
from datetime import datetime
5
6
from pydantic import BaseModel
7
from pydantic import conlist
8
from pydantic import Field
9
from pydantic import validator
10
11
12
class DocumentBaseModel(BaseModel):
13
    """DocumentBaseModel."""
14
15
    id: str = Field(None, alias="_id")
16
    inserted_at: Optional[datetime]
17
    updated_at: Optional[datetime]
18
19
    def dict(self, **kwargs) -> dict:
20
        values = super().dict(**kwargs)
21
        if "id" in values and values["id"]:
22
            values["_id"] = values["id"]
23
        if "exclude" in kwargs and "_id" in kwargs["exclude"]:
24
            values.pop("_id")
25
        return values
26
27
28
class InterfaceSubDoc(BaseModel):
29
    """Interface DB SubDocument Model."""
30
31
    id: str
32
    enabled: bool
33
    active: bool
34
    mac: str
35
    speed: float
36
    port_number: int
37
    name: str
38
    nni: bool = False
39
    lldp: bool
40
    switch: str
41
    link: Optional[str]
42
    link_side: Optional[str]
43
    metadata: dict = {}
44
    updated_at: Optional[datetime]
45
46
47
class SwitchDoc(DocumentBaseModel):
48
    """Switch DB Document Model."""
49
50
    enabled: bool
51
    active: bool
52
    data_path: Optional[str]
53
    hardware: Optional[str]
54
    manufacturer: Optional[str]
55
    software: Optional[str]
56
    connection: Optional[str]
57
    ofp_version: Optional[str]
58
    serial: Optional[str]
59
    metadata: dict = {}
60
    interfaces: List[InterfaceSubDoc] = []
61
62
    @validator("interfaces", pre=True)
63
    def preset_interfaces(cls, v, values, **kwargs) -> List[InterfaceSubDoc]:
64
        """Preset interfaces."""
65
        if isinstance(v, dict):
66
            return list(v.values())
67
        return v
68
69
    @staticmethod
70
    def projection() -> dict:
71
        """Base projection of this model."""
72
        return {
73
            "_id": 0,
74
            "id": 1,
75
            "enabled": 1,
76
            "active": 1,
77
            "data_path": 1,
78
            "hardware": 1,
79
            "manufacturer": 1,
80
            "software": 1,
81
            "connection": 1,
82
            "ofp_version": 1,
83
            "serial": 1,
84
            "metadata": 1,
85
            "interfaces": {
86
                "$arrayToObject": {
87
                    "$map": {
88
                        "input": "$interfaces",
89
                        "as": "intf",
90
                        "in": {"k": "$$intf.id", "v": "$$intf"},
91
                    }
92
                }
93
            },
94
            "updated_at": 1,
95
            "inserted_at": 1,
96
        }
97
98
99
class InterfaceIdSubDoc(BaseModel):
100
    """InterfaceId DB SubDocument Model."""
101
102
    id: str
103
104
105
class LinkDoc(DocumentBaseModel):
106
    """Link DB Document Model."""
107
108
    enabled: bool
109
    active: bool
110
    metadata: dict = {}
111
    endpoints: conlist(InterfaceIdSubDoc, min_items=2, max_items=2)
112
113
    @staticmethod
114
    def projection() -> dict:
115
        """Base projection of this model."""
116
        return {
117
            "_id": 0,
118
            "id": 1,
119
            "enabled": 1,
120
            "active": 1,
121
            "metadata": 1,
122
            "endpoint_a": {"$first": "$endpoints"},
123
            "endpoint_b": {"$last": "$endpoints"},
124
            "updated_at": 1,
125
            "inserted_at": 1,
126
        }
127
128
129
class InterfaceDetailDoc(DocumentBaseModel):
130
    """InterfaceDetail DB Document Model."""
131
132
    available_vlans: List[int]
133