Creating a dp_id from a string in openolt
Change-Id: I244ab778c01a8fbab92b21509680121dd0dcdaca
(cherry picked from commit 00041761161a9f87a3337af4ad0216541ef0ec47)
diff --git a/voltha/adapters/openolt/openolt.py b/voltha/adapters/openolt/openolt.py
index 5c8768c..995be78 100644
--- a/voltha/adapters/openolt/openolt.py
+++ b/voltha/adapters/openolt/openolt.py
@@ -117,6 +117,7 @@
self.devices[device.id] = OpenoltDevice(**kwargs)
except Exception as e:
log.error('Failed to adopt OpenOLT device', error=e)
+ # TODO set status to ERROR so that is clear something went wrong
del self.devices[device.id]
raise
else:
diff --git a/voltha/adapters/openolt/openolt_device.py b/voltha/adapters/openolt/openolt_device.py
index bdd7c74..1afac68 100644
--- a/voltha/adapters/openolt/openolt_device.py
+++ b/voltha/adapters/openolt/openolt_device.py
@@ -16,7 +16,8 @@
import threading
import binascii
import grpc
-
+import socket
+import re
import structlog
from twisted.internet import reactor
from scapy.layers.l2 import Ether, Dot1Q
@@ -102,8 +103,11 @@
ip=self.host_and_port)
self.proxy = registry('core').get_proxy('/')
+ self.log.info('openolt-device-init')
+
# Device already set in the event of reconciliation
if not is_reconciliation:
+ self.log.info('updating-device')
# It is a new device
# Update device
device.root = True
@@ -115,8 +119,15 @@
# If logical device does not exist create it
if not device.parent_id:
if dpid == None:
- dpid = '00:00:' + self.ip_hex(self.host_and_port.split(":")[0])
+ uri = self.host_and_port.split(":")[0]
+ try:
+ socket.inet_pton(socket.AF_INET, uri)
+ dpid = '00:00:' + self.ip_hex(uri)
+ except socket.error:
+ # this is not an IP
+ dpid = self.stringToMacAddr(uri)
+ self.log.info('creating-openolt-logical-device', dp_id=dpid)
# Create logical OF device
ld = LogicalDevice(
root_device_id=self.device_id,
@@ -137,6 +148,8 @@
ld_init = self.adapter_agent.create_logical_device(ld,
dpid=dpid)
self.logical_device_id = ld_init.id
+
+ self.log.info('created-openolt-logical-device', logical_device_id=ld_init.id)
else:
# logical device already exists
self.logical_device_id = device.parent_id
@@ -150,6 +163,20 @@
send_event=True, initial='state_null')
self.go_state_init()
+ def stringToMacAddr(self, uri):
+ regex = re.compile('[^a-zA-Z]')
+ uri = regex.sub('', uri)
+
+ l = len(uri)
+ if l > 6:
+ uri = uri[0:6]
+ else:
+ uri = uri + uri[0:6 - l]
+
+ print uri
+
+ return ":".join([hex(ord(x))[-2:] for x in uri])
+
def do_state_init(self, event):
# Initialize gRPC
self.channel = grpc.insecure_channel(self.host_and_port)