aboutsummaryrefslogtreecommitdiffstats
path: root/runtime
diff options
context:
space:
mode:
Diffstat (limited to 'runtime')
-rw-r--r--runtime/__init__.py17
-rw-r--r--runtime/model_api.py215
-rw-r--r--runtime/models/__init__.py17
-rw-r--r--runtime/models/api/__init__.py17
-rw-r--r--runtime/models/api/model_request.py48
-rw-r--r--runtime/models/api/model_response.py31
-rw-r--r--runtime/models/api/optim_request.py60
-rw-r--r--runtime/models/api/optim_response.py30
-rw-r--r--runtime/optim_engine.py79
-rw-r--r--runtime/solvers/__init__.py17
-rw-r--r--runtime/solvers/mzn/__init__.py17
-rw-r--r--runtime/solvers/mzn/mzn_solver.py102
-rw-r--r--runtime/solvers/py/__init__.py17
-rw-r--r--runtime/solvers/py/py_solver.py92
14 files changed, 759 insertions, 0 deletions
diff --git a/runtime/__init__.py b/runtime/__init__.py
new file mode 100644
index 0000000..2aa67d8
--- /dev/null
+++ b/runtime/__init__.py
@@ -0,0 +1,17 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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/runtime/model_api.py b/runtime/model_api.py
new file mode 100644
index 0000000..fd87333
--- /dev/null
+++ b/runtime/model_api.py
@@ -0,0 +1,215 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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 json
+import traceback
+
+import mysql.connector
+from flask import g, Flask, Response
+
+from osdf.config.base import osdf_config
+from osdf.logging.osdf_logging import debug_log, error_log
+from osdf.operation.exceptions import BusinessException
+
+
+def init_db():
+ if is_db_enabled():
+ get_db()
+
+
+def get_db():
+ """Opens a new database connection if there is none yet for the
+ current application context.
+ """
+ if not hasattr(g, 'pg'):
+ properties = osdf_config['deployment']
+ host, db_port, db = properties["osdfDatabaseHost"], properties["osdfDatabasePort"], \
+ properties.get("osdfDatabaseSchema")
+ user, password = properties["osdfDatabaseUsername"], properties["osdfDatabasePassword"]
+ g.pg = mysql.connector.connect(host=host, port=db_port, user=user, password=password, database=db)
+ return g.pg
+
+
+def close_db():
+ """Closes the database again at the end of the request."""
+ if hasattr(g, 'pg'):
+ g.pg.close()
+
+
+app = Flask(__name__)
+
+
+def create_model_data(model_api):
+ with app.app_context():
+ try:
+ model_info = model_api['modelInfo']
+ model_id = model_info['modelId']
+ debug_log.debug(
+ "persisting model_api {}".format(model_id))
+ connection = get_db()
+ cursor = connection.cursor(buffered=True)
+ query = "SELECT model_id FROM optim_model_data WHERE model_id = %s"
+ values = (model_id,)
+ cursor.execute(query, values)
+ if cursor.fetchone() is None:
+ query = "INSERT INTO optim_model_data (model_id, model_content, description, solver_type) VALUES " \
+ "(%s, %s, %s, %s)"
+ values = (model_id, model_info['modelContent'], model_info.get('description'), model_info['solver'])
+ cursor.execute(query, values)
+ g.pg.commit()
+
+ debug_log.debug("A record successfully inserted for request_id: {}".format(model_id))
+ return retrieve_model_data(model_id)
+ close_db()
+ else:
+ query = "UPDATE optim_model_data SET model_content = %s, description = %s, solver_type = %s where " \
+ "model_id = %s "
+ values = (model_info['modelContent'], model_info.get('description'), model_info['solver'], model_id)
+ cursor.execute(query, values)
+ g.pg.commit()
+
+ return retrieve_model_data(model_id)
+ close_db()
+ except Exception as err:
+ error_log.error("error for request_id: {} - {}".format(model_id, traceback.format_exc()))
+ close_db()
+ raise BusinessException(err)
+
+
+def retrieve_model_data(model_id):
+ status, resp_data = get_model_data(model_id)
+
+ if status == 200:
+ resp = json.dumps(build_model_dict(resp_data))
+ return build_response(resp, status)
+ else:
+ resp = json.dumps({
+ 'modelId': model_id,
+ 'statusMessage': "Error retrieving the model data for model {} due to {}".format(model_id, resp_data)
+ })
+ return build_response(resp, status)
+
+
+def build_model_dict(resp_data, content_needed=True):
+ resp = {'modelId': resp_data[0], 'description': resp_data[2] if resp_data[2] else '',
+ 'solver': resp_data[3]}
+ if content_needed:
+ resp.update({'modelContent': resp_data[1]})
+ return resp
+
+
+def build_response(resp, status):
+ response = Response(resp, content_type='application/json; charset=utf-8')
+ response.headers.add('content-length', len(resp))
+ response.status_code = status
+ return response
+
+
+def delete_model_data(model_id):
+ with app.app_context():
+ try:
+ debug_log.debug("deleting model data given model_id = {}".format(model_id))
+ d = dict();
+ connection = get_db()
+ cursor = connection.cursor(buffered=True)
+ query = "delete from optim_model_data WHERE model_id = %s"
+ values = (model_id,)
+ cursor.execute(query, values)
+ g.pg.commit()
+ close_db()
+ resp = {
+ "statusMessage": "model data for modelId {} deleted".format(model_id)
+ }
+ return build_response(json.dumps(resp), 200)
+ except Exception as err:
+ error_log.error("error deleting model_id: {} - {}".format(model_id, traceback.format_exc()))
+ close_db()
+ raise BusinessException(err)
+
+
+def get_model_data(model_id):
+ with app.app_context():
+ try:
+ debug_log.debug("getting model data given model_id = {}".format(model_id))
+ d = dict();
+ connection = get_db()
+ cursor = connection.cursor(buffered=True)
+ query = "SELECT model_id, model_content, description, solver_type FROM optim_model_data WHERE model_id = %s"
+ values = (model_id,)
+ cursor.execute(query, values)
+ if cursor is None:
+ return 400, "FAILED"
+ else:
+ rows = cursor.fetchone()
+ if rows is not None:
+ index = 0
+ for row in rows:
+ d[index] = row
+ index = index + 1
+ return 200, d
+ else:
+ close_db()
+ return 500, "NOT_FOUND"
+ except Exception:
+ error_log.error("error for request_id: {} - {}".format(model_id, traceback.format_exc()))
+ close_db()
+ return 500, "FAILED"
+
+
+def retrieve_all_models():
+ status, resp_data = get_all_models()
+ model_list = []
+ if status == 200:
+ for r in resp_data:
+ model_list.append(build_model_dict(r, False))
+ resp = json.dumps(model_list)
+ return build_response(resp, status)
+
+ else:
+ resp = json.dumps({
+ 'statusMessage': "Error retrieving all the model data due to {}".format(resp_data)
+ })
+ return build_response(resp, status)
+
+
+def get_all_models():
+ with app.app_context():
+ try:
+ debug_log.debug("getting all model data".format())
+ connection = get_db()
+ cursor = connection.cursor(buffered=True)
+ query = "SELECT model_id, model_content, description, solver_type FROM optim_model_data"
+
+ cursor.execute(query)
+ if cursor is None:
+ return 400, "FAILED"
+ else:
+ rows = cursor.fetchall()
+ if rows is not None:
+ return 200, rows
+ else:
+ close_db()
+ return 500, "NOT_FOUND"
+ except Exception:
+ error_log.error("error for request_id: {}".format(traceback.format_exc()))
+ close_db()
+ return 500, "FAILED"
+
+
+def is_db_enabled():
+ return osdf_config['deployment'].get('isDatabaseEnabled', False)
diff --git a/runtime/models/__init__.py b/runtime/models/__init__.py
new file mode 100644
index 0000000..2aa67d8
--- /dev/null
+++ b/runtime/models/__init__.py
@@ -0,0 +1,17 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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/runtime/models/api/__init__.py b/runtime/models/api/__init__.py
new file mode 100644
index 0000000..2aa67d8
--- /dev/null
+++ b/runtime/models/api/__init__.py
@@ -0,0 +1,17 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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/runtime/models/api/model_request.py b/runtime/models/api/model_request.py
new file mode 100644
index 0000000..710da4b
--- /dev/null
+++ b/runtime/models/api/model_request.py
@@ -0,0 +1,48 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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 schematics.types import StringType
+from schematics.types.compound import ModelType
+
+from osdf.models.api.common import OSDFModel
+
+
+class RequestInfo(OSDFModel):
+ """Info for northbound request from client such as PCI-mS Handler"""
+ transactionId = StringType(required=True)
+ requestID = StringType(required=True)
+ sourceId = StringType(required=True)
+
+
+class OptimModelInfo(OSDFModel):
+ """Optimizer request info details."""
+ # ModelId from the database
+ modelId = StringType()
+ # type of solver (mzn, or-tools, etc.)
+ solver = StringType(required=True)
+ # Description of the model
+ description = StringType()
+ # a large blob string containing the model (which is not that
+ # problematic since models are fairly small).
+ modelContent = StringType()
+
+
+class OptimModelRequestAPI(OSDFModel):
+ """Request for Optimizer API (specific to optimization and additional metadata"""
+ requestInfo = ModelType(RequestInfo, required=True)
+ modelInfo = ModelType(OptimModelInfo, required=True)
diff --git a/runtime/models/api/model_response.py b/runtime/models/api/model_response.py
new file mode 100644
index 0000000..e4a41a5
--- /dev/null
+++ b/runtime/models/api/model_response.py
@@ -0,0 +1,31 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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 schematics.types import StringType
+
+from osdf.models.api.common import OSDFModel
+
+
+class OptimModelResponse(OSDFModel):
+ modelId = StringType()
+ # type of solver (mzn, or-tools, etc.)
+ solver = StringType()
+ # a large blob string containing the model
+ modelContent = StringType()
+ # statusMessage
+ statusMessage = StringType()
diff --git a/runtime/models/api/optim_request.py b/runtime/models/api/optim_request.py
new file mode 100644
index 0000000..4a046d2
--- /dev/null
+++ b/runtime/models/api/optim_request.py
@@ -0,0 +1,60 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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 schematics.types import BaseType, DictType, StringType, IntType
+from schematics.types.compound import ModelType
+
+from osdf.models.api.common import OSDFModel
+
+"""
+"""
+class RequestInfo(OSDFModel):
+ """Info for northbound request from client """
+ transactionId = StringType(required=True)
+ requestID = StringType(required=True)
+ callbackUrl = StringType()
+ sourceId = StringType(required=True)
+ timeout = IntType()
+
+
+class DataInfo(OSDFModel):
+ """Optimization data info"""
+ text = StringType()
+ json = DictType(BaseType)
+
+
+class OptimInfo(OSDFModel):
+ """Optimizer request info details."""
+ # ModelId from the database, if its not populated,
+ # assume that solverModel will be populated.
+ modelId = StringType()
+ # type of solver (mzn, or-tools, etc.)
+ solver = StringType()
+ # Arguments for solver
+ solverArgs = DictType(BaseType)
+ # NOTE: a large blob string containing the model (which is not that
+ # problematic since models are fairly small).
+ modelContent = StringType()
+ # Data Payload, input data for the solver
+ optData = ModelType(DataInfo)
+
+
+class OptimizationAPI(OSDFModel):
+ """Request for Optimizer API (specific to optimization and additional metadata"""
+ requestInfo = ModelType(RequestInfo, required=True)
+ optimInfo = ModelType(OptimInfo, required=True)
diff --git a/runtime/models/api/optim_response.py b/runtime/models/api/optim_response.py
new file mode 100644
index 0000000..6fd0f6b
--- /dev/null
+++ b/runtime/models/api/optim_response.py
@@ -0,0 +1,30 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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 schematics.types import StringType, BaseType
+from schematics.types.compound import DictType
+
+from osdf.models.api.common import OSDFModel
+
+
+class OptimResponse(OSDFModel):
+ transactionId = StringType(required=True)
+ requestID = StringType(required=True)
+ requestStatus = StringType(required=True)
+ statusMessage = StringType()
+ solutions = DictType(BaseType)
diff --git a/runtime/optim_engine.py b/runtime/optim_engine.py
new file mode 100644
index 0000000..4a8788e
--- /dev/null
+++ b/runtime/optim_engine.py
@@ -0,0 +1,79 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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 flask import Response
+
+from osdf.operation.exceptions import BusinessException
+from .model_api import get_model_data
+from .models.api.optim_request import OptimizationAPI
+from .solvers.mzn.mzn_solver import solve as mzn_solve
+from .solvers.py.py_solver import solve as py_solve
+
+
+def is_valid_optim_request(request_json):
+ # Method to check whether the requestinfo/optimizer value is valid.
+ opt_info = request_json['optimInfo']
+ if not opt_info.get('modelId'):
+ if not opt_info.get('modelContent') or not opt_info.get('solver'):
+ raise BusinessException('modelContent and solver needs to be populated if model_id is not set')
+ if not opt_info.get('optData'):
+ raise BusinessException('optimInfo.optData needs to be populated to solve for a problem')
+
+ return True
+
+
+def validate_request(request_json):
+ OptimizationAPI(request_json).validate()
+ if not is_valid_optim_request(request_json):
+ raise BusinessException('Invalid optim request ')
+ return True
+
+
+def process_request(request_json):
+ response_code, response_message = run_optimizer(request_json)
+ response = Response(response_message, content_type='application/json; charset=utf-8')
+ response.headers.add('content-length', len(response_message))
+ response.status_code = response_code
+ return response
+
+
+def run_optimizer(request_json):
+ validate_request(request_json)
+
+ model_content, solver = get_model_content(request_json)
+
+ if solver == 'mzn':
+ return mzn_solve(request_json, model_content)
+ elif solver == 'py':
+ return py_solve(request_json, model_content)
+ raise BusinessException('Unsupported optimization solver requested {} '.format(solver))
+
+
+def get_model_content(request_json):
+ model_id = request_json['optimInfo'].get('modelId')
+ if model_id:
+ status, data = get_model_data(model_id)
+ if status == 200:
+ model_content = data[1]
+ solver = data[3]
+ else:
+ raise BusinessException('model_id [{}] not found in the model database'.format(model_id))
+ else:
+ model_content = request_json['optimInfo']['modelContent']
+ solver = request_json['optimInfo']['solver']
+ return model_content, solver
diff --git a/runtime/solvers/__init__.py b/runtime/solvers/__init__.py
new file mode 100644
index 0000000..2aa67d8
--- /dev/null
+++ b/runtime/solvers/__init__.py
@@ -0,0 +1,17 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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/runtime/solvers/mzn/__init__.py b/runtime/solvers/mzn/__init__.py
new file mode 100644
index 0000000..2aa67d8
--- /dev/null
+++ b/runtime/solvers/mzn/__init__.py
@@ -0,0 +1,17 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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/runtime/solvers/mzn/mzn_solver.py b/runtime/solvers/mzn/mzn_solver.py
new file mode 100644
index 0000000..cf002e7
--- /dev/null
+++ b/runtime/solvers/mzn/mzn_solver.py
@@ -0,0 +1,102 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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 json
+from datetime import datetime
+
+from pymzn import Status, minizinc, cbc, gecode, chuffed, or_tools
+
+from osdf.utils.file_utils import delete_file_folder
+
+error_status_map = {
+ Status.INCOMPLETE: "incomplete",
+ Status.COMPLETE: "complete",
+ Status.UNSATISFIABLE: "unsatisfiable",
+ Status.UNKNOWN: "unknown",
+ Status.UNBOUNDED: "unbounded",
+ Status.UNSATorUNBOUNDED: "unsat_or_unbounded",
+ Status.ERROR: "error"
+}
+
+solver_dict = {
+ 'cbc': cbc,
+ 'geocode': gecode,
+ 'chuffed': chuffed,
+ 'cp': chuffed,
+ 'or_tools': or_tools
+}
+
+
+def map_status(status):
+ return error_status_map.get(status, "failed")
+
+
+def solve(request_json, mzn_content):
+ req_info = request_json['requestInfo']
+ opt_info = request_json['optimInfo']
+ try:
+ mzn_solution = mzn_solver(mzn_content, opt_info)
+
+ response = {
+ 'transactionId': req_info['transactionId'],
+ 'requestID': req_info['requestID'],
+ 'requestStatus': 'done',
+ 'statusMessage': map_status(mzn_solution.status),
+ 'solutions': mzn_solution[0] if mzn_solution else {}
+ }
+ return 200, json.dumps(response)
+ except Exception as e:
+ response = {
+ 'transactionId': req_info['transactionId'],
+ 'requestID': req_info['requestID'],
+ 'requestStatus': 'failed',
+ 'statusMessage': 'Failed due to {}'.format(e)
+ }
+ return 400, json.dumps(response)
+
+
+def mzn_solver(mzn_content, opt_info):
+ args = opt_info['solverArgs']
+ solver = get_mzn_solver(args.pop('solver'))
+ mzn_opts = dict()
+
+ try:
+ file_name = persist_opt_data(opt_info)
+ mzn_opts.update(args)
+ return minizinc(mzn_content, file_name, **mzn_opts, solver=solver)
+
+ finally:
+ delete_file_folder(file_name)
+
+
+def persist_opt_data(opt_info):
+
+ if opt_info['optData'].get('json'):
+ data_content = json.dumps(opt_info['optData']['json'])
+ file_name = '/tmp/optim_engine_{}.json'.format(datetime.timestamp(datetime.now()))
+ elif opt_info['optData'].get('text'):
+ data_content = opt_info['optData']['text']
+ file_name = '/tmp/optim_engine_{}.dzn'.format(datetime.timestamp(datetime.now()))
+
+ with open(file_name, "wt") as data:
+ data.write(data_content)
+ return file_name
+
+
+def get_mzn_solver(solver):
+ return solver_dict.get(solver)
diff --git a/runtime/solvers/py/__init__.py b/runtime/solvers/py/__init__.py
new file mode 100644
index 0000000..a8aa582
--- /dev/null
+++ b/runtime/solvers/py/__init__.py
@@ -0,0 +1,17 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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.
+#
+# -------------------------------------------------------------------------
+# \ No newline at end of file
diff --git a/runtime/solvers/py/py_solver.py b/runtime/solvers/py/py_solver.py
new file mode 100644
index 0000000..6b200ab
--- /dev/null
+++ b/runtime/solvers/py/py_solver.py
@@ -0,0 +1,92 @@
+# -------------------------------------------------------------------------
+# Copyright (c) 2020 AT&T Intellectual Property
+#
+# 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 json
+import subprocess
+import traceback
+from datetime import datetime
+
+from osdf.logging.osdf_logging import error_log, debug_log
+from osdf.utils.file_utils import delete_file_folder
+
+
+def py_solver(py_content, opt_info):
+ py_file = '/tmp/custom_heuristics_{}.py'.format(datetime.timestamp(datetime.now()))
+ with open(py_file, "wt") as f:
+ f.write(py_content)
+ if opt_info['optData'].get('json'):
+ data_content = json.dumps(opt_info['optData']['json'])
+ input_file = '/tmp/optim_engine_{}.json'.format(datetime.timestamp(datetime.now()))
+ elif opt_info['optData'].get('text'):
+ data_content = opt_info['optData']['text']
+ input_file = '/tmp/optim_engine_{}.txt'.format(datetime.timestamp(datetime.now()))
+ with open(input_file, "wt") as f:
+ f.write(data_content)
+
+ output_file = '/tmp/opteng_output_{}.json'.format(datetime.timestamp(datetime.now()))
+
+ command = ['python', py_file, input_file, output_file]
+
+ try:
+ p = subprocess.run(command, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
+
+ debug_log.debug('Process return code {}'.format(p.returncode))
+ if p.returncode > 0:
+ error_log.error('Process return code {} {}'.format(p.returncode, p.stdout))
+ return 'error', {}
+ with open(output_file) as file:
+ data = file.read()
+ return 'success', json.loads(data)
+
+ except Exception as e:
+ error_log.error("Error running optimizer {}".format(traceback.format_exc()))
+ return 'error', {}
+ finally:
+ cleanup((input_file, output_file, py_file))
+
+
+def cleanup(file_tup):
+ for f in file_tup:
+ try:
+ delete_file_folder(f)
+ except Exception as e:
+ error_log.error("Failed deleting the file {} - {}".format(f, traceback.format_exc()))
+
+
+def solve(request_json, py_content):
+ req_info = request_json['requestInfo']
+ opt_info = request_json['optimInfo']
+ try:
+ status, solution = py_solver(py_content, opt_info)
+
+ response = {
+ 'transactionId': req_info['transactionId'],
+ 'requestID': req_info['requestID'],
+ 'requestStatus': status,
+ 'statusMessage': "completed",
+ 'solutions': solution if solution else {}
+ }
+ return 200, json.dumps(response)
+ except Exception as e:
+ response = {
+ 'transactionId': req_info['transactionId'],
+ 'requestID': req_info['requestID'],
+ 'requestStatus': 'failed',
+ 'statusMessage': 'Failed due to {}'.format(e)
+ }
+ return 400, json.dumps(response)