Updated function test environment

Added interface stub for MR

Added simulator monitor

Added function test and test cases

Added a server for catching callbacks from the agent

Added test cases for SNDC A1-Controller NB

README files are generally not updated. Will be made in later commits.

Change-Id: Ibf35f70b3e6f2d3f9c117aaf26ecdb03f80158f8
Issue-ID: NONRTRIC-154
Signed-off-by: BjornMagnussonXA <bjorn.magnusson@est.tech>
diff --git a/near-rt-ric-simulator/cr/cr.py b/near-rt-ric-simulator/cr/cr.py
new file mode 100644
index 0000000..28e3b10
--- /dev/null
+++ b/near-rt-ric-simulator/cr/cr.py
@@ -0,0 +1,158 @@
+
+#  ============LICENSE_START===============================================
+#  Copyright (C) 2020 Nordix Foundation. All rights reserved.
+#  ========================================================================
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+#  ============LICENSE_END=================================================
+#
+
+from flask import Flask, request
+from time import sleep
+import time
+import datetime
+import json
+from flask import Flask
+from flask import Response
+import traceback
+
+app = Flask(__name__)
+
+# list of callback messages
+msg_callbacks={}
+
+# Server info
+HOST_IP = "0.0.0.0"
+HOST_PORT = 8090
+
+# Metrics vars
+cntr_msg_callbacks=0
+cntr_msg_fetched=0
+
+# Request and response constants
+CALLBACK_URL="/callbacks/<string:id>"
+APP_READ_URL="/get-event/<string:id>"
+
+MIME_TEXT="text/plain"
+MIME_JSON="application/json"
+CAUGHT_EXCEPTION="Caught exception: "
+SERVER_ERROR="Server error :"
+
+#I'm alive function
+@app.route('/',
+    methods=['GET'])
+def index():
+    return 'OK', 200
+
+### Callback interface, for control
+
+# Fetch the oldest callback message for an id
+# URI and parameter, (GET): /get-event/<id>
+# response: message + 200 or 204
+@app.route(APP_READ_URL,
+    methods=['GET'])
+def receiveresponse(id):
+    global msg_callbacks
+    global cntr_msg_fetched
+
+    try:
+        if ((id in msg_callbacks.keys()) and (len(msg_callbacks[id]) > 0)):
+            cntr_msg_fetched+=1
+            msg=str(msg_callbacks[id][0])
+            print("Fetching msg for id: "+id+", msg="+msg)
+            del msg_callbacks[id][0]
+            return msg,200
+        print("No messages for id: "+id)
+    except Exception as e:
+        print(CAUGHT_EXCEPTION+str(e))
+
+    return "",204
+
+
+# Receive a callback message
+# URI and payload, (PUT or POST): /callbacks/<id> <json array of response messages>
+# response: OK 200 or 500 for other errors
+@app.route(CALLBACK_URL,
+    methods=['PUT','POST'])
+def events_write(id):
+    global msg_callbacks
+    global cntr_msg_callbacks
+
+    try:
+        print("Received callback for id: "+id +", content-type="+request.content_type)
+        try:
+            print("data:"+request.get_data)
+            if (request.content_type == MIME_JSON):
+                msg = request.json
+                print("Payload(json): "+str(msg))
+            elif (request.content_type == MIME_TEXT):
+                msg= request.form
+                print("Payload(text): "+str(msg))
+            else:
+                msg="\"\""
+                print("Payload(content-type="+request.content_type+"). Setting data to empty, quoted, string")
+        except:
+            msg="\"\""
+            print("Payload does not contain any json or text data, setting empty string as payload")
+
+        cntr_msg_callbacks += 1
+        if (id in msg_callbacks.keys()):
+            msg_callbacks[id].append(msg)
+        else:
+            msg_callbacks[id]=[]
+            msg_callbacks[id].append(msg)
+    except Exception as e:
+        print(CAUGHT_EXCEPTION+str(e))
+        return 'OK',500
+
+    return 'OK',200
+
+
+### Functions for metrics read out ###
+
+@app.route('/counter/received_callbacks',
+    methods=['GET'])
+def requests_submitted():
+    return Response(str(cntr_msg_callbacks), status=200, mimetype=MIME_TEXT)
+
+@app.route('/counter/fetched_callbacks',
+    methods=['GET'])
+def requests_fetched():
+    return Response(str(cntr_msg_fetched), status=200, mimetype=MIME_TEXT)
+
+@app.route('/counter/current_messages',
+    methods=['GET'])
+def current_messages():
+    return Response(str(cntr_msg_callbacks-cntr_msg_fetched), status=200, mimetype=MIME_TEXT)
+
+
+
+### Admin ###
+
+# Reset all messsages and counters
+@app.route('/reset',
+    methods=['GET', 'POST', 'PUT'])
+def reset():
+    global msg_callbacks
+    global cntr_msg_fetched
+    global cntr_msg_callbacks
+
+    msg_callbacks={}
+    cntr_msg_fetched=0
+    cntr_msg_callbacks=0
+
+    return Response('OK', status=200, mimetype=MIME_TEXT)
+
+### Main function ###
+
+if __name__ == "__main__":
+    app.run(port=HOST_PORT, host=HOST_IP)