Support https for callback_reciever

Currently callback_reciever only supports http
This commit makes it support both http/https

Change-Id: I6a8a7001231b113516f8d256b23b2b84d994bbce
Signed-off-by: ecaiyanlinux <martin.c.yan@est.tech>
Issue-ID: NONRTRIC-268
diff --git a/test/cr/app/cr.py b/test/cr/app/cr.py
new file mode 100644
index 0000000..bc6e28f
--- /dev/null
+++ b/test/cr/app/cr.py
@@ -0,0 +1,157 @@
+
+#  ============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 = "::"
+HOST_PORT = 2222
+
+# 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:
+            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("(Exception) 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)
diff --git a/test/cr/app/nginx.conf b/test/cr/app/nginx.conf
new file mode 100644
index 0000000..479d896
--- /dev/null
+++ b/test/cr/app/nginx.conf
@@ -0,0 +1,100 @@
+user www-data;
+worker_processes auto;
+pid /run/nginx.pid;
+include /etc/nginx/modules-enabled/*.conf;
+
+events {
+    worker_connections 768;
+    # multi_accept on;
+}
+
+http {
+
+    ##
+    # Basic Settings
+    ##
+
+    sendfile on;
+    tcp_nopush on;
+    tcp_nodelay on;
+    keepalive_timeout 65;
+    types_hash_max_size 2048;
+    # server_tokens off;
+
+    # server_names_hash_bucket_size 64;
+    # server_name_in_redirect off;
+
+    include /etc/nginx/mime.types;
+    default_type application/octet-stream;
+
+    server { # simple reverse-proxy
+        listen      8090;
+        listen      [::]:8090;
+        listen      8091 ssl;
+        listen      [::]:8091 ssl;
+        server_name  localhost;
+        ssl_certificate     /usr/src/app/cert/cert.crt;
+        ssl_certificate_key /usr/src/app/cert/key.crt;
+        ssl_password_file   /usr/src/app/cert/pass;
+
+        # serve dynamic requests
+        location / {
+        proxy_pass      http://localhost:2222;
+        }
+    }
+    ##
+    # SSL Settings
+    ##
+
+    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
+    ssl_prefer_server_ciphers on;
+
+    ##
+    # Logging Settings
+    ##
+
+    access_log /var/log/nginx/access.log;
+    error_log /var/log/nginx/error.log;
+
+    ##
+    # Gzip Settings
+    ##
+
+    gzip on;
+
+    # gzip_vary on;
+    # gzip_proxied any;
+    # gzip_comp_level 6;
+    # gzip_buffers 16 8k;
+    # gzip_http_version 1.1;
+    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
+
+    ##
+    # Virtual Host Configs
+    ##
+
+    include /etc/nginx/conf.d/*.conf;
+    include /etc/nginx/sites-enabled/*;
+}
+
+
+#mail {
+#	# See sample authentication script at:
+#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
+#
+#	# auth_http localhost/auth.php;
+#	# pop3_capabilities "TOP" "USER";
+#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
+#
+#	server {
+#		listen     localhost:110;
+#		protocol   pop3;
+#		proxy      on;
+#	}
+#
+#	server {
+#		listen     localhost:143;
+#		protocol   imap;
+#		proxy      on;
+#	}
+#}
diff --git a/test/cr/app/requirements.txt b/test/cr/app/requirements.txt
new file mode 100644
index 0000000..1dabaa8
--- /dev/null
+++ b/test/cr/app/requirements.txt
@@ -0,0 +1,3 @@
+Flask==1.1.1
+
+
diff --git a/test/cr/app/start.sh b/test/cr/app/start.sh
new file mode 100755
index 0000000..16ee34d
--- /dev/null
+++ b/test/cr/app/start.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+#  ============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=================================================
+#
+
+#start nginx
+nginx -c /usr/src/app/nginx.conf
+
+#start mrstub
+python3 -u cr.py