Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - nso-nipap rewrite #1049

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6a69d47
Yang rewrite to support from-prefix
fredsod Sep 12, 2016
f053177
Cleanup and added support for the new yang model
fredsod Sep 12, 2016
dd5402b
Removed unused variables
fredsod Sep 12, 2016
f41b656
Added support for from-prefix
fredsod Sep 12, 2016
84bc007
nso-nipap: Added config false to error code
fredsod Sep 13, 2016
4e9bff3
nso-nipap: Added writeError function
fredsod Sep 13, 2016
c97ffbe
nso-nipap: Added updatePrefix function
fredsod Sep 13, 2016
aa0535d
nso-nipap: Refactoring yang model
fredsod Sep 13, 2016
b71599b
nso-nipap: Added support for the new yang model
fredsod Sep 13, 2016
75a8c60
nso-nipap: Clean up
fredsod Sep 14, 2016
1772018
nso-nipap: Add javadoc
fredsod Sep 19, 2016
06186c6
fix
fredsod Sep 19, 2016
44e3d47
nso-nipap: Indent fix
fredsod Sep 20, 2016
fff25bd
nso-nipap: Code style fix
fredsod Sep 20, 2016
d9a7131
nso-nipap: Various style fixes
garberg Dec 5, 2016
c7ce355
nso-nipap: Rename functions for clarity
garberg Dec 5, 2016
c3e48d7
nso-nipap: Renamed oldPrefix to basePrefix
garberg Dec 5, 2016
3f0d561
nso-nipap: Detail thrown exceptions
garberg Dec 5, 2016
a55701c
nso-nipap: Handle IPv6 in addHostPrefixFromPrefix
garberg Dec 5, 2016
531073a
nso-nipap: Use recrusive delete when removing a prefix
fredsod Jan 2, 2017
29d60b5
nso-nipap: Added support for deallocate from-prefix
fredsod Jan 2, 2017
296c54d
nso-nipap: Changed response-choice to leaf
fredsod Jan 23, 2017
6561b50
nso-nipap: Added allocation request templates
garberg Mar 6, 2017
ca2dc15
nso-nipap: Added python helper functions
garberg Mar 9, 2017
ea2bd52
Merge pull request #2 from garberg/helper_libs
fredsod Mar 22, 2017
cdeea83
nso-nipap: Improve error handling to avoid lockup
garberg Apr 5, 2017
9d3cad9
Merge pull request #3 from garberg/improve_error_handling
fredsod Apr 10, 2017
4c5197f
nso-nipap: Write status on from-prefix-requests
fredsod Apr 11, 2017
8b8a0da
nso-nipap: Pass re-deploy path as ConfPath
garberg May 18, 2017
6fea8da
Merge pull request #4 from garberg/redeploy_confpath
fredsod May 18, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
195 changes: 195 additions & 0 deletions nso-nipap/nso-nipap/python/nso_nipap/prefix_allocator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import logging

import ncs
import ncs.maapi as maapi
import ncs.maagic as maagic

log = logging.getLogger()


def prefix_request(service, svc_xpath, pool_name, allocation_name,
prefix_length, family=4, prefix_attributes=None):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent

""" Create a prefix allocation request

Arguments:
service -- the requesting service node
svc_xpath -- xpath to the requesting service
pool_name -- name of pool to request from
allocation_name -- unique allocation name
prefix_length -- the prefix length of the allocated network
family -- address family of the network, 4 (IPv4) or 6 (IPv6)
prefix_attributes -- dict with prefix attributes
"""

if prefix_attributes is None:
prefix_attributes = {}

template = ncs.template.Template(service)
vars = ncs.template.Variables()

# required variables
vars.add("POOL_NAME", pool_name)
vars.add("ALLOCATION_NAME", allocation_name)
vars.add("SERVICE", svc_xpath)
vars.add("PREFIX_LENGTH", prefix_length)
vars.add("FAMILY", family)

# optional prefix attributes
_set_prefix_attributes(prefix_attributes, vars)

log.debug("Placing prefix request with data %s" % vars)

template.apply('nso-nipap-prefix-request', vars)


def from_prefix_request(service, pool_name, main_allocation_name,
from_pref_allocation_name, prefix_attributes=None):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent

