Add k8s yml file for oru application

kubernetes deployment file for oru application

Change-Id: I6608a544a75dca1f7bad0c827201d5ed95bedbfb
Issue-ID: NONRTRIC-496
Signed-off-by: ecaiyanlinux <martin.c.yan@est.tech>
diff --git a/test/mrstub/app/main.py b/test/mrstub/app/main.py
index 40707d6..90924b1 100644
--- a/test/mrstub/app/main.py
+++ b/test/mrstub/app/main.py
@@ -54,6 +54,8 @@
 cntr_msg_responses_fetched=0
 
 # Request and response constants
+ORU_WRITE_URL="/events/unauthenticated.SEC_FAULT_OUTPUT"
+ORU_READ_URL="/events/unauthenticated.SEC_FAULT_OUTPUT/users/test/"
 AGENT_WRITE_URL="/events/A1-POLICY-AGENT-WRITE"
 AGENT_READ_URL="/events/A1-POLICY-AGENT-READ/users/policy-agent"
 APP_WRITE_URL="/send-request"
@@ -344,6 +346,24 @@
 
         return Response('{}', status=200, mimetype=MIME_JSON)
 
+@app.route(ORU_WRITE_URL,
+    methods=['PUT','POST'])
+def oru_write():
+    global msg_requests
+    msg=json.dumps(request.json)
+    msg_requests.append(msg)
+    return Response('{}', status=200, mimetype=MIME_JSON)
+
+@app.route(ORU_READ_URL,
+    methods=['GET'])
+def oru_read():
+    global msg_requests
+    if(len(msg_requests)>0):
+        rsp=msg_requests.pop(0)
+        res=[]
+        res.append(rsp)
+        return Response(json.dumps(res), status=200, mimetype=MIME_JSON)
+    return Response("[]", status=200, mimetype=MIME_JSON)
 
 ### Functions for metrics read out ###
 
diff --git a/test/mrstub/mrstub-build-start.sh b/test/mrstub/mrstub-build-start.sh
index c9f70a1..1874f97 100755
--- a/test/mrstub/mrstub-build-start.sh
+++ b/test/mrstub/mrstub-build-start.sh
@@ -21,4 +21,4 @@
 
 docker build --build-arg NEXUS_PROXY_REPO=nexus3.onap.org:10001/  -t mrstub .
 
-docker run --rm -it -p 3904:3904 -p 3905:3905 -v "$PWD/cert:/usr/src/app/cert" mrstub
+docker run --rm --name=dmaap-mr -it -p 3904:3904 -p 3905:3905 -v "$PWD/cert:/usr/src/app/cert" mrstub
diff --git a/test/usecases/oruclosedlooprecovery/scriptversion/app/Dockerfile b/test/usecases/oruclosedlooprecovery/scriptversion/app/Dockerfile
index d5d6c2c..4cb03c7 100644
--- a/test/usecases/oruclosedlooprecovery/scriptversion/app/Dockerfile
+++ b/test/usecases/oruclosedlooprecovery/scriptversion/app/Dockerfile
@@ -22,6 +22,11 @@
 
 WORKDIR /usr/src/app
 
+## install curl & ping to help debug
+RUN apt update
+RUN apt install curl -y
+RUN apt-get install iputils-ping -y
+
 RUN pip install -r requirements.txt
 
-CMD [ "python3", "main.py" ]
+CMD [ "python3", "-u", "main.py" ]
diff --git a/test/usecases/oruclosedlooprecovery/scriptversion/docker-compose/start.sh b/test/usecases/oruclosedlooprecovery/scriptversion/docker-compose/start.sh
index 5dba4fe..419bef1 100755
--- a/test/usecases/oruclosedlooprecovery/scriptversion/docker-compose/start.sh
+++ b/test/usecases/oruclosedlooprecovery/scriptversion/docker-compose/start.sh
@@ -30,7 +30,7 @@
 docker build -f Dockerfile-message-generator -t message-generator .
 
 # start up dmaap message router
-cd ${SHELL_FOLDER}/../../../../docker-compose/
+cd ${SHELL_FOLDER}/../../../../../docker-compose/
 docker-compose -f docker-compose.yaml -f mr/docker-compose.yml up -d
 
 # wait until mr up & running
