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