def trace_request(element, profile, path_spec, timeout): """ Trace request allows applications to gather network device information along a path without addressing each device directly. @param element: NetworkElement class @type element: {NetworkElement<onep.element.NetworkElement>} @param profile: mtrace.ProfileType of trace @type profile: {ProfileType<onep.mediatrace.mtrace.ProfileType>} @param path_spec: PathSpecifier class @type path_spec: {PathSpecifier<onep.mediatrace.mtrace.PathSpecifier>} @param timeout: Timeout value of trace @type timeout: C{int} """ if not ProfileType._is_valid(profile): raise OnepIllegalArgumentException('Invalid profile type ' + str(profile)) if not isinstance(path_spec, PathSpecifier): raise OnepIllegalArgumentException('Invalid path spec ' + str(path_spec)) if timeout < 0: raise OnepIllegalArgumentException('Invalid timeout ' + str(timeout)) try: client = PathTraceIDL.Client(element.api_protocol) ptreq_idl = PathTraceRequestIDL(profile, timeout, path_spec._to_idl()) graph_idl = client.PathTrace_executeRequestIDL( element.session_handle._id, ptreq_idl) return MediatraceRoute(graph_idl) except ExceptionIDL as e: return OnepRemoteProcedureException(e)
def _get_entry(file_path, host): if not file_path: raise OnepIllegalArgumentException('file_path', file_path) if not host: raise OnepIllegalArgumentException('host', host) f = None line = None ret = None error = False _LOCK.acquire() try: f = open(file_path, 'rb') if _fcntl_exists: fcntl.lockf(f, fcntl.LOCK_SH) for line in f: if ret is not None: continue if line.startswith('#'): continue if line.startswith('<'): continue content = line.partition('#')[0] tokens = content.translate(_TRANSTABLE).split() if len(tokens) != 3: error = True continue if host.lower() == tokens[0].lower(): ret = tuple(tokens) finally: if f: f.close() _LOCK.release() if not error and (not line or _CLOSING_SENTINEL != line.rstrip()): error = True if error: _LOGGER.error( "One or more errors occurred while processing file '%s'. The file is possibly corrupted.", file_path) return ret
def __init__(self, type, element): """ Class PolicyCapabilities. @param type: PolicyCapabilitiesType for type of policy @type type: {PolicyCapabilitiesType<onep.policyservice.caps.PolicyCapabilitiesType>} @param element: NetworkElement instance. @type element: {NetworkElement<onep.element.NetworkElement>} @raise OnepIllegalArgumentException: If any of the constructor argument is invalid. """ if not PolicyCapabilitiesType._is_valid(type): raise OnepIllegalArgumentException('invalid policy type') self.policy_type = type self.network_element = element self.session_id = element.session_handle._id self.client = Client(element.api_protocol) self.table = {'type': None, 'actions': [], 'matches': []} self.table['matches'] = self.matches = [] self.table['actions'] = self.actions = [] self.table_classes = [] try: self.all = self.client.Policy_getGlobalCapIDL(self.session_id) except ExceptionIDL as e: raise OnepRemoteProcedureException(e) type_supported = False for tbl in self.all.tables: if tbl.type == type: self.table['type'] = type self.table['actions'] = self.actions = _translate_action_enums( tbl) self.table['matches'] = self.matches = _translate_match_enums( tbl) pt = PolicyTable(tbl) pt.actions = self.actions pt.matches = self.matches self.table_classes.append(pt) break if not self.table['type']: raise OnepNotSupportedException( 'Policy type ' + PolicyCapabilitiesType.enumval(type))
def get_param(self, parameter): """ This function is used to retrieve the particular Interface Statistic. The Interface Statistic is specified by using L{InterfaceStatisticsParameter<interfaces.InterfaceStatistics.InterfaceStatistics.InterfaceStatisticsParameter>} @param parameter: The specific InterfaceStatistics parameter which is required. @type parameter: L{InterfaceStatisticsParameter<interfaces.InterfaceStatistics.InterfaceStatistics.InterfaceStatisticsParameter>} @return: The value of Interface Statistic @rtype: C{int} """ if not self.InterfaceStatisticsParameter._is_valid(parameter): raise OnepIllegalArgumentException(parameter, 'Invalid Value') try: param = self.stats_list[parameter] except IndexError: raise OnepNotSupportedException( '%s is not supported.' % self.InterfaceStatisticsParameter.enumval(parameter)) if param.retcode == OnepStatus.ONEP_OK: return param.stats raise OnepException('Error', ExceptionIDL(param.retcode))
def __init__(self, src_ip, src_port, dst_ip, dst_port, protocol): """ Constructor @ivar src_ip: Valid IPv4 or IPv6 source address @type src_ip: C{str} @ivar src_port: Layer 4 source port number <0 - 65535> @type src_port: C{int} @ivar dst_ip: Valid IPv4 or IPv6 destination address @type dst_ip: C{str} @ivar dst_port: Layer 4 destination port number <0 - 65535> @type src_port: C{int} @ivar protocol: enum mtrace.Protocol - TCP or UDP @type protocol: {Protocol<onep.mediatrace.mtrace.Protocol>} """ self.src_ip = _validate_ip(src_ip) self.src_port = _validate_port(src_port) self.dst_ip = _validate_ip(dst_ip) self.dst_port = _validate_port(dst_port) if not Protocol._is_valid(protocol): raise OnepIllegalArgumentException('Invalid protocol type ' + str(protocol)) self.protocol = protocol
def __init__(self, host='localhost', port=9090, validate=True, keyfile=None, certfile=None, ca_certs=None, unix_socket=None, pinning_file=None, unverified_handler=None, accept_once=[]): if not isinstance(accept_once, list): raise OnepIllegalArgumentException( 'accept_once must be a list but it', str(accept_once)) self.log = logging.getLogger(__name__) if validate and accept_once: validate = False if validate and ca_certs is None: validate = False self.ssl_exception = 'No CA certificates were given so the network element could not be verified.' else: self.ssl_exception = None TSSLSocket.__init__(self, host, port, validate, ca_certs, unix_socket) self.certfile = certfile self.keyfile = keyfile self.pinning_file = pinning_file self.unverified_handler = unverified_handler self.accept_once = accept_once if validate: if certfile and not os.access(certfile, os.R_OK): raise IOError( 'Client Certificate certfile file "%s" is not readable, cannot validate SSL certificates.' % certfile) if keyfile and not os.access(keyfile, os.R_OK): raise IOError( 'Client keyfile file "%s" is not readable, cannot validate SSL certificates.' % keyfile)
def _validate_port(port_num): if port_num < 0 or port_num > 65535: raise OnepIllegalArgumentException('Invalid port number ' + str(port_num)) return port_num
def _validate_ip(ip_addr): if not HostIpCheck(ip_addr).is_ipaddress(): raise OnepIllegalArgumentException('Invalid IP address ' + str(ip_addr)) return ip_addr
def _update_entry(file_path, entry): if not file_path: raise OnepIllegalArgumentException('file_path', file_path) if not isinstance(entry, (tuple, list)) or len(entry) != 3: raise OnepIllegalArgumentException('entry', entry) fd = -1 f = None fcopy = None line = None error = False _LOCK.acquire() try: try: fd = os.open(file_path, os.O_RDWR | os.O_CREAT | os.O_EXCL, 420) except OSError as e: if e.errno == errno.EEXIST: f = open(file_path, 'r+b') else: raise e else: f = os.fdopen(fd, 'r+b') if _fcntl_exists: fcntl.lockf(f, fcntl.LOCK_EX) try: fdcopy = os.open(file_path + '.tmp', os.O_RDWR | os.O_CREAT | os.O_EXCL, 384) except OSError as e: if e.errno == errno.EEXIST: fcopy = open(file_path + '.tmp', 'r+b') for line in fcopy: pass if line and _CLOSING_SENTINEL == line.rstrip(): f.truncate(0) fcopy.seek(0) f.writelines(fcopy.readlines()) _LOGGER.warn( "The file '%s' was recovered from a working copy.", file_path) else: _LOGGER.error( "A working copy of the file '%s' was found but could not be recovered.", file_path) fcopy.truncate(0) fcopy.seek(0) else: raise e else: fcopy = os.fdopen(fdcopy, 'r+b') fcopy.write(_VERSION + os.linesep) past_first_line = False line = None for line in f: if line.startswith('#'): if not past_first_line: continue elif line.startswith('<'): continue else: content = line.partition('#')[0] tokens = content.translate(_TRANSTABLE).split() if len(tokens) == 3: if tokens[0].lower() == entry[0].lower(): continue else: error = True fcopy.write(line.rstrip()) fcopy.write(os.linesep) past_first_line = True fcopy.write('%s,%s,%s' % entry) fcopy.write(os.linesep) fcopy.write(_CLOSING_SENTINEL) fcopy.write(os.linesep) fcopy.flush() os.fsync(fcopy.fileno()) f.truncate(0) f.seek(0) fcopy.seek(0) f.writelines(fcopy.readlines()) finally: if f: f.close() if fcopy: fcopy.close() os.remove(file_path + '.tmp') _LOCK.release() if not error and fd == -1 and not line or _CLOSING_SENTINEL != line.rstrip( ): error = True if error: _LOGGER.error( "One or more errors occurred while processing file '%s'. The file is possibly corrupted.", file_path)