diff --git a/test/usecases/oruclosedlooprecovery/scriptversion/k8s/README.md b/test/usecases/oruclosedlooprecovery/scriptversion/k8s/README.md
new file mode 100644
index 0000000..255e220
--- /dev/null
+++ b/test/usecases/oruclosedlooprecovery/scriptversion/k8s/README.md
@@ -0,0 +1,24 @@
+## Run script
+
+`bash start.sh`
+
+This scripts will build images and start up four components:
+- dmaap-mr
+- oru-app
+- sdnr-simulator
+- message-generator
+
+## License
+
+Copyright (C) 2021 Nordix Foundation.
+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.
diff --git a/test/usecases/oruclosedlooprecovery/scriptversion/k8s/linkfailure.yml b/test/usecases/oruclosedlooprecovery/scriptversion/k8s/linkfailure.yml
new file mode 100644
index 0000000..ede8602
--- /dev/null
+++ b/test/usecases/oruclosedlooprecovery/scriptversion/k8s/linkfailure.yml
@@ -0,0 +1,200 @@
+#  Copyright (C) 2021 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=================================================
+#
+apiVersion: v1
+kind: Namespace
+metadata:
+  name: nonrtric
+---
+apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
+kind: Deployment
+metadata:
+  name: dmaap-mr
+  namespace: nonrtric
+  labels:
+    app: dmaap-mr
+spec:
+  selector:
+    matchLabels:
+      app: dmaap-mr
+  replicas: 1
+  template:
+    metadata:
+      labels:
+        app: dmaap-mr
+    spec:
+      containers:
+      - name: dmaap-mr
+        image: mrstub
+        imagePullPolicy: IfNotPresent
+        ports:
+        - name: http
+          containerPort: 3904
+        - name: https
+          containerPort: 3905
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: dmaap-mr
+  namespace: nonrtric
+  labels:
+    app: dmaap-mr
+spec:
+  type: ClusterIP
+  ports:
+    - name: http
+      protocol: TCP
+      port: 3904
+    - name: https
+      protocol: TCP
+      port: 3905
+  selector:
+    app: dmaap-mr
+---
+apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
+kind: Deployment
+metadata:
+  name: message-generator
+  namespace: nonrtric
+  labels:
+    app: message-generator
+spec:
+  selector:
+    matchLabels:
+      app: message-generator
+  replicas: 1
+  template:
+    metadata:
+      labels:
+        app: message-generator
+    spec:
+      containers:
+      - name: message-generator
+        image: message-generator
+        imagePullPolicy: IfNotPresent
+        env:
+        - name: MR-HOST
+          value: http://dmaap-mr
+        - name: MR-PORT
+          value: "3904"
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: message-generator
+  namespace: nonrtric
+  labels:
+    app: message-generator
+spec:
+  # type: ClusterIP
+  ports:
+    - protocol: TCP
+      port: 80
+  selector:
+    app: message-generator
+---
+apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
+kind: Deployment
+metadata:
+  name: sdnr-simulator
+  namespace: nonrtric
+  labels:
+    app: sdnr-simulator
+spec:
+  selector:
+    matchLabels:
+      app: sdnr-simulator
+  replicas: 1
+  template:
+    metadata:
+      labels:
+        app: sdnr-simulator
+    spec:
+      containers:
+      - name: sdnr-simulator
+        image: sdnr-simulator
+        imagePullPolicy: IfNotPresent
+        ports:
+        - name: tcp
+          containerPort: 9990
+        env:
+        - name: MR-HOST
+          value: http://dmaap-mr
+        - name: MR-PORT
+          value: "3904"
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: sdnr-simulator
+  namespace: nonrtric
+  labels:
+    app: sdnr-simulator
+spec:
+  type: ClusterIP
+  ports:
+    - protocol: TCP
+      port: 9990
+  selector:
+    app: sdnr-simulator
+---
+apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
+kind: Deployment
+metadata:
+  name: oru-app
+  namespace: nonrtric
+  labels:
+    app: oru-app
+spec:
+  selector:
+    matchLabels:
+      app: oru-app
+  replicas: 1
+  template:
+    metadata:
+      labels:
+        app: oru-app
+    spec:
+      containers:
+      - name: oru-app
+        image: oru-app
+        imagePullPolicy: IfNotPresent
+        env:
+        - name: MR-HOST
+          value: http://dmaap-mr
+        - name: MR-PORT
+          value: "3904"
+        - name: SDNR-HOST
+          value: http://sdnr-simulator
+        - name: SDNR-PORT
+          value: "9990"
+        - name: VERBOSE
+          value: "on"
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: oru-app
+  namespace: nonrtric
+  labels:
+    app: oru-app
+spec:
+  # type: ClusterIP
+  ports:
+    - protocol: TCP
+      port: 80
+  selector:
+    app: oru-app
\ No newline at end of file
diff --git a/test/usecases/oruclosedlooprecovery/scriptversion/k8s/start.sh b/test/usecases/oruclosedlooprecovery/scriptversion/k8s/start.sh
new file mode 100755
index 0000000..0bedbd9
--- /dev/null
+++ b/test/usecases/oruclosedlooprecovery/scriptversion/k8s/start.sh
@@ -0,0 +1,42 @@
+#  Copyright (C) 2021 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=================================================
+#
+
+SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)
+cd ${SHELL_FOLDER}
+kubectl delete -f linkfailure.yml
+
+# be careful if other stopped containers are on the same system
+docker stop $(docker ps -aq)
+docker system prune -f
+
+# build o-ru application image
+cd ${SHELL_FOLDER}/../app/
+docker build -t oru-app .
+
+# build simulator image of sdnr
+cd ${SHELL_FOLDER}/../simulators/
+docker build -f Dockerfile-sdnr-sim -t sdnr-simulator .
+
+# build message generator image
+docker build -f Dockerfile-message-generator -t message-generator .
+
+# build dmaap-mr sim
+cd ${SHELL_FOLDER}/../../../../mrstub/
+docker build -t mrstub .
+
+# apply k8s yaml file
+cd ${SHELL_FOLDER}
+kubectl apply -f linkfailure.yml
\ No newline at end of file
diff --git a/test/usecases/oruclosedlooprecovery/scriptversion/simulators/Dockerfile-message-generator b/test/usecases/oruclosedlooprecovery/scriptversion/simulators/Dockerfile-message-generator
index 139b791..841cf7f 100644
--- a/test/usecases/oruclosedlooprecovery/scriptversion/simulators/Dockerfile-message-generator
+++ b/test/usecases/oruclosedlooprecovery/scriptversion/simulators/Dockerfile-message-generator
@@ -22,6 +22,11 @@
 
 WORKDIR /usr/src/app
 
