def test_vdur_registration(self): """ When a VDUR is registered with the Monitor it is registered with the VdurNfviMetricsManager. Thus it is assigned a plugin that can be used to retrieve the NFVI metrics associated with the VDU. """ # Define the VDUR to be registered vdur = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_Vdur() vdur.vm_flavor.vcpu_count = 4 vdur.vm_flavor.memory_mb = 100 vdur.vm_flavor.storage_gb = 2 vdur.vim_id = 'test-vim-id' vdur.id = 'test-vdur-id' # Before registering the VDUR, the cloud account needs to be added to # the monitor. self.monitor.add_cloud_account(self.account) # Register the VDUR with the monitor self.monitor.add_vdur(self.account, vdur) self.assertTrue(self.monitor.is_registered_vdur(vdur.id)) # Check that the VDUR has been added to the metrics cache self.assertTrue(self.monitor.cache.contains_vdur_id(vdur.id)) # Unregister the VDUR self.monitor.remove_vdur(vdur.id) self.assertFalse(self.monitor.is_registered_vdur(vdur.id)) # Check that the VDUR has been removed from the metrics cache self.assertFalse(self.monitor.cache.contains_vdur_id(vdur.id))
def test_alarm_create_and_destroy(self): alarm = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_Vdur_Alarms() alarm.name = "test-alarm" alarm.description = "test-description" alarm.vdur_id = "test-vdur-id" alarm.metric = "CPU_UTILIZATION" alarm.statistic = "MINIMUM" alarm.operation = "GT" alarm.value = 0.1 alarm.period = 10 alarm.evaluations = 1 plugin_impl = TestNfviInterface.NfviPluginImpl() plugin = self.plugin_manager.plugin(self.account.name) plugin.set_impl(plugin_impl) self.assertEqual(len(plugin_impl._alarms), 0) @asyncio.coroutine @wait_for_pending_tasks(self.loop) def wait_for_create(): coro = self.nfvi_interface.alarm_create( self.account, "test-vim-id", alarm, ) yield from asyncio.wait_for( coro, timeout=2, loop=self.loop, ) self.loop.run_until_complete(wait_for_create()) self.assertEqual(len(plugin_impl._alarms), 1) self.assertTrue(alarm.alarm_id is not None) @asyncio.coroutine @wait_for_pending_tasks(self.loop) def wait_for_destroy(): coro = self.nfvi_interface.alarm_destroy( self.account, alarm.alarm_id, ) yield from asyncio.wait_for( coro, timeout=2, loop=self.loop, ) self.loop.run_until_complete(wait_for_destroy()) self.assertEqual(len(plugin_impl._alarms), 0)
def on_prepare(xact_info, action, ks_path, msg): try: self.log.debug("{}:on_prepare".format(self.classname)) self.log.debug("{}:on_preparef:msg {}".format(self.classname, msg)) xpath = ks_path.to_xpath(VnfrYang.get_schema()) xact_info.respond_xpath(rwdts.XactRspCode.ACK, xpath) dispatch.get(action, ignore)(msg) except Exception as e: self.log.exception(e) finally: self.log.debug("{}:on_prepare complete".format(self.classname))
def on_prepare(xact_info, action, ks_path, msg): try: response = VnfrYang.YangOutput_Vnfr_CreateAlarm() response.alarm_id = yield from self.tasklet.on_create_alarm( msg.cloud_account, msg.vdur_id, msg.alarm, ) xact_info.respond_xpath( rwdts.XactRspCode.ACK, "O,/vnfr:create-alarm", response, ) except Exception as e: self.log.exception(e) xact_info.respond_xpath(rwdts.XactRspCode.NACK)
def setUp(self): self.loop = asyncio.new_event_loop() self.logger = logging.getLogger('test-logger') self.account = RwcalYang.CloudAccount( name='test-cloud-account', account_type="mock", ) self.plugin_manager = NfviMetricsPluginManager(self.logger) self.plugin_manager.register(self.account, "mock") mock = self.plugin_manager.plugin(self.account.name) mock.set_impl(TestNfviMetricsCache.Plugin()) self.vdur = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_Vdur() self.vdur.id = "test-vdur-id" self.vdur.vim_id = "test-vim-id" self.vdur.vm_flavor.vcpu_count = 4 self.vdur.vm_flavor.memory_mb = 1 self.vdur.vm_flavor.storage_gb = 1
def make_vdur(id=str(uuid.uuid4()), vim_id=str(uuid.uuid4())): vdur = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_Vdur() vdur.id = id vdur.vim_id = vim_id return vdur
def make_vnfr(id=str(uuid.uuid4())): vnfr = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr() vnfr.id = id return vnfr
class MonParamsPingStatsTest(AsyncioTornadoTest): ping_path = r"/api/v1/ping/stats" ping_response = {'ping-request-tx-count': 5, 'ping-response-rx-count': 10} mon_param_msg = VnfrYang.YangData_RwProject_Project_VnfrCatalog_Vnfr_MonitoringParam( ) mon_param_msg.from_dict({ 'id': '1', 'name': 'ping-request-tx-count', 'json_query_method': "NAMEKEY", 'http_endpoint_ref': ping_path, 'value_type': "INT", 'description': 'no of ping requests', 'group_tag': 'Group1', 'widget_type': 'COUNTER', 'units': 'packets' }) endpoint_msg = VnfrYang.YangData_RwProject_Project_VnfrCatalog_Vnfr_HttpEndpoint( ) endpoint_msg.from_dict({ 'path': ping_path, 'polling_interval_secs': 1, 'username': '******', 'password': '******', 'headers': [{ 'key': 'TEST_KEY', 'value': 'TEST_VALUE' }], }) def create_endpoint(self, endpoint_msg): self.mon_port = self.get_http_port() endpoint = mon_params.HTTPEndpoint( logger, self._loop, "127.0.0.1", self.endpoint_msg, ) # For each creation, update the descriptor as well endpoint_msg.port = self.mon_port return endpoint def create_mon_param(self): return mon_params.MonitoringParam(logger, self.mon_param_msg) def get_app(self): class PingStatsHandler(tornado.web.RequestHandler): def get(this): test_header = this.request.headers.get('TEST_KEY') if test_header is None or test_header != 'TEST_VALUE': this.set_status(401) this.finish() return None auth_header = this.request.headers.get('Authorization') if auth_header is None or not auth_header.startswith('Basic '): this.set_status(401) this.set_header('WWW-Authenticate', 'Basic realm=Restricted') this._transforms = [] this.finish() return None auth_header = auth_header.encode('ascii') auth_decoded = base64.decodestring( auth_header[6:]).decode('ascii') login, password = auth_decoded.split(':', 2) login = login.encode('ascii') password = password.encode('ascii') is_auth = (login == b"admin" and password == b"password") if not is_auth: this.set_status(401) this.set_header('WWW-Authenticate', 'Basic realm=Restricted') this._transforms = [] this.finish() return None this.write(self.ping_response) return tornado.web.Application([ (self.ping_path, PingStatsHandler), ]) def test_value_convert(self): float_con = mon_params.ValueConverter("DECIMAL") int_con = mon_params.ValueConverter("INT") text_con = mon_params.ValueConverter("STRING") a = float_con.convert("1.23") self.assertEqual(a, 1.23) a = float_con.convert(1) self.assertEqual(a, float(1)) t = text_con.convert(1.23) self.assertEqual(t, "1.23") t = text_con.convert("asdf") self.assertEqual(t, "asdf") i = int_con.convert(1.23) self.assertEqual(i, 1) def test_json_key_value_querier(self): kv_querier = mon_params.JsonKeyValueQuerier(logger, "ping-request-tx-count") value = kv_querier.query(tornado.escape.json_encode( self.ping_response)) self.assertEqual(value, 5) def test_json_path_value_querier(self): kv_querier = mon_params.JsonPathValueQuerier( logger, '$.ping-request-tx-count') value = kv_querier.query(tornado.escape.json_encode( self.ping_response)) self.assertEqual(value, 5) def test_object_path_value_querier(self): kv_querier = mon_params.ObjectPathValueQuerier( logger, "$.*['ping-request-tx-count']") value = kv_querier.query(tornado.escape.json_encode( self.ping_response)) self.assertEqual(value, 5) def test_endpoint(self): @asyncio.coroutine def run_test(): endpoint = self.create_endpoint(self.endpoint_msg) resp = yield from endpoint.poll() resp_json = tornado.escape.json_decode(resp) self.assertEqual(resp_json["ping-request-tx-count"], 5) self.assertEqual(resp_json["ping-response-rx-count"], 10) self._loop.run_until_complete( asyncio.wait_for(run_test(), 10, loop=self._loop)) def test_mon_param(self): a = self.create_mon_param() a.extract_value_from_response( tornado.escape.json_encode(self.ping_response)) self.assertEqual(a.current_value, 5) self.assertEqual(a.msg.value_integer, 5) def test_endpoint_poller(self): endpoint = self.create_endpoint(self.endpoint_msg) mon_param = self.create_mon_param() poller = mon_params.EndpointMonParamsPoller( logger, self._loop, endpoint, [mon_param], ) poller.start() self._loop.run_until_complete(asyncio.sleep(1, loop=self._loop)) self.assertEqual(mon_param.current_value, 5) poller.stop() def test_params_controller(self): new_port = self.get_http_port() # Update port after new port is initialized self.endpoint_msg.port = new_port ctrl = mon_params.VnfMonitoringParamsController( logger, self._loop, "1", "127.0.0.1", [self.endpoint_msg], [self.mon_param_msg], ) ctrl.start() self._loop.run_until_complete(asyncio.sleep(1, loop=self._loop)) ctrl.stop() self.assertEqual(1, len(ctrl.mon_params)) mon_param = ctrl.mon_params[0] self.assertEqual(mon_param.current_value, 5)