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

Common - Tools - gRPC:

- Extended method to manage Constraints to enable specifying a new action
parent 8882ddab
Loading
Loading
Loading
Loading
+43 −9
Original line number Diff line number Diff line
@@ -18,11 +18,12 @@

import json
from typing import Any, Dict, List, Optional, Tuple
from common.proto.context_pb2 import Constraint, EndPointId
from common.proto.context_pb2 import Constraint, ConstraintActionEnum, EndPointId
from common.tools.grpc.Tools import grpc_message_to_json_string

def update_constraint_custom_scalar(
    constraints, constraint_type : str, value : Any, raise_if_differs : bool = False
    constraints, constraint_type : str, value : Any, raise_if_differs : bool = False,
    new_action : ConstraintActionEnum = ConstraintActionEnum.CONSTRAINTACTION_SET
) -> Constraint:

    for constraint in constraints:
@@ -36,6 +37,8 @@ def update_constraint_custom_scalar(
        constraint.custom.constraint_type = constraint_type
        json_constraint_value = None

    constraint.action = new_action

    if (json_constraint_value is None) or not raise_if_differs:
        # missing or raise_if_differs=False, add/update it
        json_constraint_value = value
@@ -47,7 +50,10 @@ def update_constraint_custom_scalar(
    constraint.custom.constraint_value = json.dumps(json_constraint_value, sort_keys=True)
    return constraint

def update_constraint_custom_dict(constraints, constraint_type : str, fields : Dict[str, Tuple[Any, bool]]) -> Constraint:
def update_constraint_custom_dict(
    constraints, constraint_type : str, fields : Dict[str, Tuple[Any, bool]],
    new_action : ConstraintActionEnum = ConstraintActionEnum.CONSTRAINTACTION_SET
) -> Constraint:
    # fields: Dict[field_name : str, Tuple[field_value : Any, raise_if_differs : bool]]

    for constraint in constraints:
@@ -61,6 +67,8 @@ def update_constraint_custom_dict(constraints, constraint_type : str, fields : D
        constraint.custom.constraint_type = constraint_type
        json_constraint_value = {}

    constraint.action = new_action

    for field_name,(field_value, raise_if_differs) in fields.items():
        if (field_name not in json_constraint_value) or not raise_if_differs:
            # missing or raise_if_differs=False, add/update it
@@ -75,7 +83,8 @@ def update_constraint_custom_dict(constraints, constraint_type : str, fields : D

def update_constraint_endpoint_location(
    constraints, endpoint_id : EndPointId,
    region : Optional[str] = None, gps_position : Optional[Tuple[float, float]] = None
    region : Optional[str] = None, gps_position : Optional[Tuple[float, float]] = None,
    new_action : ConstraintActionEnum = ConstraintActionEnum.CONSTRAINTACTION_SET
) -> Constraint:
    # gps_position: (latitude, longitude)
    if region is not None and gps_position is not None:
@@ -103,6 +112,8 @@ def update_constraint_endpoint_location(
        _endpoint_id.topology_id.topology_uuid.uuid = topology_uuid
        _endpoint_id.topology_id.context_id.context_uuid.uuid = context_uuid

    constraint.action = new_action

    location = constraint.endpoint_location.location
    if region is not None:
        location.region = region
@@ -111,7 +122,10 @@ def update_constraint_endpoint_location(
        location.gps_position.longitude = gps_position[1]
    return constraint

def update_constraint_endpoint_priority(constraints, endpoint_id : EndPointId, priority : int) -> Constraint:
def update_constraint_endpoint_priority(
    constraints, endpoint_id : EndPointId, priority : int,
    new_action : ConstraintActionEnum = ConstraintActionEnum.CONSTRAINTACTION_SET
) -> Constraint:
    endpoint_uuid = endpoint_id.endpoint_uuid.uuid
    device_uuid = endpoint_id.device_id.device_uuid.uuid
    topology_uuid = endpoint_id.topology_id.topology_uuid.uuid
@@ -134,10 +148,15 @@ def update_constraint_endpoint_priority(constraints, endpoint_id : EndPointId, p
        _endpoint_id.topology_id.topology_uuid.uuid = topology_uuid
        _endpoint_id.topology_id.context_id.context_uuid.uuid = context_uuid

    constraint.action = new_action

    constraint.endpoint_priority.priority = priority
    return constraint

def update_constraint_sla_capacity(constraints, capacity_gbps : float) -> Constraint:
def update_constraint_sla_capacity(
    constraints, capacity_gbps : float,
    new_action : ConstraintActionEnum = ConstraintActionEnum.CONSTRAINTACTION_SET
) -> Constraint:
    for constraint in constraints:
        if constraint.WhichOneof('constraint') != 'sla_capacity': continue
        break   # found, end loop
@@ -145,10 +164,15 @@ def update_constraint_sla_capacity(constraints, capacity_gbps : float) -> Constr
        # not found, add it
        constraint = constraints.add()      # pylint: disable=no-member

    constraint.action = new_action

    constraint.sla_capacity.capacity_gbps = capacity_gbps
    return constraint

def update_constraint_sla_latency(constraints, e2e_latency_ms : float) -> Constraint:
def update_constraint_sla_latency(
    constraints, e2e_latency_ms : float,
    new_action : ConstraintActionEnum = ConstraintActionEnum.CONSTRAINTACTION_SET
) -> Constraint:
    for constraint in constraints:
        if constraint.WhichOneof('constraint') != 'sla_latency': continue
        break   # found, end loop
@@ -156,11 +180,14 @@ def update_constraint_sla_latency(constraints, e2e_latency_ms : float) -> Constr
        # not found, add it
        constraint = constraints.add()      # pylint: disable=no-member

    constraint.action = new_action

    constraint.sla_latency.e2e_latency_ms = e2e_latency_ms
    return constraint

def update_constraint_sla_availability(
    constraints, num_disjoint_paths : int, all_active : bool, availability : float
    constraints, num_disjoint_paths : int, all_active : bool, availability : float,
    new_action : ConstraintActionEnum = ConstraintActionEnum.CONSTRAINTACTION_SET
) -> Constraint:
    for constraint in constraints:
        if constraint.WhichOneof('constraint') != 'sla_availability': continue
@@ -169,12 +196,17 @@ def update_constraint_sla_availability(
        # not found, add it
        constraint = constraints.add()      # pylint: disable=no-member

    constraint.action = new_action

    constraint.sla_availability.num_disjoint_paths = num_disjoint_paths
    constraint.sla_availability.all_active = all_active
    constraint.sla_availability.availability = availability
    return constraint

def update_constraint_sla_isolation(constraints, isolation_levels : List[int]) -> Constraint:
def update_constraint_sla_isolation(
    constraints, isolation_levels : List[int],
    new_action : ConstraintActionEnum = ConstraintActionEnum.CONSTRAINTACTION_SET
) -> Constraint:
    for constraint in constraints:
        if constraint.WhichOneof('constraint') != 'sla_isolation': continue
        break   # found, end loop
@@ -182,6 +214,8 @@ def update_constraint_sla_isolation(constraints, isolation_levels : List[int]) -
        # not found, add it
        constraint = constraints.add()      # pylint: disable=no-member

    constraint.action = new_action

    for isolation_level in isolation_levels:
        if isolation_level in constraint.sla_isolation.isolation_level: continue
        constraint.sla_isolation.isolation_level.append(isolation_level)