Example #1
0
 def onboard(self, request, **kwargs):
     try:
         generic_template = GenericTemplate.objects.get(
             templateId=kwargs['id'])
     except GenericTemplate.DoesNotExist:
         response = {
             "templateId": "Template does not exist",
             "status": "OperationFailed"
         }
         return JsonResponse(response, status=400)
     if generic_template.status == 'onboard':
         response = {"status": "OperationFailed"}
         return JsonResponse(response, status=400)
     generic_template.status = 'onboard'
     data = request.FILES['file']
     buffer = data.read()
     self_zipfile.decompression(buffer, kwargs['id'], generic_template.type)
     serializer = GenericTemplateSerializer(generic_template,
                                            data=request.data,
                                            partial=True)
     if serializer.is_valid():
         serializer.save()
         # Delete download zip file
         try:
             os.remove(
                 os.path.join(os.getcwd(), "nssmf", 'template_example',
                              generic_template.type + '.zip'))
         except OSError:
             pass
         response = {"status": "OperationSucceeded"}
         return JsonResponse(response)
     else:
         response = {"status": "OperationFailed"}
         return JsonResponse(response, status=400)
Example #2
0
 def read(self, request, **kwargs):
     # The GET method reads information about an Generic Template resource.
     if kwargs['id']:
         try:
             generic_template = GenericTemplate.objects.get(templateId=kwargs['id'])
             serializer = GenericTemplateSerializer(generic_template)
             response = serializer.data
         except GenericTemplate.DoesNotExist:
             response = {
                 "templateId": "Template does not exist",
                 "status": "OperationFailed"
             }
             return JsonResponse(response, status=400)
         return JsonResponse([response], safe=False)
     else:
         try:
             generic_template = GenericTemplate.objects.all()
             response = []
             for i in generic_template.values():
                 i['templateId'] = str(i['templateId'])
                 response.append(i)
         except GenericTemplate.DoesNotExist:
             response = {
                 "templateId": "Template does not exist",
                 "status": "OperationFailed"
             }
             return JsonResponse(response, status=400)
         return JsonResponse(response, safe=False)
Example #3
0
    def update(self, request, **kwargs):
        if kwargs['id']:
            # Check template is exist
            try:
                generic_template = GenericTemplate.objects.get(templateId=kwargs['id'])
            except GenericTemplate.DoesNotExist:
                response = {
                    "templateId": "Template does not exist",
                    "status": "OperationFailed"
                }
                return JsonResponse(response, status=400)
            # Check and Save data
            if request.data:
                # Check type setting
                if request.data['type'] not in \
                        ('VNF', 'NSD'):
                    response = {
                        "attributeListOut": {
                            "type": "Invalid type"
                        },
                        "status": "OperationFailed"
                    }
                    return JsonResponse(response, status=400)

                # Check nfvo setting
                if not request.data['nfvo']:
                    response = {
                        "attributeListOut": {
                            "nfvo": "Should be provide nfvo"
                        },
                        "status": "OperationFailed"
                    }
                    return JsonResponse(response)

                request.data['download_link'] = request.build_absolute_uri() + 'download/' + str(
                    generic_template.templateId)
                serializer = GenericTemplateSerializer(generic_template, data=request.data,
                                                       partial=True)
                if serializer.is_valid():
                    serializer.save()
                    response = serializer.data
                    return JsonResponse(response)
                else:
                    response = {
                        "status": "OperationFailed"
                    }
                    return JsonResponse(response, status=400)
            else:
                response = {
                    "attributeListOut": {
                        "nfvo": "xos_nfvo | tacker_nfvo | <other_nfvo>",
                        "type": "VNF | NSD"
                    },
                    "status": "OperationFailed"
                }
                return JsonResponse(response, status=400)
        else:
            response = {
                "attributeListOut": {
                    "templateId": "Should be provide templateId"
                },
                "status": "OperationFailed"
            }
            return JsonResponse(response, status=400)
Example #4
0
    def create(self, request, **kwargs):
        # The POST method is used to create a new Generic Template resource.
        if kwargs['id']:
            response = {
                "attributeListOut": {
                    "templateId": "Shouldn't be provide templateId"
                },
                "status": "OperationFailed"
            }
            return JsonResponse(response, status=400)
        # Check and Save data
        if request.data:
            # Check type setting
            if request.data['type'] not in \
                    ('VNF', 'NSD', 'all'):
                response = {
                    "attributeListOut": {
                        "type": "Invalid type"
                    },
                    "status": "OperationFailed"
                }
                return JsonResponse(response, status=400)

            # Check nfvo setting
            if not request.data['nfvo']:
                response = {
                    "attributeListOut": {
                        "nfvo": "Should be provide nfvo"
                    },
                    "status": "OperationFailed"
                }
                return JsonResponse(response)

            if request.data['type'] == 'all':
                response_all_list = list()
                for _ in ('VNF', 'NSD'):
                    generic_template = GenericTemplate()
                    request.data[
                        'download_link'] = request.build_absolute_uri() + 'download/' + str(
                        generic_template.templateId)
                    request.data['type'] = _
                    serializer = GenericTemplateSerializer(generic_template, data=request.data,
                                                           partial=True)
                    if serializer.is_valid():
                        serializer.save()
                        response = serializer.data
                        response_all_list.append(response)
                    else:
                        response = {
                            "status": "OperationFailed"
                        }
                        return JsonResponse(response, status=400)
                return JsonResponse(response_all_list, safe=False)
            else:
                generic_template = GenericTemplate()
                request.data['download_link'] = request.build_absolute_uri() + 'download/' + str(
                    generic_template.templateId)
                serializer = GenericTemplateSerializer(generic_template, data=request.data,
                                                       partial=True)
                if serializer.is_valid():
                    serializer.save()
                    response = serializer.data
                    return JsonResponse(response)
                else:
                    response = {
                        "status": "OperationFailed"
                    }
                    return JsonResponse(response, status=400)
        else:
            response = {
                "attributeListOut": {
                    "nfvo": "xos_nfvo | tacker_nfvo | <other_nfvo>",
                    "type": "VNF | NSD | all"
                },
                "status": "OperationFailed"
            }
            return JsonResponse(response, status=400)