summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/tools/test/topos/obeliskHostCheck.py
blob: e928f89f26d51c6b270c39ea904bb0bac01882c4 (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
#!/usr/bin/python

import sys
import os
import json

# TODO: if none given, use OCI
try:
    onosIp = sys.argv[1]
    print "Reading hosts view from ONOS node " + onosIp + ":"
except Exception as e:
    print "Error reading ONOS IP arguement"
    print e
# Grab the json objects from ONOS
output = os.popen("onos " + onosIp + " \"hosts -j\"" )
hosts = json.loads( output.read() )
#hosts = json.loads( output.split( 'Logging in as karaf\n' )[1] )

hostAttachment = True
# FIXME: topo-HA/obelisk specific mappings:
# key is mac and value is dpid
mappings = {}
for i in range( 1, 29 ):  # hosts 1 through 28
    # set up correct variables:
    macId = "00:" * 5 + hex( i ).split( "0x" )[1].upper().zfill(2)
    if i == 1:
        deviceId = "1000".zfill(16)
    elif i == 2:
        deviceId = "2000".zfill(16)
    elif i == 3:
        deviceId = "3000".zfill(16)
    elif i == 4:
        deviceId = "3004".zfill(16)
    elif i == 5:
        deviceId = "5000".zfill(16)
    elif i == 6:
        deviceId = "6000".zfill(16)
    elif i == 7:
        deviceId = "6007".zfill(16)
    elif i >= 8 and i <= 17:
        dpid = '3' + str( i ).zfill( 3 )
        deviceId = dpid.zfill(16)
    elif i >= 18 and i <= 27:
        dpid = '6' + str( i ).zfill( 3 )
        deviceId = dpid.zfill(16)
    elif i == 28:
        deviceId = "2800".zfill(16)
    mappings[ macId ] = deviceId

if hosts or "Error" not in hosts:
    if hosts == []:
        print "WARNING: There are no hosts discovered"
    else:
        for host in hosts:
            mac = None
            location = None
            device = None
            port = None
            try:
                mac = host.get( 'mac' )
                assert mac, "mac field could not be found for this host object"

                location = host.get( 'location' )
                assert location, "location field could not be found for this host object"

                # Trim the protocol identifier off deviceId
                device = str( location.get( 'elementId' ) ).split(':')[1]
                assert device, "elementId field could not be found for this host location object"

                port = location.get( 'port' )
                assert port, "port field could not be found for this host location object"

                # Now check if this matches where they should be
                if mac and device and port:
                    if device != mappings[ str( mac ) ]:
                        print "The attachment device is incorrect for host " + str( mac ) +\
                              ". Expected: " + mappings[ str( mac ) ] + "; Actual: " + device
                        hostAttachment = False
                    if str( port ) != "1":
                        print "The attachment port is incorrect for host " + str( mac ) +\
                              ". Expected: 1; Actual: " + str( port)
                        hostAttachment = False
                else:
                    hostAttachment = False
            except AssertionError as e:
                print "ERROR: Json object not as expected:"
                print e
                print "host object: " + repr( host )
                hostAttachment = False
else:
    print "No hosts json output or \"Error\" in output. hosts = " + repr( hosts )