Change location of VES5.0 code
[demo.git] / vnfs / VES5.0 / evel / evel-test-collector / code / collector / rest_dispatcher.py
1 #!/usr/bin/env python
2 '''
3 Simple dispatcher for the REST API.
4
5 Only intended for test purposes.
6
7 License
8 -------
9
10 Copyright(c) <2016>, AT&T Intellectual Property.  All other rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14
15 1. Redistributions of source code must retain the above copyright notice,
16    this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright notice,
18    this list of conditions and the following disclaimer in the documentation
19    and/or other materials provided with the distribution.
20 3. All advertising materials mentioning features or use of this software
21    must display the following acknowledgement:  This product includes
22    software developed by the AT&T.
23 4. Neither the name of AT&T nor the names of its contributors may be used to
24    endorse or promote products derived from this software without specific
25    prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
28 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
31 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 '''
38
39 import logging
40 logger = logging.getLogger('collector.disp')
41
42 base_url = ''
43
44 template_404 = b'''POST {0}'''
45
46 def set_404_content(url):
47     '''
48     Called at initialization to set the base URL so that we can serve helpful
49     diagnostics as part of the 404 response. 
50     '''
51     global base_url
52     base_url = url
53     return
54
55 def notfound_404(environ, start_response):
56     '''
57     Serve the 404 Not Found response.
58     
59     Provides diagnostics in the 404 response showing the hierarchy of valid
60     REST resources.
61     '''
62     logger.warning('Unexpected URL/Method: {0} {1}'.format(
63                                              environ['REQUEST_METHOD'].upper(),
64                                              environ['PATH_INFO']))
65     start_response('404 Not Found', [ ('Content-type', 'text/plain') ])
66     return [template_404.format(base_url)]
67
68 class PathDispatcher:
69     '''
70     A dispatcher which can take HTTP requests in a WSGI environment and invoke
71     appropriate methods for each request.
72     '''
73     def __init__(self):
74         '''Constructor: initialize the pathmap to be empty.'''
75         self.pathmap = { }
76
77     def __call__(self, environ, start_response):
78         '''
79         The main callable that the WSGI app will invoke with each request.
80         '''
81         #----------------------------------------------------------------------
82         # Extract the method and path from the environment.
83         #----------------------------------------------------------------------
84         method = environ['REQUEST_METHOD'].lower()
85         path = environ['PATH_INFO']
86         logger.info('Dispatcher called for: {0} {1}'.format(method, path))
87         logger.debug('Dispatcher environment is: {0}'.format(environ))
88
89         #----------------------------------------------------------------------
90         # See if we have a handler for this path, and if so invoke it.
91         # Otherwise, return a 404.
92         #----------------------------------------------------------------------
93         handler = self.pathmap.get((method, path), notfound_404)
94         logger.debug('Dispatcher will use handler: {0}'.format(handler))
95         return handler(environ, start_response)
96
97     def register(self, method, path, function):
98         '''
99         Register a handler for a method/path, adding it to the pathmap.
100         '''
101         logger.debug('Registering for {0} at {1}'.format(method, path))
102         print('Registering for {0} at {1}'.format(method, path))
103         self.pathmap[method.lower(), path] = function
104         return function