Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 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 | |
| 16 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 17 | from grpc_client.main import GRPC_Client |
| 18 | from klein import Klein |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 19 | import os |
| 20 | from tosca.parser import TOSCA_Parser |
| 21 | from tosca.default import TOSCA_DEFS_DIR |
| 22 | import json |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 23 | |
| 24 | BANNER = """ |
| 25 | _ ______ _____ __________ _____ _________ |
| 26 | | |/ / __ \/ ___/ /_ __/ __ \/ ___// ____/ | |
| 27 | | / / / /\__ \ / / / / / /\__ \/ / / /| | |
| 28 | / / /_/ /___/ / / / / /_/ /___/ / /___/ ___ | |
| 29 | /_/|_\____//____/ /_/ \____//____/\____/_/ |_| |
| 30 | """ |
| 31 | |
| 32 | class TOSCA_WebServer: |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 33 | |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 34 | current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 35 | template_dir = os.path.join(current_dir, 'templates/') |
| 36 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 37 | app = Klein() |
| 38 | |
| 39 | def execute_tosca(self, recipe): |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 40 | self.parser.execute() |
Matteo Scandolo | c468075 | 2018-05-17 12:16:30 -0700 | [diff] [blame] | 41 | if self.parser.delete: |
| 42 | response_text = "Deleted models: %s" % str(self.parser.ordered_models_name) |
| 43 | else: |
| 44 | response_text = "Created models: %s" % str(self.parser.ordered_models_name) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 45 | return response_text |
| 46 | |
| 47 | def errorCallback(self, failure, request): |
| 48 | request.setResponseCode(500) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 49 | try: |
Matteo Scandolo | b2eff2c | 2018-03-02 12:58:10 -0800 | [diff] [blame] | 50 | f = failure.getErrorMessage() |
| 51 | print "[XOS-TOSCA] Error while loading TOSCA: \n\n", f |
| 52 | return f |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 53 | except: |
| 54 | print failure |
| 55 | return "Internal server error, please report this along with the failed recipe." |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 56 | |
| 57 | @app.route('/', methods=['GET']) |
| 58 | def index(self, request): |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 59 | request.responseHeaders.addRawHeader(b"content-type", b"application/json") |
| 60 | tosca_defs = [f for f in os.listdir(TOSCA_DEFS_DIR) if not f.startswith('.')] |
| 61 | |
| 62 | response = {} |
| 63 | for d in tosca_defs: |
| 64 | name = d.replace('.yaml', '') |
| 65 | response[name] = "/custom_type/%s" % name |
| 66 | return json.dumps(response) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 67 | |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 68 | @app.route("/custom_type/<name>") |
| 69 | def custom_type(self, request, name): |
| 70 | request.responseHeaders.addRawHeader(b"content-type", b"text/plain") |
| 71 | custom_type = open(TOSCA_DEFS_DIR + '/' + name + '.yaml').read() |
| 72 | return custom_type |
| 73 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 74 | @app.route('/run', methods=['POST']) |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 75 | def run(self, request): |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 76 | recipe = request.content.read() |
| 77 | headers = request.getAllHeaders() |
| 78 | username = headers['xos-username'] |
| 79 | password = headers['xos-password'] |
| 80 | |
| 81 | d = GRPC_Client().create_secure_client(username, password, recipe) |
| 82 | self.parser = TOSCA_Parser(recipe, username, password) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 83 | tosca_execution = d.addCallback(self.execute_tosca) |
| 84 | tosca_execution.addErrback(self.errorCallback, request) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 85 | return d |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 86 | |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 87 | @app.route('/delete', methods=['POST']) |
| 88 | def delete(self, request): |
| 89 | recipe = request.content.read() |
| 90 | headers = request.getAllHeaders() |
| 91 | username = headers['xos-username'] |
| 92 | password = headers['xos-password'] |
| 93 | |
| 94 | d = GRPC_Client().create_secure_client(username, password, recipe) |
| 95 | self.parser = TOSCA_Parser(recipe, username, password, delete=True) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 96 | tosca_execution = d.addCallback(self.execute_tosca) |
| 97 | tosca_execution.addErrback(self.errorCallback, request) |
Matteo Scandolo | 78ca3eb | 2017-07-13 16:58:22 -0700 | [diff] [blame] | 98 | return d |
Matteo Scandolo | 09d469c | 2017-07-07 11:46:48 -0700 | [diff] [blame] | 99 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 100 | def __init__(self): |
Matteo Scandolo | 5c0af1b | 2017-07-05 14:51:21 -0700 | [diff] [blame] | 101 | self.app.run('0.0.0.0', '9102') |