Skip to content

Commit

Permalink
Merge pull request #269 from pvelumul/configpush_changes
Browse files Browse the repository at this point in the history
mangling the names of Tenants, App Profiles, EPGs, VRFs, Contracts, F…
  • Loading branch information
michsmit99 committed Dec 16, 2016
2 parents d76d040 + 5b1cf92 commit 01dfa96
Showing 1 changed file with 83 additions and 3 deletions.
86 changes: 83 additions & 3 deletions applications/configpush/apicservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ def _replace_invalid_chars(name, valid_char_set):
return stripped_name

def replace_invalid_name_chars(self, name):
valid_char_set = set('_.:-')
return self._replace_invalid_chars(name, valid_char_set)
valid_char_set = set('_.-')
valid_name = self._replace_invalid_chars(name, valid_char_set)
return valid_name[:63] # valid name validators. Range: min: "1" max: "63"

def replace_invalid_descr_chars(self, name):
valid_char_set = set('\\!#$%()*,-./:;@ _{|}~?&+')
Expand Down Expand Up @@ -230,6 +231,31 @@ def prefix_len(self):
return self._policy['prefix_len']


class RouteTagPolicy(PolicyObject):
"""
RouteTag Policy
"""

def __init__(self, policy):
super(RouteTagPolicy, self).__init__(policy)

@property
def name(self):
"""
route_tag name
:return: String containing the route_tag name
"""
return self._policy['name']

@property
def subnet_mask(self):
"""
route_tag subnet_mask address
:return: String containing the route_tag subnet_mask
"""
return self._policy['subnet_mask']


class EPGPolicy(PolicyObject):
"""
EPG Policy
Expand All @@ -239,6 +265,14 @@ def __init__(self, policy):
super(EPGPolicy, self).__init__(policy)
self._node_policies = []
self._populate_node_policies()
self._populate_routeTag_policy()

def _populate_routeTag_policy(self):
"""
Fill in the RouteTag policy
:return: None
"""
self._routeTag_policy = RouteTagPolicy(self._policy['route_tag'])

def _populate_node_policies(self):
"""
Expand Down Expand Up @@ -299,6 +333,13 @@ def get_node_policies(self):
"""
return self._node_policies

def get_route_tag_policy(self):
"""
Get the RouteTag policy
:return: List of RouteTagPolicy instances
"""
return self._routeTag_policy


class ApplicationPolicy(PolicyObject):
"""
Expand All @@ -316,6 +357,10 @@ def name(self):
def clusters(self):
return self._policy['clusters']

@name.setter
def name(self, value):
self._policy['name'] = value


class ContextPolicy(PolicyObject):
"""
Expand All @@ -337,6 +382,10 @@ def tenant_id(self):
def tenant_name(self):
return self._policy['tenant_name']

@name.setter
def name(self, value):
self._policy['name'] = value


class ContractPolicy(PolicyObject):
"""
Expand Down Expand Up @@ -1328,9 +1377,14 @@ def pushing_epgs(self, apic, tenant, app, THROTTLE_SIZE):
epg.set_base_epg(base_epg)
criterion = AttributeCriterion('criterion', epg)
ipaddrs = []

route_tag_policy = epg_policy.get_route_tag_policy()
ipnetwork = ipaddress.ip_network(unicode(route_tag_policy.subnet_mask))

for node_policy in epg_policy.get_node_policies():
ipaddr = ipaddress.ip_address(unicode(node_policy.ip))
if not ipaddr.is_multicast: # Skip multicast addresses. They cannot be IP based EPGs
# Skip multicast addresses and broadcast address. They cannot be IP based EPGs
if ipnetwork.broadcast_address != ipaddr and not ipaddr.is_multicast:
ipaddrs.append(ipaddr)
nets = ipaddress.collapse_addresses(ipaddrs)
for net in nets:
Expand Down Expand Up @@ -1465,6 +1519,24 @@ def pushing_remaining_epgs(self, tenant, app, bd=None, base_epg=None):
epg.descr = epg_policy.descr[0:127]
self.consume_and_provide_contracts_for_epgs(epg_policy, epg, tenant)

def _replace_invalid_chars(self, name, valid_char_set):
stripped_name = ''
name_len = 0
for char in name:
if name_len < 63: # valid name validators. Range: min: "1" max: "63"
if char not in valid_char_set and not char.isalnum():
stripped_name += '_'
else:
stripped_name += char
name_len = name_len + 1
return stripped_name

def replace_invalid_name_chars(self):
valid_char_set = set('_.-')
self._tenant_name = self._replace_invalid_chars(self._tenant_name, valid_char_set)
self._app_name = self._replace_invalid_chars(self._app_name, valid_char_set)
self._l3ext_name = self._replace_invalid_chars(self._l3ext_name, valid_char_set)

def push_config_to_apic(self):
"""
Push the configuration to the APIC
Expand All @@ -1477,6 +1549,9 @@ def push_config_to_apic(self):
self.set_tenant_name(self.cdb.get_context_config().tenant_name)
elif self._tenant_name == '':
self.set_tenant_name('acitoolkit')
logging.debug('Removing invalid characters in tenant_name, app_name, l3ext_name')
self.replace_invalid_name_chars()

logging.debug('Removing duplicate contracts')
self.remove_duplicate_contracts()

Expand Down Expand Up @@ -1615,6 +1690,11 @@ def push_config_to_apic(self):
def mangle_names(self):
unique_id = 0
name_db_by_id = {}
if not self.cdb._context_policy is None:
context_policy = self.cdb._context_policy
context_policy.name = context_policy.replace_invalid_name_chars(context_policy.name)
for application_policy in self.cdb.get_application_policies():
application_policy.name = application_policy.replace_invalid_name_chars(application_policy.name)
for epg_policy in self.cdb.get_epg_policies():
epg_policy.descr = epg_policy.name + ':' + epg_policy.id
epg_policy.descr = epg_policy.replace_invalid_descr_chars(epg_policy.descr)
Expand Down

0 comments on commit 01dfa96

Please sign in to comment.