blob: 6074e76349092dada96191f4efc0827095554956 [file] [log] [blame]
Enbo Wangb764f222020-09-18 11:06:12 +08001#! /usr/bin/python3
2
3# ============LICENSE_START=======================================================
4# Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved.
5# ================================================================================
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17# ============LICENSE_END=========================================================
18
19import json
20from flask import Flask, request, Response
21from schematics.exceptions import DataError
22
23from .utils import REST_PORT, LOGGING_LEVEL
24from .SliceDataType import AllocateNssi, DeAllocateNssi
25from . import AuthManager
26from . import NssManager
27
28
29app = Flask(__name__)
30app.logger.setLevel(LOGGING_LEVEL)
31
32
33@app.errorhandler(DataError)
34def handleRequestException(e):
35 app.logger.error(e)
36 response = Response()
37 response.status_code = 400
38 return response
39
40
41@app.errorhandler(AuthManager.AuthError)
42def handleAuthException(e):
43 app.logger.error(e)
44 response = Response()
45 response.status_code = 400
46 return response
47
48
49@app.errorhandler(AuthManager.TokenError)
50def handleAuthException(e):
51 app.logger.error(e)
52 response = Response()
53 response.status_code = 401
54 return response
55
56
57@app.errorhandler(NssManager.NssError)
58def handleNssException(e):
59 app.logger.error(e)
60 response = Response()
61 response.status_code = 400
62 return response
63
64
65@app.route("/api/rest/securityManagement/v1/oauth/token", methods=['POST'])
66def handleAuthToken():
67 """
68 Used to get Access Token by SO NSSMF adapter.
69 """
70 app.logger.debug("Receive request:\n%s" % json.dumps(request.json, indent=2))
71
72 AuthManager.AuthRequest(request.json).validate()
73 AuthManager.checkAuth(request.json)
74
75 return AuthManager.generateAuthToken(request.json), 201
76
77
78@app.route("/ObjectManagement/NSS/SliceProfiles", methods=['POST'])
79def handleAllocateNssi():
80 AuthManager.checkAuthToken(request.headers)
81
82 app.logger.info("Receive AllocateNssi request:\n%s" % json.dumps(request.json, indent=2))
83
84 AllocateNssi(request.json).validate()
85
86 return NssManager.allocateNssi(request.json), 200
87
88
89@app.route("/ObjectManagement/NSS/SliceProfiles/<string:sliceProfileId>", methods=['DELETE'])
90def handleDeallocateNssi(sliceProfileId):
91 AuthManager.checkAuthToken(request.headers)
92
93 app.logger.info("Receive DeallocateNssi request for sliceProfileId %s:\n%s"
94 % (sliceProfileId, json.dumps(request.json, indent=2)))
95
96 DeAllocateNssi(request.json).validate()
97
98 return NssManager.deallocateNssi(sliceProfileId, request.json), 200
99
100
101def main():
102 AuthManager.startAuthManagerJob()
103 app.run("0.0.0.0", REST_PORT, False, ssl_context="adhoc")
104
105
106if __name__ == '__main__':
107 main()