X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2ims%2Fviews%2Focloud_view.py;h=ae8b204edc74402e3ecf430cbab373bbf841cccf;hb=3da89330f3837ac6cffd2cad4c4018c9f8c3327d;hp=513e9e002da6fb1b0df735898626bb660bdc56ec;hpb=5c501cb5e57a1ac3e0e7d38e22a3759e7958777a;p=pti%2Fo2.git diff --git a/o2ims/views/ocloud_view.py b/o2ims/views/ocloud_view.py index 513e9e0..ae8b204 100644 --- a/o2ims/views/ocloud_view.py +++ b/o2ims/views/ocloud_view.py @@ -12,173 +12,157 @@ # See the License for the specific language governing permissions and # limitations under the License. -from sqlalchemy import select +import logging +import uuid +from datetime import datetime -from o2ims.adapter.orm import ocloud, resource, resourcetype, \ - resourcepool, deploymentmanager, subscription -from o2ims.adapter import unit_of_work -# from o2ims.domain.ocloud import Ocloud +from o2common.service import unit_of_work, messagebus +from o2ims.domain import events +from o2ims.views.ocloud_dto import RegistrationDTO, SubscriptionDTO +from o2ims.domain.subscription_obj import Registration, Subscription -def oclouds(uow: unit_of_work.SqlAlchemyUnitOfWork): +def oclouds(uow: unit_of_work.AbstractUnitOfWork): with uow: - # res = uow.session.execute( - # """ - # SELECT "oCloudId", "name" FROM ocloud - # """, - # ) - - res = uow.session.execute(select(ocloud)) - return [dict(r) for r in res] + li = uow.oclouds.list() + return [r.serialize() for r in li] def ocloud_one(ocloudid: str, uow: unit_of_work.AbstractUnitOfWork): with uow: - # res = uow.session.execute( - # """ - # SELECT "oCloudId", "name" FROM ocloud - # WHERE "oCloudId" = :oCloudId - # """, - # dict(oCloudId=ocloudid), - # ) - res = uow.session.execute( - select(ocloud).where(ocloud.c.oCloudId == ocloudid)) - first = res.first() - return None if first is None else dict(first) + first = uow.oclouds.get(ocloudid) + return first.serialize() if first is not None else None -def resource_types(uow: unit_of_work.SqlAlchemyUnitOfWork): +def resource_types(uow: unit_of_work.AbstractUnitOfWork): with uow: - # res = uow.session.execute( - # """ - # SELECT "resourceTypeId", "oCloudId", "name" FROM resourcetype - # """, - # ) - res = uow.session.execute(select(resourcetype)) - return [dict(r) for r in res] + li = uow.resource_types.list() + return [r.serialize() for r in li] def resource_type_one(resourceTypeId: str, - uow: unit_of_work.SqlAlchemyUnitOfWork): + uow: unit_of_work.AbstractUnitOfWork): with uow: - # res = uow.session.execute( - # """ - # SELECT "resourceTypeId", "oCloudId", "name" - # FROM resourcetype WHERE "resourceTypeId" = :resourceTypeId - # """, - # dict(resourceTypeId=resourceTypeId), - # ) - res = uow.session.execute(select(resourcetype).where( - resourcetype.c.resourceTypeId == resourceTypeId)) - first = res.first() - return None if first is None else dict(first) + first = uow.resource_types.get(resourceTypeId) + return first.serialize() if first is not None else None -def resource_pools(uow: unit_of_work.SqlAlchemyUnitOfWork): +def resource_pools(uow: unit_of_work.AbstractUnitOfWork): with uow: - # res = uow.session.execute( - # """ - # SELECT "resourcePoolId", "oCloudId", "location", "name" - # FROM resourcepool - # """, - # ) - res = uow.session.execute(select(resourcepool)) - return [dict(r) for r in res] + li = uow.resource_pools.list() + return [r.serialize() for r in li] def resource_pool_one(resourcePoolId: str, - uow: unit_of_work.SqlAlchemyUnitOfWork): - with uow: - # res = uow.session.execute( - # """ - # SELECT "resourcePoolId", "oCloudId", "location", "name" - # FROM resourcepool - # WHERE "resourcePoolId" = :resourcePoolId - # """, - # dict(resourcePoolId=resourcePoolId), - # ) - res = uow.session.execute(select(resourcepool).where( - resourcepool.c.resourcePoolId == resourcePoolId)) - first = res.first() - return None if first is None else dict(first) - - -def resources(resourcePoolId: str, uow: unit_of_work.SqlAlchemyUnitOfWork): - with uow: - # res = uow.session.execute( - # """ - # SELECT "resourceId", "parentId", "resourceTypeId", - # "resourcePoolId", "oCloudId" - # FROM resource - # WHERE "resourcePoolId" = :resourcePoolId - # """, - # dict(resourcePoolId=resourcePoolId), - # ) - res = uow.session.execute(select(resource).where( - resource.c.resourcePoolId == resourcePoolId)) - return [dict(r) for r in res] - - -def resource_one(resourceId: str, uow: unit_of_work.SqlAlchemyUnitOfWork): - with uow: - # res = uow.session.execute( - # """ - # SELECT "resourceId", "parentId", "resourceTypeId", - # "resourcePoolId", "oCloudId" - # FROM resource - # WHERE "resourceId" = :resourceId - # """, - # # AND "resourcePoolId" = :resourcePoolId - # # dict(resourcePoolId=resourcePoolId, - # dict(resourceId=resourceId), - # ) - res = uow.session.execute(select(resource).where( - resource.c.resourceId == resourceId)) - first = res.first() - return None if first is None else dict(first) - - -def deployment_managers(uow: unit_of_work.SqlAlchemyUnitOfWork): - with uow: - # res = uow.session.execute( - # """ - # SELECT "deploymentManagerId", "oCloudId", - # "deploymentManagementServiceEndpoint", "name" - # FROM deploymentmanager - # """, - # ) - res = uow.session.execute(select(deploymentmanager)) - return [dict(r) for r in res] + uow: unit_of_work.AbstractUnitOfWork): + with uow: + first = uow.resource_pools.get(resourcePoolId) + return first.serialize() if first is not None else None + + +def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork): + with uow: + li = uow.resources.list(resourcePoolId) + return [r.serialize() for r in li] + + +def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork): + with uow: + first = uow.resources.get(resourceId) + return first.serialize() if first is not None else None + + +def deployment_managers(uow: unit_of_work.AbstractUnitOfWork): + with uow: + li = uow.deployment_managers.list() + return [r.serialize() for r in li] def deployment_manager_one(deploymentManagerId: str, - uow: unit_of_work.SqlAlchemyUnitOfWork): + uow: unit_of_work.AbstractUnitOfWork): with uow: - # res = uow.session.execute( - # """ - # SELECT "deploymentManagerId", "oCloudId", - # "deploymentManagementServiceEndpoint", "name" - # FROM deploymentmanager - # WHERE "deploymentManagerId" = :deploymentManagerId - # """, - # dict(deploymentManagerId=deploymentManagerId), - # ) - res = uow.session.execute(select(deploymentmanager).where( - deploymentmanager.c.deploymentManagerId == deploymentManagerId)) - first = res.first() - return None if first is None else dict(first) + first = uow.deployment_managers.get(deploymentManagerId) + return first.serialize() if first is not None else None -def subscriptions(uow: unit_of_work.SqlAlchemyUnitOfWork): +def subscriptions(uow: unit_of_work.AbstractUnitOfWork): with uow: - res = uow.session.execute(select(subscription)) - return [dict(r) for r in res] + li = uow.subscriptions.list() + return [r.serialize() for r in li] def subscription_one(subscriptionId: str, - uow: unit_of_work.SqlAlchemyUnitOfWork): + uow: unit_of_work.AbstractUnitOfWork): with uow: - res = uow.session.execute(select(subscription).where( - subscription.c.subscriptionId == subscriptionId)) - first = res.first() - return None if first is None else dict(first) + first = uow.subscriptions.get(subscriptionId) + return first.serialize() if first is not None else None + + +def subscription_create(subscriptionDto: SubscriptionDTO.subscription, + uow: unit_of_work.AbstractUnitOfWork): + + sub_uuid = str(uuid.uuid4()) + subscription = Subscription( + sub_uuid, subscriptionDto['callback'], + subscriptionDto['consumerSubscriptionId'], + subscriptionDto['filter']) + with uow: + uow.subscriptions.add(subscription) + uow.commit() + return {"subscriptionId": sub_uuid} + + +def subscription_delete(subscriptionId: str, + uow: unit_of_work.AbstractUnitOfWork): + with uow: + uow.subscriptions.delete(subscriptionId) + uow.commit() + return True + + +def registrations(uow: unit_of_work.AbstractUnitOfWork): + with uow: + li = uow.registrations.list() + return [r.serialize() for r in li] + + +def registration_one(registrationId: str, + uow: unit_of_work.AbstractUnitOfWork): + with uow: + first = uow.registrations.get(registrationId) + return first.serialize() if first is not None else None + + +def registration_create(registrationDto: RegistrationDTO.registration, + bus: messagebus.MessageBus): + + reg_uuid = str(uuid.uuid4()) + registration = Registration( + reg_uuid, registrationDto['callback']) + with bus.uow as uow: + uow.registrations.add(registration) + logging.debug('before event length {}'.format( + len(registration.events))) + registration.events.append(events.RegistrationChanged( + reg_uuid, + datetime.now())) + logging.debug('after event length {}'.format(len(registration.events))) + uow.commit() + _handle_events(bus) + return {"registrationId": reg_uuid} + + +def registration_delete(registrationId: str, + uow: unit_of_work.AbstractUnitOfWork): + with uow: + uow.registrations.delete(registrationId) + uow.commit() + return True + + +def _handle_events(bus: messagebus.MessageBus): + # handle events + events = bus.uow.collect_new_events() + for event in events: + bus.handle(event) + return True