Commit 3a9237ed authored by Alexander Lercher's avatar Alexander Lercher

Merge branch 'feature/business-logic-interface' into develop

parents 9ca7503c fca8e1d2
......@@ -69,3 +69,18 @@ else:
REST_GATEWAY_DB_PORT = 30402
#endregion Rest Gateway
#region Participation Hub
## Business Logic
if server:
BUSINESS_LOGIC_HOSTNAME = 'business-logic'
BUSINESS_LOGIC_DB_HOSTNAME = f'{BUSINESS_LOGIC_HOSTNAME}-db'
BUSINESS_LOGIC_REST_PORT = 80
BUSINESS_LOGIC_DB_PORT = 27017
else:
BUSINESS_LOGIC_HOSTNAME = 'articonf1.itec.aau.at'
BUSINESS_LOGIC_DB_HOSTNAME = 'articonf1.itec.aau.at'
BUSINESS_LOGIC_REST_PORT = 30420
BUSINESS_LOGIC_DB_PORT = 30421
#endregion Participation Hub
FROM python:3
LABEL maintainer="Alexander Lercher"
ENV http_proxy http://proxy.uni-klu.ac.at:3128/
ENV https_proxy http://proxy.uni-klu.ac.at:3128/
RUN apt-get update
EXPOSE 5000
WORKDIR /app
COPY src/participation-hub/business-logic-microservice/app/requirements.txt /app/
RUN pip install -r requirements.txt
COPY src/modules/ /app/
COPY src/participation-hub/business-logic-microservice/app/ /app/
RUN chmod a+x main.py
CMD ["python", "./main.py"]
\ No newline at end of file
# Business Logic Microservice
The business model microservice serves as an interface for the individual use cases. Here, schema information for all use cases is stored to enable context-aware processing.
## Technologies
- Python 3.x
- Docker
- Kubernetes
\ No newline at end of file
from typing import Dict, List
class LayerAdapter:
'''
represents the mapping from an internal layer with a set of attributes and what
attributes from the dataset correspond to each one
'''
def __init__(self, name: str, use_case: str, properties: Dict[str, str], cluster_properties: List[str]):
'''
Creates a new instance of LayerAdapter
@params:
name - Required : unique identifier for the layer
properties - Required : maps InternalPropertyName->DatasetPropertyName
cluster_properties - Required : subset of the keys of properties, marks properties to cluster with
use_case - Required : identifier for the use-case this layer belongs to
'''
self.name = name
self.properties = properties
self.use_case = use_case
for prop in cluster_properties:
if prop not in properties.keys():
raise ValueError(f"{prop} is no property in the layer!")
self.cluster_properties = cluster_properties
def add_mapping(self, internal: str, external: str):
'''
Add a new mapping to the layer. This mapping consists of an internal representation.
@params:
internal - Required: string identifier used internally, f.e. "startTime"
external - Required: string identifier the column has in the external dataset f.e. "arrivalTimeOfCustomer"
'''
self.properties[internal] = external
def delete_mapping(self, internal: str):
'''
Removes a mapping from the layer. Raises a ValueError if the mapping identified by the internal representation
does not exist.
@params:
internal - Required: string identifier used internally, f.e. "startTime"
'''
if internal not in self.properties.keys():
raise ValueError(f"Attribute {internal} is not an internal attribute!")
if internal in self.cluster_properties:
self.delete_cluster_mapping(internal)
del self.properties[internal]
def add_cluster_mapping(self, attribute:str):
'''
Adds an attribute of the internal representations to the set of attributes to cluster after. The set of cluster
attribute is always a subset of self.properties.keys().
@params:
attribute - Required: string identifier used internally, f.e. "startTime"
'''
if attribute not in self.properties.keys():
raise ValueError(f"Attribute {attribute} is not an internal attribute!")
if attribute not in self.cluster_properties:
self.cluster_properties.append(attribute)
def delete_cluster_mapping(self, attribute:str):
'''
Removes an attribute from the set of cluster-attributes. Raises a ValueError if either the attribute does not exist
as an internal representation or if the attribute exists, but was not added as a cluster-attribute yet
@params:
attribute - Required: string identifier used internally, f.e. "startTime"
'''
if attribute not in self.properties.keys():
raise ValueError(f"Attribute {attribute} is not an internal attribute!")
self.cluster_properties.remove(attribute)
def to_serializable_dict(self) -> Dict:
return {
"name": self.name,
"properties": self.properties,
"cluster_properties": self.cluster_properties,
"use_case": self.use_case
}
@staticmethod
def from_serializable_dict(user_dict: Dict):
'''
creates a layer object from a dictionary. has to have the following keys:
- name
- properties
- cluster_properties
- use_case
'''
return LayerAdapter(
user_dict["name"],
user_dict["use_case"],
user_dict["properties"],
user_dict["cluster_properties"]
)
from typing import Dict, List
class UseCase:
'''
represents the mapping from an internal layer with a set of attributes and what
attributes from the dataset correspond to each one
'''
def __init__(self, name: str):
'''
Creates a new instance of LayerAdapter
@params:
name - Required : unique identifier for the layer
properties - Required : maps InternalPropertyName->DatasetPropertyName
cluster_properties - Required : subset of the keys of properties, marks properties to cluster with
use_case - Required : identifier for the use-case this layer belongs to
'''
self.name = name
def to_serializable_dict(self) -> Dict:
return {
"name": self.name,
}
@staticmethod
def from_serializable_dict(user_dict: Dict):
'''
creates a layer object from a dictionary. has to have the following keys:
- name
- properties
- cluster_properties
- use_case
'''
return UseCase(
user_dict["name"],
)
# global imports (dont't worry, red is normal)
import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
from db.entities.layer_adapter import LayerAdapter
from db.entities.use_case import UseCase
import pymongo
import json
from typing import List, Dict
class Repository(MongoRepositoryBase):
'''This is a repository for MongoDb.'''
def __init__(self):
super().__init__(netconst.BUSINESS_LOGIC_DB_HOSTNAME,
netconst.BUSINESS_LOGIC_DB_PORT,
'rest-gateway-db')
self._adapter_collection = 'layer_adapters'
self._use_case_collection = 'use_cases'
def delete_all_use_cases_with_name(self, name: str):
collection = self._database[self._use_case_collection]
collection.delete_many({"name": name})
def delete_all_layers(self):
collection = self._database[self._adapter_collection]
collection.delete_many({})
def delete_all_use_cases(self):
collection = self._database[self._use_case_collection]
collection.delete_many({})
def all_use_cases(self) -> List[UseCase]:
dicts = list(super().get_entries(self._use_case_collection, projection={'_id': False}))
return [UseCase.from_serializable_dict(d) for d in dicts]
def put_use_case(self, use_case_name: str):
use_cases = self.all_use_cases()
existing_use_cases = list(filter(lambda use_case: use_case.name == use_case_name, use_cases))
if len(existing_use_cases) == 0:
use_case = UseCase(use_case_name)
super().insert_entry(self._use_case_collection, use_case.to_serializable_dict())
def add(self, adapter : LayerAdapter):
self.put_use_case(adapter.use_case)
super().insert_entry(self._adapter_collection, adapter.to_serializable_dict())
def one_by_name_and_usecase(self, name : str, use_case: str) -> LayerAdapter:
return list(super().get_entries(self._adapter_collection, selection={"name": name, "use_case": use_case}))
def update_use_case(self, adapter : LayerAdapter, use_case: str):
collection = self._database[self._adapter_collection]
collection.update_one({"name":adapter.name, "use_case": use_case}, {"$set": adapter.to_serializable_dict()})
def update(self, adapter : LayerAdapter):
collection = self._database[self._adapter_collection]
collection.update_one({"name":adapter.name, "use_case": adapter.use_case}, {"$set": adapter.to_serializable_dict()})
def delete_all_with_name_and_use_case(self, name: str, use_case: str):
collection = self._database[self._adapter_collection]
collection.delete_many({"name": name, "use_case": use_case})
def all(self) -> List[Dict]:
result = super().get_entries(self._adapter_collection, projection={'_id': False})
return list(result)
def all_for_use_case(self, use_case: str) -> List[Dict]:
result = super().get_entries(self._adapter_collection, projection={'_id': False}, selection={"use_case": use_case})
return list(result)
\ No newline at end of file
from flask import request
def echo():
return request.json
\ No newline at end of file
# add modules folder to interpreter path
import sys
import os
from pathlib import Path
from typing import Dict, Any
modules_path = '../../../modules/'
if os.path.exists(modules_path):
sys.path.insert(1, modules_path)
# load swagger config
import connexion
from security import swagger_util
app = connexion.App(__name__, specification_dir='configs/')
from db.entities.layer_adapter import LayerAdapter
@app.route('/', methods=['GET'])
def api_root():
return 'Endpoint of business-logic-microservice!'
# SSL configuration
try:
certificate_path = os.environ['ARTICONF_CERTIFICATE_PATH']
except KeyError:
certificate_path = '/srv/articonf/'
context = (os.path.normpath(f'{certificate_path}/articonf1.crt'), os.path.normpath(f'{certificate_path}/articonf1.key')) # certificate and key files
# Local Mode
try:
print("Running with local settings...")
local = os.environ['ARTICONF_LOCAL']
app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger_local.yml")),
resolver = connexion.RestyResolver("cms_rest_api"))
except KeyError:
app.add_api(swagger_util.get_bundled_specs(Path("configs/swagger.yml")),
resolver = connexion.RestyResolver("cms_rest_api"))
# start app
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=False, ssl_context=context)
astroid==2.4.2
attrs==19.3.0
autopep8==1.5.4
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
clickclick==1.2.2
colorama==0.4.3
connexion==2.7.0
Flask==1.1.2
idna==2.10
importlib-metadata==1.7.0
inflection==0.5.0
isort==4.3.21
itsdangerous==1.1.0
Jinja2==2.11.2
jsonschema==3.2.0
lazy-object-proxy==1.4.3
MarkupSafe==1.1.1
mccabe==0.6.1
openapi-spec-validator==0.2.9
prance==0.19.0
pycodestyle==2.6.0
pylint==2.5.3
pymongo==3.11.0
pyrsistent==0.16.0
PyYAML==5.3.1
requests==2.24.0
semver==2.10.2
six==1.15.0
swagger-ui-bundle==0.0.8
toml==0.10.1
typed-ast==1.4.1
urllib3==1.25.10
Werkzeug==1.0.1
wrapt==1.12.1
zipp==3.1.0
#global imports
from db.entities.layer_adapter import LayerAdapter
from services.layer_adapter_service import LayerAdapterService
import json
from flask import Response, request
def use_cases():
return [adapter.to_serializable_dict() for adapter in LayerAdapterService.all_use_cases()]
def delete_uses_cases():
LayerAdapterService.delete_all_use_cases()
return Response(status=200)
def all():
return LayerAdapterService.all()
def get_all_for_use_case(use_case: str):
return LayerAdapterService.all_for_use_case(use_case)
def add(name: str, use_case: str):
'''
add an empty layer to the DB
@params:
name - Required : unique identifier for the layer
use_case - Required : String-identifier for the use-case
'''
if LayerAdapterService.one(name, use_case) != None:
return Response(status=400, response=f"Layer with name '{name}' already exists!")
LayerAdapterService.add(name, use_case)
return Response(status=200)
def add_complete():
'''
add a layer already containing attribute mappings to the BD.
'''
data = request.json
if "name" not in data or "properties" not in data or "cluster_properties" not in data or "use_case" not in data:
return Response(status=400, response=f"Field missing! Fields required: (name, properties, cluster_properties)")
layer = LayerAdapterService.one(data["name"], data["use_case"])
if layer != None:
return Response(status=400, response=f'Layer with name "{data["name"]}" already exists!')
try:
layer_new = LayerAdapter.from_serializable_dict(data)
LayerAdapterService.add_complete(layer_new)
return Response(status=200)
except BaseException as e:
print(f"Exception: {e}")
return Response(status=400)
def one(name: str, use_case: str):
'''
fetch a single layer from the DB
@params:
name - Required : unique identifier for the layer
use_case - Required : String-identifier for the use-case
'''
layer = LayerAdapterService.one(name, use_case)
if layer == None:
return Response(status=404, response=f"Layer with name '{name}' does not exist!")
return Response(status=200, response=json.dumps(layer.to_serializable_dict()))
def add_mapping_for_all(use_case: str):
'''
add a new mapping to each layer of the use-case
@params:
use_case - Required : String-identifier for the use-case
'''
data = request.json
if "internal" not in data or "external" not in data:
return Response(status=400, response=f"Field missing! Fields required: (internal, external)")
layers = LayerAdapterService.all_for_use_case(use_case)
layers = [LayerAdapter.from_serializable_dict(d) for d in layers]
for layer in layers:
layer.add_mapping(data["internal"], data["external"])
LayerAdapterService.update(layer)
return Response(status=200)
def add_mapping(name: str, use_case: str):
'''
add a new mapping to the layer identified by name
@params:
name - Required : unique identifier for the layer
use_case - Required : String-identifier for the use-case
'''
layer = LayerAdapterService.one(name, use_case)
if layer == None:
return Response(status=404, response=f"Layer with name '{name}' does not exist!")
data = request.json
if "internal" not in data or "external" not in data:
return Response(status=400, response=f"Field missing! Fields required: (internal, external)")
layer.add_mapping(data["internal"], data["external"])
LayerAdapterService.update(layer)
return Response(status=200)
def update_use_case_for_all(use_case: str):
'''
update the use-case for each layer in the use-case
this changes the use-case of all layers, meaning the use-case used
for this request might not be usable after the request
@params:
use_case - Required : String-identifier for the use-case
'''
data = request.json
if "use_case" not in data:
return Response(status=400, response=f"Field missing! Fields required: (use_case)")
layers = LayerAdapterService.all_for_use_case(use_case)
layers = [LayerAdapter.from_serializable_dict(d) for d in layers]
for layer in layers:
layer.use_case = data["use_case"]
LayerAdapterService.update_use_case(layer, use_case)
return Response(status=200)
def delete_mapping_for_all(use_case: str):
'''
delete a mapping from each layer in the use-case
@params:
use_case - Required : String-identifier for the use-case
'''
data = request.json
if "internal" not in data:
return Response(status=400, response=f"Field missing! Fields required: (internal)")
layers = LayerAdapterService.all_for_use_case(use_case)
layers = [LayerAdapter.from_serializable_dict(d) for d in layers]
for layer in layers:
try:
layer.delete_mapping(data["internal"])
except ValueError:
continue
LayerAdapterService.update(layer)
return Response(status=200)
def delete_mapping(name: str, use_case: str):
'''
delete a mapping from the layer identified by name
@params:
name - Required : unique identifier for the layer
use_case - Required : String-identifier for the use-case
'''
layer = LayerAdapterService.one(name, use_case)
if layer == None:
return Response(status=404, response=f"Layer with name '{name}' does not exist!")
data = request.json
if "internal" not in data:
return Response(status=400, response=f"Field missing! Fields required: (internal)")
try:
layer.delete_mapping(data["internal"])
LayerAdapterService.update(layer)
except ValueError:
return Response(status=400, response=f'{data["internal"]} is not a property of the layer!')
return Response(status=200)
def add_cluster_mapping(name: str, use_case: str):
'''
add a mapped property to the list of properties to cluster with
@params:
name - Required : unique identifier for the layer
use_case - Required : String-identifier for the use-case
'''
layer = LayerAdapterService.one(name, use_case)
if layer == None:
return Response(status=404, response=f"Layer with name '{name}' does not exist!")
data = request.json
if "attribute" not in data:
return Response(status=400, response=f"Field missing! Fields required: (attribute)")
try:
layer.add_cluster_mapping(data["attribute"])
LayerAdapterService.update(layer)
return Response(status=200)
except:
return Response(status=400, response=f'{data["attribute"]} is no attribute in the layer!')
def delete_cluster_mapping(name: str, use_case: str):
'''
remove a mapped property from the list of properties to cluster with
@params:
name - Required : unique identifier for the layer
use_case - Required : String-identifier for the use-case
'''
layer = LayerAdapterService.one(name, use_case)
if layer == None:
return Response(status=404, response=f"Layer with name '{name}' does not exist!")
data = request.json
if "attribute" not in data:
return Response(status=400, response=f"Field missing! Fields required: (attribute)")
try:
layer.delete_cluster_mapping(data["attribute"])
LayerAdapterService.update(layer)
return Response(status=200)
except ValueError as e:
print(e)
return Response(status=400, response=f'{data["attribute"]} is no attribute in the layer!')
def delete_one(name: str, use_case: str):
'''
delete a layer and all its mappings from the Db
@params:
name - Required : unique identifier for the layer
use_case - Required : String-identifier for the use-case
'''
layer = LayerAdapterService.one(name, use_case)
if layer == None:
return Response(status=404, response=f"Layer with name '{name}' does not exist!")
LayerAdapterService.delete(layer)
return Response(status=200)
#global imports
from db.repository import Repository
from db.entities.layer_adapter import LayerAdapter
from typing import List
class LayerAdapterService:
_repository = Repository()
@staticmethod
def all_use_cases() -> List[str]:
return LayerAdapterService._repository.all_use_cases()
@staticmethod
def delete_all_use_cases():
LayerAdapterService._repository.delete_all_use_cases()
LayerAdapterService._repository.delete_all_layers()
@staticmethod
def delete_use_case(name:str):
LayerAdapterService._repository.delete_all_use_cases_with_name(name)
@staticmethod
def all() -> List[LayerAdapter]:
'''
Return all currently defined layers
'''
return LayerAdapterService._repository.all()
@staticmethod
def all_for_use_case(use_case: str) -> List[LayerAdapter]:
'''
Return all currently defined layers that belong to the
given use-case
'''
return LayerAdapterService._repository.all_for_use_case(use_case)
@staticmethod
def update(layer: LayerAdapter):
'''
Overwrite the stored instance with the given one which is
identified by the layer name
@params:
layer - Required : layer object holding the current data
'''
LayerAdapterService._repository.update(layer)
@staticmethod
def update_use_case(layer: LayerAdapter, use_case:str):
'''
Overwrite the stored instance with the given one which is
identified by the layer name
@params:
layer - Required : layer object holding the current data
'''
LayerAdapterService._repository.update_use_case(layer, use_case)
@staticmethod
def add(name: str, use_case: str):
'''
Add a new layer to the DB. Attribute mapping and cluster
attributes will be empty per default
@params:
name - Required : Unique name for a layer.
use_case - Required : String-identifier for the use-case
'''
adapter_new = LayerAdapter(name, use_case, {}, [])
LayerAdapterService._repository.add(adapter_new)
@staticmethod
def add_complete(layer: LayerAdapter):
'''
Add a new layer to the DB. Attribute mappings and cluster
attributes of the given layer are used
@params:
layer - Required : layer object holding correct data
'''
LayerAdapterService._repository.add(layer)
@staticmethod
def delete(layer: LayerAdapter):
'''
delete all layers with the given name.
@params:
layer - Required : layer object to remove from the DB
'''
use_case = layer.use_case
LayerAdapterService._repository.delete_all_with_name_and_use_case(layer.name, use_case)
# remove use-case if it is not needed
layers_in_use_case = LayerAdapterService.all_for_use_case(use_case)
if len(layers_in_use_case) == 0:
LayerAdapterService.delete_use_case(use_case)
@staticmethod
def one(name: str, use_case: str) -> LayerAdapter:
'''
Retrieve a single layer from the DB. Returns None if no layer
was found under this name.
@params:
name - Required : Unique name for a layer
use_case - Required : String-identifier for the use-case
'''
result = LayerAdapterService._repository.one_by_name_and_usecase(name, use_case)
if len(result) == 1:
return LayerAdapter.from_serializable_dict(result[0])
else:
return None
import unittest
from db.entities.layer_adapter import LayerAdapter
class Layer_Adapter_Test(unittest.TestCase):
def test_valid_adapter(self):
adapter1 = LayerAdapter("layer1", {"a":"b", "c":"d"}, ["a"])
print(adapter1.to_serializable_dict)
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
apiVersion: v1
kind: Service
metadata:
name: business-logic
spec:
type: LoadBalancer
selector:
app: business-logic
ports:
- name: http
port: 80
targetPort: 5000
nodePort: 30420
protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: business-logic
spec:
replicas: 1
selector:
matchLabels:
app: business-logic
template:
metadata:
labels:
app: business-logic
spec:
containers:
- name: business-logic
image: alexx882/business-logic-microservice
ports:
- containerPort: 5000
volumeMounts:
- mountPath: /srv/articonf
name: articonf
volumes:
- name: articonf
hostPath:
path: /srv/articonf
type: Directory
---
apiVersion: v1
kind: Service
metadata:
name: business-logic-db
spec:
type: LoadBalancer
selector:
app: business-logic-db
ports:
- name: http
port: 27017
targetPort: 27017
nodePort: 30421
protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: business-logic-db
spec:
replicas: 1
selector:
matchLabels:
app: business-logic-db
template:
metadata:
labels:
app: business-logic-db
spec:
containers:
- name: business-logic-db
image: mongo
env:
- name: MONGO_INITDB_ROOT_USERNAME
value: root
- name: MONGO_INITDB_ROOT_PASSWORD
value: root
ports:
- containerPort: 27017
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment