Merge branch 'master' into feature/volt-autoconfig
diff --git a/xos/configurations/cord-pod/vtn-external.yaml b/xos/configurations/cord-pod/vtn-external.yaml
index 74e7ef7..51cca08 100644
--- a/xos/configurations/cord-pod/vtn-external.yaml
+++ b/xos/configurations/cord-pod/vtn-external.yaml
@@ -8,7 +8,7 @@
 topology_template:
   node_templates:
 
-    service_ONOS_VTN:
+    service_ONOS_CORD:
       type: tosca.nodes.ONOSService
       requirements:
       properties:
@@ -21,8 +21,16 @@
       type: tosca.nodes.ONOSVTNApp
       requirements:
           - onos_tenant:
-              node: service_ONOS_VTN
+              node: service_ONOS_CORD
               relationship: tosca.relationships.TenantOfService
+          - vtn_service:
+              node: service_vtn
+              relationship: tosca.relationships.UsedByService
       properties:
           dependencies: org.onosproject.drivers, org.onosproject.drivers.ovsdb, org.onosproject.openflow-base, org.onosproject.ovsdb-base, org.onosproject.dhcp, org.onosproject.cordvtn, org.onosproject.olt, org.onosproject.igmp, org.onosproject.cordmcast
           autogenerate: vtn-network-cfg
+
+    service_vtn:
+      type: tosca.nodes.VTNService
+      properties:
+          view_url: /admin/vtn/vtnservice/$id$/
diff --git a/xos/services/vtn/__init__.py b/xos/services/vtn/__init__.py
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/xos/services/vtn/__init__.py
@@ -0,0 +1 @@
+
diff --git a/xos/services/vtn/admin.py b/xos/services/vtn/admin.py
new file mode 100644
index 0000000..c64e054
--- /dev/null
+++ b/xos/services/vtn/admin.py
@@ -0,0 +1,87 @@
+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.vtn.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 VTNServiceForm(forms.ModelForm):
+    privateGatewayMac = forms.CharField(required=False)
+    localManagementIp = forms.CharField(required=False)
+    ovsdbPort = forms.CharField(required=False)
+    sshPort = forms.CharField(required=False)
+    sshUser = forms.CharField(required=False)
+    sshKeyFile = forms.CharField(required=False)
+    mgmtSubnetBits = forms.CharField(required=False)
+
+    def __init__(self,*args,**kwargs):
+        super (VTNServiceForm,self ).__init__(*args,**kwargs)
+        if self.instance:
+            self.fields['privateGatewayMac'].initial = self.instance.privateGatewayMac
+            self.fields['localManagementIp'].initial = self.instance.localManagementIp
+            self.fields['ovsdbPort'].initial = self.instance.ovsdbPort
+            self.fields['sshPort'].initial = self.instance.sshPort
+            self.fields['sshUser'].initial = self.instance.sshUser
+            self.fields['sshKeyFile'].initial = self.instance.sshKeyFile
+            self.fields['mgmtSubnetBits'].initial = self.instance.mgmtSubnetBits
+
+    def save(self, commit=True):
+        self.instance.privateGatewayMac = self.cleaned_data.get("privateGatewayMac")
+        self.instance.localManagementIp = self.cleaned_data.get("localManagementIp")
+        self.instance.ovsdbPort = self.cleaned_data.get("ovsdbPort")
+        self.instance.sshPort = self.cleaned_data.get("sshPort")
+        self.instance.sshUser = self.cleaned_data.get("sshUser")
+        self.instance.sshKeyFile = self.cleaned_data.get("sshKeyFile")
+        self.instance.mgmtSubnetBits = self.cleaned_data.get("mgmtSubnetBits")
+        return super(VTNServiceForm, self).save(commit=commit)
+
+    class Meta:
+        model = VTNService
+
+class VTNServiceAdmin(ReadOnlyAwareAdmin):
+    model = VTNService
+    form = VTNServiceForm
+    verbose_name = "VTN Service"
+    verbose_name_plural = "VTN 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",
+                                    'privateGatewayMac', 'localManagementIp', 'ovsdbPort', 'sshPort', 'sshUser', 'sshKeyFile', 'mgmtSubnetBits' ], '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', 'VTN Service Details'),
+#        ('administration', 'Administration'),
+        ('slices','Slices'),
+        ('serviceattrs','Additional Attributes'),
+        ('serviceprivileges','Privileges'),
+    )
+
+    suit_form_includes = ( # ('vtnadmin.html', 'top', 'administration'),
+                           ) #('hpctools.html', 'top', 'tools') )
+
+    def queryset(self, request):
+        return VTNService.get_service_objects_by_user(request.user)
+
+admin.site.register(VTNService, VTNServiceAdmin)
+
diff --git a/xos/services/vtn/models.py b/xos/services/vtn/models.py
new file mode 100644
index 0000000..cb254d7
--- /dev/null
+++ b/xos/services/vtn/models.py
@@ -0,0 +1,44 @@
+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
+
+VTN_KIND = "VTN"
+
+# -------------------------------------------
+# VTN
+# -------------------------------------------
+
+class VTNService(Service):
+    KIND = VTN_KIND
+
+    class Meta:
+        app_label = "vtn"
+        verbose_name = "VTN Service"
+        proxy = True
+
+    simple_attributes = ( ("privateGatewayMac", "00:00:00:00:00:01"),
+                          ("localManagementIp", "172.27.0.1/24"),
+                          ("ovsdbPort", "6641"),
+                          ("sshPort", "22"),
+                          ("sshUser", "root"),
+                          ("sshKeyFile", "/root/node_key") ,
+                          ("mgmtSubnetBits", "24"),
+
+                         )
+
+VTNService.setup_simple_attributes()
+
diff --git a/xos/services/vtn/templates/vtnadmin.html b/xos/services/vtn/templates/vtnadmin.html
new file mode 100644
index 0000000..a3a2a52
--- /dev/null
+++ b/xos/services/vtn/templates/vtnadmin.html
@@ -0,0 +1,5 @@
+<div class = "row text-center">
+    <div class="col-xs-12">
+        <a href="/admin/vtn/vnrtenant/">vTN Tenants</a>
+    </div>
+</div>
diff --git a/xos/tools/xos-manage b/xos/tools/xos-manage
index 23cda72..7fed845 100755
--- a/xos/tools/xos-manage
+++ b/xos/tools/xos-manage
@@ -148,6 +148,7 @@
     python ./manage.py makemigrations onos
     python ./manage.py makemigrations vtr
     python ./manage.py makemigrations vrouter
