Add the command that registers to the SMO; Make the create registration and create...
[pti/o2.git] / o2ims / views / ocloud_view.py
index 386f8f5..ae8b204 100644 (file)
 #  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\r
+from o2common.service import unit_of_work, messagebus\r
+from o2ims.domain import events\r
 from o2ims.views.ocloud_dto import RegistrationDTO, SubscriptionDTO\r
 from o2ims.domain.subscription_obj import Registration, Subscription\r
 \r
@@ -131,14 +134,21 @@ def registration_one(registrationId: str,
 \r
 \r
 def registration_create(registrationDto: RegistrationDTO.registration,\r
-                        uow: unit_of_work.AbstractUnitOfWork):\r
+                        bus: messagebus.MessageBus):\r
 \r
     reg_uuid = str(uuid.uuid4())\r
     registration = Registration(\r
         reg_uuid, registrationDto['callback'])\r
-    with uow:\r
+    with bus.uow as uow:\r
         uow.registrations.add(registration)\r
+        logging.debug('before event length {}'.format(\r
+            len(registration.events)))\r
+        registration.events.append(events.RegistrationChanged(\r
+            reg_uuid,\r
+            datetime.now()))\r
+        logging.debug('after event length {}'.format(len(registration.events)))\r
         uow.commit()\r
+    _handle_events(bus)\r
     return {"registrationId": reg_uuid}\r
 \r
 \r
@@ -148,3 +158,11 @@ def registration_delete(registrationId: str,
         uow.registrations.delete(registrationId)\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