X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2ims%2Fviews%2Focloud_view.py;h=9404d9a59001c92bb28360f61949c369d912df77;hb=f73c8e3b01b8f5b7438ba544870e06d8f30cdea0;hp=7005b5a699bf32bef51770cc8b3cac1dd063df8a;hpb=81e3575a77366f30c2049f98c48a3087db0ea992;p=pti%2Fo2.git diff --git a/o2ims/views/ocloud_view.py b/o2ims/views/ocloud_view.py index 7005b5a..9404d9a 100644 --- a/o2ims/views/ocloud_view.py +++ b/o2ims/views/ocloud_view.py @@ -12,25 +12,128 @@ # See the License for the specific language governing permissions and # limitations under the License. -from o2ims.service import unit_of_work +import uuid +from sqlalchemy import select +from o2ims.adapter.orm import ocloud, resource, resourcetype, \ + resourcepool, deploymentmanager, subscription +from o2common.service import unit_of_work +from o2ims.views.ocloud_dto import SubscriptionDTO +from o2ims.domain.ocloud import Subscription -def ocloud_one(ocloudid: str, uow: unit_of_work.SqlAlchemyUnitOfWork): + +def oclouds(uow: unit_of_work.AbstractUnitOfWork): + with uow: + res = uow.session.execute(select(ocloud)) + return [dict(r) for r in res] + + +def ocloud_one(ocloudid: str, uow: unit_of_work.AbstractUnitOfWork): + with uow: + res = uow.session.execute( + select(ocloud).where(ocloud.c.oCloudId == ocloudid)) + first = res.first() + return None if first is None else dict(first) + + +def resource_types(uow: unit_of_work.AbstractUnitOfWork): + with uow: + res = uow.session.execute(select(resourcetype)) + return [dict(r) for r in res] + + +def resource_type_one(resourceTypeId: str, + uow: unit_of_work.AbstractUnitOfWork): + with uow: + res = uow.session.execute(select(resourcetype).where( + resourcetype.c.resourceTypeId == resourceTypeId)) + first = res.first() + return None if first is None else dict(first) + + +def resource_pools(uow: unit_of_work.AbstractUnitOfWork): + with uow: + res = uow.session.execute(select(resourcepool)) + return [dict(r) for r in res] + + +def resource_pool_one(resourcePoolId: str, + uow: unit_of_work.AbstractUnitOfWork): + with uow: + 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.AbstractUnitOfWork): + with uow: + 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.AbstractUnitOfWork): + with uow: + # topq = uow.session.query(resource).filter( + # resource.c.resourceId == resourceId).cte('cte', recursive=True) + # bootomq = uow.session.query(resource).join( + # topq, resource.c.parentId == topq.c.resourceId) + # res = uow.session.query(topq.union(bootomq)) + # print(res) + 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.AbstractUnitOfWork): + with uow: + res = uow.session.execute(select(deploymentmanager)) + return [dict(r) for r in res] + + +def deployment_manager_one(deploymentManagerId: str, + uow: unit_of_work.AbstractUnitOfWork): + with uow: + res = uow.session.execute(select(deploymentmanager).where( + deploymentmanager.c.deploymentManagerId == deploymentManagerId)) + first = res.first() + return None if first is None else dict(first) + + +def subscriptions(uow: unit_of_work.AbstractUnitOfWork): + with uow: + res = uow.session.execute(select(subscription)) + return [dict(r) for r in res] + + +def subscription_one(subscriptionId: str, + 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) + + +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: - results = uow.session.execute( - """ - SELECT oCloudId, name FROM ocloud WHERE oCloudId = :ocloudid - """, - dict(ocloudid=ocloudid), - ) - return dict(results[0]) if len(results) > 0 else None + uow.subscriptions.add(subscription) + uow.commit() + return {"subscriptionId": sub_uuid} -def oclouds(uow: unit_of_work.SqlAlchemyUnitOfWork): +def subscription_delete(subscriptionId: str, + uow: unit_of_work.AbstractUnitOfWork): with uow: - results = uow.session.execute( - """ - SELECT oCloudId, name FROM ocloud - """, - ) - return [dict(r) for r in results] + uow.subscriptions.delete(subscriptionId) + uow.commit() + return True