Commit 362b28a9 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Context:

- corrected unitary test order and one-by-one execution
- extractedunitary test constant to separate file
- added updated_at refresh for Service and Slice entities
- corrected return types for Connection entity
- prepared PolicyRule entity to raise events and corrected return types of methods
parent 9593ba92
Loading
Loading
Loading
Loading
+13 −8
Original line number Diff line number Diff line
@@ -39,8 +39,8 @@ from .database.Service import service_delete, service_get, service_list_ids, ser
from .database.Slice import slice_delete, slice_get, slice_list_ids, slice_list_objs, slice_set, slice_unset
from .database.Topology import topology_delete, topology_get, topology_list_ids, topology_list_objs, topology_set
from .Events import (
    CONSUME_TIMEOUT, TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_DEVICE, TOPIC_LINK, #TOPIC_POLICY,
    TOPIC_SERVICE, TOPIC_SLICE, TOPIC_TOPOLOGY, notify_event)
    CONSUME_TIMEOUT, TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_DEVICE, TOPIC_LINK, TOPIC_POLICY, TOPIC_SERVICE,
    TOPIC_SLICE, TOPIC_TOPOLOGY, notify_event)

LOGGER = logging.getLogger(__name__)

@@ -313,22 +313,27 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer

    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def ListPolicyRuleIds(self, request : Empty, context: grpc.ServicerContext) -> PolicyRuleIdList:
        return policyrule_list_ids(self.db_engine)
        return PolicyRuleIdList(policyRuleIdList=policyrule_list_ids(self.db_engine))

    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def ListPolicyRules(self, request : Empty, context: grpc.ServicerContext) -> PolicyRuleList:
        return policyrule_list_objs(self.db_engine)
        return PolicyRuleList(policyRules=policyrule_list_objs(self.db_engine))

    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def GetPolicyRule(self, request : PolicyRuleId, context: grpc.ServicerContext) -> PolicyRule:
        return policyrule_get(self.db_engine, request)
        return PolicyRule(**policyrule_get(self.db_engine, request))

    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def SetPolicyRule(self, request : PolicyRule, context: grpc.ServicerContext) -> PolicyRuleId:
        policyrule_id,updated = policyrule_set(self.db_engine, request) # pylint: disable=unused-variable
        return policyrule_id
        policyrule_id,updated = policyrule_set(self.db_engine, request)
        event_type = EventTypeEnum.EVENTTYPE_UPDATE if updated else EventTypeEnum.EVENTTYPE_CREATE
        notify_event(self.messagebroker, TOPIC_POLICY, event_type, {'policyrule_id': policyrule_id})
        return PolicyRuleId(**policyrule_id)

    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def RemovePolicyRule(self, request : PolicyRuleId, context: grpc.ServicerContext) -> Empty:
        deleted = policyrule_delete(self.db_engine, request) # pylint: disable=unused-variable
        policyrule_id,deleted = policyrule_delete(self.db_engine, request)
        if deleted:
            event_type = EventTypeEnum.EVENTTYPE_REMOVE
            notify_event(self.messagebroker, TOPIC_POLICY, event_type, {'policyrule_id': policyrule_id})
        return Empty()
+2 −3
Original line number Diff line number Diff line
@@ -22,14 +22,13 @@ TOPIC_CONNECTION = 'connection'
TOPIC_CONTEXT    = 'context'
TOPIC_DEVICE     = 'device'
TOPIC_LINK       = 'link'
#TOPIC_POLICY     = 'policy'
TOPIC_POLICY     = 'policy'
TOPIC_SERVICE    = 'service'
TOPIC_SLICE      = 'slice'
TOPIC_TOPOLOGY   = 'topology'

TOPICS = {
    TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_DEVICE, TOPIC_LINK, #TOPIC_POLICY,
    TOPIC_SERVICE, TOPIC_SLICE, TOPIC_TOPOLOGY
    TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_DEVICE, TOPIC_LINK, TOPIC_POLICY, TOPIC_SERVICE, TOPIC_SLICE, TOPIC_TOPOLOGY
}

