Merge "Push policy adapter code (adapted from ECOMP)"
[optf/osdf.git] / osdf / utils / local_processing.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2015-2017 AT&T Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 import os
20
21 from osdf.logging.osdf_logging import metrics_log, MH, warn_audit_error
22
23
24 def local_create_job_file(req_id, json_req, fname='osdf-req-data.json'):
25     """Creates a "work" folder for local processing and place relevant
26     job task file in there"""
27
28     work_dir = 'osdf-optim/work/' + req_id
29     work_file = '{}/{}'.format(work_dir, fname)
30     try:
31         cur_task = "Making a local directory in the OSDF manager for req-id: {}".format(req_id)
32         metrics_log.info(MH.creating_local_env(cur_task))
33         os.makedirs(work_dir, exist_ok=True)
34     except Exception as err:
35         warn_audit_error(MH.error_local_env(req_id, "Can't create directory {}".format(work_dir), err))
36         return None
37     try:
38         with open(work_file, 'w') as fid:
39             fid.write(json_req['payload'])
40         return work_dir
41     except Exception as err:
42         warn_audit_error(MH.error_local_env(req_id, "can't create file {}".format(work_file), err))
43         return None