+    python ./manage.py makemigrations vtn
     #python ./manage.py makemigrations servcomp
 }
 
diff --git a/xos/tosca/custom_types/xos.m4 b/xos/tosca/custom_types/xos.m4
index b8e8581..9bd504a 100644
--- a/xos/tosca/custom_types/xos.m4
+++ b/xos/tosca/custom_types/xos.m4
@@ -252,6 +252,37 @@
             xos_base_props
             xos_base_service_props
 
+    tosca.nodes.VTNService:
+        derived_from: tosca.nodes.Root
+        description: >
+            CORD: The vRouter Service.
+        capabilities:
+            xos_base_service_caps
+        properties:
+            xos_base_props
+            xos_base_service_props
+            privateGatewayMac:
+                type: string
+                required: false
+            localManagementIp:
+                type: string
+                required: false
+            ovsdbPort:
+                type: string
+                required: false
+            sshPort:
+                type: string
+                required: false
+            sshUser:
+                type: string
+                required: false
+            sshKeyFile:
+                type: string
+                required: false
+            mgmtSubnetBits:
+                type: string
+                required: false
+
     tosca.nodes.CDNService:
         derived_from: tosca.nodes.Root
         description: >
diff --git a/xos/tosca/custom_types/xos.yaml b/xos/tosca/custom_types/xos.yaml
index d887e7f..f790cc5 100644
--- a/xos/tosca/custom_types/xos.yaml
+++ b/xos/tosca/custom_types/xos.yaml
@@ -442,6 +442,81 @@
                 required: false
                 description: Version number of Service.
 
+    tosca.nodes.VTNService:
+        derived_from: tosca.nodes.Root
+        description: >
+            CORD: The vRouter 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
+            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.
+            privateGatewayMac:
+                type: string
+                required: false
+            localManagementIp:
+                type: string
+                required: false
+            ovsdbPort:
+                type: string
+                required: false
+            sshPort:
+                type: string
+                required: false
+            sshUser:
+                type: string
+                required: false
+            sshKeyFile:
+                type: string
+                required: false
+            mgmtSubnetBits:
+                type: string
+                required: false
+
     tosca.nodes.CDNService:
         derived_from: tosca.nodes.Root
         description: >
diff --git a/xos/tosca/resources/vtnservice.py b/xos/tosca/resources/vtnservice.py
new file mode 100644
index 0000000..f0940d6
--- /dev/null
+++ b/xos/tosca/resources/vtnservice.py
@@ -0,0 +1,16 @@
+import os
+import pdb
+import sys
+import tempfile
+sys.path.append("/opt/tosca")
+from translator.toscalib.tosca_template import ToscaTemplate
+
+from services.vtn.models import VTNService
+
+from service import XOSService
+
+class XOSVTNService(XOSService):
+    provides = "tosca.nodes.VTNService"
+    xos_model = VTNService
+    copyin_props = ["view_url", "icon_url", "enabled", "published", "public_key", "versionNumber", 'privateGatewayMac', 'localManagementIp', 'ovsdbPort', 'sshPort', 'sshUser', 'sshKeyFile', 'mgmtSubnetBits']
+
diff --git a/xos/xos/settings.py b/xos/xos/settings.py
index f457476..ad08777 100644
--- a/xos/xos/settings.py
+++ b/xos/xos/settings.py
@@ -182,6 +182,7 @@
     'services.syndicate_storage',
     'services.vtr',
     'services.vrouter',
+    'services.vtn',
     'geoposition',
     'rest_framework_swagger',
 )