Fix module for linklocal_service_create

Related-Bug: PROD-26557 (PROD:26557)

Change-Id: I561a803af35cf92f83f9053d2e9ead89588d834c
diff --git a/_modules/contrail.py b/_modules/contrail.py
index f36c4ce..34b8db7 100644
--- a/_modules/contrail.py
+++ b/_modules/contrail.py
@@ -1254,16 +1254,19 @@
 
 def _get_vrouter_config(vnc_client, gvc_name=None):
     try:
+        vrouter_conf_objs = vnc_client._objects_list('global-vrouter-config', detail=True)
+        if len(vrouter_conf_objs) == 0:
+            return None
         if not gvc_name:
-            gvc_list = global_vrouter_config_list()
-            gvc_name = gvc_list.values()[0]['name']
+            return vrouter_conf_objs[0]
+        for gvc in vrouter_conf_objs:
+            if gvc.name == gvc_name:
+                return gvc
+    except Exception as e:
+       print("Error: {}".format(e))
+       pass
 
-        config = vnc_client.global_vrouter_config_read(
-            fq_name=['default-global-system-config', gvc_name])
-    except Exception:
-        config = None
-
-    return config
+    return None
 
 
 def linklocal_service_list(global_vrouter_config_name=None, **kwargs):
@@ -1334,6 +1337,11 @@
            'comment': ''}
     vnc_client = _auth(**kwargs)
     current_config = _get_vrouter_config(vnc_client, global_vrouter_config_name)
+    if current_config is None:
+        ret['result'] = False
+        ret['comment'] = "Global Vrouter Config doesn't exist and is required for LinkLocalSevices"
+        return ret
+
     service_entry = LinklocalServiceEntryType(
         linklocal_service_name=name,
         linklocal_service_ip=lls_ip,
@@ -1345,45 +1353,32 @@
         service_entry.ip_fabric_service_ip = ipf_dns_or_ip
         service_entry.ip_fabric_DNS_service_name = ''
 
-    if current_config is None:
-        new_services = LinklocalServicesTypes([service_entry])
-        new_config = GlobalVrouterConfig(linklocal_services=new_services)
-        if __opts__['test']:
-            ret['result'] = None
-            ret['comment'] = "Link local service " + name + " will be created"
-        else:
-            ret['comment'] = "Link local service " + name + " has been created"
-            ret['changes'] = {'LinkLocalSevice': {'old': '', 'new': name}}
-            vnc_client.global_vrouter_config_create(new_config)
+    _current_service_list = current_config.get_linklocal_services()
+    if _current_service_list is None:
+        service_list = {'linklocal_service_entry': []}
     else:
-        _current_service_list = current_config.get_linklocal_services()
-        if _current_service_list is None:
-            service_list = {'linklocal_service_entry': []}
-        else:
-            service_list = _current_service_list.__dict__
-        new_services = [service_entry]
-        for key, value in service_list.iteritems():
-            if key != 'linklocal_service_entry':
-                continue
-            for _entry in value:
-                entry = _entry.__dict__
-                if 'linklocal_service_name' in entry:
-                    if entry['linklocal_service_name'] == name:
-                        ret['comment'] = 'Link local service ' + name + ' already exists'
-                        return ret
-                    new_services.append(_entry)
-            if __opts__['test']:
-                ret['result'] = None
-                ret['comment'] = "LinkLocalSevices " + name + " will be created"
-            service_list[key] = new_services
-        new_config = GlobalVrouterConfig(linklocal_services=service_list)
-        if __opts__['test']:
-            ret['result'] = None
-            ret['comment'] = "LinkLocalSevices " + name + " will be updated"
-        else:
-            vnc_client.global_vrouter_config_update(new_config)
-            ret['comment'] = "LinkLocalSevices " + name + " has been created"
-            ret['changes'] = {'LinkLocalSevices': {'old': '', 'new': name}}
+        service_list = _current_service_list.__dict__
+    new_services = [service_entry]
+    value = service_list.get('linklocal_service_entry')
+    for _entry in value:
+        entry = _entry.__dict__
+        if 'linklocal_service_name' in entry:
+            if entry['linklocal_service_name'] == name:
+                ret['comment'] = 'Link local service ' + name + ' already exists'
+                return ret
+            new_services.append(_entry)
+    if __opts__['test']:
+        ret['result'] = None
+        ret['comment'] = "LinkLocalSevices " + name + " will be created"
+    service_list['linklocal_service_entry'] = new_services
+    new_config = GlobalVrouterConfig(linklocal_services=service_list)
+    if __opts__['test']:
+        ret['result'] = None
+        ret['comment'] = "LinkLocalSevices " + name + " will be updated"
+    else:
+        vnc_client.global_vrouter_config_update(new_config)
+        ret['comment'] = "LinkLocalSevices " + name + " has been created"
+        ret['changes'] = {'LinkLocalSevices': {'old': '', 'new': name}}
     return ret
 
 
@@ -1402,10 +1397,8 @@
            'result': True,
            'comment': ''}
     lls = linklocal_service_get(name)
-    print (lls)
     if name in lls:
         if __opts__['test']:
-            print " ------------ Test only  ------------"
             ret['result'] = None
             ret['comment'] = "Link local service " + name + " will be deleted"
             return ret
@@ -1433,7 +1426,10 @@
         new_config = GlobalVrouterConfig(linklocal_services=service_list)
         vnc_client.global_vrouter_config_update(new_config)
         ret['comment'] = "Link local service " + name + " will be deleted"
-        ret['changes'] = {'LinkLocalService': {'old': '', 'new': name}}
+        ret['changes'] = {'LinkLocalService': {'old': name, 'new': ''}}
+    else:
+        ret['result'] = False
+        ret['comment'] = "Global Vrouter Config doesn't exist and is required for LinkLocalSevices"
     return ret