From a1cfe26312fdfd1e8f4603873313be9234d1f91f Mon Sep 17 00:00:00 2001 From: Nisar Khan Date: Mon, 30 Oct 2023 07:52:40 +0000 Subject: [PATCH] timestamp column in the AIM table to monitor the host link entries --- aim/api/infra.py | 2 + aim/db/infra_model.py | 7 +++- .../535c1f419b15_hostlink_add_timestamp.py | 39 +++++++++++++++++++ .../alembic_migrations/versions/HEAD | 2 +- aim/tests/unit/test_infra_manager.py | 7 ++++ 5 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 aim/db/migration/alembic_migrations/versions/535c1f419b15_hostlink_add_timestamp.py diff --git a/aim/api/infra.py b/aim/api/infra.py index 34568bb1..b48c5e87 100644 --- a/aim/api/infra.py +++ b/aim/api/infra.py @@ -39,6 +39,8 @@ class HostLink(resource.ResourceBase): ('path', t.string()), ('pod_id', t.string()), ('from_config', t.bool)) + db_attributes = t.db( + ('timestamp', t.string())) def __init__(self, **kwargs): super(HostLink, self).__init__({'interface_mac': '', diff --git a/aim/db/infra_model.py b/aim/db/infra_model.py index 2563bb4b..b3897d9e 100644 --- a/aim/db/infra_model.py +++ b/aim/db/infra_model.py @@ -34,6 +34,7 @@ class HostLink(model_base.Base, model_base.AttributeMixin): path = sa.Column(VARCHAR(512, charset='latin1')) pod_id = sa.Column(sa.String(128)) from_config = sa.Column(sa.Boolean, default=False) + timestamp = sa.Column(sa.TIMESTAMP, onupdate=func.now()) class HostLinkManager(object): @@ -45,11 +46,13 @@ def __init__(self, aim_context, aim_manager): self.aim_manager = aim_manager def add_hostlink(self, host, ifname, ifmac, swid, module, - port, path, pod_id='1', from_config=False): + port, path, pod_id='1', from_config=False, + timestamp=func.now()): link = infra.HostLink( host_name=host, interface_name=ifname, interface_mac=ifmac, switch_id=swid, module=module, - port=port, path=path, pod_id=pod_id, from_config=from_config) + port=port, path=path, pod_id=pod_id, from_config=from_config, + timestamp=timestamp) self.aim_manager.create(self.aim_context, link, overwrite=True) def delete_hostlink(self, host, ifname): diff --git a/aim/db/migration/alembic_migrations/versions/535c1f419b15_hostlink_add_timestamp.py b/aim/db/migration/alembic_migrations/versions/535c1f419b15_hostlink_add_timestamp.py new file mode 100644 index 00000000..125b4f54 --- /dev/null +++ b/aim/db/migration/alembic_migrations/versions/535c1f419b15_hostlink_add_timestamp.py @@ -0,0 +1,39 @@ +# Copyright (c) 2023 Cisco Systems +# 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. + +"""add_timestamp_column +Revision ID: 535c1f419b15 +Revises: 60399662af3c +Create Date: 2023-09-27 12:24:17.786497 +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.sql.expression import func + + +# revision identifiers, used by Alembic. +revision = '535c1f419b15' +down_revision = '60399662af3c' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column('aim_host_links', sa.Column( + 'timestamp', sa.TIMESTAMP, onupdate=func.now())) + + +def downgrade(): + pass diff --git a/aim/db/migration/alembic_migrations/versions/HEAD b/aim/db/migration/alembic_migrations/versions/HEAD index 2cac48b6..7b8da813 100644 --- a/aim/db/migration/alembic_migrations/versions/HEAD +++ b/aim/db/migration/alembic_migrations/versions/HEAD @@ -1 +1 @@ -60399662af3c +535c1f419b15 diff --git a/aim/tests/unit/test_infra_manager.py b/aim/tests/unit/test_infra_manager.py index f3679bfe..46ab39fe 100644 --- a/aim/tests/unit/test_infra_manager.py +++ b/aim/tests/unit/test_infra_manager.py @@ -18,6 +18,8 @@ from aim import config # noqa from aim.db import infra_model from aim.tests import base +from datetime import datetime +from datetime import timedelta class TestAimInfraManager(base.TestAimDBBase): @@ -38,6 +40,11 @@ def test_infra_manager(self): hlink = self.infra_mgr.get_hostlink(host, ifname) self.assertEqual(hlink.path, hlinks_mgr[0].path) + self.assertIsNotNone(hlink.timestamp) + current_time = datetime.now() + time_difference = current_time - hlink.timestamp + max_time_difference = timedelta(minutes=5) + self.assertLessEqual(time_difference, max_time_difference) hlinks = self.infra_mgr.get_hostlinks() self.assertEqual(1, len(hlinks))