Skip to content

Commit

Permalink
Added Service Templates TachyonicProject#14
Browse files Browse the repository at this point in the history
  • Loading branch information
Vuader committed Sep 5, 2018
1 parent ecbde48 commit 7327c42
Show file tree
Hide file tree
Showing 5 changed files with 355 additions and 1 deletion.
126 changes: 126 additions & 0 deletions netrino/models/service_templates.py
@@ -0,0 +1,126 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018 Christiaan Frans Rademan, David Kruger.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
from uuid import uuid4

from luxon import register
from luxon import SQLModel
from luxon.utils.timezone import now

from netrino.models.elements import netrino_element
from netrino.models.elements import netrino_element_tag
#from netrino.models.elements import netrino_element_interface

@register.model()
class netrino_service_template(SQLModel):
id = SQLModel.Uuid(default=uuid4, internal=True)
name = SQLModel.String(null=False)
creation_time = SQLModel.DateTime(default=now, readonly=True)
primary_key = id
unique_service_template = SQLModel.UniqueIndex(name)

@register.model()
class netrino_service_template_entry(SQLModel):
id = SQLModel.Uuid(default=uuid4, internal=True)
service_template = SQLModel.Uuid()
entry_no = SQLModel.Integer(null=False)
yang_model = SQLModel.String(null=False)
interface = SQLModel.Text(default="netconf")
mappings = SQLModel.MediumText(null=True)
user_select = SQLModel.MediumText(null=True)
task_output = SQLModel.MediumText(null=True)
element = SQLModel.Uuid(null=True)
element_tag = SQLModel.String(null=True)
creation_time = SQLModel.DateTime(default=now, readonly=True)
service_template_ref = SQLModel.ForeignKey(service_template,
netrino_service_template.id)
element_ref = SQLModel.ForeignKey(element, netrino_element.id)
element_tag_ref = SQLModel.ForeignKey(element_tag,netrino_element_tag.name)
# interface_ref = SQLModel.ForeignKey(interface,
# netrino_element_interface.interface)
unique_entry = SQLModel.UniqueIndex(service_template, entry_no)
primary_key = id

@register.model()
class netrino_servicetemplate_pool(SQLModel):
id = SQLModel.Uuid(default=uuid4, internal=True)
path = SQLModel.String(null=False)
type = SQLModel.String(null=False)
pool = SQLModel.String(null=False)
servicetemplate_entry = SQLModel.Uuid(null=False)
servicetemplate_entry_ref = SQLModel.ForeignKey(servicetemplate_entry,
netrino_service_template_entry.id)
servicetemplate_entry_index = SQLModel.Index(servicetemplate_entry)
primary_key = id

@register.model()
class netrino_servicetemplate_mappings(SQLModel):
id = SQLModel.Uuid(default=uuid4, internal=True)
path = SQLModel.String(null=False)
mapper = SQLModel.String(null=False)
servicetemplate_entry = SQLModel.Uuid(null=False)
servicetemplate_entry_ref = SQLModel.ForeignKey(servicetemplate_entry,
netrino_service_template_entry.id)
servicetemplate_entry_index = SQLModel.Index(servicetemplate_entry)
primary_key = id

@register.model()
class netrino_user_select(SQLModel):
id = SQLModel.Uuid(default=uuid4, internal=True)
path = SQLModel.String(null=False)
url = SQLModel.Uri(null=False)
servicetemplate_entry = SQLModel.Uuid(null=False)
servicetemplate_entry_ref = SQLModel.ForeignKey(servicetemplate_entry,
netrino_service_template_entry.id)
servicetemplate_entry_index = SQLModel.Index(servicetemplate_entry)
primary_key = id

@register.model()
class netrino_task_output(SQLModel):
id = SQLModel.Uuid(default=uuid4, internal=True)
path = SQLModel.String(null=False)
entry_no = SQLModel.Integer(null=False)
output_path = SQLModel.String(null=False)
servicetemplate_entry = SQLModel.Uuid(null=False)
servicetemplate_entry_ref = SQLModel.ForeignKey(servicetemplate_entry,
netrino_service_template_entry.id)
servicetemplate_entry_index = SQLModel.Index(servicetemplate_entry)
primary_key = id

@register.model()
class netrino_servicetemplate_static(SQLModel):
id = SQLModel.Uuid(default=uuid4, internal=True)
path = SQLModel.String(null=False)
value = SQLModel.String(null=False)
type = SQLModel.String(default="str")
servicetemplate_entry = SQLModel.Uuid(null=False)
servicetemplate_entry_ref = SQLModel.ForeignKey(servicetemplate_entry,
netrino_service_template_entry.id)
servicetemplate_entry_index = SQLModel.Index(servicetemplate_entry)
primary_key = id
55 changes: 55 additions & 0 deletions netrino/utils/service_template.py
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018 Dave Kruger.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.


def separate_entries(d, keys=[]):
"""Removes certain keys from a dict, and returns them in in a separate list
Usage:
removed, left = separate_entries({"foo1": "bar1", "foo2": "bar2"}, ["foo2"])
will return:
removed = {"foo2": "bar2"}
left = {"foo1": "bar1"}
Args:
d (dict): Dictionary to strip from.
keys (list): List of keys to be returned in a separate list.
Returns:
tuple containing two dicts.
"""
removed = {}
for k in keys:
try:
removed[k]= d[k]
del d[k]
except KeyError:
pass
return removed, d
1 change: 1 addition & 0 deletions netrino/views/__init__.py
@@ -1,3 +1,4 @@
from netrino.views import elements
#from netrino.views import ipam
from netrino.views import interface
from netrino.views import service_templates
171 changes: 171 additions & 0 deletions netrino/views/service_templates.py
@@ -0,0 +1,171 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018 David Kruger.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
from uuid import uuid4
from luxon import router
from luxon import register
from luxon import db

