summaryrefslogtreecommitdiffstats
path: root/Monitoring/src/main/python/Service/MonSrvImpl.py
blob: 36da1cc376f329db34fbb6a805251b96a5ddf85a (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
'''
Created on 08.08.2011

@author: Sandor Laki
'''

from __future__ import with_statement
from rdflib import Graph
from StringIO import StringIO
from Service.interface import MSInterface
from eu.novi.monitoring import MonDiscoveryImpl
import sys
from Util.MonitoringQueryImpl import MonitoringQueryImpl
from threading import Lock
from org.slf4j import Logger
from org.slf4j import LoggerFactory
from os import path, access, R_OK
#import eu.novi.feedback.event.ReportEvent
import traceback
import java.lang.StackOverflowError
import java.lang.Error

try:
    import site
    site.addsitedir('../site-packages')
except ImportError, e:
    sys.stderr.write("[EXCEPTION] import Site -> %s\n" % e)



try:
    from eu.novi.monitoring import MonSrv
except ImportError:
    MonSrv = object

try:
    from eu.novi.monitoring import Wiring
except ImportError:
    Wiring = object

try:
    from eu.novi.im.core import Resource
except ImportError, e:
    sys.stderr.write("[EXCEPTION] Resource -> %s\n" % e)
    Resource = None


class MonSrvImpl(MonSrv,Wiring):
    testbed = "Undefined"
    userFeedback = None
    lock = Lock()

    log = LoggerFactory.getLogger("eu.novi.monitoring.MonSrv")

    def __init__(self):
        #self.testbed = "Unknown"
        self._msi = None
        self.framework = MonDiscoveryImpl()
        self.log.info("MonSrvImpl has started... Testbed=%s" % self.testbed)

    def createQuery(self):
        return MonitoringQueryImpl(self.getMSI()._ms)

    def getMSI(self):
        print "getMSI %s" % self.getTestbed()
        self.log.info("getMSI %s" % self.getTestbed())
        tbname = self.getTestbed()
        with self.lock:
            if self._msi is None:
               baseurl = ""
               config_owl = "config_%s.owl" % (tbname.lower())
               self.log.info("Testbed specific configuration: %s" % config_owl)
               #config_owl = "config_planetlab.owl"
               try:
                   self._msi = MSInterface(self.framework, self.getTestbed(), baseurl, config_owl)
               except:
                   self.log.info("Error occured at %s" % config_owl)
                   config_owl = "config_planetlab.owl"
                   self._msi = MSInterface(self.framework, self.getTestbed(), baseurl, config_owl)
               self.log.info("MSInterface has been instanciated... Testbed=%s" % self.getTestbed() )
        return self._msi


    def setPolicy(self, policy):
        self.policy = policy

    def getPolicy(self):
        return self.policy

    def getPlatform(self):
        return self.testbed

    def setResource(self, resource):
        self.resource = resource

    def getResource(self):
        return self.resource

    def getTestbed(self):
        return self.testbed

    def setTestbed(self, testbed):
        self.testbed = testbed

    def getUserFeedback(self):
        return self.userFeedback

    def setUserFeedback(self, userFeedback):
        self.userFeedback = userFeedback

   # 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
        '''
        return self.getMSI().echo(platform)

    def extractCredential(self, credential):
        cred = []
        if credential.getType()=="UsernamePassword": cred=[{'username' : credential.username, 'password' : credential.password}]
        elif credential.getType()=="UsernameRSAKey": cred=[{'username' : credential.username, 'password' : credential.password, 'rsakey' : credential.RSAKey}]
        else: return "Error - unknown credential...."

        # Hardcoded credential - TODO: FIX IT ASAP!!!
        PATH="/home/novi/apache-servicemix-4.4.1-fuse-01-06/instances/system-tests/etc/root_planetlab_rsa"
 
        try:
            #PATH="/home/novi/apache-servicemix-4.4.1-fuse-01-06/instances/system-tests/etc/sfademo_key"
            if path.exists(PATH) and path.isfile(PATH) and access(PATH, R_OK):
                cred=[{'username' : "root", 'password' : "", 'rsakey' : PATH}]
                self.log.info("root path exists and readable")
        except:
            self.log.info("root key cannot be accessed at %s" % PATH)
            if not path.exists(PATH):
                self.log.info("path doesn't exists")
            if not path.isfile(PATH):
                self.log.info("path is not a file")
            if not access(PATH, R_OK):
                self.log.info("file cannot be accessed, permission issue?")
            #pass
        cred.append({'username':'monitor1','password':'m/n.t,r1'}) # G3 Access
        return cred


    # 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
        '''
        cred = self.extractCredential( credential )
        self.log.info("New substrate monitoring query has arrived: %s" % query)
        try:
            print "Call measure"
            #TODO: split query and concatenate results
            return self.getMSI().measure(cred, query)
        except Exception, e:
            self.log.info("Exception %s %s" % (e, traceback.format_exc()))
        except java.lang.StackOverflowError, se:
            se.printStackTrace()
            self.log.info("unknown %s" % se.toString())
        except java.lang.Error, er:
            er.printStackTrace()
        return "[]"

    def substrateFB(self, credential, query, sessionID):
        try:
            self.getUserFeedback().instantInfo(sessionID, "MS", "A substrate monitoring task has been submitted.", "http://fp7-novi.eu");
        except:
            self.log.info("Feedback thrown an exception")
        return self.measure(credential, query)
    
    def substrate(self, credential, query):
        return self.measure(credential, query)

    # Slice monitoring functions
    def sliceTasks(self, credential, query):
        return "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
        cred = self.extractCredential( credential )
        return self.getMSI().launchTasks(cred, 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
        '''
        cred = self.extractCredential( credential )
        return self.getMSI().describeTaskData(cred, query)
        
   
    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
        '''
        cred = self.extractCredential( credential )
        return self.getMSI().fetchTaskData(cred, query)
   
    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 
        '''
        cred = self.extractCredential( credential )
        return self.getMSI().removeTask(cred, query)
 
    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 
        '''
        cred = self.extractCredential( credential )
        return self.getMSI().enableTask(cred, query)
 
    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 
        '''
        cred = self.extractCredential( credential )
        return self.getMSI().disableTask(cred, query)
   
    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
        '''
        cred = self.extractCredential( credential )
        return self.getMSI().getTaskStatus(cred, query)
   
    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
        cred = self.extractCredential( credential )
        return self.getMSI().addAggregator(cred, 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 
        '''
        cred = self.extractCredential( credential )
        return self.getMSI().removeAggregator(cred, query)
   
    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
        '''
        cred = self.extractCredential( credential )
        return self.getMSI().fetchAggregatorData(cred, query)
   
    def addCondition(self, credential, query):
        raise InterfaceError("addCondition() method is not implemented")
    
    def modifyCondition(self, credential, query):
        raise InterfaceError("modifyCondition() method is not implemented")
    
    def removeCondition(self, credential, query):
        raise InterfaceError("removeCondition() method is not implemented")