aboutsummaryrefslogtreecommitdiffstats
path: root/genericparser/jobs
diff options
context:
space:
mode:
Diffstat (limited to 'genericparser/jobs')
-rw-r--r--genericparser/jobs/__init__.py13
-rw-r--r--genericparser/jobs/job_get.py46
-rw-r--r--genericparser/jobs/tests/__init__.py13
-rw-r--r--genericparser/jobs/tests/tests.py40
-rw-r--r--genericparser/jobs/urls.py20
-rw-r--r--genericparser/jobs/views.py124
6 files changed, 256 insertions, 0 deletions
diff --git a/genericparser/jobs/__init__.py b/genericparser/jobs/__init__.py
new file mode 100644
index 0000000..c7b6818
--- /dev/null
+++ b/genericparser/jobs/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
diff --git a/genericparser/jobs/job_get.py b/genericparser/jobs/job_get.py
new file mode 100644
index 0000000..56e20b6
--- /dev/null
+++ b/genericparser/jobs/job_get.py
@@ -0,0 +1,46 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+import logging
+
+from genericparser.pub.utils.jobutil import JobUtil
+
+logger = logging.getLogger(__name__)
+
+
+class GetJobInfoService(object):
+ def __init__(self, job_id, response_id=0):
+ self.job_id = job_id
+ self.response_id = response_id if response_id else 0
+
+ def do_biz(self):
+ logger.debug("[getjob]job_id=%s, response_id=%s", self.job_id, self.response_id)
+ jobs = JobUtil.query_job_status(self.job_id, self.response_id)
+ if not jobs:
+ return {"jobId": self.job_id}
+ ret = {
+ "jobId": self.job_id,
+ "responseDescriptor": {
+ "status": jobs[0].status,
+ "progress": jobs[0].progress,
+ "statusDescription": jobs[0].descp,
+ "errorCode": jobs[0].errcode,
+ "responseId": jobs[0].indexid,
+ "responseHistoryList": [
+ {
+ "status": job.status,
+ "progress": job.progress,
+ "statusDescription": job.descp,
+ "errorCode": job.errcode,
+ "responseId": job.indexid} for job in jobs[1:]]}}
+ return ret
diff --git a/genericparser/jobs/tests/__init__.py b/genericparser/jobs/tests/__init__.py
new file mode 100644
index 0000000..c7b6818
--- /dev/null
+++ b/genericparser/jobs/tests/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
diff --git a/genericparser/jobs/tests/tests.py b/genericparser/jobs/tests/tests.py
new file mode 100644
index 0000000..42231bf
--- /dev/null
+++ b/genericparser/jobs/tests/tests.py
@@ -0,0 +1,40 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+from django.test import TestCase, Client
+from rest_framework import status
+
+from genericparser.pub.database.models import JobModel, JobStatusModel
+
+
+class JobsViewTest(TestCase):
+ def setUp(self):
+ self.job_id = 'test_job_id'
+ self.client = Client()
+
+ def tearDown(self):
+ JobModel.objects.all().delete()
+
+ def test_job_normal(self):
+ JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
+ JobStatusModel(indexid=1, jobid=self.job_id, status='inst', errcode='0', progress=20, descp='inst').save()
+ response = self.client.get("/api/genericparser/v1/jobs/%s" % self.job_id)
+ self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
+
+ def test_job_when_jobid_not_exist(self):
+ job_id = 'test_new_job_id'
+ JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save()
+ JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst').save()
+ response = self.client.get("/api/genericparser/v1/jobs/%s" % job_id)
+ self.assertIn('jobId', response.data)
+ self.assertNotIn('responseDescriptor', response.data)
diff --git a/genericparser/jobs/urls.py b/genericparser/jobs/urls.py
new file mode 100644
index 0000000..d484703
--- /dev/null
+++ b/genericparser/jobs/urls.py
@@ -0,0 +1,20 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+from django.conf.urls import url
+
+from genericparser.jobs.views import JobView
+
+urlpatterns = [
+ url(r'^api/genericparser/v1/jobs/(?P<job_id>[0-9a-zA-Z_-]+)$', JobView.as_view()),
+]
diff --git a/genericparser/jobs/views.py b/genericparser/jobs/views.py
new file mode 100644
index 0000000..b9c5121
--- /dev/null
+++ b/genericparser/jobs/views.py
@@ -0,0 +1,124 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+import logging
+
+from drf_yasg import openapi
+from drf_yasg.utils import swagger_auto_schema
+from rest_framework import status
+from rest_framework.response import Response
+from rest_framework.views import APIView
+
+from genericparser.jobs.job_get import GetJobInfoService
+from genericparser.packages.serializers.genericparser_serializers import GetJobResponseSerializer
+from genericparser.packages.serializers.genericparser_serializers import PostJobRequestSerializer
+from genericparser.packages.serializers.genericparser_serializers import PostJobResponseResultSerializer
+from genericparser.pub.utils.jobutil import JobUtil
+from genericparser.pub.utils.values import ignore_case_get
+
+logger = logging.getLogger(__name__)
+
+
+class JobView(APIView):
+
+ input_job_id = openapi.Parameter(
+ 'job_id',
+ openapi.IN_QUERY,
+ description="job id",
+ type=openapi.TYPE_STRING)
+ input_response_id = openapi.Parameter(
+ 'responseId',
+ openapi.IN_QUERY,
+ description="response id",
+ type=openapi.TYPE_STRING)
+
+ @swagger_auto_schema(
+ operation_description="Get job status",
+ manual_parameters=[input_job_id, input_response_id],
+ responses={
+ status.HTTP_200_OK: GetJobResponseSerializer(),
+ status.HTTP_500_INTERNAL_SERVER_ERROR: PostJobResponseResultSerializer()
+ })
+ def get(self, request, job_id):
+ response_id = ignore_case_get(request.META, 'responseId')
+ ret = GetJobInfoService(job_id, response_id).do_biz()
+ response_serializer = GetJobResponseSerializer(data=ret)
+ validataion_error = self.handleValidatonError(
+ response_serializer, False)
+ if validataion_error:
+ return validataion_error
+
+ return Response(
+ data=response_serializer.data,
+ status=status.HTTP_200_OK)
+
+ @swagger_auto_schema(
+ request_body=PostJobRequestSerializer(),
+ operation_description="Update job status",
+ manual_parameters=[input_job_id],
+ responses={
+ status.HTTP_202_ACCEPTED: PostJobResponseResultSerializer(),
+ status.HTTP_500_INTERNAL_SERVER_ERROR: PostJobResponseResultSerializer()
+ }
+ )
+ def post(self, request, job_id):
+ job_result_ok = {'result': 'ok'}
+
+ logger.debug("Enter JobView:post, %s, %s ", job_id, request.data)
+ jobs = JobUtil.query_job_status(job_id)
+ if len(jobs) > 0 and jobs[-1].errcode == '255':
+ return Response(data=job_result_ok)
+
+ request_serializer = PostJobRequestSerializer(data=request.data)
+ validataion_error = self.handleValidatonError(
+ request_serializer, True)
+ if not validataion_error:
+ return validataion_error
+
+ requestData = request_serializer.data
+ progress = ignore_case_get(requestData, "progress")
+ desc = ignore_case_get(requestData, "desc", '%s' % progress)
+ errcode = '0' if ignore_case_get(
+ requestData, 'errcode') in (
+ 'true', 'active') else '255'
+ logger.debug("errcode=%s", errcode)
+ JobUtil.add_job_status(job_id, progress, desc, error_code=errcode)
+
+ response_serializer = PostJobResponseResultSerializer(
+ data=job_result_ok)
+ validataion_error = self.handleValidatonError(
+ response_serializer, False)
+ if validataion_error:
+ return validataion_error
+
+ return Response(
+ data=response_serializer.data,
+ status=status.HTTP_202_ACCEPTED)
+
+ def handleValidatonError(self, base_serializer, is_request):
+ response = None
+
+ if not base_serializer.is_valid():
+ errormessage = base_serializer.errors
+ logger.error(errormessage)
+
+ if is_request:
+ message = 'Invalid request'
+ else:
+ message = 'Invalid response'
+ logger.error(message)
+
+ Response(
+ data={'result': message, 'msg': errormessage},
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+ return response