Merge "Unify code for scenario v0.2 v0.3"
[osm/RO.git] / vim_thread.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openvim
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24 '''
25 This is thread that interact with the host and the libvirt to manage VM
26 One thread will be launched per host
27 '''
28 __author__ = "Alfonso Tierno"
29 __date__ = "$10-feb-2017 12:07:15$"
30
31 import threading
32 import time
33 import Queue
34 import logging
35 import vimconn
36
37
38
39 # from logging import Logger
40 # import auxiliary_functions as af
41
42 # TODO: insert a logging system
43
44
45 class vim_thread(threading.Thread):
46
47 def __init__(self, vimconn, name=None):
48 '''Init a thread.
49 Arguments:
50 'id' number of thead
51 'name' name of thread
52 'host','user': host ip or name to manage and user
53 'db', 'db_lock': database class and lock to use it in exclusion
54 '''
55 threading.Thread.__init__(self)
56 self.vim = vimconn
57 if not name:
58 self.name = vimconn["id"] + "-" + vimconn["config"]["datacenter_tenant_id"]
59 else:
60 self.name = name
61
62 self.logger = logging.getLogger('openmano.vim.'+self.name)
63
64 self.queueLock = threading.Lock()
65 self.taskQueue = Queue.Queue(2000)
66
67
68 def insert_task(self, task, *aditional):
69 try:
70 self.queueLock.acquire()
71 task = self.taskQueue.put( (task,) + aditional, timeout=5)
72 self.queueLock.release()
73 return 1, None
74 except Queue.Full:
75 return -1, "timeout inserting a task over host " + self.name
76
77 def run(self):
78 self.logger.debug("Starting")
79 while True:
80 #TODO reload service
81 while True:
82 self.queueLock.acquire()
83 if not self.taskQueue.empty():
84 task = self.taskQueue.get()
85 else:
86 task = None
87 self.queueLock.release()
88
89 if task is None:
90 now=time.time()
91 time.sleep(1)
92 continue
93
94 if task[0] == 'instance':
95 pass
96 elif task[0] == 'image':
97 pass
98 elif task[0] == 'exit':
99 print self.name, ": processing task exit"
100 self.terminate()
101 return 0
102 elif task[0] == 'reload':
103 print self.name, ": processing task reload terminating and relaunching"
104 self.terminate()
105 break
106 elif task[0] == 'edit-iface':
107 pass
108 elif task[0] == 'restore-iface':
109 pass
110 elif task[0] == 'new-ovsbridge':
111 pass
112 elif task[0] == 'new-vxlan':
113 pass
114 elif task[0] == 'del-ovsbridge':
115 pass
116 elif task[0] == 'del-vxlan':
117 pass
118 elif task[0] == 'create-ovs-bridge-port':
119 pass
120 elif task[0] == 'del-ovs-port':
121 pass
122 else:
123 self.logger.error("unknown task %s", str(task))
124
125 self.logger.debug("Finishing")
126
127 def terminate(self):
128 pass
129