[SEBA-450] (part 1)

Refactoring, python3 compat, and tox tests on:

- xosconfig
- xosgenx
- xosutil

Eliminate use of yaml.load() which is unsafe, switch to yaml.safe_load()

More diagnostics during database migration

Change-Id: I0fae5782fca401603a7c4e4ec2b9269ad24bda97
diff --git a/lib/xos-genx/xosgenx/xos2jinja.py b/lib/xos-genx/xosgenx/xos2jinja.py
index 471be93..e4ff7db 100644
--- a/lib/xos-genx/xosgenx/xos2jinja.py
+++ b/lib/xos-genx/xosgenx/xos2jinja.py
@@ -12,18 +12,14 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from __future__ import absolute_import, print_function
 
-from __future__ import print_function
+import copy
+import sys
+
 import plyxproto.model as m
 from plyxproto.helpers import Visitor
-import argparse
-import plyxproto.parser as plyxproto
-import traceback
-import sys
-import jinja2
-import os
-import copy
-import pdb
+from six.moves import map, range
 
 
 class MissingPolicyException(Exception):
@@ -175,7 +171,10 @@
         return True
 
     def visit_FieldType(self, obj):
-        """Field type, if type is name, then it may need refactoring consistent with refactoring rules according to the table"""
+        """
+        Field type, if type is name, then it may need refactoring consistent
+        with refactoring rules according to the table
+        """
         return True
 
     def visit_LinkDefinition(self, obj):
@@ -295,13 +294,12 @@
         stack_num = self.count_stack.pop()
         fields = []
         links = []
-        last_field = None
+        last_field = {}
         try:
-            obj.bases = map(dotname_to_fqn, obj.bases)
+            obj.bases = list(map(dotname_to_fqn, obj.bases))
         except AttributeError:
             pass
 
-        last_field = {}
         for i in range(0, stack_num):
             f = self.stack.pop()
             if f["_type"] == "link":
@@ -340,7 +338,7 @@
         self.models[model_name] = model_def
 
         # Set message options
-        for k, v in self.options.iteritems():
+        for k, v in sorted(self.options.items(), key=lambda t: t[0]):  # sorted by key
             try:
                 if k not in self.message_options:
                     self.message_options[k] = v
@@ -406,47 +404,47 @@
             "onetomany": "manytoone",
         }
 
-        for m in messages:
-            for l in m["links"]:
-                rlink = copy.deepcopy(l)
+        for msg in messages:
+            for lnk in msg["links"]:
+                rlink = copy.deepcopy(lnk)
 
                 rlink["_type"] = "rlink"  # An implicit link, not declared in the model
-                rlink["src_port"] = l["dst_port"]
-                rlink["dst_port"] = l["src_port"]
+                rlink["src_port"] = lnk["dst_port"]
+                rlink["dst_port"] = lnk["src_port"]
                 rlink["peer"] = {
-                    "name": m["name"],
-                    "package": m["package"],
-                    "fqn": m["fqn"],
+                    "name": msg["name"],
+                    "package": msg["package"],
+                    "fqn": msg["fqn"],
                 }
-                rlink["link_type"] = link_opposite[l["link_type"]]
-                rlink["reverse_id"] = l["reverse_id"]
+                rlink["link_type"] = link_opposite[lnk["link_type"]]
+                rlink["reverse_id"] = lnk["reverse_id"]
 
-                if (not l["reverse_id"]) and (self.args.verbosity >= 1):
+                if (not lnk["reverse_id"]) and (self.args.verbosity >= 1):
                     print(
                         "WARNING: Field %s in model %s has no reverse_id"
-                        % (l["src_port"], m["name"]),
+                        % (lnk["src_port"], msg["name"]),
                         file=sys.stderr,
                     )
 
-                if l["reverse_id"] and (
-                    (int(l["reverse_id"]) < 1000) or (int(l["reverse_id"]) >= 1900)
+                if lnk["reverse_id"] and (
+                    (int(lnk["reverse_id"]) < 1000) or (int(lnk["reverse_id"]) >= 1900)
                 ):
                     raise Exception(
                         "reverse id for field %s in model %s should be between 1000 and 1899"
-                        % (l["src_port"], m["name"])
+                        % (lnk["src_port"], msg["name"])
                     )
 
                 try:
                     try:
-                        rev_links[l["peer"]["fqn"]].append(rlink)
+                        rev_links[lnk["peer"]["fqn"]].append(rlink)
                     except TypeError:
                         pass
                 except KeyError:
-                    rev_links[l["peer"]["fqn"]] = [rlink]
+                    rev_links[lnk["peer"]["fqn"]] = [rlink]
 
-        for m in messages:
+        for msg in messages:
             try:
-                m["rlinks"] = rev_links[m["name"]]
-                message_dict[m["name"]]["rlinks"] = m["rlinks"]
+                msg["rlinks"] = rev_links[msg["name"]]
+                message_dict[msg["name"]]["rlinks"] = msg["rlinks"]
             except KeyError:
                 pass