Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 16 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 17 | import os |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 18 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 19 | from xosapi.xos_grpc_client import Empty |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 20 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
| 21 | |
| 22 | from multistructlog import create_logger |
| 23 | from xosconfig import Config |
| 24 | |
| 25 | from .default import TOSCA_DEFS_DIR, TOSCA_KEYS_DIR |
| 26 | |
| 27 | log = create_logger(Config().get("logging")) |
| 28 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 29 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 30 | current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 31 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 32 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 33 | class TOSCA_Generator: |
Scott Baker | 3f7739c | 2018-02-20 20:17:34 -0800 | [diff] [blame] | 34 | def clean(self, dir=TOSCA_DEFS_DIR): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 35 | filesToRemove = [f for f in os.listdir(dir)] |
| 36 | for f in filesToRemove: |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 37 | if not f.startswith("."): |
| 38 | os.remove(dir + "/" + f) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 39 | |
Scott Baker | 3f7739c | 2018-02-20 20:17:34 -0800 | [diff] [blame] | 40 | def clean_keys(self, dir=TOSCA_KEYS_DIR): |
| 41 | keys_fn = os.path.join(dir, "KEYS.py") |
| 42 | if os.path.exists(keys_fn): |
| 43 | os.remove(keys_fn) |
| 44 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 45 | def generate(self, client): |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 46 | log.info("[XOS-TOSCA] Generating TOSCA") |
Matteo Scandolo | 2c1a001 | 2017-09-12 17:08:04 -0700 | [diff] [blame] | 47 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 48 | try: |
Matteo Scandolo | 2c1a001 | 2017-09-12 17:08:04 -0700 | [diff] [blame] | 49 | xproto = client.utility.GetXproto(Empty()) |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 50 | args = XOSProcessorArgs( |
| 51 | output=TOSCA_DEFS_DIR, |
| 52 | inputs=str(xproto.xproto), |
| 53 | target=os.path.join(current_dir, "xtarget/tosca.xtarget"), |
| 54 | write_to_file="target", |
| 55 | ) |
Sapan Bhatia | 4d711e7 | 2018-02-09 19:08:32 -0800 | [diff] [blame] | 56 | XOSProcessor.process(args) |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 57 | log.info("[XOS-TOSCA] Recipes generated in %s" % args.output) |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 58 | except Exception: |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 59 | log.exception("[XOS-TOSCA] Failed to generate TOSCA") |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 60 | |
Matteo Scandolo | 1bd1076 | 2017-10-18 09:53:14 +0200 | [diff] [blame] | 61 | try: |
| 62 | xproto = client.utility.GetXproto(Empty()) |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 63 | args = XOSProcessorArgs( |
| 64 | output=TOSCA_KEYS_DIR, |
| 65 | inputs=str(xproto.xproto), |
| 66 | target=os.path.join(current_dir, "xtarget/tosca_keys.xtarget"), |
| 67 | write_to_file="single", |
| 68 | dest_file="KEYS.py", |
| 69 | ) |
Sapan Bhatia | 4d711e7 | 2018-02-09 19:08:32 -0800 | [diff] [blame] | 70 | XOSProcessor.process(args) |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 71 | log.info("[XOS-TOSCA] TOSCA Keys generated in %s" % args.output) |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 72 | except Exception: |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 73 | log.exception("[XOS-TOSCA] Failed to generate TOSCA Keys") |