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
10 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 from flask import Flask, request, Response
21 from schematics.exceptions import DataError
23 from .utils import REST_PORT, LOGGING_LEVEL
24 from .SliceDataType import AllocateNssi, DeAllocateNssi
25 from . import AuthManager
26 from . import NssManager
30 app.logger.setLevel(LOGGING_LEVEL)
33 @app.errorhandler(DataError)
34 def handleRequestException(e):
37 response.status_code = 400
41 @app.errorhandler(AuthManager.AuthError)
42 def handleAuthException(e):
45 response.status_code = 400
49 @app.errorhandler(AuthManager.TokenError)
50 def handleAuthException(e):
53 response.status_code = 401
57 @app.errorhandler(NssManager.NssError)
58 def handleNssException(e):
61 response.status_code = 400
65 @app.route("/api/rest/securityManagement/v1/oauth/token", methods=['POST'])
66 def handleAuthToken():
68 Used to get Access Token by SO NSSMF adapter.
70 app.logger.debug("Receive request:\n%s" % json.dumps(request.json, indent=2))
72 AuthManager.AuthRequest(request.json).validate()
73 AuthManager.checkAuth(request.json)
75 return AuthManager.generateAuthToken(request.json), 201
78 @app.route("/ObjectManagement/NSS/SliceProfiles", methods=['POST'])
79 def handleAllocateNssi():
80 AuthManager.checkAuthToken(request.headers)
82 app.logger.info("Receive AllocateNssi request:\n%s" % json.dumps(request.json, indent=2))
84 AllocateNssi(request.json).validate()
86 return NssManager.allocateNssi(request.json), 200
89 @app.route("/ObjectManagement/NSS/SliceProfiles/<string:sliceProfileId>", methods=['DELETE'])
90 def handleDeallocateNssi(sliceProfileId):
91 AuthManager.checkAuthToken(request.headers)
93 app.logger.info("Receive DeallocateNssi request for sliceProfileId %s:\n%s"
94 % (sliceProfileId, json.dumps(request.json, indent=2)))
96 DeAllocateNssi(request.json).validate()
98 return NssManager.deallocateNssi(sliceProfileId, request.json), 200
102 AuthManager.startAuthManagerJob()
103 app.run("0.0.0.0", REST_PORT, False, ssl_context="adhoc")
106 if __name__ == '__main__':