-
Notifications
You must be signed in to change notification settings - Fork 132
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
base: master
Are you sure you want to change the base?
WIP - nso-nipap rewrite #1049
Changes from 25 commits
6a69d47
f053177
dd5402b
f41b656
84bc007
4e9bff3
c97ffbe
aa0535d
b71599b
75a8c60
1772018
06186c6
44e3d47
fff25bd
d9a7131
c7ce355
c3e48d7
3f0d561
a55701c
531073a
29d60b5
296c54d
6561b50
ca2dc15
ea2bd52
cdeea83
9d3cad9
4c5197f
8b8a0da
6fea8da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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): | ||
""" 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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" % | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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