1
|
|
|
"""Models for Mongo DB""" |
2
|
|
|
# pylint: disable=unused-argument,invalid-name,unused-argument |
3
|
|
|
# pylint: disable=no-self-argument,no-name-in-module |
4
|
|
|
|
5
|
1 |
|
import re |
6
|
1 |
|
from datetime import datetime |
7
|
1 |
|
from typing import Dict, List, Literal, Optional, Union |
8
|
|
|
|
9
|
1 |
|
from pydantic import BaseModel, Field, validator |
10
|
|
|
|
11
|
|
|
|
12
|
1 |
|
class DocumentBaseModel(BaseModel): |
13
|
|
|
"""Base model for Mongo documents""" |
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
|
|
|
"""Return a dictionary representation of the model""" |
21
|
1 |
|
values = super().dict(**kwargs) |
22
|
1 |
|
if "id" in values and values["id"]: |
23
|
1 |
|
values["_id"] = values["id"] |
24
|
1 |
|
if "exclude" in kwargs and "_id" in kwargs["exclude"]: |
25
|
1 |
|
del values["_id"] |
26
|
1 |
|
return values |
27
|
|
|
|
28
|
|
|
|
29
|
1 |
|
class CircuitScheduleDoc(BaseModel): |
30
|
|
|
"""EVC circuit schedule model""" |
31
|
|
|
|
32
|
1 |
|
id: str |
33
|
1 |
|
date: Optional[str] |
34
|
1 |
|
frequency: Optional[str] |
35
|
1 |
|
interval: Optional[int] |
36
|
1 |
|
action: str |
37
|
|
|
|
38
|
|
|
|
39
|
1 |
|
class TAGDoc(BaseModel): |
40
|
|
|
"""TAG model""" |
41
|
1 |
|
tag_type: int |
42
|
1 |
|
value: Union[int, str] |
43
|
|
|
|
44
|
1 |
|
@validator('value') |
45
|
1 |
|
def validate_value(cls, value): |
46
|
|
|
"""Validate value when is a string""" |
47
|
1 |
|
if isinstance(value, int): |
48
|
1 |
|
return value |
49
|
|
|
if isinstance(value, str): |
50
|
|
|
if value in ("any", "untagged"): |
51
|
|
|
return value |
52
|
|
|
regex = r"^(?=.{1,9}$)\d{1,4}\/\d{1,4}$" |
53
|
|
|
left, right = map(int, value.split('/')) |
54
|
|
|
if re.match(regex, value) and left < 4097 and right < 4097: |
55
|
|
|
return value |
56
|
|
|
raise ValueError("value as string allows 'any', 'untagged' and the ", |
57
|
|
|
"format 'n/n' where n > 4097") |
58
|
|
|
|
59
|
|
|
|
60
|
1 |
|
class UNIDoc(BaseModel): |
61
|
|
|
"""UNI model""" |
62
|
1 |
|
tag: Optional[TAGDoc] |
63
|
1 |
|
interface_id: str |
64
|
|
|
|
65
|
|
|
|
66
|
1 |
|
class LinkConstraints(BaseModel): |
67
|
|
|
"""LinkConstraints.""" |
68
|
1 |
|
bandwidth: Optional[float] |
69
|
1 |
|
ownership: Optional[str] |
70
|
1 |
|
reliability: Optional[float] |
71
|
1 |
|
utilization: Optional[float] |
72
|
1 |
|
delay: Optional[float] |
73
|
1 |
|
priority: Optional[int] |
74
|
|
|
|
75
|
|
|
|
76
|
1 |
|
class PathConstraints(BaseModel): |
77
|
|
|
"""Pathfinder Constraints.""" |
78
|
1 |
|
spf_attribute: Literal["hop", "delay", "priority"] = "hop" |
79
|
1 |
|
spf_max_path_cost: Optional[float] |
80
|
1 |
|
mandatory_metrics: Optional[LinkConstraints] |
81
|
1 |
|
flexible_metrics: Optional[LinkConstraints] |
82
|
1 |
|
minimum_flexible_hits: Optional[int] |
83
|
1 |
|
desired_links: Optional[List[str]] |
84
|
1 |
|
undesired_links: Optional[List[str]] |
85
|
|
|
|
86
|
|
|
|
87
|
1 |
|
class EVCBaseDoc(DocumentBaseModel): |
88
|
|
|
"""Base model for EVC documents""" |
89
|
|
|
|
90
|
1 |
|
uni_a: UNIDoc |
91
|
1 |
|
uni_z: UNIDoc |
92
|
1 |
|
name: str |
93
|
1 |
|
request_time: Optional[datetime] |
94
|
1 |
|
start_date: Optional[datetime] |
95
|
1 |
|
end_date: Optional[datetime] |
96
|
1 |
|
queue_id: Optional[int] |
97
|
1 |
|
flow_removed_at: Optional[datetime] |
98
|
1 |
|
execution_rounds: int = 0 |
99
|
1 |
|
bandwidth: int = 0 |
100
|
1 |
|
primary_path: Optional[List] |
101
|
1 |
|
backup_path: Optional[List] |
102
|
1 |
|
current_path: Optional[List] |
103
|
1 |
|
failover_path: Optional[List] |
104
|
1 |
|
primary_links: Optional[List] |
105
|
1 |
|
backup_links: Optional[List] |
106
|
1 |
|
backup_links: Optional[List] |
107
|
1 |
|
dynamic_backup_path: bool |
108
|
1 |
|
primary_constraints: Optional[PathConstraints] |
109
|
1 |
|
secondary_constraints: Optional[PathConstraints] |
110
|
1 |
|
creation_time: datetime |
111
|
1 |
|
owner: Optional[str] |
112
|
1 |
|
sb_priority: Optional[int] |
113
|
1 |
|
service_level: int = 0 |
114
|
1 |
|
circuit_scheduler: List[CircuitScheduleDoc] |
115
|
1 |
|
archived: bool = False |
116
|
1 |
|
metadata: Optional[Dict] = None |
117
|
1 |
|
active: bool |
118
|
1 |
|
enabled: bool |
119
|
|
|
|
120
|
1 |
|
@staticmethod |
121
|
1 |
|
def projection() -> Dict: |
122
|
|
|
"""Base projection of EVCBaseDoc model.""" |
123
|
1 |
|
time_fmt = "%Y-%m-%dT%H:%M:%S" |
124
|
1 |
|
return { |
125
|
|
|
"_id": 0, |
126
|
|
|
"id": 1, |
127
|
|
|
"uni_a": 1, |
128
|
|
|
"uni_z": 1, |
129
|
|
|
"name": 1, |
130
|
|
|
"bandwidth": 1, |
131
|
|
|
"primary_path": 1, |
132
|
|
|
"backup_path": 1, |
133
|
|
|
"current_path": 1, |
134
|
|
|
"failover_path": 1, |
135
|
|
|
"dynamic_backup_path": 1, |
136
|
|
|
"sb_priority": {"$ifNull": ["$sb_priority", "$priority", None]}, |
137
|
|
|
"service_level": 1, |
138
|
|
|
"circuit_scheduler": 1, |
139
|
|
|
"archived": 1, |
140
|
|
|
"metadata": 1, |
141
|
|
|
"active": 1, |
142
|
|
|
"enabled": 1, |
143
|
|
|
"execution_rounds": {"$ifNull": ["$execution_rounds", 0]}, |
144
|
|
|
"owner": {"$ifNull": ["$owner", None]}, |
145
|
|
|
"queue_id": {"$ifNull": ["$queue_id", None]}, |
146
|
|
|
"primary_constraints": {"$ifNull": ["$primary_constraints", {}]}, |
147
|
|
|
"secondary_constraints": {"$ifNull": ["$secondary_constraints", |
148
|
|
|
{}]}, |
149
|
|
|
"primary_links": {"$ifNull": ["$primary_links", []]}, |
150
|
|
|
"backup_links": {"$ifNull": ["$backup_links", []]}, |
151
|
|
|
"start_date": {"$dateToString": { |
152
|
|
|
"format": time_fmt, "date": "$start_date" |
153
|
|
|
}}, |
154
|
|
|
"creation_time": {"$dateToString": { |
155
|
|
|
"format": time_fmt, "date": "$creation_time" |
156
|
|
|
}}, |
157
|
|
|
"request_time": {"$dateToString": { |
158
|
|
|
"format": time_fmt, "date": { |
159
|
|
|
"$ifNull": ["$request_time", "$inserted_at"] |
160
|
|
|
} |
161
|
|
|
}}, |
162
|
|
|
"end_date": {"$dateToString": { |
163
|
|
|
"format": time_fmt, "date": { |
164
|
|
|
"$ifNull": ["$end_date", None] |
165
|
|
|
} |
166
|
|
|
}}, |
167
|
|
|
"flow_removed_at": {"$dateToString": { |
168
|
|
|
"format": time_fmt, "date": { |
169
|
|
|
"$ifNull": ["$flow_removed_at", None] |
170
|
|
|
} |
171
|
|
|
}}, |
172
|
|
|
"updated_at": {"$dateToString": { |
173
|
|
|
"format": time_fmt, "date": "$updated_at" |
174
|
|
|
}} |
175
|
|
|
} |
176
|
|
|
|