opticalUtils.py modifications

o Topology.json matches what the network configuration system expects. New method
for converting opticalJSON added to add layer of indirection to (hopefully) make
changes easier in the future if syntax changes for any files.

o onos-topo-cfg takes a third argument for REST uri path. This is for backwards
compatibility, and can hopefully be removed when everything is ported away from
using the ConfigProvider.

Change-Id: I56a117f33194dd420ea4970cd612c980b5c020b9
diff --git a/tools/test/topos/opticalUtils.py b/tools/test/topos/opticalUtils.py
index 6fc3c1e..a1ae834 100644
--- a/tools/test/topos/opticalUtils.py
+++ b/tools/test/topos/opticalUtils.py
@@ -291,7 +291,7 @@
             json.dump(crossConnectJSON, fd, indent=4, separators=(',', ': '))
         info('*** Pushing crossConnect.json to ONOS\n')
         output = quietRun('%s/tools/test/bin/onos-topo-cfg %s\
-         Topology.json' % (self.onosDir, self.controllers[ 0 ].ip), shell=True)
+         Topology.json network/configuration/' % (self.onosDir, self.controllers[ 0 ].ip), shell=True)
 
     def stop_oe(self):
         '''
@@ -374,16 +374,17 @@
         LINCSwitch.opticalJSON[ 'links' ] = linkConfig
 
         info('*** Writing Topology.json file\n')
+        topoJSON = LINCSwitch.makeTopoJSON()
         with open('Topology.json', 'w') as outfile:
-            json.dump(LINCSwitch.opticalJSON, outfile, indent=4, separators=(',', ': '))
+            json.dump(topoJSON, outfile, indent=4, separators=(',', ': '))
 
         info('*** Converting Topology.json to linc-oe format (TopoConfig.json) file (no oecfg) \n')
         
-        topoConfigJson = {};
-        dpIdToName = {};
+        topoConfigJson = {}
+        dpIdToName = {}
 
-        topoConfigJson["switchConfig"] = LINCSwitch.getSwitchConfig(dpIdToName);
-        topoConfigJson["linkConfig"] = LINCSwitch.getLinkConfig(dpIdToName);
+        topoConfigJson["switchConfig"] = LINCSwitch.getSwitchConfig(dpIdToName)
+        topoConfigJson["linkConfig"] = LINCSwitch.getLinkConfig(dpIdToName)
 
         #Writing to TopoConfig.json
         with open( 'TopoConfig.json', 'w' ) as outfile:
@@ -470,7 +471,8 @@
 
         info('*** Pushing Topology.json to ONOS\n')
         for index in range(len(LINCSwitch.controllers)):
-            output = quietRun('%s/tools/test/bin/onos-topo-cfg %s Topology.json &' % (LINCSwitch.onosDir, LINCSwitch.controllers[ index ].ip), shell=True)
+            output = quietRun('%s/tools/test/bin/onos-topo-cfg %s Topology.json network/configuration/ &'\
+                               % (LINCSwitch.onosDir, LINCSwitch.controllers[ index ].ip), shell=True)
             # successful output contains the two characters '{}'
             # if there is more output than this, there is an issue
             if output.strip('{}'):
@@ -486,6 +488,48 @@
         return nodeDpid[0:-1];
 
     @staticmethod
+    def makeTopoJSON():
+        """
+        Builds ONOS network conifg system compatible dicts to be written as Topology.json file.
+        """
+        topology = {}
+        links = {}
+        devices = {}
+        ports = {}
+
+        for switch in LINCSwitch.opticalJSON[ 'devices' ]:
+            # build device entries - keyed on uri (DPID) and config key 'basic'
+            devDict = {}
+            devDict[ 'driver' ] = switch[ 'hw' ]
+            devDict[ 'mfr' ] = switch[ 'mfr' ]
+            devDict[ 'mac' ] = switch[ 'mac' ]
+            devDict[ 'type' ] = switch[ 'type' ]
+            devDict.update(switch[ 'annotations' ])
+
+            devSubj = switch[ 'uri' ]
+            devices[ devSubj ] = { 'basic': devDict }
+
+            # build port entries - keyed on "uri/port" and config key 'optical'
+            for port in switch[ 'ports' ]:
+                portSubj = devSubj + '/' + str(port[ 'port' ])
+                ports[ portSubj ] = { 'optical': port }
+
+        # build link entries - keyed on "uri/port-uri/port" and config key 'basic'
+        for link in LINCSwitch.opticalJSON[ 'links' ]:
+            linkDict = {}
+            linkDict[ 'type' ] = link[ 'type' ]
+            linkDict.update(link[ 'annotations' ])
+
+            linkSubj = link[ 'src' ] + '-' + link[ 'dst' ]
+            links[ linkSubj ] = { 'basic': linkDict }
+
+        topology[ 'links' ] = links
+        topology[ 'devices' ] = devices
+        topology[ 'ports' ] = ports
+
+        return topology
+
+    @staticmethod
     def getSwitchConfig (dpIdToName):
         switchConfig = [];
         #Iterate through all switches and convert the ROADM switches to linc-oe format