CONSUME_TIMEOUT = 0.5 # seconds
+2 −2
Original line number Diff line number Diff line
@@ -136,7 +136,7 @@ def connection_set(db_engine : Engine, request : Connection) -> Tuple[Dict, bool
        return updated

    updated = run_transaction(sessionmaker(bind=db_engine), callback)
    return ConnectionId(**json_connection_id(connection_uuid)),updated
    return json_connection_id(connection_uuid),updated

def connection_delete(db_engine : Engine, request : ConnectionId) -> Tuple[Dict, bool]:
    connection_uuid = connection_get_uuid(request, allow_random=False)
@@ -144,4 +144,4 @@ def connection_delete(db_engine : Engine, request : ConnectionId) -> Tuple[Dict,
        num_deleted = session.query(ConnectionModel).filter_by(connection_uuid=connection_uuid).delete()
        return num_deleted > 0
    deleted = run_transaction(sessionmaker(bind=db_engine), callback)
    return ConnectionId(**json_connection_id(connection_uuid)),deleted
    return json_connection_id(connection_uuid),deleted
+22 −13
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 json
import datetime, json
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, sessionmaker
@@ -28,19 +28,19 @@ from .models.PolicyRuleModel import PolicyRuleDeviceModel, PolicyRuleKindEnum, P
from .uuids.PolicuRule import policyrule_get_uuid
from .uuids.Service import service_get_uuid

def policyrule_list_ids(db_engine : Engine) -> PolicyRuleIdList:
def policyrule_list_ids(db_engine : Engine) -> List[Dict]:
    def callback(session : Session) -> List[Dict]:
        obj_list : List[PolicyRuleModel] = session.query(PolicyRuleModel).all()
        #.options(selectinload(PolicyRuleModel.topology)).filter_by(context_uuid=context_uuid).one_or_none()
        return [obj.dump_id() for obj in obj_list]
    return PolicyRuleIdList(policyRuleIdList=run_transaction(sessionmaker(bind=db_engine), callback))
    return run_transaction(sessionmaker(bind=db_engine), callback)

def policyrule_list_objs(db_engine : Engine) -> PolicyRuleList:
def policyrule_list_objs(db_engine : Engine) -> List[Dict]:
    def callback(session : Session) -> List[Dict]:
        obj_list : List[PolicyRuleModel] = session.query(PolicyRuleModel).all()
        #.options(selectinload(PolicyRuleModel.topology)).filter_by(context_uuid=context_uuid).one_or_none()
        return [obj.dump() for obj in obj_list]
    return PolicyRuleList(policyRules=run_transaction(sessionmaker(bind=db_engine), callback))
    return run_transaction(sessionmaker(bind=db_engine), callback)

def policyrule_get(db_engine : Engine, request : PolicyRuleId) -> PolicyRule:
    policyrule_uuid = policyrule_get_uuid(request, allow_random=False)
@@ -54,7 +54,7 @@ def policyrule_get(db_engine : Engine, request : PolicyRuleId) -> PolicyRule:
        raise NotFoundException('PolicyRule', raw_policyrule_uuid, extra_details=[
            'policyrule_uuid generated was: {:s}'.format(policyrule_uuid)
        ])
    return PolicyRule(**obj)
    return obj

def policyrule_set(db_engine : Engine, request : PolicyRule) -> Tuple[PolicyRuleId, bool]:
    policyrule_kind = request.WhichOneof('policy_rule')
@@ -74,6 +74,8 @@ def policyrule_set(db_engine : Engine, request : PolicyRule) -> Tuple[PolicyRule
        'actionList': json_policyrule_basic.get('actionList', []),
    }, sort_keys=True)

    now = datetime.datetime.utcnow()

    policyrule_data = [{
        'policyrule_uuid'         : policyrule_uuid,
        'policyrule_kind'         : policyrule_kind,
@@ -81,6 +83,8 @@ def policyrule_set(db_engine : Engine, request : PolicyRule) -> Tuple[PolicyRule
        'policyrule_state_message': policyrule_state_message,
        'policyrule_priority'     : policyrule_basic.priority,
        'policyrule_eca_data'     : policyrule_eca_data,
        'created_at'              : now,
        'updated_at'              : now,
    }]

    policyrule_service_uuid = None
@@ -99,7 +103,7 @@ def policyrule_set(db_engine : Engine, request : PolicyRule) -> Tuple[PolicyRule
        })
        device_uuids.add(device_uuid)

    def callback(session : Session) -> None:
    def callback(session : Session) -> bool:
        stmt = insert(PolicyRuleModel).values(policyrule_data)
        stmt = stmt.on_conflict_do_update(
            index_elements=[PolicyRuleModel.policyrule_uuid],
@@ -108,22 +112,27 @@ def policyrule_set(db_engine : Engine, request : PolicyRule) -> Tuple[PolicyRule
                policyrule_state_message = stmt.excluded.policyrule_state_message,
                policyrule_priority      = stmt.excluded.policyrule_priority,
                policyrule_eca_data      = stmt.excluded.policyrule_eca_data,
                updated_at               = stmt.excluded.updated_at,
            )
        )
        session.execute(stmt)
        stmt = stmt.returning(PolicyRuleModel.created_at, PolicyRuleModel.updated_at)
        created_at,updated_at = session.execute(stmt).fetchone()
        updated = updated_at > created_at

        if len(related_devices) > 0:
            session.execute(insert(PolicyRuleDeviceModel).values(related_devices).on_conflict_do_nothing(
                index_elements=[PolicyRuleDeviceModel.policyrule_uuid, PolicyRuleDeviceModel.device_uuid]
            ))

    run_transaction(sessionmaker(bind=db_engine), callback)
    updated = False # TODO: improve and check if created/updated
    return PolicyRuleId(**json_policyrule_id(policyrule_uuid)),updated
        return updated

    updated = run_transaction(sessionmaker(bind=db_engine), callback)
    return json_policyrule_id(policyrule_uuid),updated

def policyrule_delete(db_engine : Engine, request : PolicyRuleId) -> bool:
def policyrule_delete(db_engine : Engine, request : PolicyRuleId) -> Tuple[Dict, bool]:
    policyrule_uuid = policyrule_get_uuid(request, allow_random=False)
    def callback(session : Session) -> bool:
        num_deleted = session.query(PolicyRuleModel).filter_by(policyrule_uuid=policyrule_uuid).delete()
        return num_deleted > 0
    return run_transaction(sessionmaker(bind=db_engine), callback)
    deleted = run_transaction(sessionmaker(bind=db_engine), callback)
    return json_policyrule_id(policyrule_uuid),deleted
+1 −0
Original line number Diff line number Diff line
@@ -111,6 +111,7 @@ def service_set(db_engine : Engine, request : Service) -> Tuple[Dict, bool]:
                service_name   = stmt.excluded.service_name,
                service_type   = stmt.excluded.service_type,
                service_status = stmt.excluded.service_status,
                updated_at     = stmt.excluded.updated_at,
            )
        )
        stmt = stmt.returning(ServiceModel.created_at, ServiceModel.updated_at)
Loading