Add  netconf support to pnfsimulator.

Issue-ID: INT-458

Change-Id: Iebc9cf2a9f35c92dc354d1c6e857dc8345c1172f
Signed-off-by: Pawel Kadlubanski <pawel.kadlubanski@nokia.com>
diff --git a/test/mocks/pnfsimulator/simulator.sh b/test/mocks/pnfsimulator/simulator.sh
index ff3c07c..cf16e24 100755
--- a/test/mocks/pnfsimulator/simulator.sh
+++ b/test/mocks/pnfsimulator/simulator.sh
@@ -2,10 +2,15 @@
 
 set -euo pipefail
 
-CONTAINER_NAME=pnf-simulator
-CONFIG_FILE_PATH=/config/body.json
-SIMULATOR_DOCKER_HUB=nexus3.onap.org:10003/onap
-SIMULATOR_TAG=1.1.0-SNAPSHOT-latest
+COMPOSE_FILE_NAME=docker-compose.yml
+DEV_COMPOSE_FILE_NAME=docker-compose.dev.yml
+NETOPEER_CONTAINER_NAME=netopeer
+SIMULATOR_CONTAINER_NAME=pnf-simulator
+SIMULATOR_PORT=5000
+SIMULATOR_START_URL=http://localhost:$SIMULATOR_PORT/simulator/start
+SIMULATOR_STOP_URL=http://localhost:$SIMULATOR_PORT/simulator/stop
+SIMULATOR_STATUS_URL=http://localhost:$SIMULATOR_PORT/simulator/status
+RUNNING_COMPOSE_CONFIG=$COMPOSE_FILE_NAME
 
 function main(){
 
@@ -15,17 +20,21 @@
         "build")
             build_image;;
         "start")
-            start_simulator $2 $CONFIG_FILE_PATH $SIMULATOR_DOCKER_HUB/pnf-simulator:$SIMULATOR_TAG;;
+            start $COMPOSE_FILE_NAME;;
         "start-dev")
-            start_simulator $2 $CONFIG_FILE_PATH pnf-simulator:$SIMULATOR_TAG;;
+            start $DEV_COMPOSE_FILE_NAME;;
+        "start-debug")
+            start_netconf_server $DEV_COMPOSE_FILE_NAME;;
         "stop")
+            stop;;
+        "run-simulator")
+            run_simulator;;
+        "stop-simulator")
             stop_simulator;;
         "status")
-            print_status;;
-        "logs")
-            get_logs;;
-        "help")
-            print_help;;
+             get_status;;
+        "clear-logs")
+             clear_logs;;
         *)
             print_help;;
     esac
@@ -33,76 +42,142 @@
 
 function build_image(){
     if [ -f pom.xml ]; then
-        mvn clean package
+        mvn clean package 
     else
         echo "pom.xml file not found"
         exit 1
     fi
 }
 
