903a4bb48f925e7a3e1fd368dcd55a2411137a5c
[pti/o2.git] / o2ims / views / provision_view.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 import logging
16 import uuid
17 from datetime import datetime
18
19 from o2common.service import unit_of_work, messagebus
20 from o2ims.domain import events
21 from o2ims.views.provision_dto import SmoEndpointDTO
22 from o2ims.domain.configuration_obj import Configuration, ConfigurationTypeEnum
23
24
25 def configurations(uow: unit_of_work.AbstractUnitOfWork):
26     with uow:
27         li = uow.configurations.list()
28     return [r.serialize_smo() for r in li]
29
30
31 def configuration_one(configurationId: str,
32                       uow: unit_of_work.AbstractUnitOfWork):
33     with uow:
34         first = uow.configurations.get(configurationId)
35         return first.serialize_smo() if first is not None else None
36
37
38 def configuration_create(configurationDto: SmoEndpointDTO.endpoint,
39                          bus: messagebus.MessageBus):
40
41     conf_uuid = str(uuid.uuid4())
42     configuration = Configuration(
43         conf_uuid, configurationDto['endpoint'], ConfigurationTypeEnum.SMO)
44     with bus.uow as uow:
45         uow.configurations.add(configuration)
46         logging.debug('before event length {}'.format(
47             len(configuration.events)))
48         configuration.events.append(events.ConfigurationChanged(
49             conf_uuid,
50             datetime.now()))
51         logging.debug('after event length {}'.format(
52             len(configuration.events)))
53         uow.commit()
54     _handle_events(bus)
55     return {"id": conf_uuid}
56
57
58 def configuration_delete(configurationId: str,
59                          uow: unit_of_work.AbstractUnitOfWork):
60     with uow:
61         uow.configurations.delete(configurationId)
62         uow.commit()
63     return True
64
65
66 def _handle_events(bus: messagebus.MessageBus):
67     # handle events
68     events = bus.uow.collect_new_events()
69     for event in events:
70         bus.handle(event)
71     return True