Add o2dms api endpoint
[pti/o2.git] / o2common / service / unit_of_work.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
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 # pylint: disable=attribute-defined-outside-init
16 from __future__ import annotations
17 import abc
18
19 from o2ims.domain.ocloud_repo import OcloudRepository,\
20     ResourcePoolRepository, ResourceRepository, ResourceTypeRepository,\
21     DeploymentManagerRepository
22 from o2ims.domain.stx_repo import StxObjectRepository
23
24
25 class AbstractUnitOfWork(abc.ABC):
26     oclouds: OcloudRepository
27     resource_types: ResourceTypeRepository
28     resource_pools: ResourcePoolRepository
29     resources: ResourceRepository
30     deployment_managers: DeploymentManagerRepository
31     stxobjects: StxObjectRepository
32
33     def __enter__(self):
34         return self
35
36     def __exit__(self, *args):
37         self.rollback()
38
39     def commit(self):
40         self._commit()
41
42     def collect_new_events(self):
43         return self._collect_new_events()
44
45     def _collect_new_events(self):
46         raise NotImplementedError
47
48     @abc.abstractmethod
49     def _commit(self):
50         raise NotImplementedError
51
52     @abc.abstractmethod
53     def rollback(self):
54         raise NotImplementedError