-function start_simulator(){
+function start_netconf_server() {
+    docker-compose -f $1 up -d $NETOPEER_CONTAINER_NAME
+    echo
+    echo "NETCONF server container's logs:"
+    docker exec $NETOPEER_CONTAINER_NAME /bin/bash -c "sysrepoctl --install --yang=/netconf/\$NETCONF_MODEL.yang --owner=netconf:nogroup --permissions=777"
+    docker exec $NETOPEER_CONTAINER_NAME /bin/bash -c "sysrepocfg --import=/netconf/\$NETCONF_MODEL.data.xml --datastore=startup --format=xml --level=3 \$NETCONF_MODEL"
+    docker exec -d $NETOPEER_CONTAINER_NAME /bin/bash -c "/opt/dev/sysrepo/build/examples/application_example \$NETCONF_MODEL"
+    echo
+}
 
-    stop_and_remove_container || true
+function start(){
 
-    if [ $(docker run -d --name $CONTAINER_NAME -v $(pwd):/config -e VES_ADDRESS=$1 -e CONFIG_FILE_PATH=$2 $3) > /dev/null ]; then
-        echo "Simulator started"
+    if [[ $(running_containers) ]]; then
+        echo "Simulator containers are already up"
     else
-        echo "Failed to start simulator"
+        echo "Starting simulator containers using netconf model specified in config/netconf.env"
+
+        archive_logs
+        start_netconf_server $1
+        docker-compose -f $1 up -d $SIMULATOR_CONTAINER_NAME
+        RUNNING_COMPOSE_CONFIG=$1
     fi
 }
 
-function stop_and_remove_container(){
-    docker rm -f $CONTAINER_NAME 1> /dev/null
+function running_containers(){
+   docker-compose -f $COMPOSE_FILE_NAME ps -q && docker-compose -f $DEV_COMPOSE_FILE_NAME ps -q
+}
+
+function stop(){
+
+    if [[ $(running_containers) ]]; then
+        docker-compose -f $RUNNING_COMPOSE_CONFIG down
+    else
+        echo "Simulator containers are already down"
+    fi
+}
+
+function run_simulator(){
+cat << EndOfMessage
+Simulator response:
+$(curl -s -X POST -H "Content-Type: application/json" -d @config/config.json $SIMULATOR_START_URL | json_pp)
+EndOfMessage
 }
 
 function stop_simulator(){
-    if [ $(docker kill $CONTAINER_NAME) > /dev/null ]; then
-        echo "Simulator stopped"
-    else
-        echo "Failed to stop simulator"
-    fi
+cat << EndOfMessage
+Simulator response:
+$(curl -s -X POST $SIMULATOR_STOP_URL | json_pp)
+EndOfMessage
+}
 
+function get_status(){
+
+    if [[ $(running_containers) ]]; then
+        print_status
+    else
+        echo "Simulator containers are down"
+    fi
 }
 
 function print_status(){
 cat << EndOfMessage
+$(docker-compose -f $RUNNING_COMPOSE_CONFIG ps)
 
-Simulator container status:
-
-$(docker ps -a -f name=$CONTAINER_NAME)
-
+Simulator response:
+$(curl -s -X GET $SIMULATOR_STATUS_URL | json_pp)
 EndOfMessage
 }
 
 function print_help(){
 cat << EndOfMessage
-
 Available options:
 build - locally builds simulator image from existing code
-start <ves-url> - starts simulator using remote docker image and connects to given VES server
-start-dev <ves-url> - starts simulator using local docker image and connects to given VES server
-stop - stops simulator
-status - prints container status
-logs - prints logs
-help - prints this message
+start - starts simulator and netopeer2 containers using remote simulator image and specified model name
+start-dev - starts simulator and netopeer2 containers using remote simulator image
+start-debug - starts only  netopeer2 container
+run-simulator - starts sending PNF registration messages with parameters specified in config.json
+stop-simulator - stop sending PNF registration messages
+stop - stops both containers
+status - prints simulator status
+clear-logs - deletes log folder
 
 Starting simulation:
-Use "./simulator.sh start". It will download required docker image from the internet and start simulator using body.json file
 
-To stop simulation use "./simulator.sh stop" command. To check simulator's status use "./simulator.sh status".
-If you want to change message parameters simply edit body.json file then run simulator again.
+- Setup environment with "./simulator.sh start". It will download required docker images from the internet and run them on docker machine
+- To start the simulation use "./simulator.sh run-simulator", which will start sending PNF registration messages with parameters specified in config.json
+
+To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status".
+If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again
+Logs are written to logs/pnf-simulator.log. After each "start/start-dev" old log files are moved to the archive
 
 FOR DEVELOPERS
 1. Build local simulator image using "./simulator.sh build"
-2. Run simulation with "./simulator.sh start-dev"
+2. Run containers with "./simulator.sh start-dev"
 
-If you change the source code you have to rebuild image with "./simulator.sh build" and run  "./simulator.sh start-dev" again
-
+If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start/start-dev" again
 EndOfMessage
 }
 
-function get_logs(){
-    docker logs --tail all $CONTAINER_NAME
+function archive_logs(){
+
+    if [ -d logs ]; then
+        echo "Moving log file to archive"
+        DIR_PATH=logs/archive/simulator[$(timestamp)]
+        mkdir -p $DIR_PATH
+        if [ -f logs/pnfsimulator.log ]; then
+           mv logs/pnfsimulator.log $DIR_PATH
+        fi
+
+        if [ -f logs/*.xml ]; then
+            mv logs/*.xml $DIR_PATH
+        fi
+
+    else
+        mkdir logs
+    fi
+}
+
+function clear_logs(){
+
+    if [[ $(running_containers) ]]; then
+        echo "Cannot delete logs when simulator is running"
+    else
+         rm -rf logs
+    fi
+}
+
+function timestamp(){
+  date "+%Y-%m-%d_%T"
 }
 
 main $@
\ No newline at end of file