Juniper SDN contrail: used VNI stored in a set in the class 32/8832/1
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 29 Apr 2020 07:29:08 +0000 (07:29 +0000)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 29 Apr 2020 07:29:08 +0000 (07:29 +0000)
Change-Id: I562d297614055965bef066697566161dc97e8a8e
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
RO-SDN-juniper_contrail/osm_rosdn_juniper_contrail/sdn_assist_juniper_contrail.py

index e29ac79..32b80dd 100644 (file)
@@ -110,8 +110,9 @@ class JuniperContrail(SdnConnectorBase):
             self.domain = 'default'
             self.logger.info("No domain was provided. Using 'default'")
         if not self.vni_range:
-            self.vni_range = '1000001-2000000'
-            self.logger.info("No vni_range was provided. Using '1000001-2000000'")
+            self.vni_range = ['1000001-2000000']
+            self.logger.info("No vni_range was provided. Using ['1000001-2000000']")
+        self.used_vni = set()
 
         if overlay_url:
             if not overlay_url.startswith("http"):
@@ -156,6 +157,28 @@ class JuniperContrail(SdnConnectorBase):
         self.logger.info("Juniper Contrail Connector Initialized.")
 
 
+    def _generate_vni(self):
+        """
+         Method to get unused VxLAN Network Identifier (VNI)
+            Args:
+                None
+            Returns:
+                VNI
+        """
+        #find unused VLAN ID
+        for vlanID_range in self.vni_range:
+            try:
+                start_vni , end_vni = map(int, vlanID_range.replace(" ", "").split("-"))
+                for vni in range(start_vni, end_vni + 1):
+                    if vni not in self.used_vni:
+                        return vni
+            except Exception as exp:
+                raise SdnConnectorError("Exception {} occurred while searching a free VNI.".format(exp))
+        else:
+            raise SdnConnectorError("Unable to create the virtual network."\
+                " All VNI in VNI range {} are in use.".format(self.vni_range))
+
+
     def _get_token(self):
         self.logger.debug('Current Token:'.format(str(self.token)))
         auth_url = self.auth_url + 'auth/tokens'
@@ -605,6 +628,17 @@ if __name__ == '__main__':
     juniper_contrail = JuniperContrail(wim=wim, wim_account=wim_account, config=config, logger=logger)
 
     # Tests
+    # Generate VNI
+    for i in range(5):
+        vni = juniper_contrail._generate_vni()
+        juniper_contrail.used_vni.add(vni)
+    print(juniper_contrail.used_vni)
+    juniper_contrail.used_vni.remove(1000003)
+    print(juniper_contrail.used_vni)
+    for i in range(2):
+        vni = juniper_contrail._generate_vni()
+        juniper_contrail.used_vni.add(vni)
+    print(juniper_contrail.used_vni)
     # 0. Check credentials
     print('0. Check credentials')
     juniper_contrail.check_credentials()