Skip to content

Commit

Permalink
Tag all expected hosts to allow addressing them without IP address etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
raulikak committed May 21, 2024
1 parent 374ed1b commit b56d709
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
41 changes: 41 additions & 0 deletions tcsfw/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def is_global(self) -> bool:
"""Is global address?"""
return False

def is_tag(self) -> bool:
"""Is entity tag?"""
return False

def change_host(self, _host: 'AnyAddress') -> Self:
"""Change host to given address. As default, returns this address"""
return self
Expand All @@ -97,6 +101,38 @@ def __lt__(self, other):
return self.__repr__() < other.__repr__()


class EntityTag(AnyAddress):
"""An unique tag for entity"""
def __init__(self, tag: str):
self.tag = tag

def is_global(self) -> bool:
return True # well, perhaps a flag for this later

def is_multicast(self) -> bool:
return False

def is_tag(self) -> bool:
return True

def priority(self) -> int:
return 3

def get_parseable_value(self) -> str:
return f"{self.tag}|tag"

def __eq__(self, other):
if not isinstance(other, EntityTag):
return False
return self.tag == other.tag

def __hash__(self):
return self.tag.__hash__()

def __repr__(self):
return self.tag


class PseudoAddress(AnyAddress):
"""Pseudo-address"""
def __init__(self, name: str, wildcard=False, multicast=False, hardware=False):
Expand Down Expand Up @@ -158,6 +194,8 @@ def parse_address(cls, address: str) -> AnyAddress:
return IPAddress.new(v)
if t == "hw":
return HWAddress.new(v)
if t == "tag":
return EntityTag(v)
if t == "name":
return DNSName(v)
raise ValueError(f"Unknown address type '{t}', allowed are 'ip', 'hw', and 'name'")
Expand Down Expand Up @@ -388,6 +426,9 @@ def is_multicast(self) -> bool:
def is_global(self) -> bool:
return self.host.is_global()

def is_tag(self) -> bool:
return self.host.is_tag()

def is_loopback(self) -> bool:
return self.host.is_loopback()

Expand Down
4 changes: 2 additions & 2 deletions tcsfw/builder_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import sys
from typing import Any, Callable, Dict, List, Optional, Self, Tuple, Union

from tcsfw.address import (Addresses, AnyAddress, DNSName, EndpointAddress, HWAddress,
from tcsfw.address import (Addresses, AnyAddress, DNSName, EndpointAddress, EntityTag, HWAddress,
HWAddresses, IPAddress, IPAddresses, Protocol)
from tcsfw.address_resolver import AddressResolver
from tcsfw.basics import ConnectionType, ExternalActivity, HostType, Status
Expand Down Expand Up @@ -146,7 +146,7 @@ def get_host_(self, name: str, description: str) -> 'HostBackend':
"""Get or create a host"""
hb = self.hosts_by_name.get(name)
if hb is None:
h = Host(self.system, name)
h = Host(self.system, name, tag=EntityTag(name)) # tag is not renamed, name can be
h.description = description
h.match_priority = 10
hb = HostBackend(h, self)
Expand Down
7 changes: 4 additions & 3 deletions tcsfw/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def _add_host(self, entity: Addressable) -> 'MatchEndpoint':
if entity.any_host:
ads = [] # always match by wildcards
else:
ads = {a.get_host(): None for a in entity.get_addresses()}.keys()
ads = {a.get_host(): None for a in entity.get_addresses() if not a.is_tag()}.keys()

if ads:
# addressed host
Expand Down Expand Up @@ -441,7 +441,7 @@ class MatchEndpoint:
"""Match endpoint"""
def __init__(self, entity: Addressable, match_no_service=True, priority_services=False):
self.entity = entity
self.addresses = entity.addresses.copy()
self.addresses = set(a for a in entity.addresses if not a.is_tag())
self.match_no_service = match_no_service # match without service?
self.match_priority = entity.match_priority
self.system = entity.get_system()
Expand All @@ -453,7 +453,8 @@ def __init__(self, entity: Addressable, match_no_service=True, priority_services

def add_address(self, address: AnyAddress) -> Self:
"""Add mapped address to endpoint"""
self.addresses.add(address)
if not address.is_tag():
self.addresses.add(address)
return self

def is_same_host(self, other: Optional['MatchEndpoint']) -> bool:
Expand Down
6 changes: 4 additions & 2 deletions tcsfw/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import List, Set, Optional, Tuple, TypeVar, Callable, Dict, Any, Self, Iterable, Iterator
from urllib.parse import urlparse

from tcsfw.address import AnyAddress, Addresses, EndpointAddress, Protocol, IPAddress, HWAddress, DNSName
from tcsfw.address import AnyAddress, Addresses, EndpointAddress, EntityTag, Protocol, IPAddress, HWAddress, DNSName
from tcsfw.basics import ConnectionType, ExternalActivity, HostType, Status
from tcsfw.entity import Entity
from tcsfw.property import PropertyKey
Expand Down Expand Up @@ -334,8 +334,10 @@ def get_parent_host(self) -> 'Host':

class Host(Addressable):
"""A host"""
def __init__(self, parent: 'IoTSystem', name: str):
def __init__(self, parent: 'IoTSystem', name: str, tag: Optional[EntityTag] = None):
super().__init__(name)
if tag:
self.addresses.add(tag)
self.concept_name = "node"
self.parent = parent
self.visual = True
Expand Down

0 comments on commit b56d709

Please sign in to comment.