e1a45e3a966af8075b912e6eefdb38615ba0711b
[dcaegen2/platform.git] / mod / distributorapi / distributor / data_access.py
1 # ============LICENSE_START=======================================================
2 # Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ============LICENSE_END=========================================================
16 """Data layer"""
17
18 from datetime import datetime
19 import uuid
20
21 # TODO: Use real storage
22 _cache = []
23
24
25 def get_distribution_targets():
26     global _cache
27     return _cache
28
29
30 def get_distribution_target(ds_id):
31     global _cache
32     result = [ i for i in _cache if i["dt_id"] == ds_id ]
33     return result[0] if result else {}
34
35 def transform_request(req):
36     """Transform request to object to store
37
38     NOTE: This method is not safe
39     """
40     ts = datetime.utcnow().isoformat()
41     req["created"] = ts
42     req["modified"] = ts
43     req["dt_id"] = str(uuid.uuid4())
44     req["processGroups"] = []
45     return req
46
47 def add_distribution_target(dt):
48     global _cache
49     _cache.append(dt)
50     return dt
51
52
53 def merge_request(dt, req):
54     dt["name"] = req["name"]
55     dt["runtimeApiUrl"] = req["runtimeApiUrl"]
56     dt["description"] = req.get("description", None)
57     dt["nextDistributionTargetId"] = req.get("nextDistributionTargetId", None)
58     dt["modified"] = datetime.utcnow().isoformat()
59     return dt
60
61 def update_distribution_target(updated_dt):
62     dt_id = updated_dt["dt_id"]
63     global _cache
64     # Did not use list comprehension blah blah because could not do the "return
65     # True" easily
66     for i, dt in enumerate(_cache):
67         if dt["dt_id"] == dt_id:
68             _cache[i] = updated_dt
69             return True
70     return False
71
72
73 def delete_distribution_target(dt_id):
74     global _cache
75     num_prev = len(_cache)
76     _cache = list(filter(lambda e: e["dt_id"] != dt_id, _cache))
77     return len(_cache) < num_prev
78
79
80 def add_process_group(ds_id, process_group):
81     global _cache
82     for dt in _cache:
83         if dt["dt_id"] == ds_id:
84             process_group["processed"] = datetime.utcnow().isoformat()
85             dt["processGroups"].append(process_group)
86             return process_group
87     return None
88
89