Commit 0296456a authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Context component:

- Add attributes total/current capacity to link entity
- Extend unitary tests of links to test attributes
parent 1f4cd3d1
Loading
Loading
Loading
Loading
+20 −6
Original line number Diff line number Diff line
@@ -99,9 +99,21 @@ def link_set(db_engine : Engine, messagebroker : MessageBroker, request : Link)
            })
            topology_uuids.add(endpoint_topology_uuid)

    total_capacity_gbps, current_capacity_gbps = None, None
    if request.HasField('attributes'):
        attributes = request.attributes
        if attributes.HasField('total_capacity_gbps'):
            total_capacity_gbps = attributes.total_capacity_gbps
        if attributes.HasField('current_capacity_gbps'):
            current_capacity_gbps = attributes.current_capacity_gbps
        elif total_capacity_gbps is not None:
            current_capacity_gbps = total_capacity_gbps

    link_data = [{
        'link_uuid'            : link_uuid,
        'link_name'            : link_name,
        'total_capacity_gbps'  : total_capacity_gbps,
        'current_capacity_gbps': current_capacity_gbps,
        'created_at'           : now,
        'updated_at'           : now,
    }]
@@ -112,6 +124,8 @@ def link_set(db_engine : Engine, messagebroker : MessageBroker, request : Link)
            index_elements=[LinkModel.link_uuid],
            set_=dict(
                link_name             = stmt.excluded.link_name,
                total_capacity_gbps   = stmt.excluded.total_capacity_gbps,
                current_capacity_gbps = stmt.excluded.current_capacity_gbps,
                updated_at            = stmt.excluded.updated_at,
            )
        )
+20 −6
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
# limitations under the License.

