Statistics
| Branch: | Tag: | Revision:

root / iooclient / eos_ned.py @ eb4ed8c6

History | View | Annotate | Download (4.3 kB)

1
# Copyright 2012 Leonidas Poulopoulos
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#    http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

    
15
from eos import eos
16
from pyparsing import *
17

    
18
class eosNed(eos):
19
    '''
20
    Subclass of eos to connect to the NE subsystem
21
    '''
22
    
23
    def __init__(self, server=None, port=None, password=None):
24
        '''
25
        Constructor for the class.      
26
        
27
        @type server: String
28
        @param server: The Hostname or IPv4 address of the Alcatel NMS.
29
        
30
        @type port: Integer
31
        @param port: The TCP port on the Alcatel NMS we will connect to.
32
        
33
        @type password: String
34
        @param password: The password for this connection.
35
        
36
        @rtype: Handler
37
        @return: A connection handler of this class.
38
        '''        
39
        if port == None:
40
            port = 20006
41
        return eos.__init__(self, server, port, password)
42
    
43
    def list_ne_dir(self, parse=True):
44
        '''
45
        Function used to obtain a listing of of NE DATA from NMS. Depending on parse value the output is either raw data from NMS 
46
        or structured python data
47
        
48
        Usage:
49
            
50
            >>> from iooclient.eos_ned import *
51
            >>> ned = eosNed('1.2.3.4', 20006, 'password')
52
            >>> ned.list_ne_dir()
53
            [{'{'userLabel': 'elementName',
54
              'locationName': Athens,
55
              'notificationType': 'pmPointList',
56
              ...
57

58
        or:
59
            
60
            >>> from iooclient.eos_ned import *
61
            >>> with eosNed('1.2.3.4', 20006, 'password') as ned:
62
            >>>    ned.list_ne_dir()
63
            [{'{'userLabel': 'elementName',
64
              'locationName': Athens,
65
              'notificationType': 'pmPointList',
66
              ... 
67
                    
68
        @rtype: String or list of dictionaries
69
        @return: Network element data in raw text format or in structured format. False in case of errors.
70
        '''
71
        
72
        message = "LIST_CUR_NE_DIR_REQ[]"
73
        if self.send(message) == False:
74
            return False
75
        reply_header = self.get_part()
76
        if reply_header != "LIST_CUR_NE_DIR_CONF[]" :
77
            print "Error: Asked for %s but got unexpected output: %s\n" %(message, reply_header)
78
            return False
79
        reply = self.get()
80
        if reply == "DATA_END_NOTIF[]":
81
            return False
82
        if not parse:
83
            return reply
84
        ret = self.parseNeDir(reply)
85
        return ret
86
    
87
    def parseNeDir(self, data):
88
        ''' 
89
        Pyparsing nedir parser
90
        
91
        @type data: String
92
        @param data: Raw output from the NMS.
93
        
94
        @rtype: list of dicts
95
        @return: Transforms the NMS string into python list of dictionaries using the NMS delimiters
96
        '''
97
        escapechar = "\\"
98
        wordtext = CharsNotIn('\\*?^():"[]|=')
99
        escape = Suppress(escapechar) + (Word(printables, exact=1) | White(exact=1))
100
        wordvalues = Combine(OneOrMore(wordtext | escape | '=>'))
101
        rStart = Suppress('LIST_CUR_NE_DIR_CONF[]')
102
        rEnd = Suppress('DATA_END_NOTIF[]')
103
        response_body = Forward()
104
        response =  response_body + rEnd
105
        expr = Group(wordvalues + Suppress('=') + wordvalues)
106
        expr1 = Group( Suppress('NE_DIR_NOTIF') + Suppress('[')+Dict(delimitedList(expr, "|"))+Suppress(']'))
107
        expr2 = OneOrMore(expr1)
108
        response_body << expr2
109
        d = response.parseString(data)
110
        e = d.asList()
111
        return self.dictionarize(e)
112
    
113
    def dictionarize(self, list):
114
        '''
115
        Parses lists of lists of lists...etc into list of dictionaries of lists or dicts...etc
116
        
117
        @type list: List of lists of lists...
118
        @param list: Output from the pyparsing function
119
        
120
        @rtype: List
121
        @return: List of dicts of lists of dicts...etc
122
        '''
123
        l = []
124
        for item in list:
125
            l.append(dict(item))
126
        return l
127