Newer
Older
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#
# 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.
from typing import List, Set
from common.proto.context_pb2 import ContextId, Empty, Link, Topology, TopologyId
from common.tools.object_factory.Topology import json_topology_id
from context.client.ContextClient import ContextClient
def get_existing_link_uuids(context_client : ContextClient) -> Set[str]:
existing_link_ids = context_client.ListLinkIds(Empty())
existing_link_uuids = {link_id.link_uuid.uuid for link_id in existing_link_ids.link_ids}
return existing_link_uuids
def add_link_to_topology(
context_client : ContextClient, context_id : ContextId, topology_uuid : str, link_uuid : str
) -> bool:
topology_id = TopologyId(**json_topology_id(topology_uuid, context_id=context_id))
topology_ro = context_client.GetTopology(topology_id)
link_uuids = {link_id.link_uuid.uuid for link_id in topology_ro.link_ids}
if link_uuid in link_uuids: return False # already existed
topology_rw = Topology()
topology_rw.CopyFrom(topology_ro)
topology_rw.link_ids.add().link_uuid.uuid = link_uuid # pylint: disable=no-member
context_client.SetTopology(topology_rw)
return True
def get_uuids_of_links_in_topology(
context_client : ContextClient, context_id : ContextId, topology_uuid : str
) -> List[str]:
topology_id = TopologyId(**json_topology_id(topology_uuid, context_id=context_id))
topology = context_client.GetTopology(topology_id)
link_uuids = [link_id.link_uuid.uuid for link_id in topology.link_ids]
return link_uuids
def get_links_in_topology(
context_client : ContextClient, context_id : ContextId, topology_uuid : str
) -> List[Link]:
link_uuids = get_uuids_of_links_in_topology(context_client, context_id, topology_uuid)
all_links = context_client.ListLinks(Empty())
links_in_topology = list()
for link in all_links.links:
link_uuid = link.link_id.link_uuid.uuid
if link_uuid not in link_uuids: continue
links_in_topology.append(link)
return links_in_topology