summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/Service/interface.py
blob: 2ba917d2da2665927f2b1a085acec7cb3ee7fe4e (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
'''
Created on 08.08.2011

@author: steger, jozsef
'''
from Service.MonitoringService import MonitoringService
import logging

class InterfaceError(Exception):
    pass

#TODO: add and handle bindings at this level
class MSInterface(object):
    '''
    @summary: Implements a thin service layer on top of the MonitoringService instance 
    to collect methods that need to be exported and mapped in the NOVI API.
    It also provides a reference to the framework to be able to communicate with
    remote MonitoringService instances.  The log() method is a place holder 
    to sink information to be pushed in the NOVI UserFeedback service.
    The emit() method is a place holder to sink signals to be pushed in the NOVI
    Policy Service component installed on top of the same platform.
    '''

    def __init__(self, framework, reference, config_owl):
        '''
        Constructor
        @param framework: a service which provides getService() method to look up MonSrv instances of different reference
        @type framework: Framework   
        @param reference: the name of the platform
        @type reference: str
        @param config_owl: platform specific configuration model
        @type config_owl: str  
        '''
        self._framework = framework
        self.platform = reference
        self._ms = MonitoringService(self, config_owl)
        self.logger = logging.getLogger(name = "NOVI.MSI.%s" % reference)

    @property
    def service(self):
        '''
        @return: the underlying monitoring service component
        @rtype: MonitoringService
        '''
        return self._ms
    
    @property
    def proxy(self):
        '''
        @return: a proxy service to look up the rest of the monitoring service components
        @rtype: Framework
        '''
        return self._framework

    def dispatchID(self, identifier):
        '''
        @summary: this method finds the MonitoringService instance that is responsible for handling an identified Task or Aggregate
        @param identifier: identifier of a task or aggregate, it follows the form: <platform>:<process|aggregate>:<id>
        @type identifier: string
        @return: the monitoring service instance
        @rtype: MonitoringService 
        '''
        try:
            platform, _, _ = identifier.split(':')
            if self.platform == platform:
                return self.service
            return self.framework.getService(platform)
        except ValueError:
            raise InterfaceError("Wrong identifier format")

    def log(self, shortmsg, message):
        # overridden by the JAVA wrapper
        self.logger.info("[%s] %s" % (shortmsg, message))
    
    def emit(self, what):
        # overridden by the JAVA wrapper
        self.framework.getPolicyService(self.platform).trigger(what)

    # Test purpose function
    def echo(self, platform):
        '''
        @summary: An integration tester function (to be exported public)
        @param platform: name of the platform
        @type platform: string
        @return: messages of the platforms taking part in the message flow
        @rtype: string
        '''
        self.logger.info("[echo] calling %s" % platform)
        otherservice = self._framework.getInterface(platform).service
        return "%s -> %s" % (str(self.service), str(otherservice))


    # Substrate monitoring function
    def measure(self, credential, query):
        '''
        @summary: Method to handle substrate monitoring queries (to be exported public)
        @param credential: 
        @type credential:
        @param query: an owl document containing several BundleQuery instances
        @type query: string 
        @return: response to the query
        @rtype: string
        '''
        #TODO: split query and concatenate results
        return self.service.measure(credential, query)
    
    # Slice monitoring functions
    def sliceTasks(self, credential, query):
        raise InterfaceError("sliceTasks() method is not implemented")
    
    def addTask(self, credential, query):
        '''
        @summary: Method to start slice monitoring tasks (to be exported public)
        @param credential: 
        @type credential:
        @param query: an owl document containing several BundleQuery instances
        @type query: string 
        @return: process identifier
        @rtype: string
        '''
        #TODO: investigate if the service instance under this interface should be the boss
        return self.service.launchTasks(credential, query)
    
    def describeTaskData(self, credential, query):
        '''
        @summary: Method to retrieve meta data of task data (to be exported public)
        @param credential: 
        @type credential:
        @param query: 
        @type query: string 
        @return: serialize the header of the data tables
        @rtype: string
        '''
        taskID = query
        ms = self.dispatchID(identifier = taskID)
        #TODO: move this in the MonitoringService
        headers = map(lambda x: x.header(), ms.formatters[taskID])
        return "[%s]" % "\n,\n".join(headers)
    
    def fetchTaskData(self, credential, query):
        '''
        @summary: Method to retrieve task data collected since last fetch or the start (to be exported public)
        @param credential: 
        @type credential:
        @param query: 
        @type query: string 
        @return: serialize the appended content of the data tables
        @rtype: string
        '''
        taskID = query
        ms = self.dispatchID(identifier = taskID)
        #TODO: move this in the MonitoringService
        response = []
        try:
            for f in ms.formatters[taskID]:
                response.append( f.serialize() )
        except Exception, e:
            print "EEE", e
            pass
        return "[%s]" % "\n,\n".join(response)
    
    def modifyTask(self, credential, query):
        raise InterfaceError("modifyTask() method is not implemented")
    
    def removeTask(self, credential, query):
        '''
        @summary: Method to remove a slice measurement task (to be exported public)
        @param credential: 
        @type credential:
        @param query: 
        @type query: string 
        '''
        taskID = query
        ms = self.dispatchID(identifier = taskID)
        #TODO: move this in the MonitoringService
        try:
            subtaskids = ms.subtaskIDs.pop(taskID)
            ms.formatters.pop(taskID)
            while len(subtaskids):
                subtaskid = subtaskids.pop()
                ms.delTask(taskidentifier = subtaskid)
        except KeyError:
            # the taskID does not belong to me
            pass

    def enableTask(self, credential, query):
        '''
        @summary: Method to enable a slice measurement task (to be exported public)
        @param credential: 
        @type credential:
        @param query: 
        @type query: string 
        '''
        taskID = query
        ms = self.dispatchID(identifier = taskID)
        try:
            for subtaskid in ms.subtaskIDs[taskID]:
                t = ms.getTask(taskidentifier = subtaskid)
                t.enable()
        except KeyError:
            # the taskID does not belong to me
            pass

    def disableTask(self, credential, query):
        '''
        @summary: Method to disable a slice measurement task temporarily (to be exported public)
        @param credential: 
        @type credential:
        @param query: 
        @type query: string 
        '''
        taskID = query
        ms = self.dispatchID(identifier = taskID)
        try:
            for subtaskid in ms.subtaskIDs[taskID]:
                t = ms.getTask(taskidentifier = subtaskid)
                t.disable()
        except KeyError:
            # the taskID does not belong to me
            pass
    
    def getTaskStatus(self, credential, query):
        '''
        @summary: Method to check the state of a slice measurement task (to be exported public)
        @param credential: 
        @type credential:
        @param query: 
        @type query: string 
        @return: True if the tasks are running
        @rtype: boolean
        '''
        taskID = query
        ms = self.dispatchID(identifier = taskID)
        try:
            for subtaskid in ms.subtaskIDs[taskID]:
                t = ms.getTask(taskidentifier = subtaskid)
                if t.state == t.STATE_RUNNING:
                    return True
        except KeyError:
            # the taskID does not belong to me
            pass
        return False
    
    def addAggregator(self, credential, query):
        '''
        @summary: Method to define new data manipulation on slice monitoring data (to be exported public)
        @param credential: 
        @type credential:
        @param query: an owl document containing several SampleManipulationQuery instances
        @type query: string 
        @return: aggregator identifier
        @rtype: string
        '''
        #TODO: investigate if the service instance under this interface should be the boss
        return self.service.attachAggregators(credential, query)
    
    def removeAggregator(self, credential, query):
        '''
        @summary: Method to remove data manipulation on slice monitoring data (to be exported public)
        @param credential: 
        @type credential:
        @param query: 
        @type query: string 
        '''
        aggregatorID = query
        ms = self.dispatchID(identifier = aggregatorID)
        try:
            aggregatorids = ms.aggregatorIDs.pop(aggregatorID)
            ms.formatters.pop(aggregatorID)
            while len(aggregatorids):
                aggregatorid = aggregatorids.pop()
                ms.delAggregator(aggregatorid)
        except KeyError:
            # the aggregatorID does not belong to me
            pass
    
    def fetchAggregatorData(self, credential, query):
        '''
        @summary: Method to refresh and serialize results of data manipulation on slice monitoring data (to be exported public)
        @param credential: 
        @type credential:
        @param query: 
        @type query: string 
        @return: result of aggregators
        @rtype: string
        '''
        aggregatorID = query
        ms = self.dispatchID(identifier = aggregatorID)
        response = []
        try:
            for f in ms.formatters[aggregatorID]:
                f.source.process()
                print "ALMA", f.source
                print "ALMA", f.source.source
                print "ALMA", f.source.data
                response.append( f.serialize() )
        except Exception, e:
            print "EEE", e
            pass
        return "[%s]" % "\n,\n".join(response)
    
    def addWatchdog(self, credential, query):
        '''
        @summary: 
        @param credential: 
        @type credential:
        @param query: an owl document containing several SampleManipulationQuery instances
        @type query: string 
        @return: watchdog identifier
        @rtype: string
        '''
        #TODO: investigate if the service instance under this interface should be the boss
        return self.service.attachWatchdogs(credential, query)
    
    def removeCondition(self, credential, query):
        '''
        @summary: Method to remove conditions bound to slice monitoring data (to be exported public)
        @param credential: 
        @type credential:
        @param query: 
        @type query: string 
        '''
        watchdogID = query
        ms = self.dispatchID(identifier = watchdogID)
        try:
            watchdogids = ms.watchdogIDs.pop(watchdogID)
            while len(watchdogids):
                watchdogid = watchdogids.pop()
                ms.delWatchdog(watchdogid)
        except KeyError:
            # the aggregatorID does not belong to me
            pass

    def checkCondition(self, credential, query):
        '''
        @summary: Method to examine a conditions bound to slice monitoring data (to be exported public)
        @param credential: 
        @type credential:
        @param query: 
        @type query: string 
        '''
        watchdogID = query
        ms = self.dispatchID(identifier = watchdogID)
        return ms.checkWatchdog(watchdogID)