Nokia: Putting in support for metro network services within XOS for E-CORD

Change-Id: I3db15d55eb5e536eac647364dc505627a4a32bdf
diff --git a/common/Makedefs b/common/Makedefs
index c4fe0ee..36602f9 100644
--- a/common/Makedefs
+++ b/common/Makedefs
@@ -40,5 +40,8 @@
 HYPERCACHE_BRANCH ?= $(DEFAULT_BRANCH)
 HYPERCACHE_GIT_URL ?= $(CORD_BASE_GIT_URL)/hypercache.git
 
+METRONETWORK_BRANCH ?= $(DEFAULT_BRANCH)
+METRONETWORK_GIT_URL ?= $(CORD_BASE_GIT_URL)/metro-net.git
+
 OPENSTACK_BRANCH ?= $(DEFAULT_BRANCH)
 OPENSTACK_GIT_URL ?= $(CORD_BASE_GIT_URL)/openstack.git
diff --git a/common/Makefile.services b/common/Makefile.services
index 2d5c693..89f7631 100644
--- a/common/Makefile.services
+++ b/common/Makefile.services
@@ -12,6 +12,7 @@
           $(SERVICE_DIR)/onos-service \
           $(SERVICE_DIR)/fabric \
           $(SERVICE_DIR)/hypercache \
+          $(SERVICE_DIR)/metro-net \
           $(SERVICE_DIR)/openstack
 
 monitoring_services: $(SERVICE_DIR)/monitoring
@@ -57,6 +58,9 @@
 $(SERVICE_DIR)/hypercache:
 	git -C $(SERVICE_DIR) $(GITOPTS) clone -b $(HYPERCACHE_BRANCH) $(HYPERCACHE_GIT_URL)
 
+$(SERVICE_DIR)/metro-net:
+	git -C $(SERVICE_DIR) $(GITOPTS) clone -b $(METRONETWORK_BRANCH) $(METRONETWORK_GIT_URL)
+
 $(SERVICE_DIR)/openstack:
 	git -C $(SERVICE_DIR) $(GITOPTS) clone -b $(OPENSTACK_BRANCH) $(OPENSTACK_GIT_URL)
 
@@ -73,4 +77,5 @@
 	git -C $(SERVICE_DIR)/onos-service pull
 	git -C $(SERVICE_DIR)/fabric pull
 	git -C $(SERVICE_DIR)/hypercache pull
+	git -C $(SERVICE_DIR)/metro-net pull
 	git -C $(SERVICE_DIR)/openstack pull
