3 # ============LICENSE_START=======================================================
4 # Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved.
5 # Contribution (C) 2022 Aarna Networks, Inc. All rights reserved.
6 # ================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
21 from flask import Flask, request, Response
22 from schematics.exceptions import DataError
24 from .utils import REST_PORT, LOGGING_LEVEL
25 from .SliceDataType import AllocateNssi, DeAllocateNssi, ActivateNssi, DeActivateNssi
26 from . import AuthManager
27 from . import NssManager
31 app.logger.setLevel(LOGGING_LEVEL)
34 @app.errorhandler(DataError)
35 def handleRequestException(e):
38 response.status_code = 400
42 @app.errorhandler(AuthManager.AuthError)
43 def handleAuthException(e):
46 response.status_code = 400
50 @app.errorhandler(AuthManager.TokenError)
51 def handleAuthException(e):
54 response.status_code = 401
58 @app.errorhandler(NssManager.NssError)
59 def handleNssException(e):
62 response.status_code = 400
66 @app.route("/api/rest/securityManagement/v1/oauth/token", methods=['POST'])
67 def handleAuthToken():
69 Used to get Access Token by SO NSSMF adapter.
71 app.logger.debug("Receive request:\n%s" % json.dumps(request.json, indent=2))
73 AuthManager.AuthRequest(request.json).validate()
74 AuthManager.checkAuth(request.json)
76 return AuthManager.generateAuthToken(request.json), 201
79 @app.route("/ObjectManagement/NSS/SliceProfiles", methods=['POST'])
80 def handleAllocateNssi():
81 AuthManager.checkAuthToken(request.headers)
83 app.logger.info("Receive AllocateNssi request:\n%s" % json.dumps(request.json, indent=2))
85 AllocateNssi(request.json).validate()
87 return NssManager.allocateNssi(request.json), 200
90 @app.route("/ObjectManagement/NSS/SliceProfiles/<string:sliceProfileId>", methods=['DELETE'])
91 def handleDeallocateNssi(sliceProfileId):
92 AuthManager.checkAuthToken(request.headers)
94 app.logger.info("Receive DeallocateNssi request for sliceProfileId %s:\n%s"
95 % (sliceProfileId, json.dumps(request.json, indent=2)))
97 DeAllocateNssi(request.json).validate()
99 return NssManager.deallocateNssi(sliceProfileId, request.json), 200
101 @app.route("/api/rest/provMns/v1/an/NSS/<string:snssai>/activations", methods=['PUT'])
102 def handleActivateNssi(snssai):
104 Method: handleActivateNssi
105 This method handles slice activation event generated by SO NSSMF adapter.
106 As part of this event, SO NSSMF adapter will send the associated 'snssai'.
107 'snssai' is string type value and example is: "01-2557D9". Wherein,
108 sst: "01" and sd: "2557D9".
110 'sst': Identifies the service (e.g eMBB, URLLC,...)
111 'sd' : service differentiator within sst.
112 Return value: http status 200
114 AuthManager.checkAuthToken(request.headers)
116 app.logger.info("Receive ActivateNssi request for snssai:%s\n%s"
117 % (snssai, json.dumps(request.json, indent=2)))
119 ActivateNssi(request.json).validate()
121 return NssManager.activateNssi(snssai, request.json), 200
123 @app.route("/api/rest/provMns/v1/an/NSS/<string:snssai>/deactivation", methods=['PUT'])
124 def handleDeActivateNssi(snssai):
126 Method: handleDeActivateNssi
127 This method handles slice deactivation event generated by SO NSSMF adapter.
128 As part of this event, SO NSSMF adapter will send the associated 'snssai'.
129 Example 'snssai' : "01-2557D9".
131 'sst': Identifies the service (e.g eMBB, URLLC,...)
132 'sd' : service differentiator within sst.
133 Return value: http status 200
135 AuthManager.checkAuthToken(request.headers)
137 app.logger.info("Receive DeActivateNssi request for snssai:%s\n%s"
138 % (snssai, json.dumps(request.json, indent=2)))
140 DeActivateNssi(request.json).validate()
142 return NssManager.deactivateNssi(snssai, request.json), 200
145 AuthManager.startAuthManagerJob()
146 app.run("0.0.0.0", REST_PORT, False, ssl_context="adhoc")
149 if __name__ == '__main__':