Fix for issue with direct value in counters

It appears that counted values may contain direct values instead of
range names which can break node generation. This commit fixes
the problem and also makes code a little more PEP8 compatible.
Also it contains a fix for erroneous inlined paramter expansion.

PROD-30700

Change-Id: Ib6fddc4a8181df9813d98f28f776e1185e455535
diff --git a/reclass/nodegenerator.py b/reclass/nodegenerator.py
index ca69339..cf004cc 100644
--- a/reclass/nodegenerator.py
+++ b/reclass/nodegenerator.py
@@ -22,10 +22,12 @@
     def sort(self, *a, **k):
         pass
 
+
 class UnsortableDict(collections.OrderedDict):
     def items(self, *a, **k):
         return UnsortableList(collections.OrderedDict.items(self, *a, **k))
 
+
 yaml.add_representer(UnsortableDict,
                      yaml.representer.SafeRepresenter.represent_dict)
 # End hack
@@ -33,6 +35,7 @@
 
 
 _param_reference = re.compile('\$\{_param\:.*?\}')
+_param_reference_esc = re.compile('\'\$\{_param\:.*?\}\'')
 _refernce_name = re.compile('\$\{_param\:(.*?)\}')
 _iprange = re.compile('<<(.*?)>>')
 NODEF = 'NOTADEFINITION'
@@ -43,7 +46,9 @@
 
 def get_references(string):
     """Exctracts all raw references from a string."""
-    return re.findall(_param_reference, string)
+    if not re.findall(_param_reference_esc, string):
+        return re.findall(_param_reference, string)
+    return []
 
 
 def get_ref_names(string):
@@ -81,7 +86,8 @@
     elif isinstance(value, collections.Iterable):
         return any(has_subst(x) for x in value)
     elif isinstance(value, dict):
-        return any(has_subst(k) or has_subst(v) for k, v in six.iteritems(value))
+        return any(has_subst(k) or has_subst(v)
+                   for k, v in six.iteritems(value))
     return False
 
 
@@ -149,6 +155,8 @@
 
 
 external = {}
+
+
 def get_configs(base, cluster):
     configs_to_process, out = [], []
     cluster_def = os.path.join(base, cluster)
@@ -220,7 +228,8 @@
 
 def dump_to_files(content, dest):
     for res in content:
-        tt = ".".join([res.get('name', NODEF), res.get('domain',NODEF), 'yml'])
+        tt = ".".join([res.get('name', NODEF),
+                       res.get('domain', NODEF), 'yml'])
         if contains(tt, NODEF):
             continue
         systemdesc = UnsortableDict()
@@ -234,16 +243,16 @@
         if params:
             contents = {'classes': res['classes'],
                         'parameters': {
-                          '_param': params,
-                          'linux': {
-                             'system':  systemdesc
+                            '_param': params,
+                            'linux': {
+                                'system':  systemdesc
             }}}
         # NOTE: this should stay here until cfg01 definition stabilizes.
         else:
             contents = {'classes': res['classes'],
                         'parameters': {
-                          'linux': {
-                             'system':  systemdesc
+                            'linux': {
+                                'system':  systemdesc
             }}}
         # NOTE: the original formula contains hints to other possible sections.
         # Since it is not immediately clear whether those are used or just
@@ -252,7 +261,6 @@
             yaml.dump(contents, f, default_flow_style=False)
 
 
-
 # Takes an internally parametrized node desciption and generates appropriate
 # number of nodes.
 # A node consists of a regular node definition fields and additional 'repeat'
@@ -287,10 +295,14 @@
                     if type(iv) is dict:
                         # apparently key is always 'value'
                         for iiv in six.itervalues(iv):
+                            if type(iiv) != str:
+                                continue
                             ranges = re.findall(_iprange, iiv)
                             if ranges:
-                                addr = six.next(ip_ranges[ranges[0]])
-                                outv[ik] = str(addr.exploded)
+                                rname = ip_ranges[ranges[0]]
+                                addr = six.next(rname)
+                                outv[ik] = iiv.replace("<<%s>>" % rname,
+                                                       str(addr.exploded))
             out[k] = outv
         return out
 
@@ -314,7 +326,7 @@
         return 'repeat' in node.keys()
 
     with open(nodes_definition, 'r') as f:
-        nodes_init = yaml.safe_load(f)['parameters']['reclass']['storage']['node']
+        nodes_init = get_nodes(yaml.safe_load(f))
     basic_nodes = copy.deepcopy(basic_nodes)  # preserve state.
     basic_nodes_extended, extra_nodes = {}, []
 
@@ -322,7 +334,7 @@
         if needs_expansion(node):
             continue
         extension = nodes_init.get(nname, {}).get('classes', [])
-        to_extend = node.get('classes', [])#.extend(extension)
+        to_extend = node.get('classes', [])
         if to_extend != extension:
             to_extend.extend(extension)
         node.get('params', {}).update(nodes_init.get(nname, {}).