blob: fbaf4e538aeb382be9f144074b13a8d86000c00b [file] [log] [blame]
Matteo Scandolo920e8fd2017-08-08 13:05:24 -07001# 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 Williams6bb2cfe2019-03-27 15:01:45 -070015from __future__ import absolute_import
Matteo Scandolo920e8fd2017-08-08 13:05:24 -070016
Matteo Scandolo9ce18252017-06-22 10:48:25 -070017import os
Zack Williams6bb2cfe2019-03-27 15:01:45 -070018
Matteo Scandolo9ce18252017-06-22 10:48:25 -070019from xosapi.xos_grpc_client import Empty
Zack Williams6bb2cfe2019-03-27 15:01:45 -070020from xosgenx.generator import XOSProcessor, XOSProcessorArgs
21
22from multistructlog import create_logger
23from xosconfig import Config
24
25from .default import TOSCA_DEFS_DIR, TOSCA_KEYS_DIR
26
27log = create_logger(Config().get("logging"))
28
Matteo Scandolo9ce18252017-06-22 10:48:25 -070029
Matteo Scandolo9ce18252017-06-22 10:48:25 -070030current_dir = os.path.dirname(os.path.realpath(__file__))
31
Matteo Scandolo9ce18252017-06-22 10:48:25 -070032
Zack Williams6bb2cfe2019-03-27 15:01:45 -070033class TOSCA_Generator:
Scott Baker3f7739c2018-02-20 20:17:34 -080034 def clean(self, dir=TOSCA_DEFS_DIR):
Matteo Scandolo9ce18252017-06-22 10:48:25 -070035 filesToRemove = [f for f in os.listdir(dir)]
36 for f in filesToRemove:
Zack Williams6bb2cfe2019-03-27 15:01:45 -070037 if not f.startswith("."):
38 os.remove(dir + "/" + f)
Matteo Scandolo9ce18252017-06-22 10:48:25 -070039
Scott Baker3f7739c2018-02-20 20:17:34 -080040 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 Scandolo9ce18252017-06-22 10:48:25 -070045 def generate(self, client):
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070046 log.info("[XOS-TOSCA] Generating TOSCA")
Matteo Scandolo2c1a0012017-09-12 17:08:04 -070047
Matteo Scandolo9ce18252017-06-22 10:48:25 -070048 try:
Matteo Scandolo2c1a0012017-09-12 17:08:04 -070049 xproto = client.utility.GetXproto(Empty())
Zack Williams6bb2cfe2019-03-27 15:01:45 -070050 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 Bhatia4d711e72018-02-09 19:08:32 -080056 XOSProcessor.process(args)
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070057 log.info("[XOS-TOSCA] Recipes generated in %s" % args.output)
Zack Williams6bb2cfe2019-03-27 15:01:45 -070058 except Exception:
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070059 log.exception("[XOS-TOSCA] Failed to generate TOSCA")
Matteo Scandolo9ce18252017-06-22 10:48:25 -070060
Matteo Scandolo1bd10762017-10-18 09:53:14 +020061 try:
62 xproto = client.utility.GetXproto(Empty())
Zack Williams6bb2cfe2019-03-27 15:01:45 -070063 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 Bhatia4d711e72018-02-09 19:08:32 -080070 XOSProcessor.process(args)
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070071 log.info("[XOS-TOSCA] TOSCA Keys generated in %s" % args.output)
Zack Williams6bb2cfe2019-03-27 15:01:45 -070072 except Exception:
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070073 log.exception("[XOS-TOSCA] Failed to generate TOSCA Keys")