+## install curl & ping to help debug
+RUN apt update
+RUN apt install curl -y
+RUN apt-get install iputils-ping -y
+
 RUN pip install -r requirements.txt
 
-CMD [ "python3", "message_generator.py" ]
+CMD [ "python3", "-u", "message_generator.py" ]
diff --git a/test/usecases/oruclosedlooprecovery/scriptversion/simulators/Dockerfile-sdnr-sim b/test/usecases/oruclosedlooprecovery/scriptversion/simulators/Dockerfile-sdnr-sim
index 103c97c..4275b17 100644
--- a/test/usecases/oruclosedlooprecovery/scriptversion/simulators/Dockerfile-sdnr-sim
+++ b/test/usecases/oruclosedlooprecovery/scriptversion/simulators/Dockerfile-sdnr-sim
@@ -22,6 +22,11 @@
 
 WORKDIR /usr/src/app
 
+## install curl & ping to help debug
+RUN apt update
+RUN apt install curl -y
+RUN apt-get install iputils-ping -y
+
 RUN pip install -r requirements.txt
 
-CMD [ "python3", "sdnr_simulator.py" ]
+CMD [ "python3", "-u", "sdnr_simulator.py" ]
diff --git a/test/usecases/oruclosedlooprecovery/scriptversion/simulators/message_generator.py b/test/usecases/oruclosedlooprecovery/scriptversion/simulators/message_generator.py
index 1b0676c..9c2ee08 100644
--- a/test/usecases/oruclosedlooprecovery/scriptversion/simulators/message_generator.py
+++ b/test/usecases/oruclosedlooprecovery/scriptversion/simulators/message_generator.py
@@ -90,6 +90,14 @@
    }
  }
 
+def sendPostRequest(url ,msg):
+    try:
+        requests.post(url, json=msg)
+    except Exception as e:
+        print(type(e))
+        print(e.args)
+        print(e)
+
 if __name__ == "__main__":
     if os.getenv("MR-HOST") is not None:
         mr_host = os.getenv("MR-HOST")
@@ -104,13 +112,13 @@
         random_time = int(10 * random.random())
         if (random_time % 3 == 1):
             print("Sent heart beat")
-            requests.post(mr_url, json=heartBeatMessage);
+            sendPostRequest(mr_url, heartBeatMessage)
 
         o_ru_id = "ERICSSON-O-RU-1122" + str(random_time)
         print("Sent link failure for O-RAN-RU: " + o_ru_id)
         msg_as_json = json.loads(json.dumps(linkFailureMessage))
         msg_as_json["event"]["commonEventHeader"]["sourceName"] = o_ru_id
-        requests.post(mr_url, json=msg_as_json);
+        sendPostRequest(mr_url, msg_as_json)
 
         time.sleep(random_time)