summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/Service/mock_framework.py
blob: 8a8874bc36f12d0dca9929ae175bf9e77db3e73b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
'''
Created on Nov 20, 2012

@author: steger
'''
from Service.interface import MSInterface
import logging
from logging.handlers import TimedRotatingFileHandler
from os import path, unlink
from flask import Flask, request
from threading import Thread
from Example.Platforms import federation
from select import select
from sys import stdin
import httplib2
from os import urandom

#FIXME: DIRTY!
from Example.credentials import novisaCredentialIARGS
import traceback
import sys

class FrameworkError(Exception):
    pass

class PolicyMock(object):
    def __init__(self):
        fn = "/tmp/ps.log"
        if path.exists(fn):
            unlink(fn)
        hdlr = TimedRotatingFileHandler(filename = fn)
        self.logger = logging.getLogger("NOVI.PS")
        self.logger.setLevel(level = logging.DEBUG)
        self.logger.addHandler(hdlr = hdlr)

    def trigger(self, what):
        self.logger.info(what)

class Framework(object):
    '''
    This class mimics the integration framework. It helps to look up remote Monitoring Service instances
    '''

    def __init__(self):
        '''
        Constructor
        '''
        self._if = {}
        self._pol = PolicyMock()
    
    def add(self, platform, config_owl):
        fn = "/tmp/ms_%s.log" % platform
        if path.exists(fn):
            unlink(fn)
        hdlr = TimedRotatingFileHandler(filename = fn)
        l = logging.getLogger("NOVI.MS.%s" % platform)
        l.setLevel(level = logging.DEBUG)
        l.addHandler(hdlr = hdlr)
        l = logging.getLogger("NOVI.MSI.%s" % platform)
        l.setLevel(level = logging.DEBUG)
        l.addHandler(hdlr = hdlr)
        iface = MSInterface(self, platform, config_owl)
        self._if[platform] = iface
        return iface
    
    def getInterface(self, platform):
        try:
            return self._if[platform]
        except:
            print "EE: %s platform not found" % platform
            raise FrameworkError

    def getPolicyService(self, platform):
        return self._pol
        
    def serviceList(self):
        return self._if.values()

