Move registration API to configuration
[pti/o2.git] / o2ims / views / provision_view.py
diff --git a/o2ims/views/provision_view.py b/o2ims/views/provision_view.py
new file mode 100644 (file)
index 0000000..54e5a15
--- /dev/null
@@ -0,0 +1,71 @@
+# Copyright (C) 2021 Wind River Systems, Inc.\r
+#\r
+#  Licensed under the Apache License, Version 2.0 (the "License");\r
+#  you may not use this file except in compliance with the License.\r
+#  You may obtain a copy of the License at\r
+#\r
+#      http://www.apache.org/licenses/LICENSE-2.0\r
+#\r
+#  Unless required by applicable law or agreed to in writing, software\r
+#  distributed under the License is distributed on an "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+#  See the License for the specific language governing permissions and\r
+#  limitations under the License.\r
+\r
+import logging\r
+import uuid\r
+from datetime import datetime\r
+\r
+from o2common.service import unit_of_work, messagebus\r
+from o2ims.domain import events\r
+from o2ims.views.provision_dto import SmoEndpointDTO\r
+from o2ims.domain.configuration_obj import Configuration, ConfigurationTypeEnum\r
+\r
+\r
+def configurations(uow: unit_of_work.AbstractUnitOfWork):\r
+    with uow:\r
+        li = uow.configurations.list()\r
+    return [r.serialize_smo() for r in li]\r
+\r
+\r
+def configuration_one(configurationId: str,\r
+                      uow: unit_of_work.AbstractUnitOfWork):\r
+    with uow:\r
+        first = uow.configurations.get(configurationId)\r
+        return first.serialize_smo() if first is not None else None\r
+\r
+\r
+def configuration_create(configurationDto: SmoEndpointDTO.endpoint,\r
+                         bus: messagebus.MessageBus):\r
+\r
+    conf_uuid = str(uuid.uuid4())\r
+    configuration = Configuration(\r
+        conf_uuid, configurationDto['endpoint'], ConfigurationTypeEnum.SMO)\r
+    with bus.uow as uow:\r
+        uow.configurations.add(configuration)\r
+        logging.debug('before event length {}'.format(\r
+            len(configuration.events)))\r
+        configuration.events.append(events.ConfigurationChanged(\r
+            conf_uuid,\r
+            datetime.now()))\r
+        logging.debug('after event length {}'.format(\r
+            len(configuration.events)))\r
+        uow.commit()\r
+    _handle_events(bus)\r
+    return {"configurationId": conf_uuid}\r
+\r
+\r
+def configuration_delete(configurationId: str,\r
+                         uow: unit_of_work.AbstractUnitOfWork):\r
+    with uow:\r
+        uow.configurations.delete(configurationId)\r
+        uow.commit()\r
+    return True\r
+\r
+\r
+def _handle_events(bus: messagebus.MessageBus):\r
+    # handle events\r
+    events = bus.uow.collect_new_events()\r
+    for event in events:\r
+        bus.handle(event)\r
+    return True\r