Skip to content

Commit

Permalink
Refactor mock service for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbosch committed Mar 5, 2024
1 parent b225773 commit 784d2eb
Show file tree
Hide file tree
Showing 55 changed files with 243 additions and 193 deletions.
31 changes: 31 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# /********************************************************************************
# * Copyright (c) 2023 Contributors to the Eclipse Foundation
# *
# * See the NOTICE file(s) distributed with this work for additional
# * information regarding copyright ownership.
# *
# * This program and the accompanying materials are made available under the
# * terms of the Apache License 2.0 which is available at
# * http://www.apache.org/licenses/LICENSE-2.0
# *
# * SPDX-License-Identifier: Apache-2.0
# ********************************************************************************/

FROM python:3.11


COPY . /tmp

WORKDIR /tmp

RUN python3 -m pip install --no-cache-dir -r requirements.txt

# useful dumps about feeding values
ENV LOG_LEVEL="info,databroker=debug,mock_service=debug"

# Vehicle Data Broker host:port
#ENV VDB_ADDRESS="localhost:55555"

ENV PYTHONUNBUFFERED=yes

# ENTRYPOINT ["./mockservice.py"]
8 changes: 8 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"runArgs": ["--network=host"]

}
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max_line_length = 120
33 changes: 33 additions & 0 deletions .github/workflows/check_license.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: spdx

on:
pull_request

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
cancel-in-progress: true

jobs:
check-spdx-headers:
runs-on: ubuntu-latest

steps:

- name: Checkout code
uses: actions/checkout@v4
with:
# required to grab the history of the PR
fetch-depth: 0

- name: Get changed files
run: |
echo "files=$(git diff --name-only ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} | tr '\n' ',')" >> $GITHUB_ENV
- name: Set license
run: |
echo "licenses=Apache-2.0" >> $GITHUB_ENV
- uses: eclipse-kuksa/kuksa-actions/spdx@2
with:
files: "${{ env.files }}"
licenses: "${{ env.licenses }}"
41 changes: 41 additions & 0 deletions .github/workflows/dash.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: dash

on:
pull_request:
push:
branches:
- main

jobs:
check-dash:
runs-on: ubuntu-latest
steps:

- name: Checkout code
uses: actions/checkout@v4

# taken from here: https://github.com/eclipse/dash-licenses#example-python
- name: Create Dash Dependency Report
run: |
python3 -m venv ./.venv/kuksa-can-provider
source ./.venv/kuksa-can-provider/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
deactivate
python3 -m venv ./.venv/pipdeptree
source ./.venv/pipdeptree/bin/activate
pip install --upgrade pip
pip install pipdeptree
pipdeptree -a -f --python ./.venv/kuksa-can-provider/bin/python \
| sed -E -e 's|([^= ]+)==([^= ]+)|pypi/pypi/-/\1/\2|' -e 's| ||g' \
| sort | uniq \
> dependencies.txt
- name: Dash license check
uses: eclipse-kuksa/kuksa-actions/check-dash@2
with:
dashinput: ${{github.workspace}}/dependencies.txt
28 changes: 14 additions & 14 deletions .github/workflows/mock_service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ on:
branches:
- main
pull_request:
paths:
- ".github/workflows/mock_service.yml"
- "mock_service/**"
- "proto/**"
workflow_call:
workflow_dispatch:

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
cancel-in-progress: true

# Needed as default_workflow_permissions is "read"
permissions:
packages: write

jobs:

check_ghcr_push:
Expand Down Expand Up @@ -100,12 +100,12 @@ jobs:
platforms: |
linux/amd64
linux/arm64
file: ./mock_service/Dockerfile
context: ./mock_service
file: ./Dockerfile
context: .
push: true
tags: |
${{ steps.meta.outputs.tags }}
ttl.sh/kuksa.val.services/mock_service-${{github.sha}}
ttl.sh/eclipse-kuksa/kuksa-mock-provider/mock-provider-${{github.sha}}
labels: ${{ steps.meta.outputs.labels }}

- name: Build container and push to ttl.sh
Expand All @@ -116,16 +116,16 @@ jobs:
platforms: |
linux/amd64
linux/arm64
file: ./mock_service/Dockerfile
context: ./mock_service
file: ./Dockerfile
context: .
push: true
tags: "ttl.sh/kuksa.val.services/mock_service-${{github.sha}}"
tags: "ttl.sh/eclipse-kuksa/kuksa-mock-provider/mock-provider-${{github.sha}}"
labels: ${{ steps.meta.outputs.labels }}