def app_launch(plif, port):
    app = Flask(__name__)
    app.secret_key = urandom(24)
    t = Thread(target = run, args = (app, port))
    t.start()

    # these are here for test and control
    @app.route("/", methods = ['GET'])
    def hello():
        return "Hello world"

    @app.route('/shutdown', methods = ['POST'])
    def shutdown():
        shutdown_server()
        return 'Server shutting down...'


    # these are the real service interfaces
    @app.route("/echo", methods = ['POST'])
    def echo():
        platform = request.form['platform']
        try:
            return plif.echo(platform)
        except Exception, e:
            print "-"*60
            traceback.print_exc(file=sys.stdout)
            print "="*60
            return "Error %s" % e

    @app.route("/measure", methods = ['POST'])
    def measure():
        q = request.form['query']
        try:
            return plif.measure(credential = [novisaCredentialIARGS], query = q)
        except Exception, e:
            print "-"*60
            traceback.print_exc(file=sys.stdout)
            print "="*60
            return "Error %s" % e

    @app.route("/addTask", methods = ['POST'])
    def addTask():
        try:
            q = request.form['query']
            return plif.addTask(credential = [novisaCredentialIARGS], query = q)
        except Exception, e:
            print "-"*60
            traceback.print_exc(file=sys.stdout)
            print "-"*60
            return "Error %s" % e
                    
    @app.route("/fetchTaskData", methods = ['POST'])
    def fetchTaskData():
        q = request.form['query']
        return plif.fetchTaskData(credential = [novisaCredentialIARGS], query = q)

    @app.route("/removeTask", methods = ['POST'])
    def removeTask():
        try:
            q = request.form['query']
            plif.removeTask(credential = [novisaCredentialIARGS], query = q)
        except Exception, e:
            print "Error %s" % e
        return "OK"    
            
    @app.route("/describeTaskData", methods = ['POST'])
    def describeTaskData():
        q = request.form['query']
        return plif.describeTaskData(credential = [novisaCredentialIARGS], query = q)

    @app.route("/getTaskStatus", methods = ['POST'])
    def getTaskStatus():
        try:
            q = request.form['query']
            return str( plif.getTaskStatus(credential = [novisaCredentialIARGS], query = q) )
        except Exception, e:
            print "-"*60
            traceback.print_exc(file=sys.stdout)
            print "-"*60
            return "Error %s" % e

    @app.route("/enableTask", methods = ['POST'])
    def enableTask():
        q = request.form['query']
        plif.enableTask(credential = [novisaCredentialIARGS], query = q)
        return "OK"

    @app.route("/disableTask", methods = ['POST'])
    def disableTask():
        q = request.form['query']
        plif.disableTask(credential = [novisaCredentialIARGS], query = q)
        return "OK"

    @app.route("/addAggregator", methods = ['POST'])
    def addAggregator():
        try:
            q = request.form['query']
            return plif.addAggregator(credential = [novisaCredentialIARGS], query = q)
        except Exception, e:
            print "-"*60
            traceback.print_exc(file=sys.stdout)
            print "-"*60
            return "Error %s" % e
                    
    @app.route("/fetchAggregatorData", methods = ['POST'])
    def fetchAggregatorData():
        q = request.form['query']
        return plif.fetchAggregatorData(credential = [novisaCredentialIARGS], query = q)

    @app.route("/removeAggregator", methods = ['POST'])
    def removeAggregator():
        try:
            q = request.form['query']
            plif.removeAggregator(credential = [novisaCredentialIARGS], query = q)
        except Exception, e:
            print "Error %s" % e
        return "OK"    

    @app.route("/addCondition", methods = ['POST'])
    def addCondition():
        try:
            q = request.form['query']
            return plif.addWatchdog(credential = [novisaCredentialIARGS], query = q)
        except Exception, e:
            print "-"*60
            traceback.print_exc(file=sys.stdout)
            print "-"*60
            return "Error %s" % e

    @app.route("/removeCondition", methods = ['POST'])
    def removeCondition():
        try:
            q = request.form['query']
            plif.removeCondition(credential = [novisaCredentialIARGS], query = q)
        except Exception, e:
            print "Error %s" % e
        return "OK"    

    @app.route("/checkCondition", methods = ['POST'])
    def checkCondition():
        try:
            q = request.form['query']
            return plif.checkCondition(credential = [novisaCredentialIARGS], query = q)
        except Exception, e:
            print "-"*60
            traceback.print_exc(file=sys.stdout)
            print "-"*60
            return "Error %s" % e
    


    return t


def run(app, port):
    app.run(debug = False, port = port)

def shutdown_server():
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug Server')
    func()

def emit(port):
    try:
        h = httplib2.Http(timeout = 10)
        url = "http://localhost:%d/shutdown" % port
        resp, _ = h.request(uri = url, method = "POST")
        if resp.status != 200:
            print "Service responded with status %s" % resp.status
    except Exception, e:
        print "Error contacting server @ localhost:%d: (%s)" % (port, e)

def start_servers():
    fw = Framework()
    t = []
    for platform, (port, config_owl) in federation.iteritems():
        plif = fw.add(platform, config_owl)
        t.append( app_launch(plif, port) )
    return fw, t

def stop_servers(t):
    # POST the shutdown methods
    for port, _ in federation.values():
        emit(port)
        
    # join threads
    while len(t):
        st = t.pop()
        st.join()
        
if __name__ == "__main__":
    print "INIT"
    # start services as separate threads
    _, t = start_servers()
    
    # wait for a keyboard interrupt
    print "PRESS ^C to stop"
    while True:
        try:
            select([stdin],[],[])
        except KeyboardInterrupt:
            break
    
    stop_servers(t)
    print "OK"