Add the command that registers to the SMO; Make the create registration and create...
[pti/o2.git] / o2ims / views / ocloud_route.py
index 1341b21..99adb5f 100644 (file)
 #  See the License for the specific language governing permissions and
 #  limitations under the License.
 
-from flask import jsonify
+from flask_restx import Resource
 
-from o2ims import config
-from o2ims.views import ocloud_view
+from o2ims.views import ocloud_view, api_ims_inventory_v1
+from o2common.config import config
+from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
+    ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO,\
+    RegistrationDTO
 
 
 apibase = config.get_o2ims_api_base()
 
 
-def configure_routes(app, bus):
+# ----------  OClouds ---------- #
+@api_ims_inventory_v1.route("/")
+@api_ims_inventory_v1.response(404, 'oCloud not found')
+class OcloudsListRouter(Resource):
+    """Ocloud get endpoint
+    O2 interface ocloud endpoint
+    """
 
-    # ----------  OClouds ---------- #
-    @app.route(apibase, methods=["GET"])
-    def oclouds():
-        result = ocloud_view.oclouds(bus.uow)
-        return jsonify(result), 200
+    ocloud_get = OcloudDTO.ocloud
 
-    # ----------  ResourceTypes ---------- #
+    @api_ims_inventory_v1.marshal_with(ocloud_get)
+    def get(self):
+        res = ocloud_view.oclouds(bus.uow)
+        if len(res) > 0:
+            return res[0]
+        api_ims_inventory_v1.abort(
+            404, "oCloud doesn't exist")
 
-    @app.route(apibase + "/resourceTypes", methods=["GET"])
-    def resource_types():
-        result = ocloud_view.resource_types(bus.uow)
-        return jsonify(result), 200
 
-    @app.route(apibase + "/resourceTypes", methods=["POST", "PUT", "PATCH",
-                                                    "DELETE"])
-    def resource_types_not_allow():
-        return "Method Not Allowed", 405
+# ----------  ResourceTypes ---------- #
+@api_ims_inventory_v1.route("/resourceTypes")
+class ResourceTypesListRouter(Resource):
 
-    @app.route(apibase + "/resourceTypes/<resourceTypeID>", methods=["GET"])
-    def resource_types_one(resourceTypeID):
+    model = ResourceTypeDTO.resource_type_get
+
+    @api_ims_inventory_v1.marshal_list_with(model)
+    def get(self):
+        return ocloud_view.resource_types(bus.uow)
+
+
+@api_ims_inventory_v1.route("/resourceTypes/<resourceTypeID>")
+@api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
+@api_ims_inventory_v1.response(404, 'Resource type not found')
+class ResourceTypeGetRouter(Resource):
+
+    model = ResourceTypeDTO.resource_type_get
+
+    @api_ims_inventory_v1.doc('Get resource type')
+    @api_ims_inventory_v1.marshal_with(model)
+    def get(self, resourceTypeID):
         result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
-        if result is None:
-            return "", 200
-        return jsonify(result), 200
+        if result is not None:
+            return result
+        api_ims_inventory_v1.abort(
+            404, "Resource type {} doesn't exist".format(resourceTypeID))
+
 
-    @app.route(apibase + "/resourceTypes/<resourceTypeID>",
-               methods=["POST", "PUT", "PATCH", "DELETE"])
-    def resource_types_one_not_allow(resourceTypeID):
-        return "Method Not Allowed", 405
+# ----------  ResourcePools ---------- #
+@api_ims_inventory_v1.route("/resourcePools")
+class ResourcePoolsListRouter(Resource):
 
-    # ----------  ResourcePools ---------- #
+    model = ResourcePoolDTO.resource_pool_get
 
-    @app.route(apibase + "/resourcePools", methods=["GET"])
-    def resource_pools():
-        result = ocloud_view.resource_pools(bus.uow)
-        return jsonify(result), 200
+    @api_ims_inventory_v1.marshal_list_with(model)
+    def get(self):
+        return ocloud_view.resource_pools(bus.uow)
 
-    @app.route(apibase + "/resourcePools", methods=["POST", "PUT", "PATCH",
-                                                    "DELETE"])
-    def resource_pools_not_allow():
-        return "Method Not Allowed", 405
 
-    @app.route(apibase + "/resourcePools/<resourcePoolID>", methods=["GET"])
-    def resource_pools_one(resourcePoolID):
+@api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>")
+@api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
+@api_ims_inventory_v1.response(404, 'Resource pool not found')
+class ResourcePoolGetRouter(Resource):
+
+    model = ResourcePoolDTO.resource_pool_get
+
+    @api_ims_inventory_v1.doc('Get resource pool')
+    @api_ims_inventory_v1.marshal_with(model)
+    def get(self, resourcePoolID):
         result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
