From 827f952ce799f2051f60f0f8002d2e3908b2b5da Mon Sep 17 00:00:00 2001 From: Raimo Niskanen Date: Thu, 22 May 2014 10:06:51 +0200 Subject: Rewrite ordering functions for maintainability --- lib/snmp/src/agent/snmp_framework_mib.erl | 12 +++--------- lib/snmp/src/manager/snmpm_config.erl | 19 ++++--------------- lib/snmp/src/misc/snmp_conf.erl | 23 ++++++++++++++++++++++- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/lib/snmp/src/agent/snmp_framework_mib.erl b/lib/snmp/src/agent/snmp_framework_mib.erl index 6589ace949..43c91bbbc8 100644 --- a/lib/snmp/src/agent/snmp_framework_mib.erl +++ b/lib/snmp/src/agent/snmp_framework_mib.erl @@ -222,15 +222,9 @@ check_agent(X) -> error({invalid_agent_attribute, X}). %% Ordering function to sort intAgentTransportDomain first -%% hence before intAgentIpAddress -order_agent({Name, _}, {Name, _}) -> - true; %% Less than or equal -order_agent({_, _}, {intAgentTransportDomain, _}) -> - false; %% Greater than -order_agent({intAgentTransportDomain, _}, {_, _}) -> - true; %% Less than or equal -order_agent({A, _}, {B, _}) -> - A =< B. +%% hence before intAgentIpAddress. Sort other entries on the key. +order_agent(EntryA, EntryB) -> + snmp_conf:keyorder(1, EntryA, EntryB, [intAgentTransportDomain | sort]). diff --git a/lib/snmp/src/manager/snmpm_config.erl b/lib/snmp/src/manager/snmpm_config.erl index 69a6b4a3a9..36c7b914ac 100644 --- a/lib/snmp/src/manager/snmpm_config.erl +++ b/lib/snmp/src/manager/snmpm_config.erl @@ -1783,21 +1783,10 @@ init_agent_config({UserId, TargetName, Config}) -> -%% Sort tdomain first then port to ensure both comes before taddress -order_agent({Item1, _}, {Item2, _}) -> - if Item1 == Item2 -> - true; % Item1 == Item2 - Item2 =:= tdomain -> - false; % Item1 > tdomain - Item2 =:= port -> - if Item1 =:= tdomain -> - true; % tdomain < port - true -> - false % Item1 > port - end; - true -> - true % Item1 < Item 2 - end. +%% Sort 'tdomain' first then 'port' to ensure both +%% sorts before 'taddress'. Keep the order of other items. +order_agent(ItemA, ItemB) -> + snmp_conf:keyorder(1, ItemA, ItemB, [tdomain, port]). fix_agent_config(Conf) -> ?vdebug("fix_agent_config -> entry with~n~n" diff --git a/lib/snmp/src/misc/snmp_conf.erl b/lib/snmp/src/misc/snmp_conf.erl index eb71c55cf4..0c9c755bf4 100644 --- a/lib/snmp/src/misc/snmp_conf.erl +++ b/lib/snmp/src/misc/snmp_conf.erl @@ -25,7 +25,7 @@ %% External exports %% Avoid warning for local function error/1 clashing with autoimported BIF. -compile({no_auto_import,[error/1]}). --export([read_files/2, no_gen/2, no_order/2, no_filter/1]). +-export([read_files/2, no_gen/2, no_order/2, no_filter/1, keyorder/4]). -export([read/2, read/3]). %% Basic (type) check functions @@ -158,6 +158,27 @@ no_gen(_Dir, _R) -> []. no_order(_, _) -> true. no_filter(X) -> X. +%% Order tuples on element N with Keys first in appearence order. +%% +%% An ordering function (A, B) shall return true iff +%% A is less than or equal to B i.e shall return +%% false iff A is to be ordered after B. +keyorder(N, A, B, _) when element(N, A) == element(N, B) -> + true; +keyorder(N, A, B, [Key | _]) + when tuple_size(A) >= 1, element(N, B) == Key -> + false; +keyorder(N, A, B, [Key | _]) + when element(N, A) == Key, tuple_size(B) >= 1 -> + true; +keyorder(N, A, B, [_ | Keys]) -> + keyorder(N, A, B, Keys); +keyorder(_, A, B, []) when tuple_size(A) >= 1, tuple_size(B) >= 1 -> + %% Do not order other keys + true; +keyorder(N, A, B, sort) -> + %% Order other keys according to standard sort order + element(N, A) =< element(N, B). read(File, Verify) -> -- cgit v1.2.3