blob: 94955b04a0d6c30ceeb646bda012fc71098fdcea [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
19from twisted.internet import defer
Matteo Scandolo9ce18252017-06-22 10:48:25 -070020from xosconfig import Config
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070021from multistructlog import create_logger
Matteo Scandolo9ce18252017-06-22 10:48:25 -070022
23current_dir = os.path.dirname(os.path.realpath(__file__))
Zack Williams6bb2cfe2019-03-27 15:01:45 -070024config_file = os.path.join(current_dir, "xos-tosca.config.yaml")
25config_schema = os.path.join(current_dir, "xos-tosca-config-schema.yaml")
Matteo Scandolo89c1f562017-10-19 19:09:45 +020026
Matteo Scandolo9ce18252017-06-22 10:48:25 -070027Config.init(config_file, config_schema)
Zack Williams6bb2cfe2019-03-27 15:01:45 -070028log = create_logger(Config().get("logging"))
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070029
Zack Williams6bb2cfe2019-03-27 15:01:45 -070030# config needs to be init before these imports, thus E402
31from grpc_client import GRPC_Client # noqa: E402
32from tosca import TOSCA_Generator # noqa: E402
33from web_server import TOSCA_WebServer # noqa: E402
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070034
Matteo Scandolo9ce18252017-06-22 10:48:25 -070035
36class Main:
Matteo Scandolo9ce18252017-06-22 10:48:25 -070037 def __init__(self):
38 self.grpc_client = None
39
40 def generate_tosca(self, client):
41
42 deferred = defer.Deferred()
43
44 TOSCA_Generator().generate(client)
45
Matteo Scandolo9ce18252017-06-22 10:48:25 -070046 return deferred
47
48 def start(self):
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070049 log.info("[XOS-TOSCA] Starting")
Matteo Scandolo9ce18252017-06-22 10:48:25 -070050
Zack Williams6bb2cfe2019-03-27 15:01:45 -070051 # Remove generated TOSCA and KEYS that may have been downloaded by a
52 # previous session. This is done here, rather than in the generator, to
53 # cover the case where the TOSCA engine is restarted and a web request
54 # is received and processed before generate_tosca() has completed.
55
Scott Baker3f7739c2018-02-20 20:17:34 -080056 TOSCA_Generator().clean()
57 TOSCA_Generator().clean_keys()
58
Matteo Scandolo9ce18252017-06-22 10:48:25 -070059 grpc_setup = GRPC_Client().start()
60 grpc_setup.addCallback(self.generate_tosca)
61
Matteo Scandolo21dde412017-07-11 18:54:12 -070062 # NOTE that TOSCA_WebServer create a Klein app that call reactor.run()
63 TOSCA_WebServer()
Matteo Scandolo9ce18252017-06-22 10:48:25 -070064
65
Zack Williams6bb2cfe2019-03-27 15:01:45 -070066if __name__ == "__main__":
67 Main().start()