""" Create a from-prefix allocation request

Arguments:
service -- the requesting service node
pool_name -- name of pool to request from
main_allocation_name -- name of main allocation which the from-prefix is appended to

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (92 > 79 characters)

from_pref_allocation_name -- name of from-prefix allocation
prefix_attributes -- dict with prefix attributes
"""

if prefix_attributes is None:
prefix_attributes = {}

template = ncs.template.Template(service)
vars = ncs.template.Variables()

# required variables
vars.add('POOL_NAME', pool_name)
vars.add('ALLOCATION_NAME', main_allocation_name)
vars.add('FROM_PREFIX_ALLOCATION_NAME', from_pref_allocation_name)

# optional prefix attributes
_set_prefix_attributes(prefix_attributes, vars)

log.debug("Placing from-prefix request with data %s" % vars)

template.apply('nso-nipap-from-prefix-request', vars)


def prefix_read(root, pool_name, allocation_name):
"""Returns the allocated network or None

Arguments:
root -- a maagic root for the current transaction
pool_name -- name of pool to request from
allocation_name -- unique allocation name
"""
# Look in the current transaction
_verify_allocation(root, pool_name, allocation_name)

# Now we switch from the current trans to actually see if
# we have received the alloc
with maapi.single_read_trans("admin", "system",
db=ncs.OPERATIONAL) as th:

oper_root = maagic.get_root(th)
alloc = _get_allocation(oper_root, pool_name, allocation_name)
if alloc is None:
return None

if alloc.response.prefix:
return alloc.response.prefix
else:
return None


def from_prefix_read(root, pool_name, main_allocation_name, from_prefix_allocation_name):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (89 > 79 characters)

"""Returns the allocated network or None

Arguments:
root -- a maagic root for the current transaction
pool_name -- name of pool to request from
main_allocation_name -- name of allocation which the from-prefix allocation belongs to

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (94 > 79 characters)

from_prefix_allocation_name -- name of from-prefix allocation
"""
# Look in the current transaction
alloc = _verify_allocation(root, pool_name, main_allocation_name)
if from_prefix_allocation_name not in alloc.from_prefix_request:
raise LookupError("from-prefix allocation %s does not exist in main allocation %s from pool %s" %

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (105 > 79 characters)

(from_prefix_allocation_name, main_allocation_name, pool_name))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (89 > 79 characters)


# Now we switch from the current trans to actually see if
# we have received the alloc
with maapi.single_read_trans("admin", "system",
db=ncs.OPERATIONAL) as th:
oper_root = maagic.get_root(th)
alloc = _get_allocation(oper_root, pool_name, main_allocation_name)
if alloc is None:
return None

if from_prefix_allocation_name not in alloc.from_prefix_request:
return None

from_pref_alloc = alloc.from_prefix_request[from_prefix_allocation_name]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (80 > 79 characters)


if from_pref_alloc.response.prefix:
return from_pref_alloc.response.prefix
else:
return None


def _set_prefix_attributes(attributes, template_vars):
""" Fetch prefix attributes from CDB and write to template vars
"""

template_vars.add('CUSTOMER_ID',
attributes['customer_id'] if 'customer_id' in attributes else '-1')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent


template_vars.add('DESCRIPTION',
attributes['description'] if 'description' in attributes else '-1')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent


template_vars.add('NODE',
attributes['node'] if 'node' in attributes else '-1')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent


template_vars.add('ORDER_ID',
attributes['order_id'] if 'order_id' in attributes else '-1')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent



def _verify_allocation(root, pool_name, allocation_name):
""" Verify that the allocation exists and return it

Throws LookupError if allocation is missing.
"""
pool_l = root.ncs__services.nipap__nipap.from_pool

if pool_name not in pool_l:
raise LookupError("Pool %s does not exist" % (pool_name))

pool = pool_l[pool_name]

if allocation_name not in pool.request:
raise LookupError("allocation %s does not exist in pool %s" %
(allocation_name, pool_name))

return pool.request[allocation_name]


def _get_allocation(root, pool_name, allocation_name):
""" Return allocation.

Returns None if allocation does not exist, raises exception if
allocation status == 'error'.
"""
alloc = None
try:
alloc = _verify_allocation(root, pool_name, allocation_name)
except LookupError as e:
return None

if alloc.response.status == 'ok':
return alloc
elif alloc.response.status == 'error':
raise AllocationError(alloc.response.status_message)


class AllocationError(Exception):
""" Exception thrown when allocation has failed.
"""
pass
Loading