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

Pre-merge code cleanup

parent 5b164809
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime, logging, json
import datetime, json, logging
from sqlalchemy import delete
#from sqlalchemy.dialects import postgresql
from sqlalchemy.dialects.postgresql import insert
@@ -38,11 +38,12 @@ def compose_constraints_data(
        dict_constraint = {
            'position'  : position,
            'kind'      : kind,
            'action'    : grpc_to_enum__constraint_action(constraint.action),
            'data'      : grpc_message_to_json_string(getattr(constraint, str_kind, {})),
            'created_at': now,
            'updated_at': now,
            'action'    : grpc_to_enum__constraint_action(constraint.action)
        }

        parent_kind,parent_uuid = '',None
        if service_uuid is not None:
            dict_constraint['service_uuid'] = service_uuid
@@ -51,7 +52,8 @@ def compose_constraints_data(
            dict_constraint['slice_uuid'] = slice_uuid
            parent_kind,parent_uuid = 'slice',slice_uuid
        else:
            MSG = 'Parent for Constraint({:s}) cannot be identified (service_uuid={:s}, slice_uuid={:s})'
            MSG = 'Parent for Constraint({:s}) cannot be identified '+\
                  '(service_uuid={:s}, slice_uuid={:s})'
            str_constraint = grpc_message_to_json_string(constraint)
            raise Exception(MSG.format(str_constraint, str(service_uuid), str(slice_uuid)))

@@ -71,7 +73,8 @@ def compose_constraints_data(
        }:
            constraint_name = '{:s}:{:s}:'.format(parent_kind, kind.value)
        else:
            MSG = 'Name for Constraint({:s}) cannot be inferred (service_uuid={:s}, slice_uuid={:s})'
            MSG = 'Name for Constraint({:s}) cannot be inferred '+\
                  '(service_uuid={:s}, slice_uuid={:s})'
            str_constraint = grpc_message_to_json_string(constraint)
            raise Exception(MSG.format(str_constraint, str(service_uuid), str(slice_uuid)))

@@ -79,7 +82,6 @@ def compose_constraints_data(
        dict_constraint['constraint_uuid'] = constraint_uuid

        dict_constraints.append(dict_constraint)

    return dict_constraints

def upsert_constraints(
@@ -93,13 +95,12 @@ def upsert_constraints(
    else:
        MSG = 'DataModel cannot be identified (service_uuid={:s}, slice_uuid={:s})'
        raise Exception(MSG.format(str(service_uuid), str(slice_uuid)))
    uuids_to_delete : Set[str] = set()
    uuids_to_upsert : Dict[str, int] = dict()
    rules_to_upsert : List[Dict] = list()
    uuids_to_delete : Set[str] = set()
    for constraint in constraints:
        constraint_uuid = constraint['constraint_uuid']
        constraint_action = constraint['action']

        if is_delete or constraint_action == ORM_ConstraintActionEnum.DELETE:
            uuids_to_delete.add(constraint_uuid)
        elif constraint_action == ORM_ConstraintActionEnum.SET:
@@ -121,7 +122,7 @@ def upsert_constraints(
        stmt = delete(klass)
        if service_uuid is not None: stmt = stmt.where(klass.service_uuid == service_uuid)
        if slice_uuid   is not None: stmt = stmt.where(klass.slice_uuid   == slice_uuid  )
        stmt = stmt.where(klass.constraint_uuid.not_in(set(uuids_to_upsert.keys())))
        stmt = stmt.where(klass.constraint_uuid.in_(uuids_to_delete)
        #str_stmt = stmt.compile(dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True})
        #LOGGER.warning('delete stmt={:s}'.format(str(str_stmt)))
        constraint_deletes = session.execute(stmt)
@@ -135,6 +136,7 @@ def upsert_constraints(
            index_elements=[klass.constraint_uuid],
            set_=dict(
                position   = stmt.excluded.position,
                action     = stmt.excluded.action,
                data       = stmt.excluded.data,
                updated_at = stmt.excluded.updated_at,
            )
+11 −4
Original line number Diff line number Diff line
@@ -15,8 +15,8 @@
import enum, json
from sqlalchemy import CheckConstraint, Column, DateTime, Enum, ForeignKey, Integer, String
from sqlalchemy.dialects.postgresql import UUID
from .enums.ConstraintAction import ORM_ConstraintActionEnum
from typing import Dict
from .enums.ConstraintAction import ORM_ConstraintActionEnum
from ._Base import _Base

# Enum values should match name of field in Constraint message
@@ -40,10 +40,10 @@ class ServiceConstraintModel(_Base):
    service_uuid    = Column(ForeignKey('service.service_uuid', ondelete='CASCADE'), nullable=False) #, index=True
    position        = Column(Integer, nullable=False)
    kind            = Column(Enum(ConstraintKindEnum), nullable=False)
    action          = Column(Enum(ORM_ConstraintActionEnum), nullable=False)
    data            = Column(String, nullable=False)
    created_at      = Column(DateTime, nullable=False)
    updated_at      = Column(DateTime, nullable=False)
    action          = Column(Enum(ORM_ConstraintActionEnum), nullable=False)

    __table_args__ = (
        CheckConstraint(position >= 0, name='check_position_value'),
@@ -51,7 +51,10 @@ class ServiceConstraintModel(_Base):
    )

    def dump(self) -> Dict:
        return {self.kind.value: json.loads(self.data)}
        return {
            'action': self.action.value,
            self.kind.value: json.loads(self.data),
        }

class SliceConstraintModel(_Base):
    __tablename__ = 'slice_constraint'
@@ -60,6 +63,7 @@ class SliceConstraintModel(_Base):
    slice_uuid      = Column(ForeignKey('slice.slice_uuid', ondelete='CASCADE'), nullable=False) #, index=True
    position        = Column(Integer, nullable=False)
    kind            = Column(Enum(ConstraintKindEnum), nullable=False)
    action          = Column(Enum(ORM_ConstraintActionEnum), nullable=False)
    data            = Column(String, nullable=False)
    created_at      = Column(DateTime, nullable=False)
    updated_at      = Column(DateTime, nullable=False)
@@ -70,4 +74,7 @@ class SliceConstraintModel(_Base):
    )

    def dump(self) -> Dict:
        return {self.kind.value: json.loads(self.data)}
        return {
            'action': self.action.value,
            self.kind.value: json.loads(self.data),
        }
+1 −1
Original line number Diff line number Diff line
@@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
from sqlalchemy import Column, DateTime, Enum, ForeignKey, String
from sqlalchemy.dialects.postgresql import ARRAY, UUID
from sqlalchemy.orm import relationship
from typing import Dict
from .enums.KpiSampleType import ORM_KpiSampleTypeEnum
from ._Base import _Base
import json

class EndPointModel(_Base):
    __tablename__ = 'endpoint'
+0 −1
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@ from common.Constants import DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME
from common.proto.kpi_sample_types_pb2 import KpiSampleType
from common.tools.object_factory.Context import json_context, json_context_id
from common.tools.object_factory.Topology import json_topology, json_topology_id
from context.service.database.uuids._Builder import get_uuid_from_string

# ----- Context --------------------------------------------------------------------------------------------------------
CONTEXT_ID = json_context_id(DEFAULT_CONTEXT_NAME)
+17 −4
Original line number Diff line number Diff line
@@ -53,7 +53,10 @@ ALBACETE_GPS = (38.998249, -1.858145)

# ----- Devices --------------------------------------------------------------------------------------------------------
DEVICE_R1_UUID          = 'R1'
DEVICE_R1_ENDPOINT_DEFS = [('EP1', 'optical', json_location(gps_position=json_gps_position(*BARCELONA_GPS))), ('EP100', 'copper', json_location(gps_position=json_gps_position(*BARCELONA_GPS)))]
DEVICE_R1_ENDPOINT_DEFS = [
    ('EP1', 'optical', json_location(gps_position=json_gps_position(*BARCELONA_GPS))),
    ('EP100', 'copper', json_location(gps_position=json_gps_position(*BARCELONA_GPS)))
]
DEVICE_R1_ID            = json_device_id(DEVICE_R1_UUID)
DEVICE_R1_ENDPOINTS     = json_endpoints(DEVICE_R1_ID, DEVICE_R1_ENDPOINT_DEFS)
DEVICE_R1_ENDPOINT_IDS  = json_endpoint_ids(DEVICE_R1_ID, DEVICE_R1_ENDPOINT_DEFS)
@@ -62,7 +65,10 @@ ENDPOINT_ID_R1_EP1 = DEVICE_R1_ENDPOINT_IDS[0]
ENDPOINT_ID_R1_EP100    = DEVICE_R1_ENDPOINT_IDS[1]

DEVICE_R2_UUID          = 'R2'
DEVICE_R2_ENDPOINT_DEFS = [('EP1', 'optical', json_location(gps_position=json_gps_position(*MADRID_GPS))), ('EP100', 'copper', json_location(gps_position=json_gps_position(*MADRID_GPS)))]
DEVICE_R2_ENDPOINT_DEFS = [
    ('EP1', 'optical', json_location(gps_position=json_gps_position(*MADRID_GPS))),
    ('EP100', 'copper', json_location(gps_position=json_gps_position(*MADRID_GPS)))
]
DEVICE_R2_ID            = json_device_id(DEVICE_R2_UUID)
DEVICE_R2_ENDPOINTS     = json_endpoints(DEVICE_R2_ID, DEVICE_R2_ENDPOINT_DEFS)
DEVICE_R2_ENDPOINT_IDS  = json_endpoint_ids(DEVICE_R2_ID, DEVICE_R2_ENDPOINT_DEFS)
@@ -71,7 +77,10 @@ ENDPOINT_ID_R2_EP1 = DEVICE_R2_ENDPOINT_IDS[0]
ENDPOINT_ID_R2_EP100    = DEVICE_R2_ENDPOINT_IDS[1]

DEVICE_R3_UUID          = 'R3'
DEVICE_R3_ENDPOINT_DEFS = [('EP1', 'optical', json_location(gps_position=json_gps_position(*MALAGA_GPS))), ('EP100', 'copper', json_location(gps_position=json_gps_position(*MALAGA_GPS)))]
DEVICE_R3_ENDPOINT_DEFS = [
    ('EP1', 'optical', json_location(gps_position=json_gps_position(*MALAGA_GPS))),
    ('EP100', 'copper', json_location(gps_position=json_gps_position(*MALAGA_GPS)))
]
DEVICE_R3_ID            = json_device_id(DEVICE_R3_UUID)
DEVICE_R3_ENDPOINTS     = json_endpoints(DEVICE_R3_ID, DEVICE_R3_ENDPOINT_DEFS)
DEVICE_R3_ENDPOINT_IDS  = json_endpoint_ids(DEVICE_R3_ID, DEVICE_R3_ENDPOINT_DEFS)
@@ -80,7 +89,11 @@ ENDPOINT_ID_R3_EP1 = DEVICE_R3_ENDPOINT_IDS[0]
ENDPOINT_ID_R3_EP100    = DEVICE_R3_ENDPOINT_IDS[1]

DEVICE_O1_UUID          = 'O1'
DEVICE_O1_ENDPOINT_DEFS = [('EP1', 'optical', json_location(gps_position=json_gps_position(*PONFERRADA_GPS))), ('EP2', 'optical', json_location(gps_position=json_gps_position(*PONFERRADA_GPS))), ('EP3', 'optical', json_location(gps_position=json_gps_position(*PONFERRADA_GPS)))]
DEVICE_O1_ENDPOINT_DEFS = [
    ('EP1', 'optical', json_location(gps_position=json_gps_position(*PONFERRADA_GPS))),
    ('EP2', 'optical', json_location(gps_position=json_gps_position(*PONFERRADA_GPS))),
    ('EP3', 'optical', json_location(gps_position=json_gps_position(*PONFERRADA_GPS)))
]
DEVICE_O1_ID            = json_device_id(DEVICE_O1_UUID)
DEVICE_O1_ENDPOINTS     = json_endpoints(DEVICE_O1_ID, DEVICE_O1_ENDPOINT_DEFS)
DEVICE_O1_ENDPOINT_IDS  = json_endpoint_ids(DEVICE_O1_ID, DEVICE_O1_ENDPOINT_DEFS)
Loading