blob: 3e2cf8f9c8db12ca5ca5046ed8523f6a2739c851 [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 Scandolo21dde412017-07-11 18:54:12 -070020from grpc_client.main import GRPC_Client
21from klein import Klein
Matteo Scandolo09d469c2017-07-07 11:46:48 -070022import os
23from tosca.parser import TOSCA_Parser
24from tosca.default import TOSCA_DEFS_DIR
25import json
Matteo Scandolo9ce18252017-06-22 10:48:25 -070026
27BANNER = """
28 _ ______ _____ __________ _____ _________
29 | |/ / __ \/ ___/ /_ __/ __ \/ ___// ____/ |
30 | / / / /\__ \ / / / / / /\__ \/ / / /| |
31 / / /_/ /___/ / / / / /_/ /___/ / /___/ ___ |
32/_/|_\____//____/ /_/ \____//____/\____/_/ |_|
33"""
34
35class TOSCA_WebServer:
Matteo Scandolo9ce18252017-06-22 10:48:25 -070036
Matteo Scandolo09d469c2017-07-07 11:46:48 -070037 current_dir = os.path.dirname(os.path.realpath(__file__))
38 template_dir = os.path.join(current_dir, 'templates/')
39
Matteo Scandolo21dde412017-07-11 18:54:12 -070040 app = Klein()
41
42 def execute_tosca(self, recipe):
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070043 self.parser.execute()
Matteo Scandoloc4680752018-05-17 12:16:30 -070044 if self.parser.delete:
45 response_text = "Deleted models: %s" % str(self.parser.ordered_models_name)
46 else:
47 response_text = "Created models: %s" % str(self.parser.ordered_models_name)
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070048 return response_text
49
50 def errorCallback(self, failure, request):
51 request.setResponseCode(500)
Matteo Scandolo21dde412017-07-11 18:54:12 -070052 try:
Matteo Scandolob2eff2c2018-03-02 12:58:10 -080053 f = failure.getErrorMessage()
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070054 log.info("[XOS-TOSCA] Error while loading TOSCA: \n\n", failure=f)
Matteo Scandolob2eff2c2018-03-02 12:58:10 -080055 return f
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070056 except Exception:
57 log.info("[XOS-TOSCA] Fatal Error: \n\n", failure=failure)
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070058 return "Internal server error, please report this along with the failed recipe."
Matteo Scandolo21dde412017-07-11 18:54:12 -070059
60 @app.route('/', methods=['GET'])
61 def index(self, request):
Matteo Scandolo09d469c2017-07-07 11:46:48 -070062 request.responseHeaders.addRawHeader(b"content-type", b"application/json")
63 tosca_defs = [f for f in os.listdir(TOSCA_DEFS_DIR) if not f.startswith('.')]
64
65 response = {}
66 for d in tosca_defs:
67 name = d.replace('.yaml', '')
68 response[name] = "/custom_type/%s" % name
69 return json.dumps(response)
Matteo Scandolo21dde412017-07-11 18:54:12 -070070
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070071 @app.route("/custom_type/<name>")
72 def custom_type(self, request, name):
73 request.responseHeaders.addRawHeader(b"content-type", b"text/plain")
74 custom_type = open(TOSCA_DEFS_DIR + '/' + name + '.yaml').read()
75 return custom_type
76
Matteo Scandolo21dde412017-07-11 18:54:12 -070077 @app.route('/run', methods=['POST'])
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070078 def run(self, request):
Matteo Scandolo21dde412017-07-11 18:54:12 -070079 recipe = request.content.read()
80 headers = request.getAllHeaders()
81 username = headers['xos-username']
82 password = headers['xos-password']
83
84 d = GRPC_Client().create_secure_client(username, password, recipe)
85 self.parser = TOSCA_Parser(recipe, username, password)
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070086 tosca_execution = d.addCallback(self.execute_tosca)
87 tosca_execution.addErrback(self.errorCallback, request)
Matteo Scandolo21dde412017-07-11 18:54:12 -070088 return d
Matteo Scandolo9ce18252017-06-22 10:48:25 -070089
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070090 @app.route('/delete', methods=['POST'])
91 def delete(self, request):
92 recipe = request.content.read()
93 headers = request.getAllHeaders()
94 username = headers['xos-username']
95 password = headers['xos-password']
96
97 d = GRPC_Client().create_secure_client(username, password, recipe)
98 self.parser = TOSCA_Parser(recipe, username, password, delete=True)
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070099 tosca_execution = d.addCallback(self.execute_tosca)
100 tosca_execution.addErrback(self.errorCallback, request)
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -0700101 return d
Matteo Scandolo09d469c2017-07-07 11:46:48 -0700102
Matteo Scandolo9ce18252017-06-22 10:48:25 -0700103 def __init__(self):
Matteo Scandolo5c0af1b2017-07-05 14:51:21 -0700104 self.app.run('0.0.0.0', '9102')