import operator
from sqlalchemy import CheckConstraint, Column, DateTime, ForeignKey, Integer, String
from sqlalchemy import CheckConstraint, Column, DateTime, Float, ForeignKey, Integer, String
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
from typing import Dict
@@ -24,17 +24,24 @@ class LinkModel(_Base):

    link_uuid             = Column(UUID(as_uuid=False), primary_key=True)
    link_name             = Column(String, nullable=False)
    total_capacity_gbps   = Column(Float, nullable=True)
    current_capacity_gbps = Column(Float, nullable=True)
    created_at            = Column(DateTime, nullable=False)
    updated_at            = Column(DateTime, nullable=False)

    #topology_links = relationship('TopologyLinkModel', back_populates='link')
    link_endpoints = relationship('LinkEndPointModel') # lazy='joined', back_populates='link'

    __table_args__ = (
        CheckConstraint(total_capacity_gbps   >= 0, name='check_value_total_capacity_gbps'  ),
        CheckConstraint(current_capacity_gbps >= 0, name='check_value_current_capacity_gbps'),
    )

    def dump_id(self) -> Dict:
        return {'link_uuid': {'uuid': self.link_uuid}}

    def dump(self) -> Dict:
        return {
        result = {
            'link_id'          : self.dump_id(),
            'name'             : self.link_name,
            'link_endpoint_ids': [
@@ -42,6 +49,13 @@ class LinkModel(_Base):
                for link_endpoint in sorted(self.link_endpoints, key=operator.attrgetter('position'))
            ],
        }
        if self.total_capacity_gbps is not None:
            attributes : Dict = result.setdefault('attributes', dict())
            attributes.setdefault('total_capacity_gbps', self.total_capacity_gbps)
        if self.current_capacity_gbps is not None:
            attributes : Dict = result.setdefault('attributes', dict())
            attributes.setdefault('current_capacity_gbps', self.current_capacity_gbps)
        return result

class LinkEndPointModel(_Base):
    __tablename__ = 'link_endpoint'
+20 −5
Original line number Diff line number Diff line
@@ -71,18 +71,33 @@ DEVICE_R3_NAME, DEVICE_R3_ID, DEVICE_R3 = compose_device('R3', ['1.1', '1.2', '2


# ----- Link -----------------------------------------------------------------------------------------------------------
def compose_link(name : str, endpoint_ids : List[Tuple[str, str]]) -> Tuple[str, Dict, Dict]:
def compose_link(
    name : str, endpoint_ids : List[Tuple[str, str]],
    total_capacity_gbps : Optional[float] = None, current_capacity_gbps : Optional[float] = None
) -> Tuple[str, Dict, Dict]:
    link_id = json_link_id(name)
    endpoint_ids = [
        json_endpoint_id(device_id, endpoint_name, topology_id=TOPOLOGY_ID)
        for device_id, endpoint_name in endpoint_ids
    ]
    link = json_link(name, endpoint_ids)
    link = json_link(
        name, endpoint_ids, total_capacity_gbps=total_capacity_gbps,
        current_capacity_gbps=current_capacity_gbps
    )
    return name, link_id, link

LINK_R1_R2_NAME, LINK_R1_R2_ID, LINK_R1_R2 = compose_link('R1==R2', [(DEVICE_R1_ID, '1.2'), (DEVICE_R2_ID, '1.1')])
LINK_R2_R3_NAME, LINK_R2_R3_ID, LINK_R2_R3 = compose_link('R2==R3', [(DEVICE_R2_ID, '1.3'), (DEVICE_R3_ID, '1.2')])
LINK_R1_R3_NAME, LINK_R1_R3_ID, LINK_R1_R3 = compose_link('R1==R3', [(DEVICE_R1_ID, '1.3'), (DEVICE_R3_ID, '1.1')])
LINK_R1_R2_NAME, LINK_R1_R2_ID, LINK_R1_R2 = compose_link(
    'R1==R2', [(DEVICE_R1_ID, '1.2'), (DEVICE_R2_ID, '1.1')],
    total_capacity_gbps=100, # current_capacity_gbps=None => current_capacity_gbps=total_capacity_gbps
)
LINK_R2_R3_NAME, LINK_R2_R3_ID, LINK_R2_R3 = compose_link(
    'R2==R3', [(DEVICE_R2_ID, '1.3'), (DEVICE_R3_ID, '1.2')],
    total_capacity_gbps=100, # current_capacity_gbps=None => current_capacity_gbps=total_capacity_gbps
)
LINK_R1_R3_NAME, LINK_R1_R3_ID, LINK_R1_R3 = compose_link(
    'R1==R3', [(DEVICE_R1_ID, '1.3'), (DEVICE_R3_ID, '1.1')],
    total_capacity_gbps=100, # current_capacity_gbps=None => current_capacity_gbps=total_capacity_gbps
)


# ----- Service --------------------------------------------------------------------------------------------------------
+18 −0
Original line number Diff line number Diff line
@@ -95,6 +95,11 @@ def test_link(context_client : ContextClient) -> None:
    assert response.link_id.link_uuid.uuid == link_uuid
    assert response.name == LINK_R1_R2_NAME
    assert len(response.link_endpoint_ids) == 2
    assert response.HasField('attributes')
    assert response.attributes.HasField('total_capacity_gbps')
    assert abs(response.attributes.total_capacity_gbps - 100) < 1.e-12
    assert response.attributes.HasField('current_capacity_gbps')
    assert abs(response.attributes.current_capacity_gbps - response.attributes.total_capacity_gbps) < 1.e-12

    # ----- List when the object exists --------------------------------------------------------------------------------
    response = context_client.ListLinkIds(Empty())
@@ -111,6 +116,8 @@ def test_link(context_client : ContextClient) -> None:
    new_link_name = 'new'
    LINK_UPDATED = copy.deepcopy(LINK_R1_R2)
    LINK_UPDATED['name'] = new_link_name
    LINK_UPDATED['attributes']['total_capacity_gbps'] = 200
    LINK_UPDATED['attributes']['current_capacity_gbps'] = 50
    response = context_client.SetLink(Link(**LINK_UPDATED))
    assert response.link_uuid.uuid == link_uuid

@@ -125,6 +132,11 @@ def test_link(context_client : ContextClient) -> None:
    assert response.link_id.link_uuid.uuid == link_uuid
    assert response.name == new_link_name
    assert len(response.link_endpoint_ids) == 2
    assert response.HasField('attributes')
    assert response.attributes.HasField('total_capacity_gbps')
    assert abs(response.attributes.total_capacity_gbps - 200) < 1.e-12
    assert response.attributes.HasField('current_capacity_gbps')
    assert abs(response.attributes.current_capacity_gbps - 50) < 1.e-12

    # ----- List when the object is modified ---------------------------------------------------------------------------
    response = context_client.ListLinkIds(Empty())
@@ -136,6 +148,12 @@ def test_link(context_client : ContextClient) -> None:
    assert response.links[0].link_id.link_uuid.uuid == link_uuid
    assert response.links[0].name == new_link_name
    assert len(response.links[0].link_endpoint_ids) == 2
    assert len(response.links[0].link_endpoint_ids) == 2
    assert response.links[0].HasField('attributes')
    assert response.links[0].attributes.HasField('total_capacity_gbps')
    assert abs(response.links[0].attributes.total_capacity_gbps - 200) < 1.e-12
    assert response.links[0].attributes.HasField('current_capacity_gbps')
    assert abs(response.links[0].attributes.current_capacity_gbps - 50) < 1.e-12

    # ----- Check relation was created ---------------------------------------------------------------------------------
    response = context_client.GetTopology(TopologyId(**TOPOLOGY_ID))