-        if result is None:
-            return "", 200
-        return jsonify(result), 200
-
-    @app.route(apibase + "/resourcePools/<resourcePoolID>",
-               methods=["POST", "PUT", "PATCH", "DELETE"])
-    def resource_pools_one_not_allow(resourcePoolID):
-        return "Method Not Allowed", 405
-
-    # ----------  Resources ---------- #
-
-    @app.route(apibase + "/resourcePools/<resourcePoolID>/resources",
-               methods=["GET"])
-    def resources(resourcePoolID):
-        result = ocloud_view.resources(resourcePoolID, bus.uow)
-        return jsonify(result), 200
-
-    @app.route(apibase + "/resourcePools/<resourcePoolID>/resources",
-               methods=["POST", "PUT", "PATCH", "DELETE"])
-    def resource_not_allow(resourcePoolID):
-        return "Method Not Allowed", 405
-
-    @app.route(apibase +
-               "/resourcePools/<resourcePoolID>/resources/<resourceID>",
-               methods=["GET"])
-    def resources_one(resourcePoolID, resourceID):
+        if result is not None:
+            return result
+        api_ims_inventory_v1.abort(
+            404, "Resource pool {} doesn't exist".format(resourcePoolID))
+
+
+# ----------  Resources ---------- #
+@api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>/resources")
+@api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
+class ResourcesListRouter(Resource):
+
+    model = ResourceDTO.resource_list
+
+    @api_ims_inventory_v1.marshal_list_with(model)
+    def get(self, resourcePoolID):
+        return ocloud_view.resources(resourcePoolID, bus.uow)
+
+
+@api_ims_inventory_v1.route(
+    "/resourcePools/<resourcePoolID>/resources/<resourceID>")
+@api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
+@api_ims_inventory_v1.param('resourceID', 'ID of the resource')
+@api_ims_inventory_v1.response(404, 'Resource not found')
+class ResourceGetRouter(Resource):
+
+    # dto = ResourceDTO()
+    # model = dto.get_resource_get()
+    model = ResourceDTO.recursive_resource_mapping()
+
+    @api_ims_inventory_v1.doc('Get resource')
+    @api_ims_inventory_v1.marshal_with(model)
+    def get(self, resourcePoolID, resourceID):
         result = ocloud_view.resource_one(resourceID, bus.uow)
-        if result is None:
-            return "", 200
-        return jsonify(result), 200
-
-    @app.route(apibase +
-               "/resourcePools/<resourcePoolID>/resources/<resourceID>",
-               methods=["POST", "PUT", "PATCH", "DELETE"])
-    def resource_one_not_allow(resourcePoolID, resourceID):
-        return "Method Not Allowed", 405
-
-    # ----------  DeploymentManagers ---------- #
-
-    @app.route(apibase + "/deploymentManagers", methods=["GET"])
-    def deployment_managers():
-        result = ocloud_view.deployment_managers(bus.uow)
-        return jsonify(result), 200
-
-    @app.route(apibase + "/deploymentManagers",
-               methods=["POST", "PUT", "PATCH", "DELETE"])
-    def deployment_managers_not_allow():
-        return "Method Not Allowed", 405
-
-    @app.route(apibase + "/deploymentManagers/<deploymentManagerID>",
-               methods=["GET"])
-    def deployment_manager_one(deploymentManagerID):
+        if result is not None:
+            return result
+        api_ims_inventory_v1.abort(
+            404, "Resource {} doesn't exist".format(resourceID))
+
+
+# ----------  DeploymentManagers ---------- #
+@api_ims_inventory_v1.route("/deploymentManagers")
+class DeploymentManagersListRouter(Resource):
+
+    model = DeploymentManagerDTO.deployment_manager_get
+
+    @api_ims_inventory_v1.marshal_list_with(model)
+    def get(self):
+        return ocloud_view.deployment_managers(bus.uow)
+
+
+@api_ims_inventory_v1.route("/deploymentManagers/<deploymentManagerID>")
+@api_ims_inventory_v1.param('deploymentManagerID',
+                            'ID of the deployment manager')
+@api_ims_inventory_v1.response(404, 'Deployment manager not found')
+class DeploymentManagerGetRouter(Resource):
+
+    model = DeploymentManagerDTO.deployment_manager_get
+
+    @api_ims_inventory_v1.doc('Get deployment manager')
+    @api_ims_inventory_v1.marshal_with(model)
+    def get(self, deploymentManagerID):
         result = ocloud_view.deployment_manager_one(
             deploymentManagerID, bus.uow)