- name: Posting message
uses: eclipse-kuksa/kuksa-actions/post-container-location@2
with:
image: ttl.sh/kuksa.val.services/mock_service-${{github.sha}}
image: ttl.sh/eclipse-kuksa/kuksa-mock-provider/mock-provider-${{github.sha}}

ensure-docs-up2date:
name: Ensure docs are up2date
Expand All @@ -141,7 +141,7 @@ jobs:

- name: (Re-) Build Pydoc Markdown
shell: bash
working-directory: mock_service
working-directory: .
run: |
./update-api-docs.sh
Expand Down Expand Up @@ -174,13 +174,13 @@ jobs:

- name: Install dependencies
shell: bash
working-directory: mock_service
working-directory: .
run: |
python3 -m pip install --pre -r ./requirements.txt
python3 -m pip install -r ./requirements-dev.txt
- name: Run tests
shell: bash
working-directory: mock_service
working-directory: .
run: |
python3 -m pytest
24 changes: 24 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: pre-commit

on: [pull_request]

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
cancel-in-progress: true


jobs:
pre-commit:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
# required to grab the history of the PR
fetch-depth: 0
- uses: actions/setup-python@v5
with:
python-version: '3.10'
- uses: pre-commit/[email protected]
with:
extra_args: --color=always --from-ref ${{ github.event.pull_request.base.sha }} --to-ref ${{ github.event.pull_request.head.sha }}
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[FORMAT]
max-line-length=120
8 changes: 1 addition & 7 deletions mock_service/Dockerfile → Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ ENV LOG_LEVEL="info,databroker=debug,mock_service=debug"

# Vehicle Data Broker host:port
#ENV VDB_ADDRESS="localhost:55555"
# Override VDB_ADDRESS port if set
#ENV DAPR_GRPC_PORT="55555"
# VDB DAPR APP ID
ENV VEHICLEDATABROKER_DAPR_APP_ID=vehicledatabroker

ENV PYTHONUNBUFFERED=yes

LABEL org.opencontainers.image.source="https://github.com/eclipse/kuksa.val.services"

CMD ["python3", "/mock/mockservice.py"]
ENTRYPOINT ["./mockprovider.py"]
21 changes: 11 additions & 10 deletions mock_service/README.md → README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Vehicle Mock Service
# KUKSA Mock Provider

![KUKSA Logo](./doc/img/logo.png)

## About

The vehicle mock service is a service dummy allowing to control all specified actuator- and sensor-signals via a configuration file. These configuration files are expressed in a Python-based [domain-specific language (DSL)](./doc/pydoc/mocking-dsl.md).
The KUKSA Mock Provider is a service dummy allowing to control all specified actuator- and sensor-signals via a configuration file. These configuration files are expressed in a Python-based [domain-specific language (DSL)](./doc/pydoc/mocking-dsl.md).

## Why

Expand All @@ -24,7 +26,7 @@ flowchart LR
ValIF <--> id3
```

# Running mockservice
# Running Mock Provider

Firstly, you will need to install all necessary Python dependencies by issuing the following command in your favorite terminal:

Expand All @@ -35,22 +37,21 @@ python3 -m pip install -r ./requirements.txt
You can then run the vehicle mock service with

```bash
python3 mockservice.py
python3 mockprovider.py
```

As an alternative, you can build and execute the container from the [Dockerfile](./Dockerfile) or through the [docker-build.sh](./docker-build.sh) script.
As an alternative, you can build and execute the container from the [Dockerfile](./Dockerfile).

Another option, when using VS Code, is to use the [provided VSCode task](../.vscode/tasks.json) `run-mockservice` which will set up all necessary environment variables and parameters for the service to start up properly.

## Configuration

### Vehicle Mock Service
### KUKSA Mock Provider

| parameter | default value | Environment variable | description |
|----------------|-----------------------|----------------------------------------------------------------------------------|---------------------------------|
| listen address | `"127.0.0.1:50053"` | `MOCK_ADDR` | Listen for rpc calls |
| broker address | `"127.0.0.1:55555"` | if DAPR_GRPC_PORT is set:<br>`"127.0.0.1:$DAPR_GRPC_PORT"` <br>else:<br> `VDB_ADDRESS`| The address of the KUKSA.val databroker to connect to |
| broker app id | `"vehicledatabroker"` | `VEHICLEDATABROKER_DAPR_APP_ID` | When using DAPR, this allows to configure the id of the KUKSA.val databroker to connect to. |
| broker address | `"127.0.0.1:55555"` | `VDB_ADDRESS`| The address of the KUKSA.val databroker to connect to |


Configuration options have the following priority (highest at top):
1. environment variable
Expand Down Expand Up @@ -83,7 +84,7 @@ Firstly, you will need to install all necessary Python dependencies by using the
python3 -m pip install -r ./requirements.txt
```

