Add the support of activate/deactivate events to RAN NSSMF simulator
[integration.git] / test / mocks / ran-nssmf-simulator / RanNssmfSimulator / MainApp.py
1 #! /usr/bin/python3
2
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
10 #
11 #       http://www.apache.org/licenses/LICENSE-2.0
12 #
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=========================================================
19
20 import json
21 from flask import Flask, request, Response
22 from schematics.exceptions import DataError
23
24 from .utils import REST_PORT, LOGGING_LEVEL
25 from .SliceDataType import AllocateNssi, DeAllocateNssi, ActivateNssi, DeActivateNssi
26 from . import AuthManager
27 from . import NssManager
28
29
30 app = Flask(__name__)
31 app.logger.setLevel(LOGGING_LEVEL)
32
33
34 @app.errorhandler(DataError)
35 def handleRequestException(e):
36     app.logger.error(e)
37     response = Response()
38     response.status_code = 400
39     return response
40
41
42 @app.errorhandler(AuthManager.AuthError)
43 def handleAuthException(e):
44     app.logger.error(e)
45     response = Response()
46     response.status_code = 400
47     return response
48
49
50 @app.errorhandler(AuthManager.TokenError)
51 def handleAuthException(e):
52     app.logger.error(e)
53     response = Response()
54     response.status_code = 401
55     return response
56
57
58 @app.errorhandler(NssManager.NssError)
59 def handleNssException(e):
60     app.logger.error(e)
61     response = Response()
62     response.status_code = 400
63     return response
64
65
66 @app.route("/api/rest/securityManagement/v1/oauth/token", methods=['POST'])
67 def handleAuthToken():
68     """
69         Used to get Access Token by SO NSSMF adapter.
70     """
71     app.logger.debug("Receive request:\n%s" % json.dumps(request.json, indent=2))
72
73     AuthManager.AuthRequest(request.json).validate()
74     AuthManager.checkAuth(request.json)
75
76     return AuthManager.generateAuthToken(request.json), 201
77
78
79 @app.route("/ObjectManagement/NSS/SliceProfiles", methods=['POST'])
80 def handleAllocateNssi():
81     AuthManager.checkAuthToken(request.headers)
82
83     app.logger.info("Receive AllocateNssi request:\n%s" % json.dumps(request.json, indent=2))
84
85     AllocateNssi(request.json).validate()
86
87     return NssManager.allocateNssi(request.json), 200
88
89
90 @app.route("/ObjectManagement/NSS/SliceProfiles/<string:sliceProfileId>", methods=['DELETE'])
91 def handleDeallocateNssi(sliceProfileId):
92     AuthManager.checkAuthToken(request.headers)
93
94     app.logger.info("Receive DeallocateNssi request for sliceProfileId %s:\n%s"
95                     % (sliceProfileId, json.dumps(request.json, indent=2)))
96
97     DeAllocateNssi(request.json).validate()
98
99     return NssManager.deallocateNssi(sliceProfileId, request.json), 200
100
101 @app.route("/api/rest/provMns/v1/an/NSS/<string:snssai>/activations", methods=['PUT'])
102 def handleActivateNssi(snssai):
103     """
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".
109         Argument: snssai
110             'sst': Identifies the service (e.g eMBB, URLLC,...)
111             'sd' : service differentiator within sst.
112         Return value: http status 200
113     """
114     AuthManager.checkAuthToken(request.headers)
115
116     app.logger.info("Receive ActivateNssi request for snssai:%s\n%s"
117                     % (snssai, json.dumps(request.json, indent=2)))
118
119     ActivateNssi(request.json).validate()
120
121     return NssManager.activateNssi(snssai, request.json), 200
122
123 @app.route("/api/rest/provMns/v1/an/NSS/<string:snssai>/deactivation", methods=['PUT'])
124 def handleDeActivateNssi(snssai):
125     """
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".
130         Argument: snssai
131             'sst': Identifies the service (e.g eMBB, URLLC,...)
132             'sd' : service differentiator within sst.
133         Return value: http status 200
134     """
135     AuthManager.checkAuthToken(request.headers)
136
137     app.logger.info("Receive DeActivateNssi request for snssai:%s\n%s"
138                     % (snssai, json.dumps(request.json, indent=2)))
139
140     DeActivateNssi(request.json).validate()
141
142     return NssManager.deactivateNssi(snssai, request.json), 200
143
144 def main():
145     AuthManager.startAuthManagerJob()
146     app.run("0.0.0.0", REST_PORT, False, ssl_context="adhoc")
147
148
149 if __name__ == '__main__':
150     main()