configurationengine/source/plugins/common/ConeLegacyRulePlugin/legacyruleplugin/evals/accesspoint_id_counter.py
changeset 3 e7e0ae78773e
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #
       
    16 '''
       
    17 Ruleml eval extension to count accesspoint id's
       
    18 '''
       
    19 
       
    20 import logging
       
    21 
       
    22 logger = logging.getLogger('cone.ruleplugin.evals.accesspoint_id_counter')
       
    23 
       
    24 def get_apindex_by_apname(aps, dns, apname):
       
    25     """
       
    26     Returns AccessPoint index by given AccessPoint name
       
    27     """
       
    28     cnt = _get_ApDnContainer_(aps, dns)
       
    29     return cnt.get_apindex_by_apname(apname)
       
    30 
       
    31 def get_apid_by_apname(aps, dns, apname, wlan_support=True):
       
    32     """
       
    33     Returns AccessPoint id by given AccessPoint name
       
    34     """
       
    35     cnt = _get_ApDnContainer_(aps, dns, wlan_support)
       
    36     return cnt.get_apid_by_apname(apname)
       
    37 
       
    38 def get_dnid_by_dnname(aps, dns, dnname, wlan_support=True):
       
    39     """
       
    40     Return DestinationNetwork id by given DestinationNetworks name
       
    41     """
       
    42     cnt = _get_ApDnContainer_(aps, dns, wlan_support)
       
    43     return cnt.get_dnid_by_dnname(dnname)
       
    44 
       
    45 def get_apid_by_dnname_and_apname(aps, dns, dnname, apname, wlan_support=True):
       
    46     """
       
    47     Returns AccessPoint id by given DestinationNetwork name and AccessPoint name.
       
    48     """
       
    49     cnt = _get_ApDnContainer_(aps, dns, wlan_support)
       
    50     return cnt.get_apid_by_dnname_and_apname(dnname, apname)
       
    51 
       
    52 def get_all_in_array(aps, dns, wlan_support=True):
       
    53     """
       
    54     Returns array containing all data:
       
    55         [DN name],[DN id], [IAPS names], [IAPS ids], [IAPS indexes] 
       
    56     """
       
    57     cnt = _get_ApDnContainer_(aps, dns, wlan_support)
       
    58     return cnt.get_all_in_array()
       
    59 
       
    60 def _get_ApDnContainer_(aps, dns, wlan_support=True):
       
    61     """
       
    62     Returns populated ApDnContainer
       
    63     """
       
    64     cnt = ApDnContainer()
       
    65     
       
    66     _read_dns_(dns, cnt)
       
    67     _read_aps_(aps, cnt)
       
    68     
       
    69     cnt._calc_dn_ids_()
       
    70     
       
    71     if wlan_support:
       
    72         cnt._calc_ap_ids_(2)
       
    73     else:
       
    74         cnt._calc_ap_ids_(1)
       
    75     
       
    76     cnt._calc_ap_indexes_(1)
       
    77     
       
    78     return cnt
       
    79 
       
    80 def _read_dns_(dns, cnt):
       
    81     """
       
    82     Reads DNs to internal objects to ApDnContainer.
       
    83     """
       
    84     
       
    85     dn_names = None
       
    86     dn_ids = None
       
    87     dn_iaps = [None]*10
       
    88     
       
    89     for dn in dns.DN:
       
    90         if dn.ref == 'Name':
       
    91             dn_names = dn.value
       
    92         if dn.ref == 'DNId':
       
    93             dn_ids = dn.value
       
    94         if dn.ref == 'IAP':
       
    95             dn_iaps[0] = dn.value
       
    96         if dn.ref == 'IAP2':
       
    97             dn_iaps[1] = dn.value
       
    98         if dn.ref == 'IAP3':
       
    99             dn_iaps[2] = dn.value
       
   100         if dn.ref == 'IAP4':
       
   101             dn_iaps[3] = dn.value
       
   102         if dn.ref == 'IAP5':
       
   103             dn_iaps[4] = dn.value
       
   104         if dn.ref == 'IAP6':
       
   105             dn_iaps[5] = dn.value
       
   106         if dn.ref == 'IAP7':
       
   107             dn_iaps[6] = dn.value
       
   108         if dn.ref == 'IAP8':
       
   109             dn_iaps[7] = dn.value
       
   110         if dn.ref == 'IAP9':
       
   111             dn_iaps[8] = dn.value
       
   112         if dn.ref == 'IAP10':
       
   113             dn_iaps[9] = dn.value
       
   114     
       
   115     logger.info('Parsed DN names: %s' % dn_names)
       
   116     logger.info('Parsed DN ids: %s' % dn_ids)
       
   117     logger.info('Parsed DN iaps: %s' % dn_iaps)
       
   118     
       
   119     for i in range(len(dn_names)):
       
   120         mydn = Dn()
       
   121         mydn.set_id(dn_ids[i])
       
   122         mydn.set_name(dn_names[i])
       
   123         myiaps = [None]*10
       
   124         for j in range(10):
       
   125             myiaps[j] = dn_iaps[j][i]
       
   126         mydn.set_iaps(myiaps)
       
   127         cnt.add_dn(mydn)
       
   128     return cnt
       
   129 
       
   130 def _read_aps_(aps, cnt):
       
   131     """
       
   132     Reads APs to internal objects to ApDnContainer.
       
   133     """
       
   134     ap_names = None
       
   135     ap_ids1 = None
       
   136     
       
   137     for ap in aps.AP:
       
   138         if ap.ref == 'ConnectionName':
       
   139             ap_names = ap.value
       
   140         if ap.ref == 'ConnectionId':
       
   141             ap_ids1 = ap.value
       
   142     
       
   143     ap_ids2 = [None]*len(ap_names)
       
   144     if ap_ids1 == None:
       
   145         ap_ids1 = []
       
   146     
       
   147     
       
   148     for i in range(len(ap_ids1)):
       
   149         ap_ids2[i] = ap_ids1[i]
       
   150         
       
   151     
       
   152     logger.info('Parsed AP names: %s' % ap_names)
       
   153     logger.info('Parsed AP ids: %s' % ap_ids2)
       
   154     
       
   155     for i in range(len(ap_names)):
       
   156         myap = Ap()
       
   157         myap.set_id(ap_ids2[i])
       
   158         myap.set_name(ap_names[i])
       
   159         cnt.add_ap(myap)
       
   160     return cnt
       
   161 
       
   162 def _get_next_free_id_(bases, start_index=1):
       
   163     """
       
   164     Returns next id as a string that is not in use.
       
   165     """
       
   166     
       
   167     biggest_id = int(start_index)
       
   168     
       
   169     for base in bases:
       
   170         current_id = base.get_id()
       
   171         if current_id != None or current_id != '':
       
   172             if current_id > biggest_id:
       
   173                 biggest_id = current_id
       
   174     
       
   175     return str(int(biggest_id) + 1)
       
   176 
       
   177 
       
   178 class ApDnContainer(object):
       
   179     """
       
   180     Container for AccessPoints and DestinationNetworks, that provides various access and search methods to them.
       
   181     """
       
   182     
       
   183     def __init__(self):
       
   184         self.dns = []
       
   185         self.aps = []
       
   186 
       
   187     def __str__(self):
       
   188         return "ApDnContainer(dns: " + str(self.dns) + ", aps:" + str(self.aps) + ")"
       
   189 
       
   190     def add_dn(self, dn):
       
   191         self.dns.append(dn)
       
   192     
       
   193     def add_ap(self, ap):
       
   194         self.aps.append(ap)
       
   195 
       
   196     def get_all_dns(self):
       
   197         return self.dns
       
   198     
       
   199     def get_all_aps(self):
       
   200         return self.aps
       
   201     
       
   202     def _calc_dn_ids_(self):
       
   203         for dn in self.dns:
       
   204             if dn.get_id() == None or dn.get_id() == '':
       
   205                 dn.set_id(_get_next_free_id_(self.dns, 1))
       
   206 
       
   207     def _calc_ap_indexes_(self, ind=1):
       
   208         index = ind
       
   209         
       
   210         for dn in self.dns:
       
   211             for iap in dn.get_iaps():
       
   212                 if iap != None:
       
   213                     for ap in self.aps:
       
   214                         if ap.get_name() == iap and ap.get_index() == '':
       
   215                             ap.set_index(str(index))
       
   216                             index += 1
       
   217 
       
   218     def _calc_ap_ids_(self, start_index=1):
       
   219         """
       
   220         Calculates unique index for every AccessPoint, if Easy_WLAN is given it always have index 1.
       
   221         """
       
   222         
       
   223         for ap in self.aps:
       
   224             if ap.name == 'Easy WLAN':
       
   225                 ap.set_id('1')
       
   226                 logger.info('Easy_WLAN AP found. Setting 1 to AP id.')
       
   227                 
       
   228         for ap in self.aps:
       
   229             if ap.get_id() == None or ap.get_id() == '':
       
   230                 ap.set_id(_get_next_free_id_(self.aps, int(start_index)))
       
   231     
       
   232     def get_apid_by_apname(self, apname):
       
   233         """
       
   234         Returns Accesspoint id by given AccessPoint name
       
   235         """
       
   236     
       
   237         for ap in self.aps:
       
   238             if ap.name == apname:
       
   239                 return ap.get_id()
       
   240         return None
       
   241     
       
   242     def get_apindex_by_apname(self, apname):
       
   243         """
       
   244         Returns Accesspoint index by given AccessPoint name
       
   245         """
       
   246         
       
   247         for ap in self.aps:
       
   248             if ap.get_name() == apname:
       
   249                 return ap.get_index()
       
   250         return None
       
   251     
       
   252     
       
   253     def get_dnid_by_dnname(self, dnname):
       
   254         """
       
   255         Return DestinationNetwork id by given DestinationNetworks name
       
   256         """
       
   257         for dn in self.dns:
       
   258             if dn.name == dnname:
       
   259                 return dn.id
       
   260         return None
       
   261     
       
   262     def get_apid_by_dnname_and_apname(self, dnname, apname):
       
   263         """
       
   264         Returns AccessPoint id by given DestinationNetwork name and AccessPoint name.
       
   265         """
       
   266         for dn in self.dns:
       
   267             if dn.name == dnname:
       
   268                 iaps = dn.get_iaps()
       
   269                 for iap in range(len(iaps)):
       
   270                     if iaps[iap] != None and iaps[iap] == apname:
       
   271                         return self.get_apid_by_apname(apname)
       
   272         return None
       
   273     
       
   274     def get_all_in_array(self):
       
   275         """
       
   276         Returns array containing all data:
       
   277             [DN name],[DN id], [IAPS names], [IAPS ids] [IAPS index]
       
   278         """
       
   279         ret = [None]*len(self.dns)
       
   280         
       
   281         for i in range(len(self.dns)):
       
   282             line = [None]*5
       
   283             line[0] = self.dns[i].get_name()
       
   284             line[1] = self.dns[i].get_id()
       
   285             line[2] = self.dns[i].get_iaps()
       
   286             
       
   287             ap_ids = [None]*10
       
   288             
       
   289             for j in range(10):
       
   290                 ap_ids[j] = self.get_apid_by_apname(self.dns[i].get_iaps()[j])
       
   291             
       
   292             line[3] = ap_ids
       
   293             
       
   294             ap_indexes = [None]*10
       
   295             
       
   296             for j in range(10):
       
   297                 ap_indexes[j] = self.get_apindex_by_apname(self.dns[i].get_iaps()[j])
       
   298             
       
   299             line[4] = ap_indexes
       
   300             
       
   301             ret[i] = line
       
   302             
       
   303         return ret
       
   304 
       
   305 class Base(object):
       
   306     """
       
   307     Base data classes for AP and DN classes.
       
   308     """
       
   309     def __init__(self):
       
   310         self.name = ''
       
   311         self.id = ''
       
   312 
       
   313     def set_name(self, name):
       
   314         self.name = name
       
   315     
       
   316     def get_name(self):
       
   317         return self.name
       
   318     
       
   319     def set_id(self, id):
       
   320         self.id = id
       
   321     
       
   322     def get_id(self):
       
   323         return self.id
       
   324 
       
   325 class Dn(Base):
       
   326     """
       
   327     Destination network
       
   328     """
       
   329     
       
   330     def __init__(self):
       
   331         self.name = None
       
   332         self.id = None
       
   333         self.iaps = [None]*10
       
   334     
       
   335     def __str__(self):
       
   336         return "Dn(name: " + self.name + ", id:" + self.id + ", iaps:" + str(self.iaps) + ")"
       
   337     
       
   338     def set_iaps(self, iaps):
       
   339         self.iaps = iaps
       
   340 
       
   341     def set_iap(self, index, value):
       
   342         self.iaps[index] = value
       
   343     
       
   344     def get_iap(self, index):
       
   345         return self.iaps[index]
       
   346     
       
   347     def get_iaps(self):
       
   348         return self.iaps
       
   349     
       
   350 class Ap(Base):
       
   351     
       
   352     def __init__(self):
       
   353         self.name = ''
       
   354         self.id = ''
       
   355         self.index = ''
       
   356     
       
   357     def __str__(self):
       
   358         return "Ap(name: " + self.name + ", id:" + self.id + ")"
       
   359     
       
   360     def set_index(self, index):
       
   361         self.index = index
       
   362     
       
   363     def get_index(self):
       
   364         return self.index
       
   365