To run the GUI do the following in your favourtie terminal:
To run the GUI do the following in your favorite terminal:

```bash
python3 showcase_gui/GUI.py
Expand Down
8 changes: 4 additions & 4 deletions mock_service/doc/concept.md → doc/concept.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ The scope of the Vehicle Mock is a CLI application without a GUI and without phy
| #F10.2.1 | post-MVP | non-looping
| #F10.2.2 | post-MVP | reset after play, play again
| #F10.2.3 | post-MVP | ping-pong (reverse play)
| #F11 | MVP | Values in behavior conditions and target values shall support liteals to indicate the use of dynamic values: `$requested_value` for the requested value of an `actuator_target` event, `$Vehicle.Speed` for dynamic evaluation of the Vehicle's current speed. All VSS sensors and actuators shall be supported in the evaluation of dynamic values.
| #F11 | MVP | Values in behavior conditions and target values shall support literals to indicate the use of dynamic values: `$requested_value` for the requested value of an `actuator_target` event, `$Vehicle.Speed` for dynamic evaluation of the Vehicle's current speed. All VSS sensors and actuators shall be supported in the evaluation of dynamic values.
| #F12 | post-MVP | It shall be possible to re-load the configuration file at runtime via external trigger

## Non-funtional requirements
## Non-functional requirements

| id | version | description |
|:---|:-------:|:-----------|
Expand Down Expand Up @@ -149,14 +149,14 @@ mock_datapoints:

```

### Class Diagarm
### Class Diagram

```mermaid
classDiagram
class BaseService {
Basic service responsible for
establishing connection to
Dapr sidecar and VDB.
KUKSA Databroker.
+ connectToDatabroker()
}
Expand Down
Binary file added doc/img/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 2 additions & 15 deletions mock_service/lib/baseservice.py → lib/baseservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

log = logging.getLogger("mock_service")

# VehicleDataBroker address, overridden if "DAPR_GRPC_PORT" is set in environment
VDB_ADDRESS = os.getenv("VDB_ADDRESS", "127.0.0.1:55555")


Expand All @@ -47,11 +46,7 @@ class BaseService(ABC):
def __init__(self, service_address: str, service_name: str, databroker_address: str = VDB_ADDRESS):
super().__init__()

if os.getenv("DAPR_GRPC_PORT") is not None:
grpc_port = os.getenv("DAPR_GRPC_PORT")
self._vdb_address = f"127.0.0.1:{grpc_port}"
else:
self._vdb_address = databroker_address
self._vdb_address = databroker_address
self._address = service_address
self._service_name = service_name
self._connected = False
Expand All @@ -66,14 +61,7 @@ def __init__(self, service_address: str, service_name: str, databroker_address:

def _connect_to_databroker(self) -> None:
log.info("Connecting to Data Broker [%s]", self._vdb_address)
if os.getenv("VEHICLEDATABROKER_DAPR_APP_ID") is not None:
self._metadata = (
("dapr-app-id", os.getenv("VEHICLEDATABROKER_DAPR_APP_ID")),
)
# give some time for dapr sidecar startup...
time.sleep(2)
else:
self._metadata = None
self._metadata = None
self._client.connect()
self._connected = True
self.on_databroker_connected()
Expand All @@ -86,7 +74,6 @@ def _run(self):
time.sleep(0.2)
continue
else:
# TODO: check if dapr grpc proxy has active connection(e.g. send last temp value)
time.sleep(1)

@abstractmethod
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 784d2eb

Please sign in to comment.