[CORD-1509] create hss service
Change-Id: I36994774a0426860feb0596104a767f7a54fc48b
diff --git a/xos/synchronizer/Dockerfile.synchronizer b/xos/synchronizer/Dockerfile.synchronizer
new file mode 100644
index 0000000..bc6eae8
--- /dev/null
+++ b/xos/synchronizer/Dockerfile.synchronizer
@@ -0,0 +1,39 @@
+FROM xosproject/xos-synchronizer-base:candidate
+
+COPY . /opt/xos/synchronizers/vhss
+
+ENTRYPOINT []
+
+WORKDIR "/opt/xos/synchronizers/vhss"
+
+# Label image
+ARG org_label_schema_schema_version=1.0
+ARG org_label_schema_name=vhss-synchronizer
+ARG org_label_schema_version=unknown
+ARG org_label_schema_vcs_url=unknown
+ARG org_label_schema_vcs_ref=unknown
+ARG org_label_schema_build_date=unknown
+ARG org_opencord_vcs_commit_date=unknown
+ARG org_opencord_component_chameleon_version=unknown
+ARG org_opencord_component_chameleon_vcs_url=unknown
+ARG org_opencord_component_chameleon_vcs_ref=unknown
+ARG org_opencord_component_xos_version=unknown
+ARG org_opencord_component_xos_vcs_url=unknown
+ARG org_opencord_component_xos_vcs_ref=unknown
+
+LABEL org.label-schema.schema-version=$org_label_schema_schema_version \
+ org.label-schema.name=$org_label_schema_name \
+ org.label-schema.version=$org_label_schema_version \
+ org.label-schema.vcs-url=$org_label_schema_vcs_url \
+ org.label-schema.vcs-ref=$org_label_schema_vcs_ref \
+ org.label-schema.build-date=$org_label_schema_build_date \
+ org.opencord.vcs-commit-date=$org_opencord_vcs_commit_date \
+ org.opencord.component.chameleon.version=$org_opencord_component_chameleon_version \
+ org.opencord.component.chameleon.vcs-url=$org_opencord_component_chameleon_vcs_url \
+ org.opencord.component.chameleon.vcs-ref=$org_opencord_component_chameleon_vcs_ref \
+ org.opencord.component.xos.version=$org_opencord_component_xos_version \
+ org.opencord.component.xos.vcs-url=$org_opencord_component_xos_vcs_url \
+ org.opencord.component.xos.vcs-ref=$org_opencord_component_xos_vcs_ref
+
+CMD bash -c "cd /opt/xos/synchronizers/vhss; ./run-from-api.sh"
+
diff --git a/xos/synchronizer/model-deps b/xos/synchronizer/model-deps
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/xos/synchronizer/model-deps
@@ -0,0 +1 @@
+{}
diff --git a/xos/synchronizer/run-from-api.sh b/xos/synchronizer/run-from-api.sh
new file mode 100755
index 0000000..3928852
--- /dev/null
+++ b/xos/synchronizer/run-from-api.sh
@@ -0,0 +1 @@
+python vhss-synchronizer.py
diff --git a/xos/synchronizer/steps/monitoring_agent.yaml b/xos/synchronizer/steps/monitoring_agent.yaml
new file mode 100644
index 0000000..08b1fe7
--- /dev/null
+++ b/xos/synchronizer/steps/monitoring_agent.yaml
@@ -0,0 +1,61 @@
+---
+- hosts: {{ instance_name }}
+ gather_facts: False
+ connection: ssh
+ user: ubuntu
+ vars:
+ keystone_tenant_id: {{ keystone_tenant_id }}
+ keystone_user_id: {{ keystone_user_id }}
+ target_uri: {{ target_uri }}
+
+ tasks:
+ - name: Verify if monitoring_agent ([] is to avoid capturing the shell process) cron job is already running
+ shell: pgrep -f [m]onitoring_agent | wc -l
+ register: cron_job_pids_count
+
+ - name: DEBUG
+ debug: var=cron_job_pids_count.stdout
+
+ - name: make sure /usr/local/share/monitoring_agent exists
+ file: path=/usr/local/share/monitoring_agent state=directory owner=root group=root
+ become: yes
+ when: cron_job_pids_count.stdout == "0"
+
+ - name: Copy cron job to destination
+ copy: src=/opt/xos/synchronizers/vhss/monitoring_agent/
+ dest=/usr/local/share/monitoring_agent/
+ become: yes
+ when: cron_job_pids_count.stdout == "0"
+
+ - name: Installing python-pip
+ apt: name=python-pip state=present update_cache=yes
+ become: yes
+ when: cron_job_pids_count.stdout == "0"
+
+ - name: Installing Flask
+ pip: name=Flask
+ become: yes
+ when: cron_job_pids_count.stdout == "0"
+
+ - name: install python-kombu
+ apt: name=python-kombu state=present
+ become: yes
+ when: cron_job_pids_count.stdout == "0"
+
+ - name: Initiate monitoring agent cron job
+ command: python monitoring_agent.py &
+ async: 9999999999999999
+ poll: 0
+ become: yes
+ when: cron_job_pids_count.stdout == "0"
+ args:
+ chdir: /usr/local/share/monitoring_agent/
+
+ - name : starting monitoring agent
+ uri:
+ url: http://localhost:5004/monitoring/agent/vhss/start
+ method: POST
+ body: '{ "target":"{{target_uri}}", "keystone_user_id": "{{keystone_user_id}}", "keystone_tenant_id": "{{keystone_tenant_id}}" }'
+ force_basic_auth: yes
+ status_code: 200
+ body_format: json
diff --git a/xos/synchronizer/steps/sync_vhss.py b/xos/synchronizer/steps/sync_vhss.py
new file mode 100644
index 0000000..a222fbf
--- /dev/null
+++ b/xos/synchronizer/steps/sync_vhss.py
@@ -0,0 +1,83 @@
+import os
+import sys
+from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
+from synchronizers.new_base.modelaccessor import *
+from xos.logger import Logger, logging
+
+parentdir = os.path.join(os.path.dirname(__file__), "..")
+sys.path.insert(0, parentdir)
+
+logger = Logger(level=logging.INFO)
+
+class SyncVHSSTenant(SyncInstanceUsingAnsible):
+
+ provides = [VHSSTenant]
+
+ observes = VHSSTenant
+
+ requested_interval = 0
+
+ template_name = "sync_vhss.yaml"
+
+ service_key_name = "/opt/xos/synchronizers/mcord/mcord_private_key"
+
+ watches = [ModelLink(ServiceDependency,via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')]
+
+ def __init__(self, *args, **kwargs):
+ super(SyncVHSSTenant, self).__init__(*args, **kwargs)
+
+ def get_vhss(self, o):
+ if not o.provider_service:
+ return None
+
+ vhss = VHSSService.objects.filter(id=o.provider_service.id)
+
+ if not vhss:
+ return None
+
+ return vhss[0]
+
+ # Gets the attributes that are used by the Ansible template but are not
+ # part of the set of default attributes.
+ def get_extra_attributes(self, o):
+ fields = {}
+ fields['tenant_message'] = o.tenant_message
+ vhss = self.get_vhss(o)
+ fields['service_message'] = vhss.service_message
+ return fields
+
+ def delete_record(self, port):
+ # Nothing needs to be done to delete an exampleservice; it goes away
+ # when the instance holding the exampleservice is deleted.
+ pass
+
+ def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info):
+ if not monitoring_agent_info.service:
+ logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info))
+ return
+
+ if not monitoring_agent_info.target_uri:
+ logger.info("handle watch notifications for service monitoring agent info...ignoring because target_uri attribute in monitoring agent info:%s is null" % (monitoring_agent_info))
+ return
+
+ objs = VHSSTenant.objects.all()
+ for obj in objs:
+ if obj.provider_service.id != monitoring_agent_info.service.id:
+ logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is not matching" % (monitoring_agent_info))
+ return
+
+ instance = self.get_instance(obj)
+ if not instance:
+ logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj)))
+ return
+
+ logger.info("handling watch notification for monitoring agent info:%s for ExampleTenant object:%s" % (monitoring_agent_info, obj))
+
+ #Run ansible playbook to update the routing table entries in the instance
+ fields = self.get_ansible_fields(instance)
+ fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_monitoring"
+ fields["target_uri"] = monitoring_agent_info.target_uri
+
+ template_name = "monitoring_agent.yaml"
+ super(SyncVHSSTenant, self).run_playbook(obj, fields, template_name)
+ pass
diff --git a/xos/synchronizer/steps/sync_vhss.yaml b/xos/synchronizer/steps/sync_vhss.yaml
new file mode 100644
index 0000000..c01980c
--- /dev/null
+++ b/xos/synchronizer/steps/sync_vhss.yaml
@@ -0,0 +1,12 @@
+---
+# exampletenant_playbook
+
+- hosts: "{{ instance_name }}"
+ connection: ssh
+ user: ubuntu
+ sudo: yes
+ gather_facts: no
+ vars:
+ - tenant_message: "{{ tenant_message }}"
+ - service_message: "{{ service_message }}"
+
diff --git a/xos/synchronizer/vhss-synchronizer.py b/xos/synchronizer/vhss-synchronizer.py
new file mode 100644
index 0000000..aafdb44
--- /dev/null
+++ b/xos/synchronizer/vhss-synchronizer.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+
+# Runs the standard XOS synchronizer
+
+import importlib
+import os
+import sys
+
+synchronizer_path = os.path.join(os.path.dirname(
+ os.path.realpath(__file__)), "../../synchronizers/new_base")
+sys.path.append(synchronizer_path)
+mod = importlib.import_module("xos-synchronizer")
+mod.main()
+
diff --git a/xos/synchronizer/vhss_config.yaml b/xos/synchronizer/vhss_config.yaml
new file mode 100644
index 0000000..e31350e
--- /dev/null
+++ b/xos/synchronizer/vhss_config.yaml
@@ -0,0 +1,22 @@
+# Sets options for the synchronizer
+[observer]
+name=vhss
+dependency_graph=/opt/xos/synchronizers/vhss/model-deps
+steps_dir=/opt/xos/synchronizers/vhss/steps
+sys_dir=/opt/xos/synchronizers/vhss/sys
+#logfile=/var/log/xos_backend.log
+log_file=console
+log_level=debug
+pretend=False
+backoff_disabled=True
+save_ansible_output=True
+proxy_ssh=True
+proxy_ssh_key=/opt/cord_profile/node_key
+proxy_ssh_user=root
+enable_watchers=True
+accessor_kind=api
+accessor_password=@/opt/xos/services/vhss/credentials/xosadmin@opencord.org
+required_models=VHSSService, VHSSTenant, ServiceDependency, ServiceMonitoringAgentInfo
+
+[networking]
+use_vtn=True
\ No newline at end of file