blob: b4e3b139cde6884dd112c3bea6e0a7a7a89e2cfa [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
Zack Williams5c2ea232019-01-30 15:23:01 -070016from __future__ import absolute_import
17
Scott Bakerbba67b62019-01-28 17:38:21 -080018import datetime
Zack Williams5c2ea232019-01-30 15:23:01 -070019
20from .modelaccessor import ModelAccessor
Scott Bakerbba67b62019-01-28 17:38:21 -080021
22
23class CoreApiModelAccessor(ModelAccessor):
24 def __init__(self, orm):
25 self.orm = orm
26 super(CoreApiModelAccessor, self).__init__()
27
28 def get_all_model_classes(self):
29 all_model_classes = {}
30 for k in self.orm.all_model_names:
31 all_model_classes[k] = getattr(self.orm, k)
32 return all_model_classes
33
34 def fetch_pending(self, main_objs, deletion=False):
35 if not isinstance(main_objs, list):
36 main_objs = [main_objs]
37
38 objs = []
39 for main_obj in main_objs:
40 if not deletion:
41 lobjs = main_obj.objects.filter_special(
42 main_obj.objects.SYNCHRONIZER_DIRTY_OBJECTS
43 )
44 else:
45 lobjs = main_obj.objects.filter_special(
46 main_obj.objects.SYNCHRONIZER_DELETED_OBJECTS
47 )
48 objs.extend(lobjs)
49
50 return objs
51
52 def fetch_policies(self, main_objs, deletion=False):
53 if not isinstance(main_objs, list):
54 main_objs = [main_objs]
55
56 objs = []
57 for main_obj in main_objs:
58 if not deletion:
59 lobjs = main_obj.objects.filter_special(
60 main_obj.objects.SYNCHRONIZER_DIRTY_POLICIES
61 )
62 else:
63 lobjs = main_obj.objects.filter_special(
64 main_obj.objects.SYNCHRONIZER_DELETED_POLICIES
65 )
66 objs.extend(lobjs)
67
68 return objs
69
70 def obj_exists(self, o):
71 # gRPC will default id to '0' for uninitialized objects
72 return (o.id is not None) and (o.id != 0)
73
74 def obj_in_list(self, o, olist):
75 ids = [x.id for x in olist]
76 return o.id in ids
77
78 def now(self):
79 """ Return the current time for timestamping purposes """
80 return (
81 datetime.datetime.utcnow() - datetime.datetime.fromtimestamp(0)
82 ).total_seconds()
83
84 def is_type(self, obj, name):
85 return obj._wrapped_class.__class__.__name__ == name
86
87 def is_instance(self, obj, name):
88 return name in obj.class_names.split(",")
89
90 def get_content_type_id(self, obj):
91 return obj.self_content_type_id
92
93 def create_obj(self, cls, **kwargs):
94 return cls.objects.new(**kwargs)