Commit 089e08f7 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Context component:

- Added extensions in Device to point to the controller node of each device
parent 50e43d79
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -74,6 +74,11 @@ def device_set(db_engine : Engine, request : Device) -> Tuple[Dict, bool]:
    device_name = raw_device_uuid if len(raw_device_name) == 0 else raw_device_name
    device_uuid = device_get_uuid(request.device_id, device_name=device_name, allow_random=True)

    if len(request.controller_id.device_uuid.uuid) > 0:
        controller_uuid = device_get_uuid(request.controller_id, allow_random=False)
    else:
        controller_uuid = None

    device_type = request.device_type
    oper_status = grpc_to_enum__device_operational_status(request.device_operational_status)
    device_drivers = [grpc_to_enum__device_driver(d) for d in request.device_drivers]
@@ -139,6 +144,9 @@ def device_set(db_engine : Engine, request : Device) -> Tuple[Dict, bool]:
        'updated_at'               : now,
    }]

    if controller_uuid is not None:
        device_data[0]['controller_uuid'] = controller_uuid

    def callback(session : Session) -> bool:
        stmt = insert(DeviceModel).values(device_data)
        stmt = stmt.on_conflict_do_update(
+8 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
# limitations under the License.

import operator
from sqlalchemy import Column, DateTime, Enum, String
from sqlalchemy import Column, DateTime, Enum, ForeignKey, String
from sqlalchemy.dialects.postgresql import ARRAY, UUID
from sqlalchemy.orm import relationship
from typing import Dict, List
@@ -29,16 +29,22 @@ class DeviceModel(_Base):
    device_type               = Column(String, nullable=False)
    device_operational_status = Column(Enum(ORM_DeviceOperationalStatusEnum), nullable=False)
    device_drivers            = Column(ARRAY(Enum(ORM_DeviceDriverEnum), dimensions=1))
    controller_uuid           = Column(UUID(as_uuid=False), ForeignKey('device.device_uuid'), nullable=True)
    created_at                = Column(DateTime, nullable=False)
    updated_at                = Column(DateTime, nullable=False)

    #topology_devices = relationship('TopologyDeviceModel', back_populates='device')
    config_rules = relationship('DeviceConfigRuleModel', passive_deletes=True) # lazy='joined', back_populates='device'
    endpoints    = relationship('EndPointModel', passive_deletes=True) # lazy='joined', back_populates='device'
    controller   = relationship('DeviceModel', remote_side=[device_uuid], passive_deletes=True) # lazy='joined', back_populates='device'

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

    def dump_controller(self) -> Dict:
        if self.controller is None: return {}
        return self.controller.dump_id()

    def dump_endpoints(self) -> List[Dict]:
        return [endpoint.dump() for endpoint in self.endpoints]

@@ -60,6 +66,7 @@ class DeviceModel(_Base):
            'device_type'              : self.device_type,
            'device_operational_status': self.device_operational_status.value,
            'device_drivers'           : [driver.value for driver in self.device_drivers],
            'controller_id'            : self.dump_controller(),
        }
        if include_endpoints: result['device_endpoints'] = self.dump_endpoints()
        if include_config_rules: result['device_config'] = self.dump_config_rules()