diff --git a/src/common/tools/context_queries/Device.py b/src/common/tools/context_queries/Device.py index e5b205d46185e12fa51a2cbd8146342abe5bed38..ed8772cf65b1fef0d950c6de3be4e178fe5fb472 100644 --- a/src/common/tools/context_queries/Device.py +++ b/src/common/tools/context_queries/Device.py @@ -12,11 +12,28 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List, Set -from common.proto.context_pb2 import ContextId, Device, Empty, Topology, TopologyId +import grpc, logging +from typing import List, Optional, Set +from common.proto.context_pb2 import ContextId, Device, DeviceId, Empty, Topology, TopologyId from common.tools.object_factory.Topology import json_topology_id from context.client.ContextClient import ContextClient +LOGGER = logging.getLogger(__name__) + +def get_device(context_client : ContextClient, device_uuid : str, rw_copy : bool = False) -> Optional[Device]: + try: + # pylint: disable=no-member + device_id = DeviceId() + device_id.device_uuid.uuid = device_uuid + ro_device = context_client.GetDevice(device_id) + if not rw_copy: return ro_device + rw_device = Device() + rw_device.CopyFrom(ro_device) + return rw_device + except grpc.RpcError: + #LOGGER.exception('Unable to get Device({:s})'.format(str(device_uuid))) + return None + def get_existing_device_uuids(context_client : ContextClient) -> Set[str]: existing_device_ids = context_client.ListDeviceIds(Empty()) existing_device_uuids = {device_id.device_uuid.uuid for device_id in existing_device_ids.device_ids}