Commit febe0556 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Common - Context Queries:

- added get_device helper method
parent d8a8c635
Loading
Loading
Loading
Loading
+19 −2
Original line number Diff line number Diff line
@@ -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}