1 # Copyright 2012 Leonidas Poulopoulos
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from pyparsing import *
20 Subclass of eos to connect to the NE subsystem
23 def __init__(self, server=None, port=None, password=None):
25 Constructor for the class.
28 @param server: The Hostname or IPv4 address of the Alcatel NMS.
31 @param port: The TCP port on the Alcatel NMS we will connect to.
33 @type password: String
34 @param password: The password for this connection.
37 @return: A connection handler of this class.
41 return eos.__init__(self, server, port, password)
43 def list_ne_dir(self, parse=True):
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
50 >>> ned = eosNed('1.2.3.4', 20006, 'password')
52 [{'{'userLabel': 'elementName',
53 'locationName': Athens,
54 'notificationType': 'pmPointList',
59 >>> with eosNed('1.2.3.4', 20006, 'password') as ned:
61 [{'{'userLabel': 'elementName',
62 'locationName': Athens,
63 'notificationType': 'pmPointList',
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.
70 message = "LIST_CUR_NE_DIR_REQ[]"
71 if self.send(message) == 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)
80 ret = self.parseNeDir(reply)
83 def parseNeDir(self, data):
85 Pyparsing nedir parser
88 @param data: Raw output from the NMS.
91 @return: Transforms the NMS string into python list of dictionaries using the NMS delimiters
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)
107 return self.dictionarize(e)
109 def dictionarize(self, list):
111 Parses lists of lists of lists...etc into list of dictionaries of lists or dicts...etc
113 @type list: List of lists of lists...
114 @param list: Output from the pyparsing function
117 @return: List of dicts of lists of dicts...etc