diff --git a/.gitignore b/.gitignore index 5dc4372a5956ba5ce2b5ef6fc4359616c7cb5cd5..7e3b0cd6a26b755aeac4422f530c331d25a0cc43 100644 --- a/.gitignore +++ b/.gitignore @@ -162,6 +162,7 @@ cython_debug/ # TeraFlowSDN-generated files tfs_runtime_env_vars.sh +tfs_bchain_runtime_env_vars.sh delete_local_deployment.sh local_docker_deployment.sh local_k8s_deployment.sh diff --git a/deploy_mock_blockchain.sh b/deploy_mock_blockchain.sh new file mode 100755 index 0000000000000000000000000000000000000000..066820fc0f9a1005823dd124798e4de122f206f8 --- /dev/null +++ b/deploy_mock_blockchain.sh @@ -0,0 +1,121 @@ +#!/bin/bash +# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +# +# 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. + + +######################################################################################################################## +# Read deployment settings +######################################################################################################################## + +# Set the URL of your local Docker registry where the images will be uploaded to. +REGISTRY_IMAGE="http://localhost:32000/tfs/" + +# Set the tag you want to use for your images. +IMAGE_TAG="dev" + +# Set the name of the Kubernetes namespace to deploy to. +K8S_NAMESPACE="tfs-bchain" + +COMPONENT="mock_blockchain" + +######################################################################################################################## +# Automated steps start here +######################################################################################################################## + +# Constants +GITLAB_REPO_URL="registry.gitlab.com/teraflow-h2020/controller" +TMP_FOLDER="./tmp" + +# Create a tmp folder for files modified during the deployment +TMP_MANIFESTS_FOLDER="$TMP_FOLDER/manifests" +mkdir -p $TMP_MANIFESTS_FOLDER +TMP_LOGS_FOLDER="$TMP_FOLDER/logs" +mkdir -p $TMP_LOGS_FOLDER + +echo "Deleting and Creating a new namespace..." +kubectl delete namespace $K8S_NAMESPACE +kubectl create namespace $K8S_NAMESPACE +printf "\n" + +echo "Deploying components and collecting environment variables..." +ENV_VARS_SCRIPT=tfs_bchain_runtime_env_vars.sh +echo "# Environment variables for TeraFlow Mock-Blockchain deployment" > $ENV_VARS_SCRIPT +PYTHONPATH=$(pwd)/src + +echo "Processing '$COMPONENT' component..." +IMAGE_NAME="$COMPONENT:$IMAGE_TAG" +IMAGE_URL=$(echo "$REGISTRY_IMAGE/$IMAGE_NAME" | sed 's,//,/,g' | sed 's,http:/,,g') + +echo " Building Docker image..." +BUILD_LOG="$TMP_LOGS_FOLDER/build_${COMPONENT}.log" +docker build -t "$IMAGE_NAME" -f ./src/dlt/mock_blockchain/Dockerfile . > "$BUILD_LOG" + +if [ -n "$REGISTRY_IMAGE" ]; then + echo " Pushing Docker image to '$REGISTRY_IMAGE'..." + + TAG_LOG="$TMP_LOGS_FOLDER/tag_${COMPONENT}.log" + docker tag "$IMAGE_NAME" "$IMAGE_URL" > "$TAG_LOG" + + PUSH_LOG="$TMP_LOGS_FOLDER/push_${COMPONENT}.log" + docker push "$IMAGE_URL" > "$PUSH_LOG" +fi + +echo " Adapting '$COMPONENT' manifest file..." +MANIFEST="$TMP_MANIFESTS_FOLDER/${COMPONENT}.yaml" +cp ./manifests/"${COMPONENT}".yaml "$MANIFEST" + +if [ -n "$REGISTRY_IMAGE" ]; then + # Registry is set + VERSION=$(grep -i "${GITLAB_REPO_URL}/${COMPONENT}:" "$MANIFEST" | cut -d ":" -f3) + sed -E -i "s#image: $GITLAB_REPO_URL/$COMPONENT:${VERSION}#image: $IMAGE_URL#g" "$MANIFEST" + sed -E -i "s#imagePullPolicy: .*#imagePullPolicy: Always#g" "$MANIFEST" +else + # Registry is not set + VERSION=$(grep -i "${GITLAB_REPO_URL}/${COMPONENT}:" "$MANIFEST" | cut -d ":" -f3) + sed -E -i "s#image: $GITLAB_REPO_URL/$COMPONENT:${VERSION}#image: $IMAGE_NAME#g" "$MANIFEST" + sed -E -i "s#imagePullPolicy: .*#imagePullPolicy: Never#g" "$MANIFEST" +fi + +echo " Deploying '$COMPONENT' component to Kubernetes..." +DEPLOY_LOG="$TMP_LOGS_FOLDER/deploy_${COMPONENT}.log" +kubectl --namespace $K8S_NAMESPACE apply -f "$MANIFEST" > "$DEPLOY_LOG" +COMPONENT_OBJNAME=$(echo "${COMPONENT}" | sed "s/\_/-/") +kubectl --namespace $K8S_NAMESPACE scale deployment --replicas=0 ${COMPONENT_OBJNAME} >> "$DEPLOY_LOG" +kubectl --namespace $K8S_NAMESPACE scale deployment --replicas=1 ${COMPONENT_OBJNAME} >> "$DEPLOY_LOG" + +echo " Collecting env-vars for '$COMPONENT' component..." +SERVICE_DATA=$(kubectl get service ${COMPONENT_OBJNAME} --namespace $K8S_NAMESPACE -o json) + +# Env vars for service's host address +SERVICE_HOST=$(echo ${SERVICE_DATA} | jq -r '.spec.clusterIP') +ENVVAR_HOST=$(echo "${COMPONENT}_SERVICE_HOST" | tr '[:lower:]' '[:upper:]') +echo "export ${ENVVAR_HOST}=${SERVICE_HOST}" >> $ENV_VARS_SCRIPT + +# Env vars for service's 'grpc' port +SERVICE_PORT_GRPC=$(echo ${SERVICE_DATA} | jq -r '.spec.ports[] | select(.name=="grpc") | .port') +ENVVAR_PORT_GRPC=$(echo "${COMPONENT}_SERVICE_PORT_GRPC" | tr '[:lower:]' '[:upper:]') +echo "export ${ENVVAR_PORT_GRPC}=${SERVICE_PORT_GRPC}" >> $ENV_VARS_SCRIPT + +printf "\n" + +echo "Waiting for '$COMPONENT' component..." +kubectl wait --namespace $K8S_NAMESPACE \ + --for='condition=available' --timeout=300s deployment/${COMPONENT_OBJNAME} +printf "\n" + +echo "Deployment Resources:" +kubectl --namespace $K8S_NAMESPACE get all +printf "\n" + +echo "Done!" diff --git a/manifests/mock_blockchain.yaml b/manifests/mock_blockchain.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b383d7db42be9eb3c9dc7758c230f5250eb43db1 --- /dev/null +++ b/manifests/mock_blockchain.yaml @@ -0,0 +1,64 @@ +# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +# +# 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. + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: mock-blockchain +spec: + selector: + matchLabels: + app: mock-blockchain + template: + metadata: + labels: + app: mock-blockchain + spec: + terminationGracePeriodSeconds: 5 + containers: + - name: server + image: registry.gitlab.com/teraflow-h2020/controller/mock_blockchain:latest + imagePullPolicy: Always + ports: + - containerPort: 50051 + env: + - name: LOG_LEVEL + value: "DEBUG" + readinessProbe: + exec: + command: ["/bin/grpc_health_probe", "-addr=:50051"] + livenessProbe: + exec: + command: ["/bin/grpc_health_probe", "-addr=:50051"] + resources: + requests: + cpu: 250m + memory: 512Mi + limits: + cpu: 700m + memory: 1024Mi +--- +apiVersion: v1 +kind: Service +metadata: + name: mock-blockchain +spec: + type: ClusterIP + selector: + app: mock-blockchain + ports: + - name: grpc + protocol: TCP + port: 50051 + targetPort: 50051 diff --git a/src/common/Constants.py b/src/common/Constants.py index f18d4384035f2310355d7a16c5a709720b5b07e9..a536ef60047eb1f210f8d98d207134d377adcbed 100644 --- a/src/common/Constants.py +++ b/src/common/Constants.py @@ -49,6 +49,9 @@ class ServiceNameEnum(Enum): PATHCOMP = 'pathcomp' WEBUI = 'webui' + # Used for test and debugging only + DLT_GATEWAY = 'dlt-gateway' + # Default gRPC service ports DEFAULT_SERVICE_GRPC_PORTS = { ServiceNameEnum.CONTEXT .value : 1010, @@ -63,6 +66,9 @@ DEFAULT_SERVICE_GRPC_PORTS = { ServiceNameEnum.CYBERSECURITY.value : 10000, ServiceNameEnum.INTERDOMAIN .value : 10010, ServiceNameEnum.PATHCOMP .value : 10020, + + # Used for test and debugging only + ServiceNameEnum.DLT_GATEWAY .value : 50051, } # Default HTTP/REST-API service ports diff --git a/src/common/tests/MockServicerImpl_DltGateway.py b/src/common/tests/MockServicerImpl_DltGateway.py index 16eae7a34686a90b1ac08010ce134c8f47d79146..2d750168238b2a041badd1974f27e57f62363d90 100644 --- a/src/common/tests/MockServicerImpl_DltGateway.py +++ b/src/common/tests/MockServicerImpl_DltGateway.py @@ -13,7 +13,7 @@ # limitations under the License. import grpc, itertools, json, logging, time -from typing import Dict, Iterator, Optional, Tuple +from typing import Any, Dict, Iterator, Optional, Tuple from common.tests.MockMessageBroker import Message, MockMessageBroker from common.tools.grpc.Tools import grpc_message_to_json_string from common.proto.context_pb2 import EVENTTYPE_CREATE, EVENTTYPE_REMOVE, EVENTTYPE_UPDATE, Empty, TeraFlowController @@ -27,8 +27,8 @@ from common.proto.dlt_gateway_pb2_grpc import DltGatewayServiceServicer LOGGER = logging.getLogger(__name__) -DltRecordKey = Tuple[str, DltRecordOperationEnum, str] # domain_uuid, operation, record_uuid -DltRecordDict = Dict[DltRecordKey, DltRecord] # dlt_record_key => dlt_record +DltRecordKey = Tuple[str, Any, str] # domain_uuid, DltRecordOperationEnum, record_uuid +DltRecordDict = Dict[DltRecordKey, DltRecord] # dlt_record_key => dlt_record class AlreadyExistsException(Exception): pass diff --git a/src/dlt/Dockerfile b/src/dlt/Dockerfile deleted file mode 100644 index bbf21eb3235116f991b873528024e302ec0ea169..0000000000000000000000000000000000000000 --- a/src/dlt/Dockerfile +++ /dev/null @@ -1,21 +0,0 @@ -FROM zenika/kotlin - -# Copy controller project -COPY . /controller - -# Build gateway -WORKDIR /controller/src/dlt/gateway -RUN ./gradlew build - -EXPOSE 50051 - -RUN echo "#!/bin/sh" >> /runscript.sh -RUN echo "echo 195.37.154.24 peer0.org1.example.com >> /etc/hosts;\ - echo 195.37.154.24 peer0.org2.example.com >> /etc/hosts;\ - echo 195.37.154.24 orderer0.example.com >> /etc/hosts;" >> /runscript.sh - -RUN echo "cd /controller/src/dlt/gateway; ./gradlew runServer" >> /runscript.sh -RUN chmod +x /runscript.sh - - -ENTRYPOINT ["sh", "/runscript.sh"] diff --git a/src/dlt/connector/Dockerfile b/src/dlt/connector/Dockerfile index d653bb21778adbbd09407c1ca54f0afdc7ae5d81..51e9ec506f0c8a6c35ceac68833e3ad683ef8e63 100644 --- a/src/dlt/connector/Dockerfile +++ b/src/dlt/connector/Dockerfile @@ -1,4 +1,18 @@ -FROM python:3-slim +# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +# +# 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 python:3.9-slim # Install dependencies RUN apt-get --yes --quiet --quiet update && \ @@ -14,25 +28,42 @@ RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \ chmod +x /bin/grpc_health_probe # Get generic Python packages -RUN python3 -m pip install --upgrade pip setuptools wheel pip-tools +RUN python3 -m pip install --upgrade pip +RUN python3 -m pip install --upgrade setuptools wheel +RUN python3 -m pip install --upgrade pip-tools -# Set working directory +# Get common Python packages +# Note: this step enables sharing the previous Docker build steps among all the Python components WORKDIR /var/teraflow +COPY common_requirements.in common_requirements.in +RUN pip-compile --quiet --output-file=common_requirements.txt common_requirements.in +RUN python3 -m pip install -r common_requirements.txt + +# Add common files into working directory +WORKDIR /var/teraflow/common +COPY src/common/. ./ +RUN rm -rf proto -# Create module sub-folders -RUN mkdir -p /var/teraflow/slice +# Create proto sub-folder, copy .proto files, and generate Python code +RUN mkdir -p /var/teraflow/common/proto +WORKDIR /var/teraflow/common/proto +RUN touch __init__.py +COPY proto/*.proto ./ +RUN python3 -m grpc_tools.protoc -I=. --python_out=. --grpc_python_out=. *.proto +RUN rm *.proto +RUN find . -type f -exec sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' {} \; -# Get Python packages per module -COPY slice/requirements.in slice/requirements.in -RUN pip-compile --output-file=slice/requirements.txt slice/requirements.in -RUN python3 -m pip install -r slice/requirements.in +# Create component sub-folders, get specific Python packages +RUN mkdir -p /var/teraflow/dlt/connector +WORKDIR /var/teraflow/dlt/connector +COPY src/dlt/connector/requirements.in requirements.in +RUN pip-compile --quiet --output-file=requirements.txt requirements.in +RUN python3 -m pip install -r requirements.txt -# Add files into working directory -COPY common/. common -COPY context/. context -COPY interdomain/. interdomain -COPY service/. service -COPY slice/. slice +# Add component files into working directory +WORKDIR /var/teraflow +COPY src/context/. context/ +COPY src/dlt/connector/. dlt/connector -# Start slice service -ENTRYPOINT ["python", "-m", "slice.service"] +# Start the service +ENTRYPOINT ["python", "-m", "dlt.connector.service"] diff --git a/src/dlt/connector/requirements.in b/src/dlt/connector/requirements.in index 162ecde82a076fce597850ac8d71de3880c9a5eb..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 --- a/src/dlt/connector/requirements.in +++ b/src/dlt/connector/requirements.in @@ -1,6 +0,0 @@ -grpcio==1.43.0 -grpcio-health-checking==1.43.0 -prometheus-client==0.13.0 -protobuf==3.19.3 -pytest==6.2.5 -pytest-benchmark==3.4.1 diff --git a/src/dlt/connector/tests/test_unitary.py b/src/dlt/connector/tests/test_unitary.py index f5f54798ffe96de3e4c7e71c4083effceaa25cae..00c1164e1becde1d56de2a6c53c51160a31fc6f7 100644 --- a/src/dlt/connector/tests/test_unitary.py +++ b/src/dlt/connector/tests/test_unitary.py @@ -18,8 +18,14 @@ from common.orm.Database import Database from common.message_broker.MessageBroker import MessageBroker from common.proto.context_pb2 import Context, ContextId, Device, DeviceId, Link, LinkId, Topology, TopologyId from context.client.ContextClient import ContextClient -from .PrepareTestScenario import context_db_mb, context_service, context_client # pylint: disable=unused-import -from .Objects import CONTEXTS, TOPOLOGIES, DEVICES, LINKS +from .PrepareTestScenario import ( + # pylint: disable=unused-import + dltgateway_service, + context_service_a, context_client_a, dltconnector_service_a, dltconnector_client_a, + context_service_b, context_client_b, dltconnector_service_b, dltconnector_client_b) +from .Objects import ( + DA_CONTEXTS, DA_TOPOLOGIES, DA_DEVICES, DA_LINKS, + DB_CONTEXTS, DB_TOPOLOGIES, DB_DEVICES, DB_LINKS) LOGGER = logging.getLogger(__name__) LOGGER.setLevel(logging.DEBUG) @@ -27,9 +33,6 @@ LOGGER.setLevel(logging.DEBUG) def test_create_events( context_client : ContextClient, # pylint: disable=redefined-outer-name context_db_mb : Tuple[Database, MessageBroker]): # pylint: disable=redefined-outer-name - context_database = context_db_mb[0] - - context_database.clear_all() for context in CONTEXTS : context_client.SetContext (Context (**context )) for topology in TOPOLOGIES: context_client.SetTopology(Topology(**topology)) @@ -41,3 +44,11 @@ def test_create_events( for device in DEVICES : context_client.RemoveDevice (DeviceId (**device ['device_id' ])) for topology in TOPOLOGIES: context_client.RemoveTopology(TopologyId(**topology['topology_id'])) for context in CONTEXTS : context_client.RemoveContext (ContextId (**context ['context_id' ])) + + + + dltgateway_client = DltGatewayClient(host='127.0.0.1', port=50051) + dltgateway_collector = DltEventsCollector(dltgateway_client, log_events_received=True) + dltgateway_collector.start() + + dltgateway_collector.stop() diff --git a/src/dlt/gateway/Dockerfile b/src/dlt/gateway/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..92ef8e425f40eaf718c4562c836517128dbb2d6f --- /dev/null +++ b/src/dlt/gateway/Dockerfile @@ -0,0 +1,41 @@ +# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +# +# 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 zenika/kotlin:1.4-jdk12 + +# Make working directory move to it and copy DLT Gateway code +RUN mkdir -p /var/teraflow/dlt/gateway +WORKDIR /var/teraflow/dlt/gateway +COPY src/dlt/gateway/. ./ + +# Make directory for proto files and copy them +RUN mkdir proto +COPY proto/*.proto ./proto/ + +# Build DLT Gateway +RUN ./gradlew build + +EXPOSE 50051 + +# Create entrypoint.sh script +RUN echo "#!/bin/sh" > /entrypoint.sh +RUN echo "echo 195.37.154.24 peer0.org1.example.com >> /etc/hosts" >> /entrypoint.sh +RUN echo "echo 195.37.154.24 peer0.org2.example.com >> /etc/hosts" >> /entrypoint.sh +RUN echo "echo 195.37.154.24 orderer0.example.com >> /etc/hosts" >> /entrypoint.sh +RUN echo "cd /var/teraflow/dlt/gateway" >> /entrypoint.sh +RUN echo "./gradlew runServer" >> /entrypoint.sh +RUN chmod +x /entrypoint.sh + +# Gateway entry point +ENTRYPOINT ["sh", "/entrypoint.sh"] diff --git a/src/dlt/gateway/build.gradle.kts b/src/dlt/gateway/build.gradle.kts index c7213fbf7a1339ed660e9e4f7c8c64f761a10814..b65aff89e18077ffaff37ea732293f585ca7920d 100644 --- a/src/dlt/gateway/build.gradle.kts +++ b/src/dlt/gateway/build.gradle.kts @@ -107,7 +107,7 @@ task("runServer", JavaExec::class) { sourceSets { main { proto { - srcDir("../../../proto") + srcDir("proto") srcDir("src/main/kotlin/proto") } } diff --git a/src/dlt/mock_blockchain/Dockerfile b/src/dlt/mock_blockchain/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..22199b5f8f442e6d4617a2aed2e1dec9ad13e31a --- /dev/null +++ b/src/dlt/mock_blockchain/Dockerfile @@ -0,0 +1,68 @@ +# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +# +# 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 python:3.9-slim + +# Install dependencies +RUN apt-get --yes --quiet --quiet update && \ + apt-get --yes --quiet --quiet install wget g++ && \ + rm -rf /var/lib/apt/lists/* + +# Set Python to show logs as they occur +ENV PYTHONUNBUFFERED=0 + +# Download the gRPC health probe +RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \ + wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \ + chmod +x /bin/grpc_health_probe + +# Get generic Python packages +RUN python3 -m pip install --upgrade pip +RUN python3 -m pip install --upgrade setuptools wheel +RUN python3 -m pip install --upgrade pip-tools + +# Get common Python packages +# Note: this step enables sharing the previous Docker build steps among all the Python components +WORKDIR /var/teraflow +COPY common_requirements.in common_requirements.in +RUN pip-compile --quiet --output-file=common_requirements.txt common_requirements.in +RUN python3 -m pip install -r common_requirements.txt + +# Add common files into working directory +WORKDIR /var/teraflow/common +COPY src/common/. ./ +RUN rm -rf proto + +# Create proto sub-folder, copy .proto files, and generate Python code +RUN mkdir -p /var/teraflow/common/proto +WORKDIR /var/teraflow/common/proto +RUN touch __init__.py +COPY proto/*.proto ./ +RUN python3 -m grpc_tools.protoc -I=. --python_out=. --grpc_python_out=. *.proto +RUN rm *.proto +RUN find . -type f -exec sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' {} \; + +# Create component sub-folders, get specific Python packages +RUN mkdir -p /var/teraflow/mock_blockchain +WORKDIR /var/teraflow/mock_blockchain +COPY src/dlt/mock_blockchain/requirements.in requirements.in +RUN pip-compile --quiet --output-file=requirements.txt requirements.in +RUN python3 -m pip install -r requirements.txt + +# Add component files into working directory +WORKDIR /var/teraflow +COPY src/dlt/mock_blockchain/. mock_blockchain + +# Start the service +ENTRYPOINT ["python", "-m", "mock_blockchain.service"] diff --git a/src/dlt/mock_blockchain/__init__.py b/src/dlt/mock_blockchain/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..9953c820575d42fa88351cc8de022d880ba96e6a --- /dev/null +++ b/src/dlt/mock_blockchain/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +# +# 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. diff --git a/src/dlt/mock_blockchain/requirements.in b/src/dlt/mock_blockchain/requirements.in new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/dlt/mock_blockchain/service/__init__.py b/src/dlt/mock_blockchain/service/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..9953c820575d42fa88351cc8de022d880ba96e6a --- /dev/null +++ b/src/dlt/mock_blockchain/service/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +# +# 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. diff --git a/src/dlt/mock_blockchain/service/__main__.py b/src/dlt/mock_blockchain/service/__main__.py new file mode 100644 index 0000000000000000000000000000000000000000..359c6990addfcd9278496338c50320c152c1810f --- /dev/null +++ b/src/dlt/mock_blockchain/service/__main__.py @@ -0,0 +1,61 @@ +# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/) +# +# 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. + +import logging, signal, sys, threading +from common.Constants import ServiceNameEnum +from common.Settings import get_log_level, get_service_port_grpc +from common.proto.dlt_gateway_pb2_grpc import add_DltGatewayServiceServicer_to_server +from common.tests.MockServicerImpl_DltGateway import MockServicerImpl_DltGateway +from common.tools.service.GenericGrpcService import GenericGrpcService + +terminate = threading.Event() + +logging.basicConfig(level=get_log_level()) +LOGGER = logging.getLogger(__name__) + +class MockDltGatewayService(GenericGrpcService): + def __init__(self, cls_name: str = 'MockDltGatewayService') -> None: + port = get_service_port_grpc(ServiceNameEnum.DLT_GATEWAY) + super().__init__(port, cls_name=cls_name) + self.dltgateway_servicer = MockServicerImpl_DltGateway() + + # pylint: disable=attribute-defined-outside-init + def install_servicers(self): + add_DltGatewayServiceServicer_to_server(self.dltgateway_servicer, self.server) + +def signal_handler(signal, frame): # pylint: disable=redefined-outer-name + LOGGER.warning('Terminate signal received') + terminate.set() + +def main(): + signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGTERM, signal_handler) + + LOGGER.info('Starting...') + + # Starting Mock DLT gateway service + grpc_service = MockDltGatewayService() + grpc_service.start() + + # Wait for Ctrl+C or termination signal + while not terminate.wait(timeout=0.1): pass + + LOGGER.info('Terminating...') + grpc_service.stop() + + LOGGER.info('Bye') + return 0 + +if __name__ == '__main__': + sys.exit(main())