1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Keywords used to connect to Honeycomb through Netconf, send messages
15 and receive replies."""
21 from robot.api import logger
23 from resources.libraries.python.honeycomb.HoneycombUtil import HoneycombError
26 class Netconf(object):
27 """Implements methods for creating and managing Netconf sessions."""
29 def __init__(self, delimiter=']]>]]>'):
32 Note: Passing the channel object as a robotframework argument closes
33 the channel. Class variables are used instead,
34 to persist the connection channel throughout test cases.
39 self.delimiter = delimiter
41 def create_session(self, node, hello, time_out=10):
42 """Create an SSH session, connect to Honeycomb on the specified node,
43 open a communication channel to the Netconf subsystem and exchange hello
46 :param node: Honeycomb node.
47 :param hello: Hello message and capability list to be sent to Honeycomb.
48 :param time_out: Timeout value for the connection in seconds.
55 client = paramiko.SSHClient()
56 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
58 client.connect(node['host'],
59 username=node['honeycomb']['user'],
60 password=node['honeycomb']['passwd'],
62 port=node['honeycomb']['netconf_port'],
65 logger.trace('Connect took {0} seconds'.format(time() - start))
66 logger.debug('New ssh: {0}'.format(client))
67 logger.debug('Connect peer: {0}'.
68 format(client.get_transport().getpeername()))
71 channel = client.get_transport().open_session()
72 channel.settimeout(time_out)
73 channel.set_combine_stderr(True)
75 channel.invoke_subsystem("netconf")
79 self.channel = channel
81 # read OpenDaylight's hello message and capability list
84 err="Timeout on getting hello message."
87 self.channel.send(hello)
88 if not self.channel.active:
89 raise HoneycombError("Channel closed on capabilities exchange.")
91 def get_response(self, size=4096, err="Unspecified Error."):
92 """Iteratively read data from the receive buffer and catenate together
93 until message ends with the message delimiter, or
94 until timeout is reached.
96 :param size: Maximum number of bytes to read in one iteration.
97 :param err: Error message to provide when timeout is reached.
100 :returns: Content of response.
102 :raises HoneycombError: If the read process times out.
108 while not reply.endswith(self.delimiter) or \
109 self.channel.recv_ready():
111 chunk = self.channel.recv(size)
115 if self.channel.exit_status_ready():
116 logger.debug('Channel exit status ready.')
118 except socket.timeout:
119 raise HoneycombError("Socket timeout.",
120 enable_logging=False)
123 raise HoneycombError(err + " Content of buffer: {0}".format(reply),
124 enable_logging=False)
127 return reply.replace(self.delimiter, "")
129 def get_all_responses(self, size=4096):
130 """Read responses from the receive buffer and catenate together
131 until a read operation times out.
133 :param size: Maximum number of bytes to read in one iteration.
135 :returns: Content of response.
140 err = "Expected timeout occurred."
144 response += self.get_response(size, err)
145 except HoneycombError:
150 def send(self, message, **params):
151 """Sends provided message through the channel.
153 :param message: Message to be sent to Honeycomb.
154 :param params: Format the message string with these parameters.
159 message = message.format(**params)
161 if not message.endswith(self.delimiter):
162 message += self.delimiter
164 logger.debug(message)
166 self.channel.send(message)