Add SYNC-vMM services

Change-Id: I3caa75294b9ded734e0f46ae6ac2e49ade58ec4a
diff --git a/.gitreview b/.gitreview
new file mode 100644
index 0000000..d2f9f1f
--- /dev/null
+++ b/.gitreview
@@ -0,0 +1,6 @@
+[gerrit]
+host=gerrit.opencord.org
+port=29418
+project=vMM.git
+defaultremote=origin
+defaultbranch=cord-3.0
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/README.md
diff --git a/xos/__init__.py b/xos/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/xos/__init__.py
diff --git a/xos/admin.py b/xos/admin.py
new file mode 100644
index 0000000..22b5707
--- /dev/null
+++ b/xos/admin.py
@@ -0,0 +1,112 @@
+from core.admin import ReadOnlyAwareAdmin, SliceInline
+from core.middleware import get_request
+from core.models import User
+
+from django import forms
+from django.contrib import admin
+
+from services.vmm.models import *
+
+class VMMServiceForm(forms.ModelForm):
+
+    class Meta:
+        model = VMMService
+        fields = '__all__'
+
+    def __init__(self, *args, **kwargs):
+        super(VMMServiceForm, self).__init__(*args, **kwargs)
+
+    def save(self, commit=True):
+        return super(VMMServiceForm, self).save(commit=commit)
+
+class VMMServiceAdmin(ReadOnlyAwareAdmin):
+
+    model = VMMService
+    verbose_name = "VMM Service"
+    verbose_name_plural = "VMM Services"
+    form = VMMServiceForm
+    inlines = [SliceInline]
+
+    list_display = ('backend_status_icon', 'name', 'enabled')
+    list_display_links = ('backend_status_icon', 'name')
+
+    fieldsets = [(None, {
+        'fields': ['backend_status_text', 'name', 'enabled', 'versionNumber', 'description',],
+        'classes':['suit-tab suit-tab-general',],
+        })]
+
+    readonly_fields = ('backend_status_text', )
+    user_readonly_fields = ['name', 'enabled', 'versionNumber', 'description',]
+
+    extracontext_registered_admins = True
+
+    suit_form_tabs = (
+        ('general', 'VMM Service Details', ),
+        ('slices', 'Slices',),
+        )
+
+    suit_form_includes = (('mcordadmin.html',
+                           'top',
+                           'administration'),)
+
+    def get_queryset(self, request):
+        return VMMService.get_service_objects_by_user(request.user)
+
+admin.site.register(VMMService, VMMServiceAdmin)
+
+class VMMTenantForm(forms.ModelForm):
+
+    class Meta:
+        model = VMMTenant
+        fields = '__all__'
+
+    creator = forms.ModelChoiceField(queryset=User.objects.all())
+
+    def __init__(self, *args, **kwargs):
+        super(VMMTenantForm, self).__init__(*args, **kwargs)
+
+        self.fields['kind'].widget.attrs['readonly'] = True
+        self.fields['kind'].initial = "vmm"
+
+        self.fields['provider_service'].queryset = VMMService.get_service_objects().all()
+
+        if self.instance:
+            self.fields['creator'].initial = self.instance.creator
+            self.fields['tenant_message'].initial = self.instance.tenant_message
+            self.fields['image_name'].initial = self.instance.image_name
+
+        if (not self.instance) or (not self.instance.pk):
+            self.fields['creator'].initial = get_request().user
+            if VMMService.get_service_objects().exists():
+                self.fields['provider_service'].initial = VMMService.get_service_objects().all()[0]
+
+    def save(self, commit=True):
+        self.instance.creator = self.cleaned_data.get('creator')
+        self.instance.tenant_message = self.cleaned_data.get('tenant_message')
+        self.instance.image_name = self.cleaned_data.get('image_name')
+        return super(VMMTenantForm, self).save(commit=commit)
+
+
+class VMMTenantAdmin(ReadOnlyAwareAdmin):
+
+    verbose_name = "VMM Service Tenant"
+    verbose_name_plural = "VMM Service Tenants"
+
+    list_display = ('id', 'backend_status_icon', 'instance', 'tenant_message', 'image_name')
+    list_display_links = ('backend_status_icon', 'instance', 'tenant_message', 'id', 'image_name')
+
+    fieldsets = [(None, {
+        'fields': ['backend_status_text', 'kind', 'provider_service', 'instance', 'creator', 'tenant_message', 'image_name'],
+        'classes': ['suit-tab suit-tab-general'],
+        })]
+
+    readonly_fields = ('backend_status_text', 'instance',)
+
+    form = VMETenantForm
+
+    suit_form_tabs = (('general', 'Details'),)
+
+    def get_queryset(self, request):
+        return VMMTenant.get_tenant_objects_by_user(request.user)
+
+admin.site.register(VMMTenant, VMMTenantAdmin)
diff --git a/xos/header.py b/xos/header.py
new file mode 100644
index 0000000..98c7297
--- /dev/null
+++ b/xos/header.py
@@ -0,0 +1,23 @@
+from django.db import models
+from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool
+from core.models.plcorebase import StrippedCharField
+import os
+from django.db import models, transaction
+from django.forms.models import model_to_dict
+from django.db.models import *
+from operator import itemgetter, attrgetter, methodcaller
+from core.models import Tag
+from core.models.service import LeastLoadedNodeScheduler
+import traceback
+from xos.exceptions import *
+from xos.config import Config
+from django.contrib.contenttypes.models import ContentType
+from django.contrib.contenttypes.fields import GenericForeignKey
+
+class ConfigurationError(Exception):
+    pass
+
+VMME_KIND = "vEPC"
+
+CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False)
+
diff --git a/xos/make_synchronizer_manifest.sh b/xos/make_synchronizer_manifest.sh
new file mode 100644
index 0000000..061a261
--- /dev/null
+++ b/xos/make_synchronizer_manifest.sh
@@ -0,0 +1,2 @@
+#! /bin/bash
+find synchronizer -type f | cut -b 14- > synchronizer/manifest
\ No newline at end of file
diff --git a/xos/synchronizer/Dockerfile.synchronizer b/xos/synchronizer/Dockerfile.synchronizer
new file mode 100644
index 0000000..73b77e4
--- /dev/null
+++ b/xos/synchronizer/Dockerfile.synchronizer
@@ -0,0 +1,39 @@
+# xosproject/vmm-synchronizer
+FROM xosproject/xos-synchronizer-base:candidate
+
+COPY . /opt/xos/synchronizers/vmm
+
+ENTRYPOINT []
+
+WORKDIR "/opt/xos/synchronizers/vmm"
+
+# Label image
+ARG org_label_schema_schema_version=1.0
+ARG org_label_schema_name=vmm-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/vmm; ./run-from-api.sh"
diff --git a/xos/synchronizer/manifest b/xos/synchronizer/manifest
new file mode 100644
index 0000000..85ec180
--- /dev/null
+++ b/xos/synchronizer/manifest
@@ -0,0 +1,8 @@
+manifest
+model-deps
+run.sh
+steps/sync_vmmtenant.py
+steps/vmmtenant_playbook.yaml
+stop.sh
+vmm-synchronizer.py
+vmmservice_config
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..7807af3
--- /dev/null
+++ b/xos/synchronizer/run-from-api.sh
@@ -0,0 +1,2 @@
+export XOS_DIR=/opt/xos
+python vmm-synchronizer.py  -C $XOS_DIR/synchronizers/vmm/vmm_from_api_config
diff --git a/xos/synchronizer/steps/sync_vmmetenant.py b/xos/synchronizer/steps/sync_vmmetenant.py
new file mode 100644
index 0000000..afc2487
--- /dev/null
+++ b/xos/synchronizer/steps/sync_vmmetenant.py
@@ -0,0 +1,54 @@
+import os
+import sys
+from django.db.models import Q, F
+from synchronizers.new_base.modelaccessor import *
+from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
+
+parentdir = os.path.join(os.path.dirname(__file__), "..")
+sys.path.insert(0, parentdir)
+
+class SyncVMMTenant(SyncInstanceUsingAnsible):
+
+    provides = [VMMTenant]
+
+    observes = VMMTenant
+
+    requested_interval = 0
+
+    template_name = "vmmtenant_playbook.yaml"
+
+    service_key_name = "/opt/xos/synchronizers/vmm/vmm_private_key"
+
+    def __init__(self, *args, **kwargs):
+        super(SyncVMMTenant, self).__init__(*args, **kwargs)
+
+    def fetch_pending(self, deleted):
+
+        if (not deleted):
+            objs = VMMTenant.get_tenant_objects().filter(
+                Q(enacted__lt=F('updated')) | Q(enacted=None), Q(lazy_blocked=False))
+        else:
+            # If this is a deletion we get all of the deleted tenants..
+            objs = VMMTenant.get_deleted_tenant_objects()
+
+        return objs
+
+    def get_vmmservice(self, o):
+        if not o.provider_service:
+            return None
+
+        vmmservice = VMMService.get_service_objects().filter(id=o.provider_service.id)
+
+        if not vmmservice:
+            return None
+
+        return vmmservice[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
+        fields['image_name'] = o.image_name
+        return fields
+
diff --git a/xos/synchronizer/steps/vmmetenant_playbook.yaml b/xos/synchronizer/steps/vmmetenant_playbook.yaml
new file mode 100644
index 0000000..c8a74d0
--- /dev/null
+++ b/xos/synchronizer/steps/vmmetenant_playbook.yaml
@@ -0,0 +1,10 @@
+--- 
+- hosts: {{ instance_name }} 
+  gather_facts: False 
+  connection: ssh 
+  user: ubuntu 
+  sudo: yes 
+  tasks: 
+ 
+ # - name: write message
+ #   shell: echo "{{ tenant_message }}" > /var/tmp/index.html
diff --git a/xos/synchronizer/stop.sh b/xos/synchronizer/stop.sh
new file mode 100644
index 0000000..de8cfce
--- /dev/null
+++ b/xos/synchronizer/stop.sh
@@ -0,0 +1,2 @@
+# Kill the observer
+pkill -9 -f vmm-synchronizer.py
diff --git a/xos/synchronizer/vmm-synchronizer.py b/xos/synchronizer/vmm-synchronizer.py
new file mode 100644
index 0000000..e91f27a
--- /dev/null
+++ b/xos/synchronizer/vmm-synchronizer.py
@@ -0,0 +1,13 @@
+#!/usr/bin/env python
+
+# Runs the standard XOS observer
+
+import importlib
+import os
+import sys
+
+observer_path = os.path.join(os.path.dirname(
+    os.path.realpath(__file__)), "../../synchronizers/new_base")
+sys.path.append(observer_path)
+mod = importlib.import_module("xos-synchronizer")
+mod.main()
diff --git a/xos/synchronizer/vmm_from_api_config b/xos/synchronizer/vmm_from_api_config
new file mode 100644
index 0000000..a28f1ff
--- /dev/null
+++ b/xos/synchronizer/vmm_from_api_config
@@ -0,0 +1,20 @@
+# Sets options for the synchronizer
+[observer]
+name=vmm
+dependency_graph=/opt/xos/synchronizers/vmm/model-deps
+steps_dir=/opt/xos/synchronizers/vmm/steps
+sys_dir=/opt/xos/synchronizers/vmm/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
+accessor_kind=api
+accessor_password=@/opt/xos/services/vmm/credentials/xosadmin@opencord.org
+
+[networking]
+use_vtn=True
diff --git a/xos/templates/mcordadmin.html b/xos/templates/mcordadmin.html
new file mode 100644
index 0000000..d8183c2
--- /dev/null
+++ b/xos/templates/mcordadmin.html
@@ -0,0 +1,9 @@
+<div class = "left-nav">
+  <ul>
+    <li>
+      <a href="/admin/mcordservice/vmmecomponent/">
+        MCORD Service Components
+      </a>
+    </li>
+  </ul>
+</div>
diff --git a/xos/tosca/custom_types/macros.m4 b/xos/tosca/custom_types/macros.m4
new file mode 100644
index 0000000..1f48f10
--- /dev/null
+++ b/xos/tosca/custom_types/macros.m4
@@ -0,0 +1,84 @@
+# Note: Tosca derived_from isn't working the way I think it should, it's not
+#    inheriting from the parent template. Until we get that figured out, use
+#    m4 macros do our inheritance
+
+define(xos_base_props,
+            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)
+# Service
+define(xos_base_service_caps,
+            scalable:
+                type: tosca.capabilities.Scalable
+            service:
+                type: tosca.capabilities.xos.Service)
+define(xos_base_service_props,
+            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.)
+# Subscriber
+define(xos_base_subscriber_caps,
+            subscriber:
+                type: tosca.capabilities.xos.Subscriber)
+define(xos_base_subscriber_props,
+            kind:
+                type: string
+                default: generic
+                description: Kind of subscriber
+            service_specific_id:
+                type: string
+                required: false
+                description: Service specific ID opaque to XOS but meaningful to service)
+define(xos_base_tenant_props,
+            kind:
+                type: string
+                default: generic
+                description: Kind of tenant
+            service_specific_id:
+                type: string
+                required: false
+                description: Service specific ID opaque to XOS but meaningful to service)
+
+# end m4 macros
+
diff --git a/xos/tosca/custom_types/vmm.m4 b/xos/tosca/custom_types/vmm.m4
new file mode 100644
index 0000000..0cd9125
--- /dev/null
+++ b/xos/tosca/custom_types/vmm.m4
@@ -0,0 +1,18 @@
+tosca_definitions_version: tosca_simple_yaml_1_0
+
+# compile this with "m4 vmm.m4 > vmm.yaml"
+
+# include macros
+include(macros.m4)
+
+node_types:
+    tosca.nodes.VMMService:
+        derived_from: tosca.nodes.Root
+        description: >
+            VMM Service
+        capabilities:
+            xos_base_service_caps
+        properties:
+            xos_base_props
+            xos_base_service_props
+
diff --git a/xos/tosca/custom_types/vmm.yaml b/xos/tosca/custom_types/vmm.yaml
new file mode 100644
index 0000000..3c1f701
--- /dev/null
+++ b/xos/tosca/custom_types/vmm.yaml
@@ -0,0 +1,81 @@
+tosca_definitions_version: tosca_simple_yaml_1_0
+
+# compile this with "m4 vmm.m4 > vmm.yaml"
+
+# include macros
+# Note: Tosca derived_from isn't working the way I think it should, it's not
+#    inheriting from the parent template. Until we get that figured out, use
+#    m4 macros do our inheritance
+
+
+# Service
+
+
+# Subscriber
+
+
+
+
+# end m4 macros
+
+
+
+node_types:
+    tosca.nodes.VMMService:
+        derived_from: tosca.nodes.Root
+        description: >
+            VMM 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.
+
diff --git a/xos/tosca/resources/vmmservice.py b/xos/tosca/resources/vmmservice.py
new file mode 100644
index 0000000..830ed1e
--- /dev/null
+++ b/xos/tosca/resources/vmmservice.py
@@ -0,0 +1,7 @@
+from services.vmm.models import VMMService
+from service import XOSService
+
+class XOSVMMService(XOSService):
+	provides = "tosca.nodes.VMMService"
+	xos_model = VMMService
+	copyin_props = ["view_url", "icon_url", "enabled", "published", "public_key", "private_key_fn", "versionNumber"]
\ No newline at end of file
diff --git a/xos/tosca/resources/vmmtenant.py b/xos/tosca/resources/vmmtenant.py
new file mode 100644
index 0000000..3d987f2
--- /dev/null
+++ b/xos/tosca/resources/vmmtenant.py
@@ -0,0 +1,30 @@
+from services.vmm.models import VMMTenant, VMMService
+from xosresource import XOSResource
+
+class XOSVMMTenant(XOSResource):
+    provides = "tosca.nodes.VMMTenant"
+    xos_model = VMMTenant
+    copyin_props = ["tenant_message", "image_name"]  
+    name_field = None  
+
+    def get_xos_args(self, throw_exception=True):
+        args = super(XOSVMMTenant, self).get_xos_args()
+
+        provider_name = self.get_requirement("tosca.relationships.MemberOfService", throw_exception=throw_exception)
+        if provider_name:
+            args["provider_service"] = self.get_xos_object(VMMService, throw_exception=throw_exception, name=provider_name)
+
+        return args
+
+    def get_existing_objs(self):
+        args = self.get_xos_args(throw_exception=False)
+        provider_service = args.get("provider", None)
+        if provider_service:
+            return [ self.get_xos_object(provider_service=provider_service) ]
+        return []
+
+    def postprocess(self, obj):
+        pass
+
+    def can_delete(self, obj):
+        return super(XOSVMMTenant, self).can_delete(obj)
diff --git a/xos/vmm-onboard.yaml b/xos/vmm-onboard.yaml
new file mode 100644
index 0000000..b4b62b1
--- /dev/null
+++ b/xos/vmm-onboard.yaml
@@ -0,0 +1,24 @@
+tosca_definitions_version: tosca_simple_yaml_1_0
+
+description: Onboard the vMM service
+
+imports:
+  - custom_types/xos.yaml
+
+topology_template:
+  node_templates:
+    servicecontroller#vmm:
+      type: tosca.nodes.ServiceController
+      properties:
+          base_url: file:///opt/xos_services/vmm/xos/
+          # The following will concatenate with base_url automatically, if
+          # base_url is non-null.
+          xproto: ./
+          admin: admin.py
+          admin_template: templates/mcordadmin.html
+          tosca_custom_types: tosca/custom_types/vmm.yaml
+          synchronizer: synchronizer/manifest
+          synchronizer_run: vmm-synchronizer.py
+          tosca_resource: tosca/resources/vmmtenant.py, tosca/resources/vmmservice.py
+          private_key: file:///opt/xos/key_import/mcord_rsa
+          public_key: file:///opt/xos/key_import/mcord_rsa.pub
diff --git a/xos/vmm.xproto b/xos/vmm.xproto
new file mode 100644
index 0000000..4a495cc
--- /dev/null
+++ b/xos/vmm.xproto
@@ -0,0 +1,15 @@
+option name = "vmm";
+option app_label = "vmm";
+option verbose_name = "vMM Service";
+option kind = "vEPC";
+
+message VMMService (Service){
+    required string service_message = 1 [help_text = "Service Message to Display", max_length = 254, null = False, db_index = False, blank = False];
+}
+
+
+message VMMTenant (TenantWithContainer){
+     option name = "vmmtenant";
+     option verbose_name = "vMM Tenant";
+     required string tenant_message = 1 [help_text = "Tenant Message to Display", max_length = 254, null = False, db_index = False, blank = False];
+}