summaryrefslogtreecommitdiffstats
path: root/lib/mapper/label.py
blob: 6baa3c01cea473e748db1b2676e1f444539b6aff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python

#
# Licence statement goes here
#

#from toscaparser.tosca_template import ToscaTemplate

#Current version:
#Parses policy rules, extracts targets, extracts policy properties
#Returns set of policy properties for each target in a dictionary object
#e.g., node_labels['VNF1'] = {label1, label2, ..., labeln}
def extract_labels(tpl):
  node_labels = dict() #stores labels for each node
  
  if tpl.has_key('topology_template'):
    if tpl['topology_template'].has_key('policies'):
      policies = tpl['topology_template']['policies']
    else:
      return node_labels
  else:
    return node_labels

  #extract label sets for each policy target
  for p in policies:
    for rule in p:
      targetlist = p[rule]['targets']
      for props in p[rule]['properties']:
        prop_list = p[rule]['properties'][props]
        for values in prop_list:
          labelkey = p[rule]['type']+ ':properties:' + props + ":" + values
          for target in targetlist:
            if node_labels.has_key(target):
              node_labels[target].update(set([labelkey]))
            else:
              node_labels[target] = set([labelkey])
  return node_labels

# Returns a map from nodes to regions based on label matching
def map_nodes(site_labels,node_labels):
  sitemap = dict() #stores mapping

  #for each target find a map of sites
  for node in node_labels:
    sitemap[node] = set()
    for site in site_labels:
      if node_labels[node].issubset(site_labels[site]):
        sitemap[node].add(site)

  return sitemap

# Selects sites for nodes if multiple candidates exist
# First iterate for nodes with single candidate site
# Rank sites with most nodes higher
def select_site( site_map ): 
  node_site = dict()
  counter = dict()
  #SHALL I CHECK IF ANY KEY HAS AN EMPTY SET TO THROW EXCEPTION?
  #For now, I assume input as safe

  for node in site_map:
    node_site[node] = [] 
    if len(site_map[node]) == 1:
      for site in site_map[node]:
        node_site[node] = site
        if counter.has_key(site):
          counter[site] = counter[site] + 1
        else:
          counter[site] = 1

  for node in site_map:
    if len(site_map[node]) > 1:
      maxval = 0
      maxkey = '-1'
      for site in site_map[node]:
        if counter.has_key(site) and counter[site] >= maxval:
          maxval = counter[site]
          maxkey = site
        elif counter.has_key(site) == False:
          counter[site] = 0
          if maxval == 0:
            maxval = 1
            maxkey = site
      node_site[node] = maxkey
      counter[node_site[node]] = counter[node_site[node]] + 1     
  return node_site