Skip to content

Commit

Permalink
Merge "Add TTL to ifOperStatus cache"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and opencontrail-ci-admin committed Apr 22, 2015
2 parents 992ae5b + 8586f6b commit 0ea8029
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,67 +51,75 @@ def __init__(self, config):
self.last = set()
self._sem = None
self._config.set_cb(self.notify)
self._mnt = MaxNinTtime(self._sleep_time, self._sleep_time)
# self._fast_scan_freq)
self._mnt = MaxNinTtime(3, self._sleep_time)
self._state = 'full_scan' # replace it w/ fsm
self._factor = 10 # replace it w/ fsm
self._curr_idx = 0 # replace it w/ fsm
self._if_data = None # replace it w/ fsm
self._if_ccnt = 0 # replace it w/ fsm
self._if_ccnt_max = 3 # replace it w/ fsm

def _make_if_cdata(self, data):
if_cdata = {}
t = time.time()
for dev in data:
if 'snmp' in data[dev]:
if 'ifMib' in data[dev]['snmp']:
if 'ifTable' in data[dev]['snmp']['ifMib']:
if_cdata[dev] = dict(map(lambda x: (
x['ifIndex'], x['ifOperStatus']), filter(
lambda x: 'ifOperStatus' in x and \
'ifDescr' in x, data[dev]['snmp'][
'ifMib']['ifTable'])))
x['ifIndex'], (x['ifOperStatus'], t)),
filter(lambda x: 'ifOperStatus' in x\
and 'ifDescr' in x, data[dev][
'snmp']['ifMib']['ifTable'])))
elif 'ifOperStatus' in data[dev]['snmp']:
if_cdata[dev] = data[dev]['snmp']['ifOperStatus']
if_cdata[dev] = {k:(v, t) for k, v in data[dev]['snmp'][
'ifOperStatus'].items()}
return if_cdata

def _set_status(self, _dict, dev, intf, val):
if dev not in _dict:
_dict[dev] = {}
_dict[dev][intf] = val

def _check_and_update_ttl(self, up2down):
t = time.time()
expry = 3 * self._fast_scan_freq
for dev in self._if_data:
for intf in self._if_data[dev]:
if self._if_data[dev][intf][0] == 1:
if t - self._if_data[dev][intf][1] > expry:
self._set_status(up2down, dev, intf, 7) #no resp

def _get_if_changes(self, if_cdata):
down2up, up2down, others = {}, {}, {}
t = time.time()
for dev in if_cdata:
if dev in self._if_data:
for intf in if_cdata[dev]:
if intf in self._if_data[dev]:
if if_cdata[dev][intf] != self._if_data[dev][intf]:
if self._if_data[dev][intf] == 1:
if if_cdata[dev][intf][0] != self._if_data[dev][
intf][0]:
if self._if_data[dev][intf][0] == 1:
self._set_status(up2down, dev, intf,
if_cdata[dev][intf])
elif if_cdata[dev][intf] == 1:
if_cdata[dev][intf][0])
elif if_cdata[dev][intf][0] == 1:
self._set_status(down2up, dev, intf,
if_cdata[dev][intf])
if_cdata[dev][intf][0])
else:
self._set_status(others, dev, intf,
if_cdata[dev][intf])
if_cdata[dev][intf][0])
self._if_data[dev][intf] = if_cdata[dev][intf]
else:
self._if_data[dev] = if_cdata[dev]
for intf in self._if_data[dev]:
if self._if_data[dev][intf] == 1:
if self._if_data[dev][intf][0] == 1:
self._set_status(down2up, dev, intf,
if_cdata[dev][intf])
if_cdata[dev][intf][0])
else:
self._set_status(others, dev, intf,
if_cdata[dev][intf])
if_cdata[dev][intf][0])
return down2up, up2down, others


