def test_forward_packet_in_role_none(self): packet_in = self.gen_packet_in() generic_obj = self.gen_generic_obj() device = self.gen_device() of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) of_proto_handler.forward_packet_in(packet_in)
def test_handle_meter_mod_request_role_master(self): generic_obj = self.gen_generic_obj() device = self.gen_device() of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) of_proto_handler.role = ofp.OFPCR_ROLE_MASTER of_proto_handler.handle_meter_mod_request(generic_obj)
def test_handle_flow_mod_request_role_salve(self): generic_obj = self.gen_generic_obj() device = self.gen_device() of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) of_proto_handler.role = ofp.OFPCR_ROLE_SLAVE with self.assertRaises(Exception) as context: of_proto_handler.handle_flow_mod_request(generic_obj) print context.exception self.assertTrue('\'function\' object has no attribute \'send\'' in str(context.exception))
def test_handle_flow_mod_request_role_salve(self): generic_obj = self.gen_generic_obj() device = self.gen_device() of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) of_proto_handler.role = ofp.OFPCR_ROLE_SLAVE with self.assertRaises(Exception) as context: of_proto_handler.handle_flow_mod_request(generic_obj) print context.exception self.assertTrue('\'function\' object has no attribute \'send\'' in context.exception)
def test_forward_packet_in_role_master(self): packet_in = self.gen_packet_in() generic_obj = self.gen_generic_obj() device = self.gen_device() of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) of_proto_handler.role = ofp.OFPCR_ROLE_MASTER with self.assertRaises(Exception) as context: of_proto_handler.forward_packet_in(packet_in) print context.exception self.assertTrue('\'function\' object has no attribute \'send\'' in str(context.exception))
def test_handle_role_request(self): generic_obj = self.gen_generic_obj() req = self.gen_role_req() device = self.gen_device() of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) with self.assertRaises(Exception) as context: of_proto_handler.handle_role_request(req) self.assertEqual(of_proto_handler.role,req.role) print context.exception self.assertTrue('\'function\' object has no attribute \'generation_is_defined\'' in str(context.exception))
def test_forward_packet_in_role_master(self): packet_in = self.gen_packet_in() generic_obj = self.gen_generic_obj() device = self.gen_device() of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) of_proto_handler.role = ofp.OFPCR_ROLE_MASTER with self.assertRaises(Exception) as context: of_proto_handler.forward_packet_in(packet_in) print context.exception self.assertTrue('\'function\' object has no attribute \'send\'' in context.exception)
def test_handle_role_request(self): generic_obj = self.gen_generic_obj() req = self.gen_role_req() device = self.gen_device() of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) with self.assertRaises(Exception) as context: of_proto_handler.handle_role_request(req) self.assertEqual(of_proto_handler.role,req.role) print context.exception self.assertTrue('\'function\' object has no attribute \'send\'' in context.exception)
def test_handle_meter_features_request_in_role_master(self): generic_obj = self.gen_generic_obj() device = self.gen_device() of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) of_proto_handler.role = ofp.OFPCR_ROLE_MASTER with self.assertRaises(Exception) as context: of_proto_handler.handle_meter_features_request(generic_obj) print context.exception self.assertTrue('\'function\' object has no attribute \'send\'' in str( context.exception))
class Agent(protocol.ClientFactory): generation_is_defined = False cached_generation_id = None def __init__(self, controller_endpoint, datapath_id, device_id, rpc_stub, conn_retry_interval=1): self.controller_endpoint = controller_endpoint self.datapath_id = datapath_id self.device_id = device_id self.rpc_stub = rpc_stub self.retry_interval = conn_retry_interval self.running = False self.connector = None # will be a Connector instance once connected self.d_disconnected = None # a deferred to signal reconnect loop when # TCP connection is lost self.connected = False self.exiting = False self.proto_handler = None def get_device_id(self): return self.device_id def start(self): log.debug('starting') if self.running: return self.running = True reactor.callLater(0, self.keep_connected) log.info('started') return self def stop(self): log.debug('stopping') self.connected = False self.exiting = True self.connector.disconnect() log.info('stopped') def resolve_endpoint(self, endpoint): # enable optional resolution via consul; # see https://jira.opencord.org/browse/CORD-820 host, port = endpoint.split(':', 2) return host, int(port) @inlineCallbacks def keep_connected(self): """Keep reconnecting to the controller""" while not self.exiting: host, port = self.resolve_endpoint(self.controller_endpoint) log.info('connecting', host=host, port=port) try: with open("/ofagent/pki/voltha.key") as keyFile: with open("/ofagent/pki/voltha.crt") as certFile: clientCert = ssl.PrivateCertificate.loadPEM( keyFile.read() + certFile.read()) ctx = clientCert.options() self.connector = reactor.connectSSL(host, port, self, ctx) except Exception as e: log.exception('failed-to-connect', reason=e) self.d_disconnected = Deferred() yield self.d_disconnected log.debug('reconnect', after_delay=self.retry_interval) yield asleep(self.retry_interval) def enter_disconnected(self, event, reason): """Internally signal entering disconnected state""" self.connected = False if not self.exiting: log.error(event, reason=reason) self.d_disconnected.callback(None) def enter_connected(self): """Handle transitioning from disconnected to connected state""" log.info('connected') self.connected = True self.read_buffer = None reactor.callLater(0, self.proto_handler.start) # protocol.ClientFactory methods def protocol(self): cxn = OpenFlowConnection(self) # Low level message handler self.proto_handler = OpenFlowProtocolHandler( self.datapath_id, self.device_id, self, cxn, self.rpc_stub) return cxn def clientConnectionFailed(self, connector, reason): self.enter_disconnected('connection-failed', reason) def clientConnectionLost(self, connector, reason): if not self.exiting: log.error('client-connection-lost', reason=reason, connector=connector) def forward_packet_in(self, ofp_packet_in): if self.proto_handler is not None: self.proto_handler.forward_packet_in(ofp_packet_in) def forward_change_event(self, event): # assert isinstance(event, ChangeEvent) log.info('got-change-event', change_event=event) if event.HasField("port_status"): if self.proto_handler is not None: self.proto_handler.forward_port_status(event.port_status) else: log.error('unknown-change-event', change_event=event)
def protocol(self): cxn = OpenFlowConnection(self) # Low level message handler self.proto_handler = OpenFlowProtocolHandler( self.datapath_id, self.device_id, self, cxn, self.rpc_stub) return cxn
def test_handle_flow_mod_request_role_master(self): generic_obj = self.gen_generic_obj() device = self.gen_device() of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) of_proto_handler.role = ofp.OFPCR_ROLE_MASTER of_proto_handler.handle_flow_mod_request(generic_obj)
class Agent(protocol.ClientFactory): generation_is_defined = False cached_generation_id = None def __init__(self, controller_endpoint, datapath_id, device_id, rpc_stub, enable_tls=False, key_file=None, cert_file=None, conn_retry_interval=1): self.controller_endpoint = controller_endpoint self.datapath_id = datapath_id self.device_id = device_id self.rpc_stub = rpc_stub self.enable_tls = enable_tls self.key_file = key_file self.cert_file = cert_file self.retry_interval = conn_retry_interval self.running = False self.connector = None # will be a Connector instance once connected self.d_disconnected = None # a deferred to signal reconnect loop when # TCP connection is lost self.connected = False self.exiting = False self.proto_handler = None def get_device_id(self): return self.device_id def start(self): log.debug('starting') if self.running: return self.running = True reactor.callLater(0, self.keep_connected) log.info('started') return self def stop(self): log.debug('stopping') self.connected = False self.exiting = True self.connector.disconnect() log.info('stopped') def resolve_endpoint(self, endpoint): # enable optional resolution via consul; # see https://jira.opencord.org/browse/CORD-820 host, port = endpoint.split(':', 2) return host, int(port) @inlineCallbacks def keep_connected(self): """Keep reconnecting to the controller""" while not self.exiting: host, port = self.resolve_endpoint(self.controller_endpoint) log.info('connecting', host=host, port=port) if self.enable_tls: try: # Check that key_file and cert_file is provided and # the files exist if self.key_file is None or \ self.cert_file is None or \ not os.path.isfile(self.key_file) or \ not os.path.isfile(self.cert_file): raise Exception('key_file "{}" or cert_file "{}"' ' is not found'. format(self.key_file, self.cert_file)) with open(self.key_file) as keyFile: with open(self.cert_file) as certFile: clientCert = ssl.PrivateCertificate.loadPEM( keyFile.read() + certFile.read()) ctx = clientCert.options() self.connector = reactor.connectSSL(host, port, self, ctx) log.info('tls-enabled') except Exception as e: log.exception('failed-to-connect', reason=e) else: self.connector = reactor.connectTCP(host, port, self) log.info('tls-disabled') self.d_disconnected = Deferred() yield self.d_disconnected log.debug('reconnect', after_delay=self.retry_interval) yield asleep(self.retry_interval) def enter_disconnected(self, event, reason): """Internally signal entering disconnected state""" self.connected = False if not self.exiting: log.error(event, reason=reason) self.d_disconnected.callback(None) def enter_connected(self): """Handle transitioning from disconnected to connected state""" log.info('connected') self.connected = True self.read_buffer = None reactor.callLater(0, self.proto_handler.start) # protocol.ClientFactory methods def protocol(self): cxn = OpenFlowConnection(self) # Low level message handler self.proto_handler = OpenFlowProtocolHandler( self.datapath_id, self.device_id, self, cxn, self.rpc_stub) return cxn def clientConnectionFailed(self, connector, reason): self.enter_disconnected('connection-failed', reason) def clientConnectionLost(self, connector, reason): if not self.exiting: log.error('client-connection-lost', reason=reason, connector=connector) def forward_packet_in(self, ofp_packet_in): if self.proto_handler is not None: self.proto_handler.forward_packet_in(ofp_packet_in) def forward_change_event(self, event): # assert isinstance(event, ChangeEvent) log.info('got-change-event', change_event=event) if event.HasField("port_status"): if self.proto_handler is not None: self.proto_handler.forward_port_status(event.port_status) else: log.error('unknown-change-event', change_event=event)