def put_service_endpoint(self, ipAddress, portNumber, profileName): error_data = {} #Check if the profile is defined profile_defined, profile_type = self.is_traffic_profile_defined(profileName) if not profile_defined: logger.warning(err.CannotFindElement("put_service_endpoint", \ "The traffic profile " + profileName + " is not defined, ignoring this request")) error_data['status'] = 400 error_data['title'] = 'CANNOT FIND TRAFFIC PROFILE' error_data['description'] = 'The traffic profile ' + profileName + ' is not defined. Ignoring this event.' return True, error_data #Check if the policy is defined if profile_type == 'AppRoute': policyName = self.app_route_traffic_profiles[profileName]['policyName'] else: policyName = self.data_traffic_profiles[profileName]['policyName'] policy_defined, _ = self.is_policy_defined(policyName, profile_type) if not policy_defined: logger.warning(err.CannotFindElement("put_service_endpoint", \ 'Ignoring request: Cannot find a policy called '+ policyName )) return False, error_data # Do the actual work error, error_data = self.delete_service_endpoint(ipAddress, portNumber) if error: return error, error_data else: error, error_data = self.post_service_endpoint(ipAddress, portNumber, profileName) return error, error_data
def delete_service_endpoint(self, ipAddress, portNumber): key = ipAddress + '_' + portNumber error = {} if key not in self.srv_endpoints.keys(): logger.warning( err.CannotFindElement( "delete_service_endpoint", "This endpoint is not defined, ignoring request.")) error['status'] = 400 error['title'] = 'ENDPOINT NOT FOUND' error[ 'description'] = 'Cannot process DELETE event: resource IP ' + ipAddress + ' and port ' + portNumber + ' does not exist. Ignoring this event.' return True, error traffic_profile = self.srv_endpoints[key]['trafficProfileName'] policy_id = self.srv_endpoints[key]['policyId'] try: if traffic_profile in self.app_route_traffic_profiles.keys(): policy = self.api_endpoint.get_approute_policy_by_id(policy_id) payload = templates.remove_endpoint(policy, ipAddress, portNumber) response = self.api_endpoint.put_approute_policy( policy_id, payload, 'delete_service_endpoint') del self.srv_endpoints[key] if len(response["masterTemplatesAffected"]) != 0: self.api_endpoint.update_active_policy( response["masterTemplatesAffected"], 'delete_service_endpoint') elif traffic_profile in self.data_traffic_profiles.keys(): policy = self.api_endpoint.get_data_policy_by_id(policy_id) payload = templates.remove_endpoint(policy, ipAddress, portNumber) response = self.api_endpoint.put_data_policy( policy_id, payload, 'delete_service_endpoint') del self.srv_endpoints[key] if len(response["masterTemplatesAffected"]) != 0: self.api_endpoint.update_active_policy( response["masterTemplatesAffected"], 'delete_service_endpoint') else: logger.warning(err.CannotFindElement('delete_service_endpoint', \ 'Traffic profile ' + traffic_profile + ' is not defined, ignoring request.')) except Exception as e: logger.error( 'An error ocurred while communicating with the SDWAN controller.' ) logger.error('Exception name: %s', repr(e)) logger.error('Details: %s', e) return False, error
def put_service_endpoint(self, ipAddress, portNumber, profileName): error_data = {} if not self.is_traffic_profile_defined(profileName): logger.warning(err.CannotFindElement("put_service_endpoint", \ "The traffic profile " + profileName + " is not defined, ignoring this request")) self.delete_service_endpoint(ipAddress, portNumber) error, error_data = self.post_service_endpoint(ipAddress, portNumber, profileName) return error, error_data
def delete_mapping(self, profile_name): self.check_config() self.test_connection() if profile_name in self.app_route_traffic_profiles.keys(): #Delete endpoints from the policy policy_name = self.app_route_traffic_profiles[profile_name]['policyName'] policy_defined, _ = self.is_policy_defined(policy_name, 'AppRoute') if policy_defined: self.empty_approute_policy(policy_name,'delete_mapping') # Regenerate merge policy self.create_approute_policy_with_all_endpoints() # Delete associated endpoints from internal variable self.delete_service_endpoint_by_profile(profile_name) # Delete traffic profile from internal variable del self.app_route_traffic_profiles[profile_name] elif profile_name in self.data_traffic_profiles.keys(): #Delete endpoints from the policy policy_name = self.data_traffic_profiles[profile_name]['policyName'] policy_defined, _ = self.is_policy_defined(policy_name, 'Data') if policy_defined: self.empty_data_policy(policy_name, 'delete_traffic_profile') #No active policies affected because these profiles are never active # Regenerate merge policy self.create_data_policy_with_all_endpoints() # Delete associated endpoints from internal variable self.delete_service_endpoint_by_profile(profile_name) # Delete traffic profile from internal variable del self.data_traffic_profiles[profile_name] else: logger.warning(err.CannotFindElement("delete_traffic_profile",\ "This traffic profile does not exist, ignoring request."))
def put_mapping(self, profile_name, data): self.check_config() self.test_connection() if not self.is_traffic_profile_defined(profile_name): logger.warning(err.CannotFindElement("put_traffic_profile", \ "This traffic profile does not exist, ignoring request.")) elif not self.is_policy_defined(data): logger.warning(err.CannotFindElement("put_traffic_profile", \ "The policy" + str(data['policyName']) +" does not exist in the sdwan controller, ignoring request.")) else: #List affected endpoints endpoints = self.get_service_endpoints_by_profile(profile_name) if profile_name in self.app_route_traffic_profiles.keys(): #Empty old policy policy_name = self.app_route_traffic_profiles[profile_name][ 'policyName'] self.empty_approute_policy(policy_name, 'put_traffic_profile') if data['policyType'] == 'AppRoute': # AppRoute to AppRoute #Add enpoints to new policy policy_id = self.add_endpoint_array_approute_policy( data['policyName'], endpoints, 'put_traffic_profile') #Update internal var old_policy = [ self.app_route_traffic_profiles[profile_name] ['policyName'] ] self.app_route_traffic_profiles[profile_name][ 'policyName'] = data['policyName'] # Regenerate merge policy self.create_approute_policy_with_all_endpoints(old_policy) else: # AppRoute to Data #Add endpoints to new policy policy_id = self.add_endpoint_array_data_policy( data['policyName'], endpoints, 'put_traffic_profile') #Change type of profile old_policy = [ self.app_route_traffic_profiles[profile_name] ['policyName'] ] self.data_traffic_profiles[profile_name] = { 'policyName': data['policyName'] } del self.app_route_traffic_profiles[profile_name] # Regenerate merge policies self.create_data_policy_with_all_endpoints() self.create_approute_policy_with_all_endpoints(old_policy) elif profile_name in self.data_traffic_profiles.keys(): #Empty old policy policy_name = self.data_traffic_profiles[profile_name][ 'policyName'] self.empty_data_policy(policy_name, 'put_traffic_profile') if data['policyType'] == 'Data': # Data to Data #Add endpoints to new policy policy_id = self.add_endpoint_array_data_policy( data['policyName'], endpoints, 'put_traffic_profile') # Update internal var old_policy = [ self.data_traffic_profiles[profile_name]['policyName'] ] self.data_traffic_profiles[profile_name][ 'policyName'] = data['policyName'] # Regenerate merge policy self.create_data_policy_with_all_endpoints(old_policy) else: # Data to AppRoute #Add endpoints to new policy policy_id = self.add_endpoint_array_approute_policy( data['policyName'], endpoints, 'put_traffic_profile') #Change type of profile old_policy = [ self.data_traffic_profiles[profile_name]['policyName'] ] self.app_route_traffic_profiles[profile_name] = { 'policyName': data['policyName'] } del self.data_traffic_profiles[profile_name] # Regenerate merge policies self.create_data_policy_with_all_endpoints(old_policy) self.create_approute_policy_with_all_endpoints() #Update internal endpoint variable for ep in endpoints: self.srv_endpoints[ep]['policyId'] = policy_id
def post_service_endpoint(self, ipAddress, portNumber, profileName): key = ipAddress + '_' + portNumber error = {} if key in self.srv_endpoints.keys(): msg = "Ignoring request: the endpoint" + key + " is already defined" logger.warning( err.ElementAlreadyDefined("post_service_endpoint", msg)) error['status'] = 400 error['title'] = 'ENDPOINT ALREADY DEFINED' error[ 'description'] = 'The endpoint IP: ' + ipAddress + ' and port ' + portNumber + ' is already defined. Ignoring this event.' return True, error if not self.is_traffic_profile_defined(profileName): logger.warning(err.CannotFindElement('post_service_endpoint', \ 'Traffic profile ' + profileName + ' is not defined, ignoring request.')) error['status'] = 400 error['title'] = 'CANNOT FIND TRAFFIC PROFILE' error[ 'description'] = 'The traffic profile ' + profileName + ' is not defined. Ignoring this event.' return True, error try: if profileName in self.app_route_traffic_profiles.keys(): policy_name = self.app_route_traffic_profiles[profileName][ 'policyName'] policy_id = self.api_endpoint.get_approute_policy_id_by_name( policy_name) if policy_id is not None: policy = self.api_endpoint.get_approute_policy_by_id( policy_id) payload = templates.add_approute_endpoint( policy, ipAddress, portNumber) response = self.api_endpoint.put_approute_policy( policy_id, payload, 'post_service_endpoint') self.srv_endpoints[key] = { 'trafficProfileName': profileName, 'policyId': policy_id } # Trigger update for centralized policies that are active # The masterTemplatesAffected array is empty if the policy is NOT active if len(response["masterTemplatesAffected"]) != 0: self.api_endpoint.update_active_policy( response["masterTemplatesAffected"], 'post_service_endpoint') else: logger.warning( 'Ignoring request: Cannot find a policy called %s, ', profileName) elif profileName in self.data_traffic_profiles.keys(): policy_name = self.data_traffic_profiles[profileName][ 'policyName'] policy_id = self.api_endpoint.get_data_policy_id_by_name( policy_name) if policy_id is not None: policy = self.api_endpoint.get_data_policy_by_id(policy_id) payload = templates.add_data_endpoint( policy, ipAddress, portNumber) response = self.api_endpoint.put_data_policy( policy_id, payload, 'post_service_endpoint') self.srv_endpoints[key] = { 'trafficProfileName': profileName, 'policyId': policy_id } # Trigger update for centralized policies that are active # The masterTemplatesAffected array is empty if the policy is NOT active if len(response["masterTemplatesAffected"]) != 0: self.api_endpoint.update_active_policy( response["masterTemplatesAffected"], 'post_service_endpoint') else: logger.warning( 'Ignoring request: Cannot find a policy called %s', profileName) except Exception as e: logger.error( 'An error ocurred while communicating with the SDWAN controller.' ) logger.error('Details: %s', e) return False, error
def put_mapping(self, profile_name, data): self.check_config() self.test_connection() profile_defined, profile_type = self.is_traffic_profile_defined(profile_name) # Verify new policiy is defined policy_defined, new_policy_id = self.is_policy_defined(data['policyName'], data['policyType']) #Verify policy NOT in use in other mappings policy_defined_in_mapping, mapping_name = self.is_policy_in_mappings(data['policyName'], data['policyType']) if not profile_defined: logger.warning(err.CannotFindElement("put_traffic_profile", \ "This traffic profile does not exist, ignoring request.")) elif not policy_defined: logger.warning(err.CannotFindElement("put_traffic_profile", \ "The policy " + str(data['policyName']) +" does not exist in the sdwan controller, ignoring request.")) elif policy_defined_in_mapping: msg = 'Ignoring request: the policy ' + data['policyName'] + ' is already defined in the mapping ' + mapping_name logger.warning(msg) raise err.DuplicatePolicy(msg) else: #List affected endpoints endpoints = self.get_service_endpoints_by_profile(profile_name) if profile_type == 'AppRoute': #Empty old policy policy_name = self.app_route_traffic_profiles[profile_name]['policyName'] self.empty_approute_policy(policy_name, 'put_traffic_profile') if data['policyType'] == 'AppRoute': # AppRoute to AppRoute #Add enpoints to new policy self.add_endpoint_array_approute_policy(new_policy_id, endpoints, 'put_traffic_profile') #Update internal var old_policy = [ self.app_route_traffic_profiles[profile_name]['policyName'] ] self.app_route_traffic_profiles[profile_name]['policyName'] = data['policyName'] # Regenerate merge policy self.create_approute_policy_with_all_endpoints(old_policy) else: # AppRoute to Data #Add endpoints to new policy self.add_endpoint_array_data_policy(new_policy_id, endpoints, 'put_traffic_profile') #Change type of profile old_policy = [ self.app_route_traffic_profiles[profile_name]['policyName'] ] self.data_traffic_profiles[profile_name] = { 'policyName' : data['policyName'] } del self.app_route_traffic_profiles[profile_name] # Regenerate merge policies self.create_data_policy_with_all_endpoints() self.create_approute_policy_with_all_endpoints(old_policy) elif profile_type == 'Data': #Empty old policy policy_name = self.data_traffic_profiles[profile_name]['policyName'] self.empty_data_policy(policy_name, 'put_traffic_profile') if data['policyType'] == 'Data': # Data to Data #Add endpoints to new policy self.add_endpoint_array_data_policy(new_policy_id, endpoints, 'put_traffic_profile') # Update internal var old_policy = [ self.data_traffic_profiles[profile_name]['policyName'] ] self.data_traffic_profiles[profile_name]['policyName'] = data['policyName'] # Regenerate merge policy self.create_data_policy_with_all_endpoints(old_policy) else: # Data to AppRoute #Add endpoints to new policy self.add_endpoint_array_approute_policy(new_policy_id, endpoints, 'put_traffic_profile') #Change type of profile old_policy = [ self.data_traffic_profiles[profile_name]['policyName'] ] self.app_route_traffic_profiles[profile_name] = { 'policyName': data['policyName'] } del self.data_traffic_profiles[profile_name] # Regenerate merge policies self.create_data_policy_with_all_endpoints(old_policy) self.create_approute_policy_with_all_endpoints() #Update internal endpoint variable for ep in endpoints: self.srv_endpoints[ep]['policyId'] = new_policy_id