diff --git a/metronetwork/Makefile b/metronetwork/Makefile
new file mode 100644
index 0000000..312604f
--- /dev/null
+++ b/metronetwork/Makefile
@@ -0,0 +1,99 @@
+include ../common/Makedefs
+
+MYIP:=$(shell hostname -i)
+CONFIG_DIR:=$(shell pwd)
+DOCKER_COMPOSE_YML=./onboarding-docker-compose/docker-compose.yml
+BOOTSTRAP_YML=./docker-compose-bootstrap.yml
+DOCKER_PROJECT=metronetwork
+BOOTSTRAP_PROJECT=metronetworkdbs
+XOS_BOOTSTRAP_PORT=9998
+XOS_UI_PORT=9999
+ADMIN_USERNAME=padmin@vicci.org
+ADMIN_PASSWORD=letmein
+RUN_TOSCA_BOOTSTRAP=python ../common/run_tosca.py $(XOS_BOOTSTRAP_PORT) $(ADMIN_USERNAME) $(ADMIN_PASSWORD)
+RUN_TOSCA=python ../common/run_tosca.py $(XOS_UI_PORT) $(ADMIN_USERNAME) $(ADMIN_PASSWORD)
+
+metronetwork: prereqs dirs download_xos download_services bootstrap onboarding
+
+prereqs:
+	sudo make -f ../common/Makefile.prereqs
+
+dirs:
+	# if this directory doesn't exist, then docker-compose will create it with root permission
+	mkdir -p key_import
+	mkdir -p onboarding-docker-compose
+
+download_xos:
+	make -f ../common/Makefile.services xos_core
+
+download_services:
+	make -f ../common/Makefile.services
+
+update_services:
+	make -f ../common/Makefile.services update
+
+update_xos:
+	make -f ../common/Makefile.services update_xos
+
+bootstrap:
+	@echo "[BOOTSTRAP]"
+	sudo rm -f onboarding-docker-compose/docker-compose.yml
+	sudo CONFIG_DIR=$(CONFIG_DIR) docker-compose -p $(BOOTSTRAP_PROJECT) -f $(BOOTSTRAP_YML) up -d
+	bash ../common/wait_for_xos_port.sh $(XOS_BOOTSTRAP_PORT)
+	sudo docker-compose -p $(BOOTSTRAP_PROJECT) -f $(BOOTSTRAP_YML) run xos_bootstrap_ui python /opt/xos/tosca/run.py none - < ../common/fixtures.yaml
+	sudo docker-compose -p $(BOOTSTRAP_PROJECT) -f $(BOOTSTRAP_YML) run xos_bootstrap_ui python /opt/xos/tosca/run.py none - < ../common/mydeployment.yaml
+	$(RUN_TOSCA_BOOTSTRAP) xos.yaml
+	$(RUN_TOSCA_BOOTSTRAP) dashboard.yaml
+
+onboarding:
+	@echo "[ONBOARDING]"
+	# on-board the metronetwork service
+	bash ../common/wait_for_onboarding_ready.sh $(XOS_BOOTSTRAP_PORT) xos
+	$(RUN_TOSCA_BOOTSTRAP) ../common/disable-onboarding.yaml
+	$(RUN_TOSCA_BOOTSTRAP) $(SERVICE_DIR)/metro-net/xos/metronetworkservice-onboard.yaml
+	$(RUN_TOSCA_BOOTSTRAP) ../common/enable-onboarding.yaml
+	bash ../common/wait_for_onboarding_ready.sh $(XOS_BOOTSTRAP_PORT) services/metronetwork
+	bash ../common/wait_for_onboarding_ready.sh $(XOS_BOOTSTRAP_PORT) xos
+	bash ../common/wait_for_xos_port.sh $(XOS_UI_PORT)
+
+containers: prereqs download_xos
+	make -f ../common/Makefile.containers xos_devel synchronizer onboarding_synchronizer
+
+rebuild:
+	bash ../common/rebuild.sh $(XOS_BOOTSTRAP_PORT)
+	bash ../common/wait_for_onboarding_ready.sh $(XOS_BOOTSTRAP_PORT) xos
+
+stop:
+	test ! -s $(DOCKER_COMPOSE_YML) || sudo docker-compose -p $(DOCKER_PROJECT) -f $(DOCKER_COMPOSE_YML) stop
+	sudo docker-compose -p $(BOOTSTRAP_PROJECT) -f $(BOOTSTRAP_YML) stop
+
+showlogs:
+	sudo docker-compose -p $(DOCKER_PROJECT) -f $(DOCKER_COMPOSE_YML) logs
+
+showlogs_bootstrap:
+	sudo docker-compose -p $(BOOTSTRAP_PROJECT) -f $(BOOTSTRAP_YML) logs
+
+rm: stop
+	test ! -s $(DOCKER_COMPOSE_YML) || sudo docker-compose -p $(DOCKER_PROJECT) -f $(DOCKER_COMPOSE_YML) rm -f
+	sudo docker-compose -p $(BOOTSTRAP_PROJECT) -f $(BOOTSTRAP_YML) rm -f
+
+ps:
+	sudo docker-compose ps
+
+enter-ui:
+	sudo docker exec -ti metronetwork_xos_ui_1 bash
+
+enter-bootstrap-ui:
+	sudo docker exec -ti metronetworkdbs_xos_bootstrap_ui_1 bash
+
+enter-db:
+	sudo docker exec -ti metronetworkdbs_xos_db_1 bash
+
+django-restart:
+	sudo docker exec metronetwork_xos_ui_1 touch /opt/xos/xos/settings.py
+
+clean-config-folder:
+	sudo docker exec metronetwork_xos_ui_1 rm -f /opt/xos/xos_configuration/xos_mcord_config
+	sudo docker exec metronetwork_xos_ui_1 rm -f /opt/xos/xos_configuration/xos_cord_config
+
+
diff --git a/metronetwork/README.md b/metronetwork/README.md
new file mode 100644
index 0000000..de027c8
--- /dev/null
+++ b/metronetwork/README.md
@@ -0,0 +1,79 @@
+# XOS MetroNetwork 
+
+This configuration launches the XOS, GUI bootstrap, and synchronizer bootstrap as separate containers. It also
+onboards the metronetwork service, which results in two other containers being created, one for the GUI and one
+for the synchronizer.
+
+## Getting Started
+
+- Navigate to `service-profile/metronetwork` folder
+- Run `make` command
+
+You'll be able to visit XOS at `0.0.0.0:9998` and the `xos/core/xoslib` folder is shared with the container. This means that any update to that folder is automatically reported in the container.
+
+### Working on the Django Application
+If you need to work on the Django application itself:
+- comment out the `sleep 86400` command for the `xos_bootstrap_ui` container in `docker-compose-bootstrap.yml`
+- comment the `python /opt/xos/manage.py runserver 0.0.0.0:9998 --insecure --makemigrations` in the same file
+- run `make containers`.
+- run `make`
+
+You'll notice that the `make` command will hang on to `Waiting for XOS to start listening on port 9998`, so in another shell:
+- run `make enter-bootstrap-ui`, this will bring you inside the running bootstrap container
+- from inside the bootstrap container run `python /opt/xos/manage.py runserver 0.0.0.0:9998 --insecure --makemigrations`
+
+As this command finish his execution you'll notice that the `make` command will resume and end. Once it has endend your development environment is up and running. 
+
+If you are working on core files (not involving services) you can use the `bootstrap_ui` container as target, so connect to port `9998`. To simplify the development `xos/core` and `xos/api/utility` folders are shared between the host filesystem and the container, so you don't have to manually copy file inside the the running `bootstrap_ui` container.
+
+If instead you are working on services or services API your target container should be the `ui` container, so connect to port `9999`. Note that the `ui` container can be rebuilt from the `synchronizer` container, and it will pull the container in the docker registry, not your local one.
+
+> _NOTE:
+> Please be careful and do not commit migrations, private keys or other autogenerated files._
+
+
+### Using Vagrant
+
+Vagrant environment is provided with a ubuntu_14.04 box (ubuntu/trusty64). 
+
+It will run xos on the following address `192.168.46.100:9999`. 
+It will also share the `service-profile` and `xos` folder with the vagrant box. These two directory are intended to be side by side.
+
+How to set it up:
+- Start the Vagrant box: `vagrant up`
+- Enter Vagrant box: `vagrant ssh`
+- Start XOS: `cd /opt/service-profile/metronetwork` and run `make`
+
+### Docker Helpers
+
+Stop the containers: `make stop`
+
+Restart the containers: `make stop; make`
+
+Delete the containers and relaunch them: `make rm; make`
+
+Build the containers from scratch using the local XOS source tree: `make containers`
+
+View logs: `make showlogs`
+
+See what containers are running: `make ps`
+
+Open a shell on the XOS container: `make enter-xos`
+
+## Docs
+
+You can find a Swagger documentation for endpoint at: `http://0.0.0.0:9998/docs/`
+
+## Populate the Data Model with custom data
+
+Sometimes while developing the GUI is usefull to have control over the DataModel. Sample `tosca` recipes for different configuration are defined in the `mocks` folder, and corresponding `make` commands are provided.
+
+- Bring up the **CORD** data model: `make mock-cord`
+- - Bring up the **M-CORD** data model: `make mock-mcord`
+
+## JS Styleguide
+
+This project is following [Google JavaScript Style Guide](https://google.github.io/styleguide/javascriptguide.xml). To contribute please install [Eslint](http://eslint.org/) in your editor and run `npm run eslint` before commit.
+
+> _NOTE_:
+> Many of the already present file were not Style compliant. Linting for them has been disabled as it was to time consuming fix all of them. If **you are going to work** on that files, please **start fixing style issues**, and then **remove the `/* eslint-disable */`** comment
diff --git a/metronetwork/Vagrantfile b/metronetwork/Vagrantfile
new file mode 100644
index 0000000..62f981c
--- /dev/null
+++ b/metronetwork/Vagrantfile
@@ -0,0 +1,19 @@
+Vagrant.configure("2") do |config|
+  # base image
+  config.vm.box = "ubuntu/trusty64"
+
+  # setup a private network to the host machine
+  config.vm.network "private_network", ip: "192.168.46.100"
+
+  # share the folder
+  config.vm.synced_folder "../", "/opt/service-profile"
+  config.vm.synced_folder "../../xos", "/opt/xos", create: true
+  config.vm.synced_folder "../../xos_services", "/opt/xos_services", create: true
+  config.vm.synced_folder ".", "/vagrant", disabled: true
+
+  # increase RAM
+  config.vm.provider :virtualbox do |vb|
+      vb.customize ['modifyvm', :id,'--memory', '4096']
+  end
+
+ end
diff --git a/metronetwork/dashboard.yaml b/metronetwork/dashboard.yaml
new file mode 100644
index 0000000..ff13598
--- /dev/null
+++ b/metronetwork/dashboard.yaml
@@ -0,0 +1,26 @@
+tosca_definitions_version: tosca_simple_yaml_1_0
+
+description: >
+    * E-CORD DEMO GUI View
+imports:
+   - custom_types/xos.yaml
+
+topology_template:
+  node_templates:
+
+    # Register the Custom view in the system
+    Ecord:
+      type: tosca.nodes.DashboardView
+      properties:
+        url: template:xosEcordTopology
+
+    # Activate the Custom view for the user padmin@vicci.org
+    padmin@vicci.org:
+      type: tosca.nodes.User
+      properties:
+        no-create: True
+        no-delete: True
+      requirements:
+        - ecord_dashboard:
+            node: Ecord
+            relationship: tosca.relationships.UsesDashboard
\ No newline at end of file
diff --git a/metronetwork/docker-compose-bootstrap.yml b/metronetwork/docker-compose-bootstrap.yml
new file mode 100644
index 0000000..66ca64e
--- /dev/null
+++ b/metronetwork/docker-compose-bootstrap.yml
@@ -0,0 +1,47 @@
+xos_db:
+    image: xosproject/xos-postgres
+    expose:
+        - "5432"
+
+xos_bootstrap_ui:
+    image: xosproject/xos
+    # command: sleep 86400
+    command: python /opt/xos/manage.py runserver 0.0.0.0:9998 --insecure --makemigrations
+    environment:
+        - CONFIG_DIR
+    ports:
+        - "9998:9998"
+    links:
+        - xos_db
+    volumes:
+      - ../common/xos_common_config:/opt/xos/xos_configuration/xos_common_config
+      - ../cord-pod/files/xos_vtn_config:/opt/xos/xos_configuration/xos_vtn_config:ro
+      - ../../xos_services:/opt/xos_services
+      - ../../xos/xos/api/utility:/opt/xos/api/utility
+      - ../../xos/xos/core/models:/opt/xos/core/models
+      - ../../xos/xos/core/templatetags:/opt/xos/core/templatetags
+      - ../../xos/xos/core/xoslib/dashboards:/opt/xos/core/xoslib/dashboards
+      - ../../xos/xos/core/xoslib/static/css:/opt/xos/core/xoslib/static/css
+      - ../../xos/xos/core/xoslib/static/images:/opt/xos/core/xoslib/static/images
+      - ../../xos/xos/core/xoslib/static/js:/opt/xos/core/xoslib/static/js
+      - ../../xos/xos/xos:/opt/xos/xos
+      - ../../xos/xos/tosca:/opt/xos/tosca
+      - ../../xos/xos/synchronizers:/opt/xos/synchronizers
+
+xos_synchronizer_onboarding:
+    image: xosproject/xos-synchronizer-onboarding
+    command: bash -c "cd /opt/xos/synchronizers/onboarding; ./run.sh"
+    labels:
+        org.xosproject.kind: synchronizer
+        org.xosproject.target: onboarding
+    links:
+        - xos_db
+    volumes:
+        - /var/run/docker.sock:/var/run/docker.sock
+        - ./key_import:/opt/xos/key_import:ro
+        - ./onboarding-docker-compose:/opt/xos/synchronizers/onboarding/docker-compose
+        - ../../xos_services:/opt/xos_services
+    log_driver: "json-file"
+    log_opt:
+            max-size: "100k"
+            max-file: "5"
diff --git a/metronetwork/docker-compose-bootstrap.yml.devel b/metronetwork/docker-compose-bootstrap.yml.devel
new file mode 100644
index 0000000..ca25483
--- /dev/null
+++ b/metronetwork/docker-compose-bootstrap.yml.devel
@@ -0,0 +1,11 @@
+# Use this version if you will be starting the Python interpreters from the command line - or in the debugger
+# Meant for development only. Starts the PostGres DB in a Docker container with a port that can be accessed
+# from outside of Docker.
+
+xos_db:
+    image: xosproject/xos-postgres
+    expose:
+        - "5432"
+    #  Show this port externally so we can access it outside of containers
+    ports:
+      - "5432:5432"
diff --git a/metronetwork/xos.yaml b/metronetwork/xos.yaml
new file mode 100644
index 0000000..7ad92f1
--- /dev/null
+++ b/metronetwork/xos.yaml
@@ -0,0 +1,26 @@
+tosca_definitions_version: tosca_simple_yaml_1_0
+
+description: Onboard the exampleservice
+
+imports:
+   - custom_types/xos.yaml
+
+topology_template:
+  node_templates:
+    xos:
+      type: tosca.nodes.XOS
+      properties:
+        ui_port: 9999
+        bootstrap_ui_port: 9998
+        docker_project_name: metronetwork 
+        db_container_name: metronetworkdbs_xos_db_1
+
+    /opt/xos/xos_configuration/xos_common_config:
+      type: tosca.nodes.XOSVolume
+      properties:
+          host_path: { path_join: [ SELF, CONFIG_DIR, ../common/xos_common_config, ENV_VAR ] }
+          read_only: false
+      requirements:
+          - xos:
+             node: xos
+             relationship: tosca.relationships.UsedByXOS