blob: a56381bdf0ed2c550cef45016d55926830390941 [file] [log] [blame]
Scott Bakerbba67b62019-01-28 17:38:21 -08001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16from modelaccessor import ModelAccessor
17import datetime
18import time
19
20
21class CoreApiModelAccessor(ModelAccessor):
22 def __init__(self, orm):
23 self.orm = orm
24 super(CoreApiModelAccessor, self).__init__()
25
26 def get_all_model_classes(self):
27 all_model_classes = {}
28 for k in self.orm.all_model_names:
29 all_model_classes[k] = getattr(self.orm, k)
30 return all_model_classes
31
32 def fetch_pending(self, main_objs, deletion=False):
33 if not isinstance(main_objs, list):
34 main_objs = [main_objs]
35
36 objs = []
37 for main_obj in main_objs:
38 if not deletion:
39 lobjs = main_obj.objects.filter_special(
40 main_obj.objects.SYNCHRONIZER_DIRTY_OBJECTS
41 )
42 else:
43 lobjs = main_obj.objects.filter_special(
44 main_obj.objects.SYNCHRONIZER_DELETED_OBJECTS
45 )
46 objs.extend(lobjs)
47
48 return objs
49
50 def fetch_policies(self, main_objs, deletion=False):
51 if not isinstance(main_objs, list):
52 main_objs = [main_objs]
53
54 objs = []
55 for main_obj in main_objs:
56 if not deletion:
57 lobjs = main_obj.objects.filter_special(
58 main_obj.objects.SYNCHRONIZER_DIRTY_POLICIES
59 )
60 else:
61 lobjs = main_obj.objects.filter_special(
62 main_obj.objects.SYNCHRONIZER_DELETED_POLICIES
63 )
64 objs.extend(lobjs)
65
66 return objs
67
68 def obj_exists(self, o):
69 # gRPC will default id to '0' for uninitialized objects
70 return (o.id is not None) and (o.id != 0)
71
72 def obj_in_list(self, o, olist):
73 ids = [x.id for x in olist]
74 return o.id in ids
75
76 def now(self):
77 """ Return the current time for timestamping purposes """
78 return (
79 datetime.datetime.utcnow() - datetime.datetime.fromtimestamp(0)
80 ).total_seconds()
81
82 def is_type(self, obj, name):
83 return obj._wrapped_class.__class__.__name__ == name
84
85 def is_instance(self, obj, name):
86 return name in obj.class_names.split(",")
87
88 def get_content_type_id(self, obj):
89 return obj.self_content_type_id
90
91 def create_obj(self, cls, **kwargs):
92 return cls.objects.new(**kwargs)