cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
network.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.cm.views.user.network
22 # @author Maciej Nabo┼╝ny <mn@mnabozny.pl>
23 #
24 # Functions for creating and deleting networks
25 #
26 
27 from cm.utils.decorators import user_log
28 from cm.utils import log
29 from cm.utils.exception import CMException
30 
31 from cm.models.available_network import AvailableNetwork
32 from cm.models.user_network import UserNetwork
33 from cm.models.user import User
34 from common.states import available_network_states
35 
36 
37 @user_log(log=True)
38 ##
39 #
40 # Tries to allocate network with specified mask for caller.
41 #
42 # @cmview_user
43 # @param_post{mask}
44 # @param_post{name}
45 #
46 # @response{dict} UserNetwork.dict property of the newly created UserNetwork
47 #
48 def request(caller_id, mask, name):
49  new_net = None
50  user = User.get(caller_id)
51  for available_network in AvailableNetwork.objects.all():
52  log.debug(user.id, "Trying to allocate in %s" % str(available_network.to_ipnetwork()))
53  if available_network.state == available_network_states['ok']:
54  try:
55  net = available_network.get_unused_ipnetwork(mask)
56 
57  new_net = UserNetwork()
58  new_net.address = str(net.network)
59  new_net.mask = mask
60  new_net.name = name
61  new_net.available_network = available_network
62  new_net.user = user
63  new_net.save()
64 
65  new_net.allocate()
66 
67  return new_net.dict
68  except:
69  continue
70  if new_net == None:
71  raise CMException('available_network_not_found')
72 
73 
74 @user_log(log=True)
75 ##
76 #
77 # When UserNetwork isn't needed anymore, it should be explicitly released
78 # by User. If UserNetwork is in use, exception is thrown. Released
79 # UserNetwork is deleted.
80 #
81 # @cmview_user
82 # @param_post{network_id}
83 #
84 def release(caller_id, network_id):
85  user = User.get(caller_id)
86  try:
87  user_network = UserNetwork.objects.filter(user=user).get(id=network_id)
88  except:
89  raise CMException('network_not_found')
90 
91  if user_network.is_in_use():
92  raise CMException('network_in_use')
93  user_network.release()
94  user_network.delete()
95 
96 
97 @user_log(log=True)
98 ##
99 #
100 # @cmview_user
101 # @response{list(dict)} AvailableNetwork.dict property for each AvailableNetwork
102 #
103 def list_available_networks(caller_id):
104  available_networks = AvailableNetwork.objects.filter(state=available_network_states['ok'])
105  response = []
106  for network in available_networks:
107  response.append(network.dict)
108  return response
109 
110 
111 @user_log(log=True)
112 ##
113 #
114 # @cmview_user
115 # @response{list(dict)} UserNetwork.dict property for each caller's
116 # UserNetwork
117 #
118 def list_user_networks(caller_id):
119  user = User.get(caller_id)
120  try:
121  user_networks = UserNetwork.objects.filter(user=user)
122  except:
123  raise CMException('network_not_found')
124  response = []
125  for network in user_networks:
126  if network.available_network.state == available_network_states['ok']:
127  response.append(network.dict)
128  return response
129 
130 
131 @user_log(log=True)
132 ##
133 #
134 # Returns all Leases in specified UserNetwork
135 #
136 # @cmview_user
137 # @param_post{network_id} id of the UserNetwork which Leases should be listed
138 # from
139 # @response{list(dict)} Lease.dict property for each Lease in specified UserNetwork
140 #
141 def list_leases(caller_id, network_id):
142  user = User.get(caller_id)
143  try:
144  user_network = UserNetwork.objects.filter(user=user).get(id=network_id)
145  except:
146  raise CMException('network_not_found')
147  response = []
148  for lease in user_network.lease_set.all():
149  response.append(lease.dict)
150  return response
151