blob: 6b026165c49d341d9c1e396c7488fc036a6a8ca9 [file] [log] [blame]
Scott Bakerc3ce3e72016-06-20 17:35:19 -07001from rest_framework.decorators import api_view
2from rest_framework.response import Response
3from rest_framework.reverse import reverse
4from rest_framework import serializers
5from rest_framework import generics
6from rest_framework import viewsets
7from rest_framework.decorators import detail_route, list_route
8from rest_framework.views import APIView
9from core.models import *
10from services.vtn.models import VTNService
11from django.forms import widgets
12from django.conf.urls import patterns, url
13from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
14from django.shortcuts import get_object_or_404
15from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
16from xos.exceptions import *
17import json
18import subprocess
19
20class VTNServiceSerializer(PlusModelSerializer):
21 id = ReadOnlyField()
22
23 privateGatewayMac = serializers.CharField(required=False)
24 localManagementIp = serializers.CharField(required=False)
25 ovsdbPort = serializers.IntegerField(required=False)
26 sshPort = serializers.IntegerField(required=False)
27 sshUser = serializers.CharField(required=False)
28 sshKeyFile = serializers.CharField(required=False)
29 mgmtSubnetBits = serializers.IntegerField(required=False)
30 xosEndpoint = serializers.CharField(required=False)
31 xosUser = serializers.CharField(required=False)
32 xosPassword = serializers.CharField(required=False)
33
34
35 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
36 class Meta:
37 model = VTNService
38 fields = ('humanReadableName', 'id', 'privateGatewayMac', 'localManagementIp', 'ovsdbPort', 'sshPort', 'sshUser', 'sshKeyFile',
39 'mgmtSubnetBits', 'xosEndpoint', 'xosUser', 'xosPassword')
40
41 def getHumanReadableName(self, obj):
42 return obj.__unicode__()
43
44class VTNViewSet(XOSViewSet):
45 base_name = "vtn"
46 method_name = "vtn"
47 method_kind = "viewset"
48
49 # these are just because ViewSet needs some queryset and model, even if we don't use the
50 # default endpoints
51 queryset = VTNService.get_service_objects().all()
52 model = VTNService
53 serializer_class = VTNServiceSerializer
54
55 @classmethod
56 def get_urlpatterns(self, api_path="^"):
57 patterns = []
58
59 patterns.append( self.detail_url("services/$", {"get": "get_services"}, "services") )
60 patterns.append( self.detail_url("services_names/$", {"get": "get_services_names"}, "services") )
61 patterns.append( self.detail_url("services/(?P<service>[a-zA-Z0-9\-_]+)/$", {"get": "get_service"}, "get_service") )
62
63 # Not as RESTful as it could be, but maintain these endpoints for compability
64 patterns.append( self.list_url("services/$", {"get": "get_services"}, "rootvtn_services") )
65 patterns.append( self.list_url("services_names/$", {"get": "get_services_names"}, "rootvtn_services") )
66 patterns.append( self.list_url("services/(?P<service>[a-zA-Z0-9\-_]+)/$", {"get": "get_service"}, "rootvtn_get_service") )
67
68 patterns = patterns + super(VTNViewSet,self).get_urlpatterns(api_path)
69
70 return patterns
71
72 def get_services_names(self, request, pk=None):
73 result = {}
74 for service in Service.objects.all():
75 for id in service.get_vtn_src_names():
76 dependencies = service.get_vtn_dependencies_names()
77 if dependencies:
78 result[id] = dependencies
79 return Response(result)
80
81 def get_services(self, request, pk=None):
82 result = {}
83 for service in Service.objects.all():
84 for id in service.get_vtn_src_ids():
85 dependencies = service.get_vtn_dependencies_ids()
86 if dependencies:
87 result[id] = dependencies
88 return Response(result)
89
90 def get_service(self, request, pk=None, service=None):
91 for xos_service in Service.objects.all():
92 if service in xos_service.get_vtn_src_ids():
93 return Response(xos_service.get_vtn_dependencies_ids())
94 return Response([])
95
96