summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/Semantics/test.py
blob: 8784e448b774d1476c13f085352389279fe8b9bc (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
'''
Created on Aug 10, 2011

@author: steger
'''
import unittest
from rdflib import Graph
from Example.Metrics import RoundTripDelay
from Example.Tools import sonomashortping
from DataProcessing.Parameter import ParameterList, Parameter
from Example.credentials import ple_credentials, fed_credentials
from DataProcessing.DataHeaderCell import CellRequestByName
from DataProcessing.DataError import SamplerError
from DataProcessing.DataReader import DataReader
from Service.mock_framework import Framework
from Example.Platforms import federation
from Semantics.FeatureModel import FeatureModel

fw = Framework()
for platform, (_, config_owl) in federation.iteritems():
    plif = fw.add(platform, config_owl)

class Test(unittest.TestCase):

    def setUp(self):
        self.MS_planetlab = fw.getInterface(platform = 'PlanetLab').service 
        self.MS_federica = fw.getInterface(platform = 'FEDERICA').service

        self.substrate_pl = self.MS_planetlab.ontology.ns('task')['Substrate']
        self.slice_pl = self.MS_planetlab.ontology.ns('task')['Slice']
        dim_ipaddress = self.MS_planetlab.dm['IPAddress']
        node = ("157.181.175.243", self.MS_planetlab.um.ipv4dotted)
        self.p_src_eltenode = Parameter(name = "SourceAddress", valuetype = str, unitmanager = self.MS_planetlab.um, dimension = dim_ipaddress, default = node)
        node = ("147.102.22.66", self.MS_planetlab.um.ipv4dotted)
        self.p_dst_ntuanode = Parameter(name = "DestinationAddress", valuetype = str, unitmanager = self.MS_planetlab.um, dimension = dim_ipaddress, default = node)
        
        self.substrate_fed = self.MS_federica.ontology.ns('task')['Substrate']
        self.slice_fed = self.MS_federica.ontology.ns('task')['Slice']
        dim_ipaddress = self.MS_federica.dm['IPAddress']
        node = ("194.132.52.2", self.MS_federica.um.ipv4dotted)     #sw01.erl.de
        self.p_src_fednode = Parameter(name = "SourceAddress", valuetype = str, unitmanager = self.MS_federica.um, dimension = dim_ipaddress, default = node)
        node = ("194.132.52.4", self.MS_federica.um.ipv4dotted)     #sw01.poz.pl
        self.p_dst_fednode = Parameter(name = "DestinationAddress", valuetype = str, unitmanager = self.MS_federica.um, dimension = dim_ipaddress, default = node)
        
        self.substrate_pl = self.MS_planetlab.ontology.ns('task')['Substrate']
        self.slice_pl = self.MS_planetlab.ontology.ns('task')['Slice']
        
        self.feat_task_pl = {
              'OnewayDelay': (['SONoMAChirp'], []),
              'RoundtripDelay': (['SONoMAPing'], ['sshpingSlice']), 
              'AvailableBandwidth': ([], ['sshabSlice']),
              'AvailableMemory': (['sshMeminfo'], ['sshMeminfoSlice']),
              'FreeMemory': (['sshMeminfo'], ['sshMeminfoSlice']),
              'MemoryUtilization': (['sshMeminfo'], ['sshMeminfoSlice']),
              'Uptime': (['sshuptime'], ['sshuptimeSlice']),
              'CPULoad': (['sshcpuload'], ['sshcpuloadSlice']),
              #'CPUCores': (['sshcpuload'], ['sshcpuloadSlice']),
              'CPUCores': (['sshcpuinfo2'], ['sshcpuinfo2Slice']),
              'CPUSockets': (['sshcpuinfo2'], ['sshcpuinfo2Slice']),
              'CPUUtilization': (['sshcpuload'], ['sshcpuloadSlice']),
              'FreeDiskSpace': (['sshdiskinfo'], ['sshdiskinfoSlice']),
              'UsedDiskSpace': (['sshdiskinfo'], ['sshdiskinfoSlice']),
              'DiskUtilization': (['sshdiskinfo'], ['sshdiskinfoSlice']),
              'LinkUtilization': ([], []),
        }

        self.feat_task_fed = {
#FIXME: 3 items?
#              'OnewayDelay': (['hadesaggregates', 'hadesaggregates', 'hadesaggregates'], []),
#              'RoundtripDelay': ([], []), 
#              'AvailableBandwidth': ([], []),
#              'AvailableMemory': (['sshMeminfo'], []),
#              'FreeMemory': (['sshMeminfo'], []),
              'MemoryUtilization': (['G3 memory utilization look up'], []),
#              'Uptime': (['sshuptime'], []),
#              'CPULoad': (['sshcpuload'], []),
              #'CPUCores': ([], []),
#              'CPUCores': (['sshcpuload'], []),
#              'CPUSockets': ([], []),
              'CPUUtilization': (['G3 CPU utilization look up'], []),
#              'FreeDiskSpace': (['sshdiskinfo'], []),
#              'UsedDiskSpace': (['sshdiskinfo'], []),
#              'DiskUtilization': ([], []),
              'LinkUtilization': (['G3 link utilization look up'], []),
        }
        
        dim_nameofsomething = self.MS_planetlab.dm['NameOfSomething']
        self.slicename = Parameter(name = "SliceName", valuetype = str, 
                                   unitmanager = self.MS_planetlab.um, dimension = dim_nameofsomething, 
                                   default = ('novi_novi', self.MS_planetlab.um.unitless))
        dim_countable = self.MS_planetlab.dm['Countable']
        self.count = Parameter(name = 'Count', valuetype = int, 
                               unitmanager = self.MS_planetlab.um, dimension = dim_countable, 
                               default = (5, self.MS_planetlab.um.piece))
        self.fm = FeatureModel(self.MS_planetlab.dm, self.MS_planetlab.um, self.MS_planetlab.ontology)
        
    def test_managers(self):
        expect = 14
        infer = len(self.MS_planetlab.pm)
        self.assertEqual(infer, expect, "Prefix: got %d expect %d" % (infer, expect))

        expect = 10
        infer = [ s for _, s in self.MS_planetlab.unitmodel.inferBaseUnits() ]
        self.assertEqual(expect, len(infer), "BaseUnit: expect %d, got %d\n%s" % (expect, len(infer), str(infer)))

        expect = 1
        infer = [ d for _, d, _ in self.MS_planetlab.unitmodel.inferProductUnits() ]
        self.assertEqual(expect, len(infer), "ProductUnit: expect %d, got %d\n%s" % (expect, len(infer), str(infer)))

        expect = 1
        infer = [ d for _, d, _, _ in self.MS_planetlab.unitmodel.inferPowerUnits() ]
        self.assertEqual(expect, len(infer), "PowerUnit: expect %d, got %d\n%s" % (expect, len(infer), str(infer)))

        expect = 12
        infer = [ d for _, d, _, _, _ in self.MS_planetlab.unitmodel.inferLinearTransformedUnits() ]
        self.assertEqual(expect, len(infer), "LinearTransformedUnit: expect %d, got %d\n%s" % (expect, len(infer), str(infer)))

        expect = 2
        infer = [ d for _, d, _, _, _ in self.MS_planetlab.unitmodel.inferRegexpTransformedUnits() ]
        self.assertEqual(expect, len(infer), "RegexpTransformedUnit: expect %d, got %d\n%s" % (expect, len(infer), str(infer)))

        expect = 8
        infer = [ d for d, _, _ in self.MS_planetlab.unitmodel.inferBaseDimensions() ]
        self.assertEqual(expect, len(infer), "BaseDimension: expect %d, got %d\n%s" % (expect, len(infer), str(infer)))

        expect = 1
        infer = [ d for d, _, _ in self.MS_planetlab.unitmodel.inferDifferenceDimensions() ]
        self.assertEqual(expect, len(infer), "DifferenceDimension: expect %d, got %d\n%s" % (expect, len(infer), str(infer)))

        expect = 1
        infer = [ d for d, _, _, _ in self.MS_planetlab.unitmodel.inferPowerDimensions() ]
        self.assertEqual(expect, len(infer), "PowerDimension: expect %d, got %d\n%s" % (expect, len(infer), str(infer)))

        expect = 1
        infer = [ d for d, _, _ in self.MS_planetlab.unitmodel.inferProductDimensions() ]
        self.assertEqual(expect, len(infer), "ProductDimension: expect %d, got %d\n%s" % (expect, len(infer), str(infer)))

        expect = 4
        infer = [ d for d, _, _ in self.MS_planetlab.unitmodel.inferRatioDimensions() ]
        self.assertEqual(expect, len(infer), "RatioDimension: expect %d, got %d\n%s" % (expect, len(infer), str(infer)))


        NS = self.MS_planetlab.ontology.ns('unit')
        for expect, uri in [(4, NS['second']), (7, NS['Byte']), (3, NS['bit']), (1, NS['unixtimestamp'])]:
            infer = [s for s in self.MS_planetlab.unitmodel.inferPossiblePrefixesOf(uri)]
            self.assertEqual(expect, len(infer), "inferPossiblePrefixesOf: expect %d, got %d\n%s" % (expect, len(infer), str(infer)))



    def test_IM_domainsfeatures(self):
        fm = self.fm
        
        expect = set(['Slice', 'Substrate'])
        infer = set([ self.MS_planetlab.ontology._tail(x) for x in fm.inferDomains() ])
        self.assertEqual(expect, infer, "inferDomains: expect %d, got %d\n%s" % (len(expect), len(infer), str(infer)))
        
        expect = len(self.feat_task_pl)
        infer = [ x for x in fm.inferFeatures()]
        self.assertEqual(expect, len(infer), "inferFeatures (PL): expect %d, got %d\n%s" % (expect, len(infer), str(infer)))

    def test_IM_featparameters(self):
        feature = self.MS_planetlab.ontology.ns('feature')['RoundtripDelay']
        expect = RoundTripDelay.p_obligatory
        infer = self.fm.inferObligatoryParametersOf(feature)
        self.assertEqual(len(expect), len(infer), "obligatory parameters for %s differ expect: %s got: %s" % (feature, expect.parameter_names(), infer.parameter_names()))
        for k, p in expect.parameter.iteritems():
            inf_v = infer.parameter[k].value
            exp_v = p.value
            inf_v = (inf_v[0], str(inf_v[1]))
            exp_v = (exp_v[0], str(exp_v[1]))
            self.assertEqual(inf_v, exp_v, "Parameter value differ %s expect:\n%s\ngot:\n%s" % (k, exp_v, inf_v))

        
    def test_IM_task(self):
        for feat, (t_subst, t_slice) in self.feat_task_pl.iteritems():
            feature = self.MS_planetlab.ontology.ns('feature')[feat]
            infer_t_subst = [ name for _, name in self.MS_planetlab.taskmodel.inferTasks(self.substrate_pl, feature)]
            infer_t_slice = [ name for _, name in self.MS_planetlab.taskmodel.inferTasks(self.slice_pl, feature)]
            self.assertEqual(infer_t_subst, t_subst, "(PL) feature: %s searchtask (substrate): expect %s, got %s" % (feat, t_subst, infer_t_subst))
            self.assertEqual(infer_t_slice, t_slice, "(PL) feature: %s searchtask (slice): expect %s, got %s" % (feat, t_slice, infer_t_slice))

        for feat, (t_subst, t_slice) in self.feat_task_fed.iteritems():
            feature = self.MS_federica.ontology.ns('feature')[feat]
            infer_t_subst = [ name for _, name in self.MS_federica.taskmodel.inferTasks(self.substrate_fed, feature)]
            infer_t_slice = [ name for _, name in self.MS_federica.taskmodel.inferTasks(self.slice_fed, feature)]
            self.assertEqual(infer_t_subst, t_subst, "(FED) feature: %s searchtask (substrate): expect %s, got %s" % (feat, t_subst, infer_t_subst))
            self.assertEqual(infer_t_slice, t_slice, "(FED) feature: %s searchtask (slice): expect %s, got %s" % (feat, t_slice, infer_t_slice))


        task = self.MS_planetlab.ontology.ns('config')['T_SONoMAPing']
        infer = self.MS_planetlab.taskmodel.inferCredentialOf(task)
        expect = set(sonomashortping.authtype)
        self.assertEqual(infer, expect, "credentials differ expect: %s got: %s" % (expect, infer))

        infer = self.MS_planetlab.taskmodel.inferDriverOf(task)
        expect = sonomashortping.driver
        self.assertEqual(infer, expect, "drivers differ expect: %s got: %s" % (expect, infer))

        infer = self.MS_planetlab.taskmodel.inferHookparametersOf(task)
        expect = sonomashortping.kwargs
        self.assertEqual(infer, expect, "hook parameters differ expect: %s got: %s" % (expect, infer))

        H = self.MS_planetlab.taskmodel.inferHookdefinitionsOf(task)
        for k, h in H.iteritems():
            exp = sonomashortping.hooks[k].strip()
            h = h.strip()
            self.assertEqual(h, exp, "%s hook differs\nexpect:\n%s\ngot:\n%s" % (k, exp, h))

        #TODO: check feature equality
        infer = [ (c.name, str(c._unit), str(c._dimension)) for c in self.MS_planetlab.taskmodel.inferDataheaderOf(task) ]
        expect = [ (c.name, str(c._unit), str(c._dimension)) for c in sonomashortping.dataheaderdeclaration ]
        self.assertEqual(infer, expect, "output header declarations differ expect:\n%s\ngot:\n%s" % (expect, infer))

        infer = self.MS_planetlab.taskmodel.inferParametersOf(task)
        expect = sonomashortping.parameters
        n_inf, n_exp = set(infer.parameter_names()), set(expect.parameter_names())
        self.assertEqual(n_inf, n_exp, "runtime parameters differ expect: %s got: %s" %(n_exp, n_inf))
        for k, p in expect.parameter.iteritems():
            inf_v = infer.parameter[k].value
            exp_v = p.value
            if exp_v is None:
                self.assertFalse(inf_v, "Expected uninitialized value, got %s" % inf_v)
            else:
                inf_v = (inf_v[0], str(inf_v[1]))
                exp_v = (exp_v[0], str(exp_v[1]))
                self.assertEqual(inf_v, exp_v, "Parameter value differ %s expect:\n%s\ngot:\n%s" % (k, exp_v, inf_v))


    def test_taskBYuri(self):
        # PlanetLab
        cases = {
             'T_SSHPingSlice': [self.p_src_eltenode, self.slicename, self.count, self.p_dst_ntuanode], 
             'T_SSHMemInfo': [self.p_src_eltenode], 
             'T_SSHMemInfoSlice': [self.p_src_eltenode, self.slicename], 
             'T_SSHCPULoad': [self.p_src_eltenode], 
             'T_SSHCPULoadSlice': [self.p_src_eltenode, self.slicename], 
             'T_SSHUptime': [self.p_src_eltenode], 
             'T_SSHUptimeSlice': [self.p_src_eltenode, self.slicename],
             'T_SSHDiskInfo': [self.p_src_eltenode],
             'T_SSHDiskInfoSlice': [self.p_src_eltenode, self.slicename],
             'T_SSHCPUInfo': [self.p_src_eltenode],
             'T_SSHCPUInfoSlice': [self.p_src_eltenode, self.slicename],
#             'T_SONoMAPing': [self.p_src_eltenode, self.p_dst_ntuanode],
        }
        for l,p in cases.iteritems():
            task_uri = self.MS_planetlab.ontology.ns('config')[l]
            print 1, task_uri
            _, task = self.MS_planetlab.newTask(task = task_uri, 
                                 cred = ple_credentials, 
                                 resource = None, 
                                 parameters = ParameterList(p))
#            print task_uri, ParameterList(p)
            task.enable()
            task.dataAdded.wait( 15 )
            self.assertGreater(len(task.data), 0, "measurement %s yielded empty result" % l) 
#            print task.data._rawrecords

    def test_taskBYuri2(self):
        # FEDERICA
        cases = {
#             'T_SSHPingSlice': [self.p_src_eltenode, self.slicename, self.count, self.p_dst_ntuanode], 
#             'T_SSHMemInfo': [self.p_src_eltenode], 
#             'T_SSHMemInfoSlice': [self.p_src_eltenode, self.slicename], 
#             'T_SSHCPULoad': [self.p_src_eltenode], 
#             'T_SSHCPULoadSlice': [self.p_src_eltenode, self.slicename], 
#             'T_SSHUptime': [self.p_src_eltenode], 
#             'T_SSHUptimeSlice': [self.p_src_eltenode, self.slicename],
#             'T_SSHDiskInfo': [self.p_src_eltenode],
#             'T_SSHDiskInfoSlice': [self.p_src_eltenode, self.slicename],
#             'T_SSHCPUInfo': [self.p_src_eltenode],
#             'T_SSHCPUInfoSlice': [self.p_src_eltenode, self.slicename],
#             'T_SONoMAPing': [self.p_src_eltenode, self.p_dst_ntuanode],
#             'T_hadesaggregate': [self.p_src_fednode, self.p_dst_fednode],
             'T_G3linkutil': [self.p_src_fednode, self.p_dst_fednode],
             'T_G3CPUutil': [self.p_src_fednode],
             'T_G3MEMutil': [self.p_src_fednode],
        }
        for l,p in cases.iteritems():
            task_uri = self.MS_federica.ontology.ns('config')[l]
            _, task = self.MS_federica.newTask(task = task_uri, 
                                 cred = fed_credentials, 
                                 resource = None, 
                                 parameters = ParameterList(p))
#            print task_uri, ParameterList(p)
            task.enable()
            task.dataAdded.wait( 15 )
            self.assertGreater(len(task.data), 0, "measurement %s yielded empty result" % l) 
#            print task.data._data._rawrecords


    def test_owlexamples(self):
        doc = "../../information-model/monitoring-model/monitoringQuery_example.owl"
        g = Graph()
        g.parse(source = doc)
        qdict = self.MS_planetlab.QI.inferBundleQueries(qgraph = g)
        self.assertTrue(len(qdict), "Got empty query")
        for q in qdict:
            domain = self.MS_planetlab.ontology.ns('task')['Substrate']
            taskgen = self.MS_planetlab.taskmodel.inferTasks(domain, q.feature)
            #we are ugly here: use the first tool
            for task_uri, _ in taskgen: break
            _, task = self.MS_planetlab.newTask(task = task_uri, 
                                 cred = ple_credentials, 
                                 resource = q.resource[1],
                                 parameters = q.paramlist)
            del task.strategy   # make sure STRAT_ONDEMAND
            task.enable()
            task.dataAdded.wait( 15 )
            task.dataAdded.clear()
            if q.samplechain:
                flow = []
                for skeleton, parlist in q.samplechain:
                    flow.append((skeleton, parlist.formkeyvaldict()))
                _, A = self.MS_planetlab.am.newAggregator(task.datasource, CellRequestByName(name = 'Free Memory'), flow)
                while True:
                    try:
                        s, a = A.data._rawrecords[0]
                        self.assertEqual(s, len(task.data), "inconsistency in length len(data)=%d, max of %d samples?" % (len(task.data), s))
                        R = DataReader(datasource = task.datasource)
                        R.extract(cellrequest = [CellRequestByName(name = 'Free Memory')])
                        expect = max( [ float(x) for x, in R ] )
                        self.assertEqual(expect, a, "inconsistency in aggregare %f <> %f" % (expect, a))
                        break
                    except SamplerError:
                        print "MEASURE SOME MORE ..."
                        task.disable()
                        task.enable()
                        task.dataAdded.wait( 15 )
                        task.dataAdded.clear()
            self.assertGreater(len(task.data), 0, "measurement yielded empty result") 
            
            

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_IM_domainsfeatures']
    unittest.main()