Skip to content

Commit

Permalink
timestamp column in the AIM table to monitor the host link entries
Browse files Browse the repository at this point in the history
  • Loading branch information
nisar123456 committed Nov 4, 2023
1 parent a94244a commit a1cfe26
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
2 changes: 2 additions & 0 deletions aim/api/infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': '',
Expand Down
7 changes: 5 additions & 2 deletions aim/db/infra_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion aim/db/migration/alembic_migrations/versions/HEAD
Original file line number Diff line number Diff line change
@@ -1 +1 @@
60399662af3c
535c1f419b15
7 changes: 7 additions & 0 deletions aim/tests/unit/test_infra_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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))
Expand Down

0 comments on commit a1cfe26

Please sign in to comment.