-        if result is None:
-            return "", 200
-        return jsonify(result), 200
-
-    @app.route(apibase + "/deploymentManagers/<deploymentManagerID>",
-               methods=["POST", "PUT", "PATCH", "DELETE"])
-    def deployment_manager_one_not_allow(deploymentManagerID):
-        return "Method Not Allowed", 405
+        if result is not None:
+            return result
+        api_ims_inventory_v1.abort(
+            404,
+            "Deployment manager {} doesn't exist".format(deploymentManagerID))
+
+
+# ----------  Subscriptions ---------- #
+@api_ims_inventory_v1.route("/subscriptions")
+class SubscriptionsListRouter(Resource):
+
+    model = SubscriptionDTO.subscription_get
+    expect = SubscriptionDTO.subscription
+    post_resp = SubscriptionDTO.subscription_post_resp
+
+    @api_ims_inventory_v1.doc('List subscriptions')
+    @api_ims_inventory_v1.marshal_list_with(model)
+    def get(self):
+        return ocloud_view.subscriptions(bus.uow)
+
+    @api_ims_inventory_v1.doc('Create a subscription')
+    @api_ims_inventory_v1.expect(expect)
+    @api_ims_inventory_v1.marshal_with(post_resp, code=201)
+    def post(self):
+        data = api_ims_inventory_v1.payload
+        result = ocloud_view.subscription_create(data, bus.uow)
+        return result, 201
+
+
+@api_ims_inventory_v1.route("/subscriptions/<subscriptionID>")
+@api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
+@api_ims_inventory_v1.response(404, 'Subscription not found')
+class SubscriptionGetDelRouter(Resource):
+
+    model = SubscriptionDTO.subscription_get
+
+    @api_ims_inventory_v1.doc('Get subscription by ID')
+    @api_ims_inventory_v1.marshal_with(model)
+    def get(self, subscriptionID):
+        result = ocloud_view.subscription_one(
+            subscriptionID, bus.uow)
+        if result is not None:
+            return result
+        api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
+            subscriptionID))
+
+    @api_ims_inventory_v1.doc('Delete subscription by ID')
+    @api_ims_inventory_v1.response(204, 'Subscription deleted')
+    def delete(self, subscriptionID):
+        result = ocloud_view.subscription_delete(subscriptionID, bus.uow)
+        return result, 204
+
+
+# ----------  Registration ---------- #
+@api_ims_inventory_v1.route("/registrations")
+class RegistrationListRouter(Resource):
+
+    model = RegistrationDTO.registration_get
+    expect = RegistrationDTO.registration
+    post_resp = RegistrationDTO.registration_post_resp
+
+    @api_ims_inventory_v1.doc('List registrations')
+    @api_ims_inventory_v1.marshal_list_with(model)
+    def get(self):
+        return ocloud_view.registrations(bus.uow)
+
+    @api_ims_inventory_v1.doc('Create a registration')
+    @api_ims_inventory_v1.expect(expect)
+    @api_ims_inventory_v1.marshal_with(post_resp, code=201)
+    def post(self):
+        data = api_ims_inventory_v1.payload
+        result = ocloud_view.registration_create(data, bus)
+        return result, 201
+
+
+@api_ims_inventory_v1.route("/registrations/<registrationID>")
+@api_ims_inventory_v1.param('registrationID', 'ID of the registration')
+@api_ims_inventory_v1.response(404, 'Registration not found')
+class RegistrationGetDelRouter(Resource):
+
+    model = RegistrationDTO.registration_get
+
+    @api_ims_inventory_v1.doc('Get registration by ID')
+    @api_ims_inventory_v1.marshal_with(model)
+    def get(self, registrationID):
+        result = ocloud_view.registration_one(
+            registrationID, bus.uow)
+        if result is not None:
+            return result
+        api_ims_inventory_v1.abort(404, "Registration {} doesn't exist".format(
+            registrationID))
+
+    @api_ims_inventory_v1.doc('Delete registration by ID')
+    @api_ims_inventory_v1.response(204, 'Registration deleted')
+    def delete(self, registrationID):
+        result = ocloud_view.registration_delete(registrationID, bus.uow)
+        return result, 204
+
+
+def configure_namespace(app, bus_new):
+
+    # Set global bus for resource
+    global bus
+    bus = bus_new
+
+    app.add_namespace(api_ims_inventory_v1, path=apibase)