Import from GitHub

Change-Id: Iae8a9ecd755e62ec97c1a0a0da943fd90131f0af
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..2e91a13
--- /dev/null
+++ b/README.md
@@ -0,0 +1,5 @@
+# CORD Service Boilerplate
+
+This is a collection of boilerplate code that you'll need to create a CORD compliant service.
+
+If you are looking a tutorial, you can find it [here](https://wiki.opencord.org/display/CORD/Assembling+and+On-Boarding+Services%3A+A+Tutorial)
\ No newline at end of file
diff --git a/xos/exampleservice-onboard.yaml b/xos/exampleservice-onboard.yaml
new file mode 100644
index 0000000..e61d98d
--- /dev/null
+++ b/xos/exampleservice-onboard.yaml
@@ -0,0 +1,21 @@
+tosca_definitions_version: tosca_simple_yaml_1_0
+
+description: Onboard the example service
+
+imports:
+   - custom_types/xos.yaml
+
+topology_template:
+  node_templates:
+    exampleservice:
+      type: tosca.nodes.ServiceController
+      properties:
+          base_url: file:///opt/xos_services/exampleservice/xos/
+          # The following will concatenate with base_url automatically, if
+          # base_url is non-null.
+          tosca_custom_types: tosca/custom_types/sample.yaml
+          tosca_resource: tosca/resources/sample.py
+          private_key: file:///opt/xos/key_import/exampleservice_rsa
+          public_key: file:///opt/xos/key_import/exampleservice_rsa.pub
+          no_build: true
+          image: xosproject/exampleservice-synchronizer
\ No newline at end of file
diff --git a/xos/gui/README.md b/xos/gui/README.md
new file mode 100644
index 0000000..940b653
--- /dev/null
+++ b/xos/gui/README.md
@@ -0,0 +1 @@
+# GUI Extension
\ No newline at end of file
diff --git a/xos/models/README.md b/xos/models/README.md
new file mode 100644
index 0000000..f0906b3
--- /dev/null
+++ b/xos/models/README.md
@@ -0,0 +1 @@
+# XPROTO Specified Models
\ No newline at end of file
diff --git a/xos/models/service-model.xproto b/xos/models/service-model.xproto
new file mode 100644
index 0000000..d32619f
--- /dev/null
+++ b/xos/models/service-model.xproto
@@ -0,0 +1,13 @@
+message SampleService {
+     optional string description = 1 [help_text = "Description of Service", max_length = 254, null = True, db_index = False, blank = True];
+     required bool enabled = 2 [default = True, null = False, db_index = False, blank = True];
+     required string kind = 3 [default = "generic", max_length = 30, content_type = "stripped", blank = False, help_text = "Kind of service", null = False, db_index = False];
+     required string name = 4 [max_length = 30, content_type = "stripped", blank = False, help_text = "Service Name", null = False, db_index = False];
+     optional string versionNumber = 5 [max_length = 30, content_type = "stripped", blank = True, help_text = "Version of Service Definition", null = True, db_index = False];
+     required bool published = 6 [default = True, null = False, db_index = False, blank = True];
+     optional string public_key = 9 [help_text = "Public key string", max_length = 1024, null = True, db_index = False, blank = True];
+     optional string private_key_fn = 10 [db_index = False, max_length = 1024, null = True, content_type = "stripped", blank = True];
+     optional string service_specific_id = 11 [db_index = False, max_length = 30, null = True, content_type = "stripped", blank = True];
+     optional string service_specific_attribute = 12 [db_index = False, null = True, blank = True];
+     optional manytoone controller->ServiceController:services = 13 [help_text = "The Service Controller this Service uses", null = True, db_index = True, blank = True];
+}
diff --git a/xos/models/tenant-model.xproto b/xos/models/tenant-model.xproto
new file mode 100644
index 0000000..11d9d1f
--- /dev/null
+++ b/xos/models/tenant-model.xproto
@@ -0,0 +1,20 @@
+sage SampleTenant {
+     optional string name = 1 [db_index = False, max_length = 200, null = True, content_type = "stripped", blank = True];
+     required string kind = 2 [default = "generic", max_length = 30, content_type = "stripped", blank = False, null = False, db_index = False];
+     required manytoone provider_service->Service:provided_tenants = 3 [db_index = True, null = False, blank = False];
+     optional manytoone subscriber_service->Service:subscribed_tenants = 4 [db_index = True, null = True, blank = True];
+     optional manytoone subscriber_tenant->Tenant:subscribed_tenants = 5 [db_index = True, null = True, blank = True];
+     optional manytoone subscriber_user->User:subscribed_tenants = 6 [db_index = True, null = True, blank = True];
+     optional manytoone subscriber_root->TenantRoot:subscribed_tenants = 7 [db_index = True, null = True, blank = True];
+     optional manytoone subscriber_network->Network:subscribed_tenants = 8 [db_index = True, null = True, blank = True];
+     optional string service_specific_id = 9 [db_index = False, max_length = 30, null = True, content_type = "stripped", blank = True];
+     optional string service_specific_attribute = 10 [db_index = False, null = True, blank = True];
+     required string connect_method = 11 [default = "na", choices = "(('public', 'Public'), ('private', 'Private'), ('private-unidirectional', 'Private Unidirectional'), ('na', 'Not Applicable'))", max_length = 30, blank = False, null = False, db_index = False];
+     required string test = 12 [choices = "(('ping', 'Ping'), ('traceroute', 'Trace Route'), ('tcpdump', 'Tcp Dump'))", max_length = 30, content_type = "stripped", blank = False, help_text = "type of test", null = False, db_index = False];
+     required string scope = 13 [choices = "(('container', 'Container'), ('vm', 'VM'))", max_length = 30, content_type = "stripped", blank = False, help_text = "scope of test", null = False, db_index = False];
+     optional string argument = 14 [db_index = False, max_length = 40, null = True, content_type = "stripped", blank = True];
+     optional string result = 15 [db_index = False, null = True, blank = True];
+     optional string result_code = 16 [db_index = False, max_length = 32, null = True, content_type = "stripped", blank = True];
+     required manytoone target_type->ContentType:vtrtenant = 17 [db_index = True, null = False, blank = False];
+     required uint32 target_id = 18 [db_index = False, null = False, blank = False];
+}
diff --git a/xos/synchronizer/Dockerfile.synchronizer b/xos/synchronizer/Dockerfile.synchronizer
new file mode 100644
index 0000000..e70aad6
--- /dev/null
+++ b/xos/synchronizer/Dockerfile.synchronizer
@@ -0,0 +1,9 @@
+FROM xosproject/xos-synchronizer-base
+
+ADD . /opt/xos/synchronizers/exampleservice
+
+ENTRYPOINT []
+
+WORKDIR "/opt/xos/synchronizers/exampleservice"
+
+CMD bash -c "cd /opt/xos/synchronizers/exampleservice; ./run-from-api.sh"
diff --git a/xos/synchronizer/docker-compose.yml b/xos/synchronizer/docker-compose.yml
new file mode 100644
index 0000000..551094a
--- /dev/null
+++ b/xos/synchronizer/docker-compose.yml
@@ -0,0 +1,24 @@
+version: '2'
+
+networks:
+  xos:
+    external: true
+
+services:
+  synchronizer:
+    image: xosproject/exampleservice-synchronizer
+    networks:
+      - xos
+    #command: bash -c "cd /opt/xos/synchronizers/exampleservice; ./run-from-api.sh"
+    #command: bash -c "sleep 86400"
+    volumes:
+        - /opt/cord_profile/key_import/exampleservice_rsa:/opt/xos/services/exampleservice/keys/exampleservice_rsa:ro
+        - /opt/cord_profile/node_key:/opt/cord_profile/node_key:ro
+        - /opt/cord/build/platform-install/credentials/xosadmin@opencord.org:/opt/xos/services/exampleservice/credentials/xosadmin@opencord.org
+    external_links: 
+        - rcordbs_xos_redis_1:redis
+    logging:
+      driver: "json-file"
+      options:
+        max-size: "1000k"
+        max-file: "5"
diff --git a/xos/synchronizer/exampleservice-synchronizer.py b/xos/synchronizer/exampleservice-synchronizer.py
new file mode 100644
index 0000000..aafdb44
--- /dev/null
+++ b/xos/synchronizer/exampleservice-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/exampleservice_from_api_config b/xos/synchronizer/exampleservice_from_api_config
new file mode 100644
index 0000000..bb76435
--- /dev/null
+++ b/xos/synchronizer/exampleservice_from_api_config
@@ -0,0 +1,19 @@
+# Sets options for the synchronizer
+[observer]
+name=exampleservice
+dependency_graph=/opt/xos/synchronizers/exampleservice/model-deps
+steps_dir=/opt/xos/synchronizers/exampleservice/steps
+sys_dir=/opt/xos/synchronizers/exampleservice/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/exampleservice/credentials/xosadmin@opencord.org
+required_models=ExampleService, ExampleTenant, ServiceDependency, ServiceMonitoringAgentInfo
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..1245084
--- /dev/null
+++ b/xos/synchronizer/run-from-api.sh
@@ -0,0 +1,2 @@
+export XOS_DIR=/opt/xos
+python exampleservice-synchronizer.py  -C $XOS_DIR/synchronizers/exampleservice/exampleservice_from_api_config
diff --git a/xos/synchronizer/steps/exampletenant_playbook.yaml b/xos/synchronizer/steps/exampletenant_playbook.yaml
new file mode 100644
index 0000000..89e4617
--- /dev/null
+++ b/xos/synchronizer/steps/exampletenant_playbook.yaml
@@ -0,0 +1,16 @@
+---
+# exampletenant_playbook
+
+- hosts: "{{ instance_name }}"
+  connection: ssh
+  user: ubuntu
+  sudo: yes
+  gather_facts: no
+  vars:
+    - tenant_message: "{{ tenant_message }}"
+    - service_message: "{{ service_message }}"
+
+  roles:
+    - install_apache
+    - create_index
+
diff --git a/xos/synchronizer/steps/monitoring_agent.yaml b/xos/synchronizer/steps/monitoring_agent.yaml
new file mode 100644
index 0000000..87cffde
--- /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/exampleservice/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/exampleservice/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/roles/create_index/tasks/main.yml b/xos/synchronizer/steps/roles/create_index/tasks/main.yml
new file mode 100644
index 0000000..91c6029
--- /dev/null
+++ b/xos/synchronizer/steps/roles/create_index/tasks/main.yml
@@ -0,0 +1,7 @@
+---
+
+- name: Write index.html file to apache document root
+  template:
+    src=index.html.j2
+    dest=/var/www/html/index.html
+
diff --git a/xos/synchronizer/steps/roles/create_index/templates/index.html.j2 b/xos/synchronizer/steps/roles/create_index/templates/index.html.j2
new file mode 100644
index 0000000..9cec084
--- /dev/null
+++ b/xos/synchronizer/steps/roles/create_index/templates/index.html.j2
@@ -0,0 +1,4 @@
+ExampleService
+ Service Message: "{{ service_message }}"
+ Tenant Message: "{{ tenant_message }}"
+
diff --git a/xos/synchronizer/steps/roles/install_apache/tasks/main.yml b/xos/synchronizer/steps/roles/install_apache/tasks/main.yml
new file mode 100644
index 0000000..d9a155c
--- /dev/null
+++ b/xos/synchronizer/steps/roles/install_apache/tasks/main.yml
@@ -0,0 +1,7 @@
+---
+
+- name: Install apache using apt
+  apt:
+    name=apache2
+    update_cache=yes
+
diff --git a/xos/synchronizer/steps/sync_exampletenant.py b/xos/synchronizer/steps/sync_exampletenant.py
new file mode 100644
index 0000000..47c045f
--- /dev/null
+++ b/xos/synchronizer/steps/sync_exampletenant.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 SyncExampleTenant(SyncInstanceUsingAnsible):
+
+    provides = [ExampleTenant]
+
+    observes = ExampleTenant
+
+    requested_interval = 0
+
+    template_name = "exampletenant_playbook.yaml"
+
+    service_key_name = "/opt/xos/synchronizers/exampleservice/exampleservice_private_key"
+
+    watches = [ModelLink(ServiceDependency,via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')]
+
+    def __init__(self, *args, **kwargs):
+        super(SyncExampleTenant, self).__init__(*args, **kwargs)
+
+    def get_exampleservice(self, o):
+        if not o.provider_service:
+            return None
+
+        exampleservice = ExampleService.objects.filter(id=o.provider_service.id)
+
+        if not exampleservice:
+            return None
+
+        return exampleservice[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
+        exampleservice = self.get_exampleservice(o)
+        fields['service_message'] = exampleservice.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 = ExampleTenant.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(SyncExampleTenant, self).run_playbook(obj, fields, template_name)
+        pass
diff --git a/xos/tosca/custom_types/sample.m4 b/xos/tosca/custom_types/sample.m4
new file mode 100644
index 0000000..6c6ff82
--- /dev/null
+++ b/xos/tosca/custom_types/sample.m4
@@ -0,0 +1,17 @@
+tosca_definitions_version: tosca_simple_yaml_1_0
+
+# compile this with "m4 sample.m4 > sample.yaml"
+
+# include macros
+include(macros.m4)
+
+node_types:
+    tosca.nodes.SampleService:
+        derived_from: tosca.nodes.Root
+        description: >
+            Sample Service
+        capabilities:
+            xos_base_service_caps
+        properties:
+            xos_base_props
+            xos_base_service_props
\ No newline at end of file
diff --git a/xos/tosca/custom_types/sample.yaml b/xos/tosca/custom_types/sample.yaml
new file mode 100644
index 0000000..ca3c6ca
--- /dev/null
+++ b/xos/tosca/custom_types/sample.yaml
@@ -0,0 +1,59 @@
+tosca_definitions_version: tosca_simple_yaml_1_0
+node_types:
+    tosca.nodes.SampleService:
+        derived_from: tosca.nodes.Root
+        description: >
+            Sample Service
+        capabilities:
+            scalable:
+                type: tosca.capabilities.Scalable
+            service:
+                type: tosca.capabilities.xos.Service
+        properties:
+            no-delete:
+                type: boolean
+                default: false
+                description: Do not allow Tosca to delete this object
+            no-create:
+                type: boolean
+                default: false
+                description: Do not allow Tosca to create this object
+            no-update:
+                type: boolean
+                default: false
+                description: Do not allow Tosca to update this object
+            replaces:
+                type: string
+                required: false
+                descrption: Replaces/renames this object
+            kind:
+                type: string
+                default: generic
+                description: Type of service.
+            view_url:
+                type: string
+                required: false
+                description: URL to follow when icon is clicked in the Service Directory.
+            icon_url:
+                type: string
+                required: false
+                description: ICON to display in the Service Directory.
+            enabled:
+                type: boolean
+                default: true
+            published:
+                type: boolean
+                default: true
+                description: If True then display this Service in the Service Directory.
+            public_key:
+                type: string
+                required: false
+                description: Public key to install into Instances to allows Services to SSH into them.
+            private_key_fn:
+                type: string
+                required: false
+                description: Location of private key file
+            versionNumber:
+                type: string
+                required: false
+                description: Version number of Service.
\ No newline at end of file
diff --git a/xos/tosca/resources/sample.py b/xos/tosca/resources/sample.py
new file mode 100644
index 0000000..2c52ed6
--- /dev/null
+++ b/xos/tosca/resources/sample.py
@@ -0,0 +1,21 @@
+from xosresource import XOSResource
+from service import XOSService
+from services.vtr.models import SampleService
+
+
+class ToscaSampleService(XOSService):
+    provides = "tosca.nodes.Service"
+    xos_model = SampleService
+    copyin_props = [
+      "description",
+      "kind",
+      "versionNumber",
+      "public_key",
+      "private_key_fn",
+      "service_specific_id"
+    ]
+
+
+class ToscaSampleTenant(XOSResource):
+    provides = "tosca.nodes.Tenant"
+    xos_model = SampleTenant