Initial commit
[ta/config-manager.git] / cmframework / src / cmframework / server / cmrestapifactory.py
1 # Copyright 2019 Nokia
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 import json
15 import logging
16
17 from cmframework.server.cmhttperrors import CMHTTPErrors
18 from cmframework.server import cmrestapiv1
19
20
21 class CMRestAPIFactory(object):
22     def __init__(self, processor, base_url):
23         self.apis = {}
24         api = cmrestapiv1.CMRestAPIV1(processor)
25         self.apis[api.get_version()] = api
26         self.base_url = base_url
27
28     def get_apis(self, rpc):
29         """
30             Request: GET http://<cm-vip:port>/cm/apis
31             Response:
32                 {
33                     "versions": [
34                         {
35                             "id": "<version>",
36                             "href": "<http address for the api>"
37                             "min-version": "<mimimum version required to support this version>",
38                             "status": "<supported|deprecated|unsupported|current>"
39                         },
40                         ...
41                     ]
42                 }
43         """
44         try:
45             logging.debug('get_apis called')
46             reply = {}
47             versions = []
48             for key, value in self.apis.iteritems():
49                 version = {}
50                 version['id'] = value.get_version()
51                 version['href'] = '%s%s/' % (self.base_url, key)
52                 version['min-version'] = value.get_minimum_version()
53                 version['status'] = value.get_status()
54                 versions.append(version)
55             reply['versions'] = versions
56             rpc.rep_status = CMHTTPErrors.get_ok_status()
57             rpc.rep_body = json.dumps(reply)
58             logging.debug('returning, rpc=%s', str(rpc))
59         except Exception as exp:  # pylint: disable=broad-except
60             logging.error('Got exception %s', str(exp))
61             rpc.rep_status = CMHTTPErrors.get_internal_error_status()
62             rpc.rep_status += ','
63             rpc.rep_status += str(exp)
64
65     def get_api(self, version):
66         logging.debug('get_api called with version %s', version)
67         api = None
68         try:
69             api = self.apis[version]
70         except KeyError:
71             logging.warn('Could not find API with version %s', version)
72         return api
73
74
75 if __name__ == '__main__':
76     rest_api_factory = CMRestAPIFactory(None, None)