Docs: Add API docs from swagger json file; Add user guide document
[pti/o2.git] / o2ims / views / provision_view.py
1 # Copyright (C) 2021 Wind River Systems, Inc.\r
2 #\r
3 #  Licensed under the Apache License, Version 2.0 (the "License");\r
4 #  you may not use this file except in compliance with the License.\r
5 #  You may obtain a copy of the License at\r
6 #\r
7 #      http://www.apache.org/licenses/LICENSE-2.0\r
8 #\r
9 #  Unless required by applicable law or agreed to in writing, software\r
10 #  distributed under the License is distributed on an "AS IS" BASIS,\r
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 #  See the License for the specific language governing permissions and\r
13 #  limitations under the License.\r
14 \r
15 import logging\r
16 import uuid\r
17 from datetime import datetime\r
18 \r
19 from o2common.service import unit_of_work, messagebus\r
20 from o2ims.domain import events\r
21 from o2ims.views.provision_dto import SmoEndpointDTO\r
22 from o2ims.domain.configuration_obj import Configuration, ConfigurationTypeEnum\r
23 \r
24 \r
25 def configurations(uow: unit_of_work.AbstractUnitOfWork):\r
26     with uow:\r
27         li = uow.configurations.list()\r
28     return [r.serialize_smo() for r in li]\r
29 \r
30 \r
31 def configuration_one(configurationId: str,\r
32                       uow: unit_of_work.AbstractUnitOfWork):\r
33     with uow:\r
34         first = uow.configurations.get(configurationId)\r
35         return first.serialize_smo() if first is not None else None\r
36 \r
37 \r
38 def configuration_create(configurationDto: SmoEndpointDTO.endpoint,\r
39                          bus: messagebus.MessageBus):\r
40 \r
41     conf_uuid = str(uuid.uuid4())\r
42     configuration = Configuration(\r
43         conf_uuid, configurationDto['endpoint'], ConfigurationTypeEnum.SMO)\r
44     with bus.uow as uow:\r
45         uow.configurations.add(configuration)\r
46         logging.debug('before event length {}'.format(\r
47             len(configuration.events)))\r
48         configuration.events.append(events.ConfigurationChanged(\r
49             conf_uuid,\r
50             datetime.now()))\r
51         logging.debug('after event length {}'.format(\r
52             len(configuration.events)))\r
53         uow.commit()\r
54     _handle_events(bus)\r
55     return {"id": conf_uuid}\r
56 \r
57 \r
58 def configuration_delete(configurationId: str,\r
59                          uow: unit_of_work.AbstractUnitOfWork):\r
60     with uow:\r
61         uow.configurations.delete(configurationId)\r
62         uow.commit()\r
63     return True\r
64 \r
65 \r
66 def _handle_events(bus: messagebus.MessageBus):\r
67     # handle events\r
68     events = bus.uow.collect_new_events()\r
69     for event in events:\r
70         bus.handle(event)\r
71     return True\r