from luxon.utils import js
from luxon.exceptions import FieldMissing, SQLIntegrityError
from luxon.exceptions import HTTPNotFound
from collections import OrderedDict

from netrino.models.service_templates import netrino_service_template
from netrino.models.service_templates import netrino_service_template_entry
from netrino.models.service_templates import netrino_servicetemplate_mappings
from netrino.models.service_templates import netrino_servicetemplate_static
from netrino.models.service_templates import netrino_servicetemplate_pool
from netrino.models.service_templates import netrino_user_select
from netrino.models.service_templates import netrino_task_output

from psychokinetic.utils.api import sql_list

ALLOCATIONS = {'pool_allocations': netrino_servicetemplate_pool,
'mappings': netrino_servicetemplate_mappings,
'user_select':netrino_user_select,
'task_output':netrino_task_output,
'static_assignments': netrino_servicetemplate_static}

@register.resources()
class ServiceTemplate():
def __init__(self):
router.add('POST',
'/v1/service-template',
self.create, tag='services:admin')
router.add('GET',
'/v1/service-template/{name}',
self.view, tag='services:view')
router.add('GET',
'/v1/service-templates',
self.list, tag='services:view')
router.add(['PUT','PATCH'],
'/v1/service-template/{name}',
self.update, tag='services:view')
router.add('DELETE',
'/v1/service-template/{name}',
self.delete, tag='services:view')

def create(self, req, resp):
name = req.json['name']
service_template = netrino_service_template()
service_template.update({'name':name})
models = req.json['models']
service_template.commit()
try:
for model in models:
nste = netrino_service_template_entry()
allocate = {k:model.pop(k) for k in ALLOCATIONS if k in model}
nste['service_template'] = service_template['id']
nste.update(model)
nste.commit()
for a in allocate:
for e in allocate[a]:
alloc_model = ALLOCATIONS[a]()
alloc_model['servicetemplate_entry'] = nste['id']
alloc_model.update(e)
alloc_model.commit()
except SQLIntegrityError as err:
service_template.delete()
service_template.commit()
raise err

return self.view(req, resp, name)

def list(self, req, resp):
return sql_list(req, 'netrino_service_template',['id', 'name'])

def view(self, req, resp, name):
result = OrderedDict()
with db() as conn:
cur = conn.execute("SELECT * FROM netrino_service_template "
"WHERE name=? OR id=?", (name,name,))
stemplate = cur.fetchone()
if not stemplate:
raise HTTPNotFound
result['name'] = stemplate['name']
result['models'] = []
msql = "SELECT * FROM netrino_service_template_entry WHERE " \
"service_template=?"
entries = conn.execute(msql,(stemplate['id'],)).fetchall()

asql = "SELECT * FROM %s WHERE servicetemplate_entry=?"
for e in entries:
result['models'].append(e)
for a in ALLOCATIONS:
result['models'][-1][a] = []
for a in ALLOCATIONS:
cur = conn.execute(asql % ALLOCATIONS[a].__name__, e['id'])
allocate = cur.fetchall()
for ae in allocate:
result['models'][-1][a].append(ae)

return js.dumps(result)

def update(self, req, resp, name):
with db() as conn:
cur = conn.execute("SELECT * FROM netrino_service_template "
"WHERE name=? OR id=?", (name,name,))
stemplate = cur.fetchone()
if not stemplate:
raise HTTPNotFound
service_template = netrino_service_template()
service_template.sql_id(stemplate['id'])
service_template.update({'name':name})
models = req.json['models']
service_template.commit()
try:
for model in models:
nste = netrino_service_template_entry()
if 'id' in model:
nste.sql_id(model.pop('id'))
allocate = {k:model.pop(k) for k in ALLOCATIONS if k in model}
nste['service_template'] = service_template['id']
nste.update(model)
nste.commit()
for a in allocate:
for e in allocate[a]:
alloc_model = ALLOCATIONS[a]()
if 'id' in e:
alloc_model.sql_id(e.pop('id'))
alloc_model['servicetemplate_entry'] = nste['id']
alloc_model.update(e)
alloc_model.commit()
except SQLIntegrityError as err:
service_template.delete()
service_template.commit()
raise err

return self.view(req, resp, name)

def delete(self, req, resp, name):
with db() as conn:
conn.execute("DELETE FROM netrino_service_template "
"WHERE name=? OR id=?", (name,name,))
conn.commit()
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -296,7 +296,8 @@ def run(self):
'contrail = netrino.interfaces.contrail.element:Element'
],
'netrino_mappers': [
'infinitystone_domain = netrino.mappers:domain_from_context'
'infinitystone_domain = netrino.mappers:domain_from_context',
'infinitystone_tenant = netrino.mappers:tenant_from_context'
],
'netrino_resources': [
'element = netrino.resources.element.element:Element',
Expand Down

0 comments on commit 7327c42

Please sign in to comment.