blob: 448305375e5f16a592ed449614812793a0cb9754 [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
16
Matteo Scandolo21dde412017-07-11 18:54:12 -070017from grpc_client.main import GRPC_Client
18from klein import Klein
Matteo Scandolo09d469c2017-07-07 11:46:48 -070019import os
20from tosca.parser import TOSCA_Parser
21from tosca.default import TOSCA_DEFS_DIR
22import json
Matteo Scandolo9ce18252017-06-22 10:48:25 -070023
24BANNER = """
25 _ ______ _____ __________ _____ _________
26 | |/ / __ \/ ___/ /_ __/ __ \/ ___// ____/ |
27 | / / / /\__ \ / / / / / /\__ \/ / / /| |
28 / / /_/ /___/ / / / / /_/ /___/ / /___/ ___ |
29/_/|_\____//____/ /_/ \____//____/\____/_/ |_|
30"""
31
32class TOSCA_WebServer:
Matteo Scandolo9ce18252017-06-22 10:48:25 -070033
Matteo Scandolo09d469c2017-07-07 11:46:48 -070034 current_dir = os.path.dirname(os.path.realpath(__file__))
35 template_dir = os.path.join(current_dir, 'templates/')
36
Matteo Scandolo21dde412017-07-11 18:54:12 -070037 app = Klein()
38
39 def execute_tosca(self, recipe):
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070040 self.parser.execute()
41 response_text = "Created models: %s" % str(self.parser.ordered_models_name)
42 return response_text
43
44 def errorCallback(self, failure, request):
45 request.setResponseCode(500)
Matteo Scandolo21dde412017-07-11 18:54:12 -070046 try:
Matteo Scandolo08803a92018-03-02 12:58:10 -080047 f = failure.getErrorMessage()
48 print "[XOS-TOSCA] Error while loading TOSCA: \n\n", f
49 return f
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070050 except:
51 print failure
52 return "Internal server error, please report this along with the failed recipe."
Matteo Scandolo21dde412017-07-11 18:54:12 -070053
54 @app.route('/', methods=['GET'])
55 def index(self, request):
Matteo Scandolo09d469c2017-07-07 11:46:48 -070056 request.responseHeaders.addRawHeader(b"content-type", b"application/json")
57 tosca_defs = [f for f in os.listdir(TOSCA_DEFS_DIR) if not f.startswith('.')]
58
59 response = {}
60 for d in tosca_defs:
61 name = d.replace('.yaml', '')
62 response[name] = "/custom_type/%s" % name
63 return json.dumps(response)
Matteo Scandolo21dde412017-07-11 18:54:12 -070064
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070065 @app.route("/custom_type/<name>")
66 def custom_type(self, request, name):
67 request.responseHeaders.addRawHeader(b"content-type", b"text/plain")
68 custom_type = open(TOSCA_DEFS_DIR + '/' + name + '.yaml').read()
69 return custom_type
70
Matteo Scandolo21dde412017-07-11 18:54:12 -070071 @app.route('/run', methods=['POST'])
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070072 def run(self, request):
Matteo Scandolo21dde412017-07-11 18:54:12 -070073 recipe = request.content.read()
74 headers = request.getAllHeaders()
75 username = headers['xos-username']
76 password = headers['xos-password']
77
78 d = GRPC_Client().create_secure_client(username, password, recipe)
79 self.parser = TOSCA_Parser(recipe, username, password)
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070080 tosca_execution = d.addCallback(self.execute_tosca)
81 tosca_execution.addErrback(self.errorCallback, request)
Matteo Scandolo21dde412017-07-11 18:54:12 -070082 return d
Matteo Scandolo9ce18252017-06-22 10:48:25 -070083
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070084 @app.route('/delete', methods=['POST'])
85 def delete(self, request):
86 recipe = request.content.read()
87 headers = request.getAllHeaders()
88 username = headers['xos-username']
89 password = headers['xos-password']
90
91 d = GRPC_Client().create_secure_client(username, password, recipe)
92 self.parser = TOSCA_Parser(recipe, username, password, delete=True)
Matteo Scandolo1fedfae2017-10-09 13:57:00 -070093 tosca_execution = d.addCallback(self.execute_tosca)
94 tosca_execution.addErrback(self.errorCallback, request)
Matteo Scandolo78ca3eb2017-07-13 16:58:22 -070095 return d
Matteo Scandolo09d469c2017-07-07 11:46:48 -070096
Matteo Scandolo9ce18252017-06-22 10:48:25 -070097 def __init__(self):
Matteo Scandolo5c0af1b2017-07-05 14:51:21 -070098 self.app.run('0.0.0.0', '9102')