[CORD-2429] Moving ip generation logic to the core
Change-Id: If6dbcf6aadae669e25e289ef6c20b02c2f8e4c5c
diff --git a/xos/core/models/.gitignore b/xos/core/models/.gitignore
index 2c710d6..75a6259 100644
--- a/xos/core/models/.gitignore
+++ b/xos/core/models/.gitignore
@@ -1,5 +1,6 @@
*.py
Makefile
+!attic/*.py
!contenttype.py
!header.py
!network_header.py
diff --git a/xos/core/models/attic/address_top.py b/xos/core/models/attic/address_top.py
new file mode 100644
index 0000000..bf71b64
--- /dev/null
+++ b/xos/core/models/attic/address_top.py
@@ -0,0 +1,17 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import socket
+import struct
diff --git a/xos/core/models/attic/addresspool_model.py b/xos/core/models/attic/addresspool_model.py
index d128f98..2aad18c 100644
--- a/xos/core/models/attic/addresspool_model.py
+++ b/xos/core/models/attic/addresspool_model.py
@@ -13,6 +13,37 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+# imported in address_top.py
+# import socket
+# import struct
+
+def expand_cidr(self, cidr):
+ (network, bits) = cidr.split("/")
+ network = network.strip()
+ bits = int(bits.strip())
+
+ dest = []
+
+ netmask = (~(pow(2, 32 - bits) - 1) & 0xFFFFFFFF)
+
+ count = pow(2, 32 - bits)
+ for i in range(2, count - 1):
+ ip = struct.unpack("!L", socket.inet_aton(network))[0]
+ ip = ip & netmask | i
+ dest.append(socket.inet_ntoa(struct.pack("!L", ip)))
+
+ return (dest, bits)
+
+def __xos_save_base(self, *args, **kwds):
+ """
+ We need to convert subnets into lists of addresses before saving
+ """
+ if self.addresses and "/" in self.addresses:
+ original_addresses = self.addresses
+ (cidr_addrs, cidr_netbits) = self.expand_cidr(self.addresses)
+ self.addresses = " ".join(cidr_addrs)
+ if not self.cidr:
+ self.cidr = original_addresses
def get_address(self):
with transaction.atomic():