1
|
|
|
"""DB models.""" |
2
|
|
|
|
3
|
1 |
|
from typing import List, Optional |
4
|
1 |
|
from datetime import datetime |
5
|
|
|
|
6
|
1 |
|
from pydantic import BaseModel |
7
|
1 |
|
from pydantic import conlist |
8
|
1 |
|
from pydantic import Field |
9
|
1 |
|
from pydantic import validator |
10
|
|
|
|
11
|
|
|
|
12
|
1 |
|
class DocumentBaseModel(BaseModel): |
13
|
|
|
"""DocumentBaseModel.""" |
14
|
|
|
|
15
|
1 |
|
id: str = Field(None, alias="_id") |
16
|
1 |
|
inserted_at: Optional[datetime] |
17
|
1 |
|
updated_at: Optional[datetime] |
18
|
|
|
|
19
|
1 |
|
def dict(self, **kwargs) -> dict: |
20
|
1 |
|
values = super().dict(**kwargs) |
21
|
1 |
|
if "id" in values and values["id"]: |
22
|
1 |
|
values["_id"] = values["id"] |
23
|
1 |
|
if "exclude" in kwargs and "_id" in kwargs["exclude"]: |
24
|
1 |
|
values.pop("_id") |
25
|
1 |
|
return values |
26
|
|
|
|
27
|
|
|
|
28
|
1 |
|
class InterfaceSubDoc(BaseModel): |
29
|
|
|
"""Interface DB SubDocument Model.""" |
30
|
|
|
|
31
|
1 |
|
id: str |
32
|
1 |
|
enabled: bool |
33
|
1 |
|
active: bool |
34
|
1 |
|
mac: str |
35
|
1 |
|
speed: float |
36
|
1 |
|
port_number: int |
37
|
1 |
|
name: str |
38
|
1 |
|
nni: bool = False |
39
|
1 |
|
lldp: bool |
40
|
1 |
|
switch: str |
41
|
1 |
|
link: Optional[str] |
42
|
1 |
|
link_side: Optional[str] |
43
|
1 |
|
metadata: dict = {} |
44
|
1 |
|
updated_at: Optional[datetime] |
45
|
|
|
|
46
|
|
|
|
47
|
1 |
|
class SwitchDoc(DocumentBaseModel): |
48
|
|
|
"""Switch DB Document Model.""" |
49
|
|
|
|
50
|
1 |
|
enabled: bool |
51
|
1 |
|
active: bool |
52
|
1 |
|
data_path: Optional[str] |
53
|
1 |
|
hardware: Optional[str] |
54
|
1 |
|
manufacturer: Optional[str] |
55
|
1 |
|
software: Optional[str] |
56
|
1 |
|
connection: Optional[str] |
57
|
1 |
|
ofp_version: Optional[str] |
58
|
1 |
|
serial: Optional[str] |
59
|
1 |
|
metadata: dict = {} |
60
|
1 |
|
interfaces: List[InterfaceSubDoc] = [] |
61
|
|
|
|
62
|
1 |
|
@validator("interfaces", pre=True) |
63
|
1 |
|
def preset_interfaces(cls, v, values, **kwargs) -> List[InterfaceSubDoc]: |
64
|
|
|
"""Preset interfaces.""" |
65
|
1 |
|
if isinstance(v, dict): |
66
|
1 |
|
return list(v.values()) |
67
|
1 |
|
return v |
68
|
|
|
|
69
|
1 |
|
@staticmethod |
70
|
1 |
|
def projection() -> dict: |
71
|
|
|
"""Base projection of this model.""" |
72
|
1 |
|
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
|
1 |
|
class InterfaceIdSubDoc(BaseModel): |
100
|
|
|
"""InterfaceId DB SubDocument Model.""" |
101
|
|
|
|
102
|
1 |
|
id: str |
103
|
|
|
|
104
|
|
|
|
105
|
1 |
|
class LinkDoc(DocumentBaseModel): |
106
|
|
|
"""Link DB Document Model.""" |
107
|
|
|
|
108
|
1 |
|
enabled: bool |
109
|
1 |
|
active: bool |
110
|
1 |
|
metadata: dict = {} |
111
|
1 |
|
endpoints: conlist(InterfaceIdSubDoc, min_items=2, max_items=2) |
112
|
|
|
|
113
|
1 |
|
@staticmethod |
114
|
1 |
|
def projection() -> dict: |
115
|
|
|
"""Base projection of this model.""" |
116
|
1 |
|
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
|
1 |
|
class InterfaceDetailDoc(DocumentBaseModel): |
130
|
|
|
"""InterfaceDetail DB Document Model.""" |
131
|
|
|
|
132
|
|
|
available_vlans: List[int] |
133
|
|
|
|