1
|
|
|
"""ELineController.""" |
2
|
|
|
# pylint: disable=unnecessary-lambda,invalid-name |
3
|
1 |
|
import os |
4
|
1 |
|
from datetime import datetime |
5
|
1 |
|
from typing import Dict, Optional |
6
|
|
|
|
7
|
1 |
|
import pymongo |
8
|
1 |
|
from pymongo.collection import ReturnDocument |
9
|
1 |
|
from pymongo.errors import AutoReconnect |
10
|
1 |
|
from pymongo.operations import UpdateOne |
11
|
1 |
|
from tenacity import retry_if_exception_type, stop_after_attempt, wait_random |
12
|
|
|
|
13
|
1 |
|
from kytos.core import log |
14
|
1 |
|
from kytos.core.db import Mongo |
15
|
1 |
|
from kytos.core.retry import before_sleep, for_all_methods, retries |
16
|
1 |
|
from napps.kytos.mef_eline.db.models import EVCBaseDoc |
17
|
|
|
|
18
|
|
|
|
19
|
1 |
|
@for_all_methods( |
20
|
|
|
retries, |
21
|
|
|
stop=stop_after_attempt( |
22
|
|
|
int(os.environ.get("MONGO_AUTO_RETRY_STOP_AFTER_ATTEMPT", "3")) |
23
|
|
|
), |
24
|
|
|
wait=wait_random( |
25
|
|
|
min=int(os.environ.get("MONGO_AUTO_RETRY_WAIT_RANDOM_MIN", "1")), |
26
|
|
|
max=int(os.environ.get("MONGO_AUTO_RETRY_WAIT_RANDOM_MAX", "1")), |
27
|
|
|
), |
28
|
|
|
before_sleep=before_sleep, |
29
|
|
|
retry=retry_if_exception_type((AutoReconnect,)), |
30
|
|
|
) |
31
|
1 |
|
class ELineController: |
32
|
|
|
"""E-Line Controller""" |
33
|
|
|
|
34
|
1 |
|
def __init__(self, get_mongo=lambda: Mongo()) -> None: |
|
|
|
|
35
|
1 |
|
self.mongo = get_mongo() |
36
|
1 |
|
self.db_client = self.mongo.client |
37
|
1 |
|
self.db = self.db_client[self.mongo.db_name] |
38
|
|
|
|
39
|
1 |
|
def bootstrap_indexes(self) -> None: |
40
|
|
|
"""Bootstrap mef_eline relaeted indexes.""" |
41
|
1 |
|
index_tuples = [ |
42
|
|
|
("evcs", [("circuit_scheduler.id", pymongo.ASCENDING)]), |
43
|
|
|
("evcs", [("archived", pymongo.ASCENDING)]), |
44
|
|
|
] |
45
|
1 |
|
for collection, keys in index_tuples: |
46
|
1 |
|
if self.mongo.bootstrap_index(collection, keys): |
47
|
1 |
|
log.info( |
48
|
|
|
f"Created DB index {keys}, collection: {collection}" |
49
|
|
|
) |
50
|
|
|
|
51
|
1 |
|
def get_circuits(self, archived: Optional[bool] = False, |
52
|
|
|
match: dict = None) -> Dict: |
53
|
|
|
"""Get all circuits from database.""" |
54
|
1 |
|
aggregation = [] |
55
|
1 |
|
match_filters = {"$match": {}} |
56
|
1 |
|
if archived is not None: |
57
|
1 |
|
match_filters["$match"]["archived"] = archived |
58
|
1 |
|
aggregation.append(match_filters) |
59
|
1 |
|
if match: |
60
|
|
|
key = next(iter(match)) |
61
|
|
|
match_filters["$match"][key] = match[key] |
62
|
1 |
|
aggregation.extend([ |
63
|
|
|
{"$sort": {"_id": 1}}, |
64
|
|
|
{"$project": EVCBaseDoc.projection()}, |
65
|
|
|
] |
66
|
|
|
) |
67
|
1 |
|
circuits = self.db.evcs.aggregate(aggregation) |
68
|
1 |
|
return {"circuits": {value["id"]: value for value in circuits}} |
69
|
|
|
|
70
|
1 |
|
def get_circuit(self, circuit_id: str) -> Optional[Dict]: |
71
|
|
|
"""Get a circuit.""" |
72
|
|
|
return self.db.evcs.find_one({"_id": circuit_id}, |
73
|
|
|
EVCBaseDoc.projection()) |
74
|
|
|
|
75
|
1 |
|
def upsert_evc(self, evc: Dict) -> Optional[Dict]: |
76
|
|
|
"""Update or insert an EVC""" |
77
|
1 |
|
utc_now = datetime.utcnow() |
78
|
1 |
|
model = EVCBaseDoc( |
79
|
|
|
**{ |
80
|
|
|
**evc, |
81
|
|
|
**{"_id": evc["id"]} |
82
|
|
|
} |
83
|
|
|
) |
84
|
1 |
|
updated = self.db.evcs.find_one_and_update( |
85
|
|
|
{"_id": evc["id"]}, |
86
|
|
|
{ |
87
|
|
|
"$set": model.dict(exclude={"inserted_at"}, exclude_none=True), |
88
|
|
|
"$setOnInsert": {"inserted_at": utc_now}, |
89
|
|
|
}, |
90
|
|
|
return_document=ReturnDocument.AFTER, |
91
|
|
|
upsert=True, |
92
|
|
|
) |
93
|
1 |
|
return updated |
94
|
|
|
|
95
|
1 |
|
def update_bulk_evc(self, circuit_ids: list, payload: dict): |
96
|
|
|
"""Update a bulk of EVC""" |
97
|
1 |
|
utc_now = datetime.utcnow() |
98
|
1 |
|
ops = [] |
99
|
1 |
|
for _id in circuit_ids: |
100
|
1 |
|
ops.append( |
101
|
|
|
UpdateOne( |
102
|
|
|
{"_id": _id}, |
103
|
|
|
{ |
104
|
|
|
**payload, |
105
|
|
|
"$setOnInsert": {"inserted_at": utc_now} |
106
|
|
|
}, |
107
|
|
|
upsert=False, |
108
|
|
|
) |
109
|
|
|
) |
110
|
|
|
return self.db.evcs.bulk_write(ops).modified_count |
111
|
|
|
|