# # Copyright(c) 2017 Intel Corporation. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Authors: # Volodymyr Mytnyk # import yaml import logging import datetime import time from threading import RLock from threading import Timer from threading import Thread import re # import YAML loader try: from yaml import CLoader as Loader except ImportError: from yaml import Loader # import synchronized queue try: # python 2.x import Queue as queue except ImportError: # python 3.x import queue class Config(object): """Configuration class used to pass config option into YAML file""" def __init__(self, interval): self.interval = interval class System(object): """System class which provides information like host, time etc., into YAML file""" def __init__(self): self.hostname = 'localhost' self._id = 0 @property def id(self): self._id = self._id + 1 return self._id @property def time(self): return time.time() @property def date(self): return datetime.date.today().isoformat() class ItemIterator(object): """Item iterator returned by Collector class""" def __init__(self, collector, items): """Item iterator init""" logging.debug('{}:__init__()'.format(self.__class__.__name__)) self._items = items self._collector = collector self._index = 0 def next(self): """Returns next item from the list""" if self._index == len(self._items): raise StopIteration curr_index = self._index self._index = curr_index + 1 return self.items[curr_index] def __getitem__(self, key): """get item by index""" return self._items[key] def __len__(self): """Return length of elements""" return len(self._items) def __del__(self): """Destroy iterator and unlock the collector""" logging.debug('{}:__del__()'.format(self.__class__.__name__)) self._collector.unlock() class ItemObject(object): """Item object returned by Collector class""" def __init__(self, collector, hash_): """Item object init""" logging.debug('{}:__init__()'.format(self.__class__.__name__)) super(ItemObject, self).__setattr__('_collector', collector) super(ItemObject, self).__setattr__('_hash', hash_) def __setattr__(self, name, value): t, item = self._collector._metrics[self._hash] logging.debug('{}:__setattr__(name={}, value={})'.format( self.__class__.__name__, name, value)) setattr(item, name, value) self._collector._metrics[self._hash] = (time.time(), item) def __del__(self): """Destroy item object and unlock the collector""" logging.debug('{}:__del__()'.format(self.__class__.__name__)) self._collector.unlock() class Collector(object): """Thread-safe collector with aging feature""" def __init__(self, age_timeout): """Initialization""" self._metrics = {} self._lock = RLock() self._age_timeout = age_timeout self._start_age_timer() def _start_age_timer(self): """Start age timer""" self._age_timer = Timer(self._age_timeout, self._on_timer) self._age_timer.start() def _stop_age_timer(self): """Stop age timer""" self._age_timer.cancel() def _on_timer(self): """Age timer""" self._start_age_timer() self._check_aging() def _check_aging(self): """Check aging time for all items""" self.lock() for data_hash, data in self._metrics.items(): age, item = data if ((time.time() - age) >= self._age_timeout): # aging time has expired, remove the item from the collector logging.debug('{}:_check_aging():value={}'.format( self.__class__.__name__, item)) self._metrics.pop(data_hash) del(item) self.unlock() def lock(self): """Lock the collector""" logging.debug('{}:lock()'.format(self.__class__.__name__)) self._lock.acquire() def unlock(self): """Unlock the collector""" logging.debug('{}:unlock()'.format(self.__class__.__name__)) self._lock.release() def get(self, hash_): self.lock() if hash_ in self._metrics: return ItemObject(self, hash_) self.unlock() return None def add(self, item): """Add an item into the collector""" self.lock() logging.debug('{}:add(item={})'.format(self.__class__.__name__, item)) self._metrics[hash(item)] = (time.time(), item) self.unlock() def items(self, select_list=[]): """Returns locked (safe) item iterator""" metrics = [] self.lock() for k, item in self._metrics.items(): _, value = item for select in select_list: if value.match(**select): metrics.append(value) return ItemIterator(self, metrics) def destroy(self): """Destroy the collector""" self._stop_age_timer() class CollectdData(object): """Base class for Collectd data""" def __init__(self, host=None, plugin=None, plugin_instance=None, type_=None, type_instance=None, time_=None): """Class initialization""" self.host = host self.plugin = plugin self.plugin_instance = plugin_instance self.type_instance = type_instance self.type = type_
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems)   #
# and others                                                                  #
#                                                                             #
# All rights reserved. This program and the accompanying materials            #
# are made available under the terms of the Apache License, Version 2.0       #
# which accompanies this distribution, and is available at                    #
# http://www.apache.org/licenses/LICENSE-2.0                                  #
###############################################################################
from test.event_based_scan.test_data.test_config import ENV_CONFIG

EVENT_PAYLOAD_NETWORK_UPDATE = {
    '_context_user_id': '13baa553aae44adca6615e711fd2f6d9', '_context_user': '13baa553aae44adca6615e711fd2f6d9',
    'priority': 'INFO',
    '_context_auth_token': 'gAAAAABYBrNJA6Io1infkUKquvCpC1bAWOCRxKE-8YQ71qLJhli200beztKmlY5ToBHSqFyPvoadkVKjA740jF' +
                           'bqeY-YtezMHhJAe-t_VyRJQ46IWAv8nPYvWRd_lmgtHrvBeId8NIPCZkhoAEmj5GwcZUZgnFYEhVlUliNO6IfV' +
                           'Oxcb17Z_1MKfdrfu1AtgD5hWb61w1F6x',
    '_context_user_name': 'admin', '_context_project_name': 'calipso-project', '_context_domain': None,
    '_unique_id': 'd1a96723db9341dca6f0d5fb9620f548', '_context_project_id': '75c0eb79ff4a42b0ae4973c8375ddf40',
    'message_id': '6b99d060-9cd6-4c14-8a0a-cbfc5c50d122', 'timestamp': '2016-10-18 23:47:31.636433',
    '_context_user_identity': '13baa553aae44adca6615e711fd2f6d9 75c0eb79ff4a42b0ae4973c8375ddf40 - - -',
    '_context_resource_uuid': None, '_context_request_id': 'req-b33cbd49-7af9-4c64-bcfb-782fcd400a5e',
    'publisher_id': 'network.node-6.cisco.com', 'payload': {
        'network': {'provider:network_type': 'vxlan', 'port_security_enabled': True, 'status': 'ACTIVE',
                    'id': '8673c48a-f137-4497-b25d-08b7b218fd17', 'shared': False, 'router:external': False,
                    'subnets': ['fcfa62ec-5ae7-46ce-9259-5f30de7af858'], 'admin_state_up': True,
                    'provider:segmentation_id': 52, 'provider:physical_network': None, 'name': '24',
                    'tenant_id': '75c0eb79ff4a42b0ae4973c8375ddf40', 'mtu': 1400}}, 'event_type': 'network.update.end',