Skip to content
Snippets Groups Projects
Commit 820bf254 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Context component:

- Implemented method to retrieve names of endpoints and devices
- Implemented method to retrieve topology details
parent ac1f6f07
No related branches found
No related tags found
2 merge requests!54Release 2.0.0,!44Improvement to show names instead of UUIDs for devices and endpoints in WebUI
...@@ -22,11 +22,11 @@ from common.proto.context_pb2 import ( ...@@ -22,11 +22,11 @@ from common.proto.context_pb2 import (
Connection, ConnectionEvent, ConnectionId, ConnectionIdList, ConnectionList, Connection, ConnectionEvent, ConnectionId, ConnectionIdList, ConnectionList,
Context, ContextEvent, ContextId, ContextIdList, ContextList, Context, ContextEvent, ContextId, ContextIdList, ContextList,
Device, DeviceEvent, DeviceId, DeviceIdList, DeviceList, Device, DeviceEvent, DeviceId, DeviceIdList, DeviceList,
Empty, Empty, EndPointIdList, EndPointNameList,
Link, LinkEvent, LinkId, LinkIdList, LinkList, Link, LinkEvent, LinkId, LinkIdList, LinkList,
Service, ServiceEvent, ServiceId, ServiceIdList, ServiceList, Service, ServiceEvent, ServiceId, ServiceIdList, ServiceList,
Slice, SliceEvent, SliceId, SliceIdList, SliceList, Slice, SliceEvent, SliceId, SliceIdList, SliceList,
Topology, TopologyEvent, TopologyId, TopologyIdList, TopologyList) Topology, TopologyDetails, TopologyEvent, TopologyId, TopologyIdList, TopologyList)
from common.proto.context_pb2_grpc import ContextServiceStub from common.proto.context_pb2_grpc import ContextServiceStub
from common.proto.context_policy_pb2_grpc import ContextPolicyServiceStub from common.proto.context_policy_pb2_grpc import ContextPolicyServiceStub
from common.proto.policy_pb2 import PolicyRuleIdList, PolicyRuleId, PolicyRuleList, PolicyRule from common.proto.policy_pb2 import PolicyRuleIdList, PolicyRuleId, PolicyRuleList, PolicyRule
...@@ -143,6 +143,13 @@ class ContextClient: ...@@ -143,6 +143,13 @@ class ContextClient:
LOGGER.debug('GetTopologyEvents result: {:s}'.format(grpc_message_to_json_string(response))) LOGGER.debug('GetTopologyEvents result: {:s}'.format(grpc_message_to_json_string(response)))
return response return response
@RETRY_DECORATOR
def GetTopologyDetails(self, request: TopologyId) -> TopologyDetails:
LOGGER.debug('GetTopologyDetails request: {:s}'.format(grpc_message_to_json_string(request)))
response = self.stub.GetTopologyDetails(request)
LOGGER.debug('GetTopologyDetails result: {:s}'.format(grpc_message_to_json_string(response)))
return response
@RETRY_DECORATOR @RETRY_DECORATOR
def ListDeviceIds(self, request: Empty) -> DeviceIdList: def ListDeviceIds(self, request: Empty) -> DeviceIdList:
LOGGER.debug('ListDeviceIds request: {:s}'.format(grpc_message_to_json_string(request))) LOGGER.debug('ListDeviceIds request: {:s}'.format(grpc_message_to_json_string(request)))
...@@ -185,6 +192,13 @@ class ContextClient: ...@@ -185,6 +192,13 @@ class ContextClient:
LOGGER.debug('GetDeviceEvents result: {:s}'.format(grpc_message_to_json_string(response))) LOGGER.debug('GetDeviceEvents result: {:s}'.format(grpc_message_to_json_string(response)))
return response return response
@RETRY_DECORATOR
def ListEndPointNames(self, request: EndPointIdList) -> EndPointNameList:
LOGGER.debug('ListEndPointNames request: {:s}'.format(grpc_message_to_json_string(request)))
response = self.stub.ListEndPointNames(request)
LOGGER.debug('ListEndPointNames result: {:s}'.format(grpc_message_to_json_string(response)))
return response
@RETRY_DECORATOR @RETRY_DECORATOR
def ListLinkIds(self, request: Empty) -> LinkIdList: def ListLinkIds(self, request: Empty) -> LinkIdList:
LOGGER.debug('ListLinkIds request: {:s}'.format(grpc_message_to_json_string(request))) LOGGER.debug('ListLinkIds request: {:s}'.format(grpc_message_to_json_string(request)))
......
...@@ -19,11 +19,11 @@ from common.proto.context_pb2 import ( ...@@ -19,11 +19,11 @@ from common.proto.context_pb2 import (
Connection, ConnectionEvent, ConnectionId, ConnectionIdList, ConnectionList, Connection, ConnectionEvent, ConnectionId, ConnectionIdList, ConnectionList,
Context, ContextEvent, ContextId, ContextIdList, ContextList, Context, ContextEvent, ContextId, ContextIdList, ContextList,
Device, DeviceEvent, DeviceId, DeviceIdList, DeviceList, Device, DeviceEvent, DeviceId, DeviceIdList, DeviceList,
Empty, EventTypeEnum, Empty, EndPointIdList, EndPointNameList, EventTypeEnum,
Link, LinkEvent, LinkId, LinkIdList, LinkList, Link, LinkEvent, LinkId, LinkIdList, LinkList,
Service, ServiceEvent, ServiceId, ServiceIdList, ServiceList, Service, ServiceEvent, ServiceId, ServiceIdList, ServiceList,
Slice, SliceEvent, SliceId, SliceIdList, SliceList, Slice, SliceEvent, SliceId, SliceIdList, SliceList,
Topology, TopologyEvent, TopologyId, TopologyIdList, TopologyList) Topology, TopologyDetails, TopologyEvent, TopologyId, TopologyIdList, TopologyList)
from common.proto.policy_pb2 import PolicyRuleIdList, PolicyRuleId, PolicyRuleList, PolicyRule from common.proto.policy_pb2 import PolicyRuleIdList, PolicyRuleId, PolicyRuleList, PolicyRule
from common.proto.context_pb2_grpc import ContextServiceServicer from common.proto.context_pb2_grpc import ContextServiceServicer
from common.proto.context_policy_pb2_grpc import ContextPolicyServiceServicer from common.proto.context_policy_pb2_grpc import ContextPolicyServiceServicer
...@@ -32,12 +32,14 @@ from .database.Connection import ( ...@@ -32,12 +32,14 @@ from .database.Connection import (
connection_delete, connection_get, connection_list_ids, connection_list_objs, connection_set) connection_delete, connection_get, connection_list_ids, connection_list_objs, connection_set)
from .database.Context import context_delete, context_get, context_list_ids, context_list_objs, context_set from .database.Context import context_delete, context_get, context_list_ids, context_list_objs, context_set
from .database.Device import device_delete, device_get, device_list_ids, device_list_objs, device_set from .database.Device import device_delete, device_get, device_list_ids, device_list_objs, device_set
from .database.EndPoint import endpoint_list_names
from .database.Link import link_delete, link_get, link_list_ids, link_list_objs, link_set from .database.Link import link_delete, link_get, link_list_ids, link_list_objs, link_set
from .database.PolicyRule import ( from .database.PolicyRule import (
policyrule_delete, policyrule_get, policyrule_list_ids, policyrule_list_objs, policyrule_set) policyrule_delete, policyrule_get, policyrule_list_ids, policyrule_list_objs, policyrule_set)
from .database.Service import service_delete, service_get, service_list_ids, service_list_objs, service_set from .database.Service import service_delete, service_get, service_list_ids, service_list_objs, service_set
from .database.Slice import slice_delete, slice_get, slice_list_ids, slice_list_objs, slice_set, slice_unset from .database.Slice import slice_delete, slice_get, slice_list_ids, slice_list_objs, slice_set, slice_unset
from .database.Topology import topology_delete, topology_get, topology_list_ids, topology_list_objs, topology_set from .database.Topology import (
topology_delete, topology_get, topology_get_details, topology_list_ids, topology_list_objs, topology_set)
from .Events import ( from .Events import (
CONSUME_TIMEOUT, TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_DEVICE, TOPIC_LINK, TOPIC_POLICY, TOPIC_SERVICE, CONSUME_TIMEOUT, TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_DEVICE, TOPIC_LINK, TOPIC_POLICY, TOPIC_SERVICE,
TOPIC_SLICE, TOPIC_TOPOLOGY, notify_event) TOPIC_SLICE, TOPIC_TOPOLOGY, notify_event)
...@@ -105,6 +107,10 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer ...@@ -105,6 +107,10 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
def GetTopology(self, request : TopologyId, context : grpc.ServicerContext) -> Topology: def GetTopology(self, request : TopologyId, context : grpc.ServicerContext) -> Topology:
return Topology(**topology_get(self.db_engine, request)) return Topology(**topology_get(self.db_engine, request))
@safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
def GetTopologyDetails(self, request : TopologyId, context : grpc.ServicerContext) -> TopologyDetails:
return TopologyDetails(**topology_get_details(self.db_engine, request))
@safe_and_metered_rpc_method(METRICS_POOL, LOGGER) @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
def SetTopology(self, request : Topology, context : grpc.ServicerContext) -> TopologyId: def SetTopology(self, request : Topology, context : grpc.ServicerContext) -> TopologyId:
topology_id,updated = topology_set(self.db_engine, request) topology_id,updated = topology_set(self.db_engine, request)
...@@ -160,6 +166,10 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer ...@@ -160,6 +166,10 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
for message in self.messagebroker.consume({TOPIC_DEVICE}, consume_timeout=CONSUME_TIMEOUT): for message in self.messagebroker.consume({TOPIC_DEVICE}, consume_timeout=CONSUME_TIMEOUT):
yield DeviceEvent(**json.loads(message.content)) yield DeviceEvent(**json.loads(message.content))
@safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
def ListEndPointNames(self, request : EndPointIdList, context : grpc.ServicerContext) -> EndPointNameList:
return EndPointNameList(endpoint_names=endpoint_list_names(self.db_engine, request))
# ----- Link ------------------------------------------------------------------------------------------------------- # ----- Link -------------------------------------------------------------------------------------------------------
......
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# 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.
import logging
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy_cockroachdb import run_transaction
from typing import Dict, List
from common.proto.context_pb2 import EndPointIdList
from .models.EndPointModel import EndPointModel
from .uuids.EndPoint import endpoint_get_uuid
LOGGER = logging.getLogger(__name__)
def endpoint_list_names(db_engine : Engine, request : EndPointIdList) -> List[Dict]:
endpoint_uuids = {
endpoint_get_uuid(endpoint_id, allow_random=False)[-1]
for endpoint_id in request.endpoint_ids
}
def callback(session : Session) -> List[Dict]:
obj_list : List[EndPointModel] = \
session.query(EndPointModel).filter(EndPointModel.endpoint_uuid.in_(endpoint_uuids)).all()
return [obj.dump_name() for obj in obj_list]
return run_transaction(sessionmaker(bind=db_engine), callback)
...@@ -58,6 +58,22 @@ def topology_get(db_engine : Engine, request : TopologyId) -> Dict: ...@@ -58,6 +58,22 @@ def topology_get(db_engine : Engine, request : TopologyId) -> Dict:
]) ])
return obj return obj
def topology_get_details(db_engine : Engine, request : TopologyId) -> Dict:
_,topology_uuid = topology_get_uuid(request, allow_random=False)
def callback(session : Session) -> Optional[Dict]:
obj : Optional[TopologyModel] = session.query(TopologyModel)\
.filter_by(topology_uuid=topology_uuid).one_or_none()
return None if obj is None else obj.dump_details()
obj = run_transaction(sessionmaker(bind=db_engine), callback)
if obj is None:
context_uuid = context_get_uuid(request.context_id, allow_random=False)
raw_topology_uuid = '{:s}/{:s}'.format(request.context_id.context_uuid.uuid, request.topology_uuid.uuid)
raise NotFoundException('Topology', raw_topology_uuid, extra_details=[
'context_uuid generated was: {:s}'.format(context_uuid),
'topology_uuid generated was: {:s}'.format(topology_uuid),
])
return obj
def topology_set(db_engine : Engine, request : Topology) -> Tuple[Dict, bool]: def topology_set(db_engine : Engine, request : Topology) -> Tuple[Dict, bool]:
topology_name = request.name topology_name = request.name
if len(topology_name) == 0: topology_name = request.topology_id.topology_uuid.uuid if len(topology_name) == 0: topology_name = request.topology_id.topology_uuid.uuid
......
...@@ -51,3 +51,11 @@ class EndPointModel(_Base): ...@@ -51,3 +51,11 @@ class EndPointModel(_Base):
'endpoint_type' : self.endpoint_type, 'endpoint_type' : self.endpoint_type,
'kpi_sample_types': [kst.value for kst in self.kpi_sample_types], 'kpi_sample_types': [kst.value for kst in self.kpi_sample_types],
} }
def dump_name(self) -> Dict:
return {
'endpoint_id' : self.dump_id(),
'device_name' : self.device.device_name,
'endpoint_name': self.name,
'endpoint_type': self.endpoint_type,
}
...@@ -45,6 +45,14 @@ class TopologyModel(_Base): ...@@ -45,6 +45,14 @@ class TopologyModel(_Base):
'link_ids' : [{'link_uuid' : {'uuid': tl.link_uuid }} for tl in self.topology_links ], 'link_ids' : [{'link_uuid' : {'uuid': tl.link_uuid }} for tl in self.topology_links ],
} }
def dump_details(self) -> Dict:
return {
'topology_id': self.dump_id(),
'name' : self.topology_name,
'devices' : [td.device.dump() for td in self.topology_devices],
'links' : [tl.link.dump() for tl in self.topology_links ],
}
class TopologyDeviceModel(_Base): class TopologyDeviceModel(_Base):
__tablename__ = 'topology_device' __tablename__ = 'topology_device'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment