Commit 4d9ebc89 authored by Manuel's avatar Manuel

businessLogic: restructured layer system and added schema information

parent 28641988
......@@ -10,3 +10,5 @@ src/modules/certificate/articonf1.key
src/modules/certificate/articonf1.crt
src/modules/certificate/articonf1-chain.crt
src/modules/security/regular_user_credentials.json
......@@ -7,14 +7,14 @@ class LayerAdapter:
attributes from the dataset correspond to each one
'''
def __init__(self, name: str, use_case: str, properties: Dict[str, str], cluster_properties: List[str]):
def __init__(self, name: str, use_case: str, properties: List[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
properties - Required : list of mappings from the Schema
cluster_properties - Required : subset of properties to indicate what properties are used for clustering
use_case - Required : identifier for the use-case this layer belongs to
'''
self.name = name
......@@ -22,12 +22,12 @@ class LayerAdapter:
self.use_case = use_case
for prop in cluster_properties:
if prop not in properties.keys():
if prop not in properties:
raise ValueError(f"{prop} is no property in the layer!")
self.cluster_properties = cluster_properties
def add_mapping(self, internal: str, external: str):
def add_mapping(self, internal: str):
'''
Add a new mapping to the layer. This mapping consists of an internal representation.
......@@ -35,23 +35,24 @@ class LayerAdapter:
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
if internal not in self.properties:
self.properties.append(internal)
def delete_mapping(self, internal: str):
'''
Removes a mapping from the layer. Raises a ValueError if the mapping identified by the internal representation
Removes a proprety from the layer. Raises a ValueError if the property 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():
if internal not in self.properties:
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]
self.properties.remove(internal)
def add_cluster_mapping(self, attribute:str):
'''
......@@ -61,7 +62,7 @@ class LayerAdapter:
@params:
attribute - Required: string identifier used internally, f.e. "startTime"
'''
if attribute not in self.properties.keys():
if attribute not in self.properties:
raise ValueError(f"Attribute {attribute} is not an internal attribute!")
if attribute not in self.cluster_properties:
......@@ -70,12 +71,12 @@ class LayerAdapter:
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
as an internal representation
@params:
attribute - Required: string identifier used internally, f.e. "startTime"
'''
if attribute not in self.properties.keys():
if attribute not in self.properties:
raise ValueError(f"Attribute {attribute} is not an internal attribute!")
self.cluster_properties.remove(attribute)
......
from typing import Dict
class Schema:
def __init__(self, use_case:str, mappings:Dict[str, str]):
self.use_case = use_case
self.mappings = mappings
def add_mapping(self, internal:str, external:str):
if internal not in self.mappings.keys():
self.mappings[internal] = external
def to_serializable_dict(self) -> Dict:
return {
"use_case": self.use_case,
"mappings": self.mappings
}
@staticmethod
def from_serializable_dict(serializable_dict: Dict):
return Schema(
serializable_dict["use_case"],
serializable_dict["mappings"]
)
\ No newline at end of file
......@@ -3,6 +3,7 @@ import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
from db.entities.layer_adapter import LayerAdapter
from db.entities.use_case import UseCase
from db.entities.schema import Schema
import pymongo
import json
......@@ -18,6 +19,39 @@ class Repository(MongoRepositoryBase):
self._adapter_collection = 'layer_adapters'
self._use_case_collection = 'use_cases'
self._schema_collection = 'schemas'
def add_schema(self, schema: Schema):
self.put_use_case(schema.use_case)
super().insert_entry(self._schema_collection, schema.to_serializable_dict())
def all_schemas(self) -> List[Schema]:
result = super().get_entries(self._schema_collection, projection={'_id': False})
return [Schema.from_serializable_dict(row) for row in list(result)]
def schema_for_use_case(self, use_case: str) -> List[Dict]:
result = super().get_entries(self._schema_collection, projection={'_id': False}, selection={"use_case": use_case})
result = list(result)
if len(result) > 1:
raise ValueError("No more than 1 Schema allowed per use-case!")
if len(result) == 1:
return Schema.from_serializable_dict(result[0])
return None
def delete_schema_with_use_case(self, use_case: str):
collection = self._database[self._schema_collection]
collection.delete_one({"use_case": use_case})
def delete_all_schemas(self):
collection = self._database[self._schema_collection]
collection.delete_many({})
def update_schema(self, schema: Schema):
collection = self._database[self._schema_collection]
collection.update_one({"use_case": schema.use_case}, {"$set": schema.to_serializable_dict()})
def delete_all_use_cases_with_name(self, name: str):
collection = self._database[self._use_case_collection]
......
......@@ -16,7 +16,52 @@ def delete_uses_cases():
def all():
return LayerAdapterService.all()
def all_schemas():
return [schema.to_serializable_dict() for schema in LayerAdapterService.all_schemas()]
def schema_for_use_case(use_case:str):
schema = LayerAdapterService.schema_for_use_case(use_case)
if schema != None:
return Response(status=200, response=json.dumps(schema.to_serializable_dict()))
return Response(status=404, response=f"Schema {use_case} does not exist")
def add_mapping_to_schema(use_case:str):
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)")
schema = LayerAdapterService.schema_for_use_case(use_case)
if schema == None:
print("schema not there, creating it...")
schema = LayerAdapterService.put_schema(use_case)
schema.add_mapping(data["internal"], data["external"])
LayerAdapterService.update_schema(schema)
return Response(status=200)
def delete_schema_for_use_case(use_case:str):
schema = LayerAdapterService.schema_for_use_case(use_case)
if schema == None:
return Response(status=404, response=f"Schema {use_case} does not exist")
LayerAdapterService.delete_schema(use_case)
return Response(status=200)
##########
# LAYERS #
##########
def get_all_for_use_case(use_case: str):
'''
get all layers assigned to the given use_case
'''
return LayerAdapterService.all_for_use_case(use_case)
def add(name: str, use_case: str):
......@@ -47,14 +92,20 @@ def add_complete():
if layer != None:
return Response(status=400, response=f'Layer with name "{data["name"]}" already exists!')
# check if schema contains mapping
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)
try:
LayerAdapterService.check_layer(layer_new)
except ValueError as e:
return Response(status = 400, response=f"{e}")
LayerAdapterService.add_complete(layer_new)
return Response(status=200)
def one(name: str, use_case: str):
'''
......@@ -80,14 +131,25 @@ def add_mapping_for_all(use_case: str):
'''
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)")
if "internal" not in data:
return Response(status=400, response=f"Field missing! Fields required: (internal)")
# check if schema contains mapping
schema = LayerAdapterService.put_schema(use_case)
if data["internal"] not in schema.mappings:
return Response(status=400, response=f'{data["internal"]} is not existent in the schema!')
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"])
layer.add_mapping(data["internal"])
try:
LayerAdapterService.check_layer(layer)
except ValueError as e:
return Response(status = 400, response=f"{e}")
LayerAdapterService.update(layer)
return Response(status=200)
......@@ -107,10 +169,15 @@ def add_mapping(name: str, use_case: str):
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)")
if "internal" not in data:
return Response(status=400, response=f"Field missing! Fields required: (internal)")
# check if schema contains mapping
schema = LayerAdapterService.put_schema(use_case)
if data["internal"] not in schema.mappings:
return Response(status=400, response=f'{data["internal"]} is not existent in the schema!')
layer.add_mapping(data["internal"], data["external"])
layer.add_mapping(data["internal"])
LayerAdapterService.update(layer)
return Response(status=200)
......@@ -134,6 +201,12 @@ def update_use_case_for_all(use_case: str):
for layer in layers:
layer.use_case = data["use_case"]
try:
LayerAdapterService.check_layer(layer)
except ValueError as e:
return Response(status = 400, response=f"{e}")
LayerAdapterService.update_use_case(layer, use_case)
return Response(status=200)
......@@ -164,7 +237,7 @@ def delete_mapping_for_all(use_case: str):
def delete_mapping(name: str, use_case: str):
'''
delete a mapping from the layer identified by name
delete a mapping from the layer identified by the internal representation
@params:
name - Required : unique identifier for the layer
......@@ -257,3 +330,10 @@ def delete_one(name: str, use_case: str):
LayerAdapterService.delete(layer)
return Response(status=200)
def delete_all_layers():
'''
delete all layers from the DB
'''
LayerAdapterService.delete_all_layers()
return Response(status=200)
\ No newline at end of file
#global imports
from db.repository import Repository
from db.entities.layer_adapter import LayerAdapter
from db.entities.schema import Schema
from typing import List
......@@ -9,6 +10,44 @@ class LayerAdapterService:
_repository = Repository()
@staticmethod
def all_schemas() -> List[Schema]:
return LayerAdapterService._repository.all_schemas()
@staticmethod
def schema_for_use_case(use_case: str):
LayerAdapterService._repository.put_use_case(use_case)
return LayerAdapterService._repository.schema_for_use_case(use_case)
@staticmethod
def put_schema(use_case: str):
schema = LayerAdapterService.schema_for_use_case(use_case)
if schema == None:
schema = Schema(use_case, mappings={})
LayerAdapterService._repository.add_schema(schema)
return schema
@staticmethod
def check_layer(layer: LayerAdapter):
'''
checks if the given layer has correct mappings regarding the schema of the use_case
'''
schema = LayerAdapterService.put_schema(layer.use_case)
for p in layer.properties:
if p not in schema.mappings:
raise ValueError(f'{p} is not existent in the schema!')
@staticmethod
def delete_schema(use_case:str):
LayerAdapterService._repository.delete_schema_with_use_case(use_case)
@staticmethod
def update_schema(schema: Schema):
LayerAdapterService._repository.update_schema(schema)
@staticmethod
def all_use_cases() -> List[str]:
return LayerAdapterService._repository.all_use_cases()
......@@ -16,6 +55,7 @@ class LayerAdapterService:
@staticmethod
def delete_all_use_cases():
LayerAdapterService._repository.delete_all_use_cases()
LayerAdapterService._repository.delete_all_schemas()
LayerAdapterService._repository.delete_all_layers()
@staticmethod
......@@ -69,7 +109,7 @@ class LayerAdapterService:
name - Required : Unique name for a layer.
use_case - Required : String-identifier for the use-case
'''
adapter_new = LayerAdapter(name, use_case, {}, [])
adapter_new = LayerAdapter(name, use_case, [], [])
LayerAdapterService._repository.add(adapter_new)
......@@ -95,10 +135,9 @@ class LayerAdapterService:
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 delete_all_layers():
LayerAdapterService._repository.delete_all_layers()
@staticmethod
def one(name: str, use_case: str) -> LayerAdapter:
......
# global import, red is normal don't worry
import network_constants
import os
import json
import requests
class TokenManager:
_instance = None
@staticmethod
def getInstance():
''' Static access method. '''
if TokenManager._instance == None:
TokenManager._instance = TokenManager()
return TokenManager._instance
def __init__(self):
self._token = None
def getToken(self) -> str:
if self._token == None:
try:
credentials_path = '../../../modules/security/'
except KeyError:
credentials_path = '/srv/articonf/'
print("Looking for credentials at ... "+str(credentials_path))
with open(f'{credentials_path}regular_user_credentials.json') as file:
credentials = json.loads(file.read())
url = f'https://{network_constants.REST_GATEWAY_HOSTNAME}:{network_constants.REST_GATEWAY_REST_PORT}/api/tokens'
response = requests.post(
url,
verify = False,
proxies = { "http":None, "https":None },
json = credentials
)
data = json.loads(response.text)
self._token = data["token"]
return self._token
\ 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