blob: 3f54a77e1244e66d3e97ab12cfd5582fedc11bb9 [file] [log] [blame]
Matteo Scandolo920e8fd2017-08-08 13:05:24 -07001
2# Copyright 2017-present Open Networking Foundation
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
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070016from xosconfig import Config
17from multistructlog import create_logger
18log = create_logger(Config().get('logging'))
Matteo Scandolo920e8fd2017-08-08 13:05:24 -070019
Matteo Scandolo9ce18252017-06-22 10:48:25 -070020import os
Matteo Scandolo1bd10762017-10-18 09:53:14 +020021from default import TOSCA_DEFS_DIR, TOSCA_KEYS_DIR
Sapan Bhatia4d711e72018-02-09 19:08:32 -080022from xosgenx.generator import XOSProcessor
Matteo Scandolo9ce18252017-06-22 10:48:25 -070023from xosapi.xos_grpc_client import Empty
24
25class Args:
26 pass
27
28current_dir = os.path.dirname(os.path.realpath(__file__))
29
30class TOSCA_Generator:
31
Scott Baker3f7739c2018-02-20 20:17:34 -080032 def clean(self, dir=TOSCA_DEFS_DIR):
Matteo Scandolo9ce18252017-06-22 10:48:25 -070033 filesToRemove = [f for f in os.listdir(dir)]
34 for f in filesToRemove:
35 if not f.startswith('.'):
36 os.remove(dir + '/' + f)
37
Scott Baker3f7739c2018-02-20 20:17:34 -080038 def clean_keys(self, dir=TOSCA_KEYS_DIR):
39 keys_fn = os.path.join(dir, "KEYS.py")
40 if os.path.exists(keys_fn):
41 os.remove(keys_fn)
42
Matteo Scandolo9ce18252017-06-22 10:48:25 -070043 def generate(self, client):
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070044 log.info("[XOS-TOSCA] Generating TOSCA")
Matteo Scandolo2c1a0012017-09-12 17:08:04 -070045
Matteo Scandolo9ce18252017-06-22 10:48:25 -070046 try:
Matteo Scandolo2c1a0012017-09-12 17:08:04 -070047 xproto = client.utility.GetXproto(Empty())
Matteo Scandolo9ce18252017-06-22 10:48:25 -070048 args = Args()
49 args.output = TOSCA_DEFS_DIR
50 args.inputs = str(xproto.xproto)
51 args.target = os.path.join(current_dir, 'xtarget/tosca.xtarget')
Matteo Scandolocb92e162017-10-03 13:12:30 -070052 args.write_to_file = 'target'
Sapan Bhatia4d711e72018-02-09 19:08:32 -080053 XOSProcessor.process(args)
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070054 log.info("[XOS-TOSCA] Recipes generated in %s" % args.output)
Matteo Scandolo9ce18252017-06-22 10:48:25 -070055 except Exception as e:
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070056 log.exception("[XOS-TOSCA] Failed to generate TOSCA")
Matteo Scandolo9ce18252017-06-22 10:48:25 -070057
Matteo Scandolo1bd10762017-10-18 09:53:14 +020058 try:
59 xproto = client.utility.GetXproto(Empty())
60 args = Args()
61 args.output = TOSCA_KEYS_DIR
62 args.inputs = str(xproto.xproto)
63 args.target = os.path.join(current_dir, 'xtarget/tosca_keys.xtarget')
64 args.write_to_file = 'single'
65 args.dest_file = 'KEYS.py'
Sapan Bhatia4d711e72018-02-09 19:08:32 -080066 XOSProcessor.process(args)
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070067 log.info("[XOS-TOSCA] TOSCA Keys generated in %s" % args.output)
Matteo Scandolo1bd10762017-10-18 09:53:14 +020068 except Exception as e:
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070069 log.exception("[XOS-TOSCA] Failed to generate TOSCA Keys")
Matteo Scandolo1bd10762017-10-18 09:53:14 +020070