blob: fe0a7f66806148d00464e6c2c5b27e4c28427d38 [file] [log] [blame]
Sapan Bhatiae41a8ec2017-04-27 01:56:28 +02001from __future__ import absolute_import
2
3import sys
4import json
5import operator
6from operator import attrgetter
7from core.models.plcorebase import *
8from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
9from django.contrib.contenttypes.models import ContentType
10from django.utils.timezone import now
11from core.acl import AccessControlList
12from django.core.exceptions import ValidationError,PermissionDenied
13
14from distutils.version import LooseVersion
15from django.db import models,transaction
16from django.db.models import *
17from django.core.validators import URLValidator
18from xos.exceptions import *
19import urlparse
20from xos.config import Config
21
22config = Config()
23
24# If true, then IP addresses will be allocated by the model. If false, then
25# we will assume the observer handles it.
26NO_OBSERVER=False
27
28def ParseNatList(ports):
29 """ Support a list of ports in the format "protocol:port, protocol:port, ..."
30 examples:
31 tcp 123
32 tcp 123:133
33 tcp 123, tcp 124, tcp 125, udp 201, udp 202
34
35 User can put either a "/" or a " " between protocol and ports
36 Port ranges can be specified with "-" or ":"
37 """
38 nats = []
39 if ports:
40 parts = ports.split(",")
41 for part in parts:
42 part = part.strip()
43 if "/" in part:
44 (protocol, ports) = part.split("/",1)
45 elif " " in part:
46 (protocol, ports) = part.split(None,1)
47 else:
48 raise TypeError('malformed port specifier %s, format example: "tcp 123, tcp 201:206, udp 333"' % part)
49
50 protocol = protocol.strip()
51 ports = ports.strip()
52
53 if not (protocol in ["udp", "tcp"]):
54 raise ValueError('unknown protocol %s' % protocol)
55
56 if "-" in ports:
57 (first, last) = ports.split("-")
58 first = int(first.strip())
59 last = int(last.strip())
60 portStr = "%d:%d" % (first, last)
61 elif ":" in ports:
62 (first, last) = ports.split(":")
63 first = int(first.strip())
64 last = int(last.strip())
65 portStr = "%d:%d" % (first, last)
66 else:
67 portStr = "%d" % int(ports)
68
69 nats.append( {"l4_protocol": protocol, "l4_port": portStr} )
70
71 return nats
72
73def ValidateNatList(ports):
74 try:
75 ParseNatList(ports)
76 except Exception,e:
77 raise ValidationError(str(e))
78
79
80
81def get_default_flavor(controller = None):
82 # Find a default flavor that can be used for a instance. This is particularly
83 # useful in evolution. It's also intended this helper function can be used
84 # for admin.py when users
85
86 if controller:
87 flavors = controller.flavors.all()
88 else:
89 from core.models.flavor import Flavor
90 flavors = Flavor.objects.all()
91
92 if not flavors:
93 return None
94
95 for flavor in flavors:
96 if flavor.default:
97 return flavor
98
99 return flavors[0]
100
101class InstanceDeletionManager(PlCoreBaseDeletionManager):
102 def get_queryset(self):
103 parent=super(InstanceDeletionManager, self)
104 try:
105 backend_type = config.observer_backend_type
106 except AttributeError:
107 backend_type = None
108
109 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
110 if (backend_type):
111 return parent_queryset.filter(Q(node__controller__backend_type=backend_type))
112 else:
113 return parent_queryset
114
115 # deprecated in django 1.7 in favor of get_queryset().
116 def get_query_set(self):
117 return self.get_queryset()
118
119
120class InstanceManager(PlCoreBaseManager):
121 def get_queryset(self):
122 parent=super(InstanceManager, self)
123
124 try:
125 backend_type = config.observer_backend_type
126 except AttributeError:
127 backend_type = None
128
129 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
130
131 if backend_type:
132 return parent_queryset.filter(Q(node__controller__backend_type=backend_type))
133 else:
134 return parent_queryset
135
136 # deprecated in django 1.7 in favor of get_queryset().
137 def get_query_set(self):
138 return self.get_queryset()
139
140def get_default_serviceclass():
141 from core.models.serviceclass import ServiceClass
142 try:
143 return ServiceClass.objects.get(name="Best Effort")
144 except ServiceClass.DoesNotExist:
145 return None
146
147class ControllerLinkDeletionManager(PlCoreBaseDeletionManager):
148 def get_queryset(self):
149 parent=super(ControllerLinkDeletionManager, self)
150 try:
151 backend_type = config.observer_backend_type
152 except AttributeError:
153 backend_type = None
154
155 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
156 if (backend_type):
157 return parent_queryset.filter(Q(controller__backend_type=backend_type))
158 else:
159 return parent_queryset
160
161 # deprecated in django 1.7 in favor of get_queryset().
162 def get_query_set(self):
163 return self.get_queryset()
164
165
166class ControllerDeletionManager(PlCoreBaseDeletionManager):
167 def get_queryset(self):
168 parent=super(ControllerDeletionManager, self)
169
170 try:
171 backend_type = config.observer_backend_type
172 except AttributeError:
173 backend_type = None
174
175 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
176
177 if backend_type:
178 return parent_queryset.filter(Q(backend_type=backend_type))
179 else:
180 return parent_queryset
181
182 # deprecated in django 1.7 in favor of get_queryset().
183 def get_query_set(self):
184 return self.get_queryset()
185
186class ControllerLinkManager(PlCoreBaseManager):
187 def get_queryset(self):
188 parent=super(ControllerLinkManager, self)
189
190 try:
191 backend_type = config.observer_backend_type
192 except AttributeError:
193 backend_type = None
194
195 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
196
197 if backend_type:
198 return parent_queryset.filter(Q(controller__backend_type=backend_type))
199 else:
200 return parent_queryset
201
202 # deprecated in django 1.7 in favor of get_queryset().
203 def get_query_set(self):
204 return self.get_queryset()
205
206
207class ControllerManager(PlCoreBaseManager):
208 def get_queryset(self):
209 parent=super(ControllerManager, self)
210
211 try:
212 backend_type = config.observer_backend_type
213 except AttributeError:
214 backend_type = None
215
216 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
217
218 if backend_type:
219 return parent_queryset.filter(Q(backend_type=backend_type))
220 else:
221 return parent_queryset
222
223 # deprecated in django 1.7 in favor of get_queryset().
224 def get_query_set(self):
225 return self.get_queryset()
226