def _chk_if_change(self, data):
if_cdata = self._make_if_cdata(data)
down2up, up2down, others = self._get_if_changes(if_cdata)
self._check_and_update_ttl(up2down)
self._logger.debug('@chk_if_change: down2up(%s), up2down(%s), ' \
'others(%s)' % (', '.join(down2up.keys()),
', '.join(up2down.keys()),
Expand Down Expand Up @@ -211,7 +219,7 @@ def _del_uves(self, l):

def do_work(self, i, devices):
self._logger.debug('@do_work(%d):started (%d)...' % (i, len(devices)))
sleep_time = self._sleep_time / self._factor
sleep_time = self._fast_scan_freq
if devices:
with self._sem:
self._work_set = devices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def send_ifstatus_update(self, data):
ifIndexOperStatusTable = []
for ifidx in data[dev]:
ifIndexOperStatusTable.append(IfIndexOperStatusTable(
ifIndex=ifidx, ifOperStatus=data[dev][ifidx]))
ifIndex=ifidx, ifOperStatus=data[dev][ifidx][0]))
PRouterUVE(data=PRouterEntry(
name=dev,
ifIndexOperStatusTable=ifIndexOperStatusTable)).send()
Expand Down
33 changes: 0 additions & 33 deletions src/analytics/contrail-topology/contrail_topology/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import argparse, os, ConfigParser, sys, re
from pysandesh.sandesh_base import *
from pysandesh.gen_py.sandesh.ttypes import SandeshLevel
import discoveryclient.client as client

class CfgParser(object):
CONF_DEFAULT_PATH = '/etc/contrail/contrail-topology.conf'
def __init__(self, argv):
self._args = None
self.__pat = None
self._argv = argv or ' '.join(sys.argv[1:])
self._disc = None

def parse(self):
'''
Expand All @@ -25,7 +23,6 @@ def parse(self):
[--use_syslog] [--syslog_facility SYSLOG_FACILITY]
[--scan_frequency SCAN_FREQUENCY]
[--http_server_port HTTP_SERVER_PORT]
[--api_serever API_SEREVER]
optional arguments:
-h, --help show this help message and exit
Expand Down Expand Up @@ -75,10 +72,6 @@ def parse(self):
'http_server_port': 5921,
'zookeeper' : '127.0.0.1:2181',
}
disc_opts = {
'disc_server_ip' : None,
'disc_server_port' : 5998,
}

config = None
if args.conf_file:
Expand All @@ -87,8 +80,6 @@ def parse(self):
config.read(args.conf_file)
if 'DEFAULTS' in config.sections():
defaults.update(dict(config.items("DEFAULTS")))
if 'DISCOVERY' in config.sections():
disc_opts.update(dict(config.items('DISCOVERY')))
# Override with CLI options
# Don't surpress add_help here so it will handle -h
parser = argparse.ArgumentParser(
Expand All @@ -99,7 +90,6 @@ def parse(self):
# Don't mess with format of description
formatter_class=argparse.RawDescriptionHelpFormatter,
)
defaults.update(disc_opts)
parser.set_defaults(**defaults)
parser.add_argument("--analytics_api",
help="List of analytics-api IP addresses in ip:port format",
Expand Down Expand Up @@ -127,12 +117,6 @@ def parse(self):
help="Time between snmp poll")
parser.add_argument("--http_server_port", type=int,
help="introspect server port")
parser.add_argument("--api_serever",
help="ip:port of api-server for snmp credentials")
parser.add_argument("--disc_server_ip",
help="Discovery Server IP address")
parser.add_argument("--disc_server_port", type=int,
help="Discovery Server port")
parser.add_argument("--zookeeper",
help="ip:port of zookeeper server")
self._args = parser.parse_args(remaining_argv)
Expand All @@ -143,31 +127,14 @@ def parse(self):

self._args.config_sections = config

def disc_svr(self, name):
if self._disc is None:
self._disc = client.DiscoveryClient(*self.discovery_params(name))
return self._disc

def _pat(self):
if self.__pat is None:
self.__pat = re.compile(', *| +')
return self.__pat

def discovery_params(self, name):
if self._args.disc_server_ip:
ip, port = self._args.disc_server_ip, \
self._args.disc_server_port
else:
ip, port = '127.0.0.1', self._args.disc_server_port
return ip, port, name

def _mklist(self, s):
return self._pat().split(s)

def discovery(self):
return {'server':self._args.discovery_server,
'port':self._args.discovery_port}

def collectors(self):
return self._args.collectors

Expand Down

0 comments on commit 0ea8029

Please sign in to comment.