vTR models, admin, and synchronizer
diff --git a/xos/services/vtr/__init__.py b/xos/services/vtr/__init__.py
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/xos/services/vtr/__init__.py
@@ -0,0 +1 @@
+
diff --git a/xos/services/vtr/admin.py b/xos/services/vtr/admin.py
new file mode 100644
index 0000000..12b48af
--- /dev/null
+++ b/xos/services/vtr/admin.py
@@ -0,0 +1,102 @@
+from django.contrib import admin
+
+from services.cord.models import *
+from django import forms
+from django.utils.safestring import mark_safe
+from django.contrib.auth.admin import UserAdmin
+from django.contrib.admin.widgets import FilteredSelectMultiple
+from django.contrib.auth.forms import ReadOnlyPasswordHashField
+from django.contrib.auth.signals import user_logged_in
+from django.utils import timezone
+from django.contrib.contenttypes import generic
+from suit.widgets import LinkedSelect
+from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline
+from core.middleware import get_request
+
+from services.vtr.models import *
+from services.cord.models import CordSubscriberRoot
+
+from functools import update_wrapper
+from django.contrib.admin.views.main import ChangeList
+from django.core.urlresolvers import reverse
+from django.contrib.admin.utils import quote
+
+class VTRServiceAdmin(ReadOnlyAwareAdmin):
+    model = VTRService
+    verbose_name = "vTR Service"
+    verbose_name_plural = "vTR Service"
+    list_display = ("backend_status_icon", "name", "enabled")
+    list_display_links = ('backend_status_icon', 'name', )
+    fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description',"view_url","icon_url" ], 'classes':['suit-tab suit-tab-general']})]
+    readonly_fields = ('backend_status_text', )
+    inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline]
+
+    extracontext_registered_admins = True
+
+    user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
+
+    suit_form_tabs =(('general', 'vTR Service Details'),
+        ('administration', 'Administration'),
+        ('slices','Slices'),
+        ('serviceattrs','Additional Attributes'),
+        ('serviceprivileges','Privileges'),
+    )
+
+    suit_form_includes = (('vtradmin.html', 'top', 'administration'),
+                           ) #('hpctools.html', 'top', 'tools') )
+
+    def queryset(self, request):
+        return VTRService.get_service_objects_by_user(request.user)
+
+class VTRTenantForm(forms.ModelForm):
+    simple_attributes = {"test": None,
+                         "argument": None,
+                         "result": None,
+                         "target": None}
+    test = forms.ChoiceField(choices=VTRTenant.TEST_CHOICES, required=True)
+    argument = forms.CharField(required=False)
+    result = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 10, 'cols': 80, 'class': 'input-xxlarge'}))
+    target = forms.ModelChoiceField(queryset=CordSubscriberRoot.objects.all())
+
+    def __init__(self,*args,**kwargs):
+        super (VTRTenantForm,self ).__init__(*args,**kwargs)
+        self.fields['provider_service'].queryset = VTRService.get_service_objects().all()
+        if self.instance:
+            # fields for the attributes
+            self.fields['test'].initial = self.instance.test
+            self.fields['argument'].initial = self.instance.argument
+            self.fields['target'].initial = self.instance.target
+            self.fields['result'].initial = self.instance.result
+        if (not self.instance) or (not self.instance.pk):
+            # default fields for an 'add' form
+            self.fields['kind'].initial = VTR_KIND
+            if VTRService.get_service_objects().exists():
+               self.fields["provider_service"].initial = VTRService.get_service_objects().all()[0]
+
+    def save(self, commit=True):
+        self.instance.test = self.cleaned_data.get("test")
+        self.instance.argument = self.cleaned_data.get("argument")
+        self.instance.target = self.cleaned_data.get("target")
+        self.instance.result = self.cleaned_data.get("result")
+        return super(VTRTenantForm, self).save(commit=commit)
+
+    class Meta:
+        model = VTRTenant
+
+class VTRTenantAdmin(ReadOnlyAwareAdmin):
+    list_display = ('backend_status_icon', 'id', 'target', 'test', 'argument' )
+    list_display_links = ('backend_status_icon', 'id')
+    fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', # 'subscriber_root', 'service_specific_id', 'service_specific_attribute',
+                                     'target', 'test', 'argument', 'result'],
+                          'classes':['suit-tab suit-tab-general']})]
+    readonly_fields = ('backend_status_text', 'service_specific_attribute')
+    form = VTRTenantForm
+
+    suit_form_tabs = (('general','Details'),)
+
+    def queryset(self, request):
+        return VTRTenant.get_tenant_objects_by_user(request.user)
+
+admin.site.register(VTRService, VTRServiceAdmin)
+admin.site.register(VTRTenant, VTRTenantAdmin)
+
diff --git a/xos/services/vtr/models.py b/xos/services/vtr/models.py
new file mode 100644
index 0000000..a3e3501
--- /dev/null
+++ b/xos/services/vtr/models.py
@@ -0,0 +1,85 @@
+from django.db import models
+from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, Subscriber, 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 Q
+from operator import itemgetter, attrgetter, methodcaller
+from core.models import Tag
+from core.models.service import LeastLoadedNodeScheduler
+from services.cord.models import CordSubscriberRoot
+import traceback
+from xos.exceptions import *
+from xos.config import Config
+
+class ConfigurationError(Exception):
+    pass
+
+VTR_KIND = "vTR"
+
+CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False)
+
+# -------------------------------------------
+# VOLT
+# -------------------------------------------
+
+class VTRService(Service):
+    KIND = VTR_KIND
+
+    class Meta:
+        app_label = "vtr"
+        verbose_name = "vTR Service"
+        proxy = True
+
+class VTRTenant(Tenant):
+    class Meta:
+        proxy = True
+
+    KIND = VTR_KIND
+
+    TEST_CHOICES = ( ("ping", "Ping"), ("traceroute", "Trace Route"), ("tcpdump", "Tcp Dump") )
+
+    simple_attributes = ( ("test", None),
+                          ("argument", None),
+                          ("result", None) )
+
+    sync_attributes = ( 'test', 'argument' )
+
+    def __init__(self, *args, **kwargs):
+        vtr_services = VTRService.get_service_objects().all()
+        if vtr_services:
+            self._meta.get_field("provider_service").default = vtr_services[0].id
+        super(VTRTenant, self).__init__(*args, **kwargs)
+
+    @property
+    def target(self):
+        if getattr(self, "cached_target", None):
+            return self.cached_target
+        target_id=self.get_attribute("target_id")
+        if not target_id:
+            return None
+        users=CordSubscriberRoot.objects.filter(id=target_id)
+        if not users:
+            return None
+        user=users[0]
+        self.cached_target = users[0]
+        return user
+
+    @target.setter
+    def target(self, value):
+        if value:
+            value = value.id
+        if (value != self.get_attribute("target_id", None)):
+            self.cached_target=None
+        self.set_attribute("target_id", value)
+
+    def save(self, *args, **kwargs):
+        super(VTRTenant, self).save(*args, **kwargs)
+
+    def delete(self, *args, **kwargs):
+        super(VTRTenant, self).delete(*args, **kwargs)
+
+
+VTRTenant.setup_simple_attributes()
+
diff --git a/xos/services/vtr/templates/vtradmin.html b/xos/services/vtr/templates/vtradmin.html
new file mode 100644
index 0000000..e8a33bc
--- /dev/null
+++ b/xos/services/vtr/templates/vtradmin.html
@@ -0,0 +1,6 @@
+<div class = "row text-center">
+    <div class="col-xs-12">
+        <a href="/admin/vtr/vtrtenant/">vTR Tenants</a>
+    </div>
+</div>
+
diff --git a/xos/synchronizers/vtr/model-deps b/xos/synchronizers/vtr/model-deps
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/xos/synchronizers/vtr/model-deps
@@ -0,0 +1 @@
+{}
diff --git a/xos/synchronizers/vtr/run-vtn.sh b/xos/synchronizers/vtr/run-vtn.sh
new file mode 100755
index 0000000..b2f9518
--- /dev/null
+++ b/xos/synchronizers/vtr/run-vtn.sh
@@ -0,0 +1,4 @@
+export XOS_DIR=/opt/xos
+cp /root/setup/node_key $XOS_DIR/synchronizers/vtr/node_key
+chmod 0600 $XOS_DIR/synchronizers/vtr/node_key
+python vtr-synchronizer.py  -C $XOS_DIR/synchronizers/vtr/vtn_vtr_synchronizer_config
diff --git a/xos/synchronizers/vtr/run.sh b/xos/synchronizers/vtr/run.sh
new file mode 100755
index 0000000..388fdf9
--- /dev/null
+++ b/xos/synchronizers/vtr/run.sh
@@ -0,0 +1,2 @@
+export XOS_DIR=/opt/xos
+python vtr-synchronizer.py  -C $XOS_DIR/synchronizers/vtr/vtr_synchronizer_config
diff --git a/xos/synchronizers/vtr/steps/sync_vtrtenant.py b/xos/synchronizers/vtr/steps/sync_vtrtenant.py
new file mode 100644
index 0000000..3c46867
--- /dev/null
+++ b/xos/synchronizers/vtr/steps/sync_vtrtenant.py
@@ -0,0 +1,150 @@
+import os
+import socket
+import sys
+import base64
+import time
+from django.db.models import F, Q
+from xos.config import Config
+from synchronizers.base.syncstep import SyncStep
+from synchronizers.base.ansible import run_template_ssh
+from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
+from core.models import Service, Slice, Tag
+from services.cord.models import VSGService, VSGTenant, VOLTTenant, CordSubscriberRoot
+from services.vtr.models import VTRService, VTRTenant
+from services.hpc.models import HpcService, CDNPrefix
+from xos.logger import Logger, logging
+
+# hpclibrary will be in steps/..
+parentdir = os.path.join(os.path.dirname(__file__),"..")
+sys.path.insert(0,parentdir)
+
+logger = Logger(level=logging.INFO)
+
+CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False)
+
+class SyncVTRTenant(SyncInstanceUsingAnsible):
+    provides=[VTRTenant]
+    observes=VTRTenant
+    requested_interval=0
+    template_name = "sync_vtrtenant.yaml"
+    service_key_name = "/opt/xos/synchronizers/vtr/vcpe_private_key"
+
+    def __init__(self, *args, **kwargs):
+        super(SyncVTRTenant, self).__init__(*args, **kwargs)
+
+    def fetch_pending(self, deleted):
+        if (not deleted):
+            objs = VTRTenant.get_tenant_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False))
+        else:
+            objs = VTRTenant.get_deleted_tenant_objects()
+
+        return objs
+
+    def get_vtr_service(self, o):
+        if not o.provider_service:
+            return None
+
+        vtrs = VTRService.get_service_objects().filter(id=o.provider_service.id)
+        if not vtrs:
+            return None
+
+        return vtrs[0]
+
+    def get_vcpe_service(self, o):
+        if o.target:
+            # o.target is a CordSubscriberRoot
+            if o.target.volt and o.target.volt.vcpe:
+                vcpes = VSGService.get_service_objects().filter(id=o.target.volt.vcpe.provider_service.id)
+                if not vcpes:
+                    return None
+                return vcpes[0]
+        return None
+
+    def get_instance(self, o):
+        if o.target and o.target.volt and o.target.volt.vcpe:
+            return o.target.volt.vcpe.instance
+        else:
+            return None
+
+    def get_extra_attributes(self, o):
+        vtr_service = self.get_vtr_service(o)
+        vcpe_service = self.get_vcpe_service(o)
+
+        if not vcpe_service:
+            raise Exception("No vcpeservice")
+
+        instance = self.get_instance(o)
+
+        if not instance:
+            raise Exception("No instance")
+
+        s_tags = []
+        c_tags = []
+        if o.target and o.target.volt:
+            s_tags.append(o.target.volt.s_tag)
+            c_tags.append(o.target.volt.c_tag)
+
+        wan_vm_ip=""
+        wan_vm_mac=""
+        tags = Tag.select_by_content_object(instance).filter(name="vm_wan_addr")
+        if tags:
+            parts=tags[0].value.split(",")
+            if len(parts)!=3:
+                raise Exception("vm_wan_addr tag is malformed: %s" % value)
+            wan_vm_ip = parts[1]
+            wan_vm_mac = parts[2]
+        else:
+            if CORD_USE_VTN:
+                raise Exception("no vm_wan_addr tag for instance %s" % instance)
+
+        fields = {"s_tags": s_tags,
+                "c_tags": c_tags,
+                "isolation": instance.isolation,
+                "wan_container_gateway_mac": vcpe_service.wan_container_gateway_mac,
+                "wan_container_gateway_ip": vcpe_service.wan_container_gateway_ip,
+                "wan_container_netbits": vcpe_service.wan_container_netbits,
+                "wan_vm_mac": wan_vm_mac,
+                "wan_vm_ip": wan_vm_ip,
+                "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]),
+                "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")],
+
+                "result_fn": "%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]) }
+
+        # add in the sync_attributes that come from the SubscriberRoot object
+
+        if o.target and hasattr(o.target, "sync_attributes"):
+            for attribute_name in o.target.sync_attributes:
+                fields[attribute_name] = getattr(o.target, attribute_name)
+
+        for attribute_name in o.sync_attributes:
+            fields[attribute_name] = getattr(o,attribute_name)
+
+        return fields
+
+    def sync_fields(self, o, fields):
+        # the super causes the playbook to be run
+
+        super(SyncVTRTenant, self).sync_fields(o, fields)
+
+    def run_playbook(self, o, fields):
+        o.result = ""
+
+        result_fn = os.path.join("result", fields["result_fn"])
+        if os.path.exists(result_fn):
+            os.remove(result_fn)
+
+        instance = self.get_instance(o)
+        if instance.isolation in ["container", "container_vm"]:
+            super(SyncVTRTenant, self).run_playbook(o, fields, "sync_vtrtenant_new.yaml")
+        else:
+            if CORD_USE_VTN:
+                super(SyncVTRTenant, self).run_playbook(o, fields, template_name="sync_vtrtenant_vtn.yaml")
+            else:
+                super(SyncVTRTenant, self).run_playbook(o, fields)
+
+        if os.path.exists(result_fn):
+            o.result = open(result_fn).read()
+
+
+    def delete_record(self, m):
+        pass
diff --git a/xos/synchronizers/vtr/steps/sync_vtrtenant.yaml b/xos/synchronizers/vtr/steps/sync_vtrtenant.yaml
new file mode 100644
index 0000000..d2e6ef7
--- /dev/null
+++ b/xos/synchronizers/vtr/steps/sync_vtrtenant.yaml
@@ -0,0 +1,30 @@
+---
+- hosts: {{ instance_name }}
+  #gather_facts: False
+  connection: ssh
+  user: ubuntu
+  sudo: yes
+  vars:
+      container_name: {{ container_name }}
+      wan_container_ip: {{ wan_container_ip }}
+      wan_container_netbits: {{ wan_container_netbits }}
+      wan_container_mac: {{ wan_container_mac }}
+      wan_container_gateway_ip: {{ wan_container_gateway_ip }}
+      wan_vm_ip: {{ wan_vm_ip }}
+      wan_vm_mac: {{ wan_vm_mac }}
+
+      test: {{ test }}
+      argument: {{ argument }}
+      result_file: {{ result_fn }}
+
+
+  tasks:
+{% if test=="ping" %}
+  - name: Send the pings
+    shell: rm -f /tmp/{{ result_fn }}
+    shell: ping -c 10 {{ argument }} > /tmp/{{ result_fn }}
+
+  - name: Fetch the ping result
+    fetch: src=/tmp/{{ result_fn }} dest=/opt/xos/synchronizers/vtr/result/{{ result_fn }} flat=yes
+{% endif %}
+
diff --git a/xos/synchronizers/vtr/steps/sync_vtrtenant_vtn.yaml b/xos/synchronizers/vtr/steps/sync_vtrtenant_vtn.yaml
new file mode 100644
index 0000000..d2e6ef7
--- /dev/null
+++ b/xos/synchronizers/vtr/steps/sync_vtrtenant_vtn.yaml
@@ -0,0 +1,30 @@
+---
+- hosts: {{ instance_name }}
+  #gather_facts: False
+  connection: ssh
+  user: ubuntu
+  sudo: yes
+  vars:
+      container_name: {{ container_name }}
+      wan_container_ip: {{ wan_container_ip }}
+      wan_container_netbits: {{ wan_container_netbits }}
+      wan_container_mac: {{ wan_container_mac }}
+      wan_container_gateway_ip: {{ wan_container_gateway_ip }}
+      wan_vm_ip: {{ wan_vm_ip }}
+      wan_vm_mac: {{ wan_vm_mac }}
+
+      test: {{ test }}
+      argument: {{ argument }}
+      result_file: {{ result_fn }}
+
+
+  tasks:
+{% if test=="ping" %}
+  - name: Send the pings
+    shell: rm -f /tmp/{{ result_fn }}
+    shell: ping -c 10 {{ argument }} > /tmp/{{ result_fn }}
+
+  - name: Fetch the ping result
+    fetch: src=/tmp/{{ result_fn }} dest=/opt/xos/synchronizers/vtr/result/{{ result_fn }} flat=yes
+{% endif %}
+
diff --git a/xos/synchronizers/vtr/vtn_vtr_synchronizer_config b/xos/synchronizers/vtr/vtn_vtr_synchronizer_config
new file mode 100644
index 0000000..2c9140a
--- /dev/null
+++ b/xos/synchronizers/vtr/vtn_vtr_synchronizer_config
@@ -0,0 +1,47 @@
+
+[plc]
+name=plc
+deployment=VICCI
+
+[db]
+name=xos
+user=postgres
+password=password
+host=localhost
+port=5432
+
+[api]
+host=128.112.171.237
+port=8000
+ssl_key=None
+ssl_cert=None
+ca_ssl_cert=None
+ratelimit_enabled=0
+omf_enabled=0
+mail_support_address=support@localhost
+nova_enabled=True
+
+[observer]
+name=vtr
+dependency_graph=/opt/xos/synchronizers/vtr/model-deps
+steps_dir=/opt/xos/synchronizers/vtr/steps
+sys_dir=/opt/xos/synchronizers/vtr/sys
+deleters_dir=/opt/xos/synchronizers/vtr/deleters
+log_file=console
+#/var/log/hpc.log
+driver=None
+pretend=False
+backoff_disabled=True
+save_ansible_output=True
+# set proxy_ssh to false on cloudlab
+full_setup=True
+proxy_ssh=True
+proxy_ssh_key=/opt/xos/synchronizers/vtr/node_key
+proxy_ssh_user=root
+
+[networking]
+use_vtn=True
+
+[feefie]
+client_id='vicci_dev_central'
+user_id='pl'
diff --git a/xos/synchronizers/vtr/vtr-synchronizer.py b/xos/synchronizers/vtr/vtr-synchronizer.py
new file mode 100755
index 0000000..84bec4f
--- /dev/null
+++ b/xos/synchronizers/vtr/vtr-synchronizer.py
@@ -0,0 +1,11 @@
+#!/usr/bin/env python
+
+# This imports and runs ../../xos-observer.py
+
+import importlib
+import os
+import sys
+observer_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),"../../synchronizers/base")
+sys.path.append(observer_path)
+mod = importlib.import_module("xos-synchronizer")
+mod.main()
diff --git a/xos/synchronizers/vtr/vtr_synchronizer_config b/xos/synchronizers/vtr/vtr_synchronizer_config
new file mode 100644
index 0000000..51bf25a
--- /dev/null
+++ b/xos/synchronizers/vtr/vtr_synchronizer_config
@@ -0,0 +1,41 @@
+
+[plc]
+name=plc
+deployment=VICCI
+
+[db]
+name=xos
+user=postgres
+password=password
+host=localhost
+port=5432
+
+[api]
+host=128.112.171.237
+port=8000
+ssl_key=None
+ssl_cert=None
+ca_ssl_cert=None
+ratelimit_enabled=0
+omf_enabled=0
+mail_support_address=support@localhost
+nova_enabled=True
+
+[observer]
+name=vtr
+dependency_graph=/opt/xos/synchronizers/vtr/model-deps
+steps_dir=/opt/xos/synchronizers/vtr/steps
+sys_dir=/opt/xos/synchronizers/vtr/sys
+deleters_dir=/opt/xos/synchronizers/vtr/deleters
+log_file=console
+driver=None
+pretend=False
+backoff_disabled=True
+save_ansible_output=True
+# set proxy_ssh to false on cloudlab
+proxy_ssh=False
+full_setup=True
+
+[feefie]
+client_id='vicci_dev_central'
+user_id='pl'
diff --git a/xos/xos/settings.py b/xos/xos/settings.py
index 8764b80..5c6c0cb 100644
--- a/xos/xos/settings.py
+++ b/xos/xos/settings.py
@@ -180,6 +180,7 @@
     'services.ceilometer',
     'services.requestrouter',
     'services.syndicate_storage',
+    'services.vtr',
     'geoposition',
     'rest_framework_swagger',
 )