From eaec86d86cb58550d1a4426c00f508f93465e548 Mon Sep 17 00:00:00 2001
From: Gregor Maier <gregor.maier@bigswitch.com>
Date: Wed, 27 Mar 2013 22:45:07 -0700
Subject: [PATCH] Make BasicFactory a singleton

---
 .../core/internal/Controller.java                   |  4 ++--
 .../core/internal/OFMessageDecoder.java             |  2 +-
 .../org/openflow/protocol/factory/BasicFactory.java | 10 ++++++++--
 .../core/internal/ControllerTest.java               |  8 ++++----
 .../core/internal/RoleChangerTest.java              |  2 +-
 .../core/test/MockFloodlightProvider.java           |  2 +-
 .../core/test/PacketFactory.java                    |  2 +-
 .../internal/LinkDiscoveryManagerTest.java          |  3 ++-
 .../util/OFMessageDamperTest.java                   |  2 +-
 .../org/openflow/protocol/BasicFactoryTest.java     | 10 +++++-----
 .../java/org/openflow/protocol/OFErrorTest.java     |  8 ++++----
 .../openflow/protocol/OFStatisticsReplyTest.java    |  2 +-
 .../openflow/protocol/OFStatisticsRequestTest.java  |  4 ++--
 .../java/org/openflow/protocol/OFVendorTest.java    | 13 ++++++++++++-
 src/test/java/org/openflow/util/OFTestCase.java     |  2 +-
 15 files changed, 46 insertions(+), 28 deletions(-)

diff --git a/src/main/java/net/floodlightcontroller/core/internal/Controller.java b/src/main/java/net/floodlightcontroller/core/internal/Controller.java
index 607835b71..2637f054a 100644
--- a/src/main/java/net/floodlightcontroller/core/internal/Controller.java
+++ b/src/main/java/net/floodlightcontroller/core/internal/Controller.java
@@ -138,7 +138,7 @@ public class Controller implements IFloodlightProviderService,
 
     protected static Logger log = LoggerFactory.getLogger(Controller.class);
 
-    private static final String ERROR_DATABASE =
+    static final String ERROR_DATABASE =
             "The controller could not communicate with the system database.";
 
     protected BasicFactory factory;
@@ -1786,7 +1786,7 @@ public class Controller implements IFloodlightProviderService,
         this.connectedSwitches = new HashSet<IOFSwitch>();
         this.controllerNodeIPsCache = new HashMap<String, String>();
         this.updates = new LinkedBlockingQueue<IUpdate>();
-        this.factory = new BasicFactory();
+        this.factory = BasicFactory.getInstance();
         this.providerMap = new HashMap<String, List<IInfoProvider>>();
         setConfigParams(configParams);
         this.role = getInitialRole(configParams);
diff --git a/src/main/java/net/floodlightcontroller/core/internal/OFMessageDecoder.java b/src/main/java/net/floodlightcontroller/core/internal/OFMessageDecoder.java
index 295e9679d..25edf396c 100644
--- a/src/main/java/net/floodlightcontroller/core/internal/OFMessageDecoder.java
+++ b/src/main/java/net/floodlightcontroller/core/internal/OFMessageDecoder.java
@@ -34,7 +34,7 @@ import org.openflow.protocol.factory.OFMessageFactory;
  */
 public class OFMessageDecoder extends FrameDecoder {
 
-    OFMessageFactory factory = new BasicFactory();
+    OFMessageFactory factory = BasicFactory.getInstance();
     
     @Override
     protected Object decode(ChannelHandlerContext ctx, Channel channel,
diff --git a/src/main/java/org/openflow/protocol/factory/BasicFactory.java b/src/main/java/org/openflow/protocol/factory/BasicFactory.java
index c4d148337..f484a167f 100644
--- a/src/main/java/org/openflow/protocol/factory/BasicFactory.java
+++ b/src/main/java/org/openflow/protocol/factory/BasicFactory.java
@@ -43,15 +43,21 @@ import org.openflow.protocol.vendor.OFVendorId;
  * @author Rob Sherwood (rob.sherwood@stanford.edu)
  *
  */
-public class BasicFactory implements OFMessageFactory, OFActionFactory,
+public enum BasicFactory implements OFMessageFactory, OFActionFactory,
         OFStatisticsFactory, OFVendorDataFactory {
+    SINGLETON_INSTANCE;
+
 
     private final OFVendorActionRegistry vendorActionRegistry;
 
-    public BasicFactory() {
+    private BasicFactory() {
         vendorActionRegistry = OFVendorActionRegistry.getInstance();
     }
 
+    public static BasicFactory getInstance() {
+        return SINGLETON_INSTANCE;
+    }
+
     /**
      * create and return a new instance of a message for OFType t. Also injects
      * factories for those message types that implement the *FactoryAware
diff --git a/src/test/java/net/floodlightcontroller/core/internal/ControllerTest.java b/src/test/java/net/floodlightcontroller/core/internal/ControllerTest.java
index fd373279e..76d3267a7 100644
--- a/src/test/java/net/floodlightcontroller/core/internal/ControllerTest.java
+++ b/src/test/java/net/floodlightcontroller/core/internal/ControllerTest.java
@@ -215,7 +215,7 @@ public class ControllerTest extends FloodlightTestCase
         byte[] testPacketSerialized = testPacket.serialize();
 
         // Build the PacketIn
-        OFPacketIn pi = ((OFPacketIn) new BasicFactory().getMessage(OFType.PACKET_IN))
+        OFPacketIn pi = ((OFPacketIn) BasicFactory.getInstance().getMessage(OFType.PACKET_IN))
                 .setBufferId(-1)
                 .setInPort((short) 1)
                 .setPacketData(testPacketSerialized)
@@ -423,7 +423,7 @@ public class ControllerTest extends FloodlightTestCase
         byte[] testPacketSerialized = testPacket.serialize();
 
         // Build the PacketIn
-        OFPacketIn pi = ((OFPacketIn) new BasicFactory().getMessage(OFType.PACKET_IN))
+        OFPacketIn pi = ((OFPacketIn) BasicFactory.getInstance().getMessage(OFType.PACKET_IN))
                 .setBufferId(-1)
                 .setInPort((short) 1)
                 .setPacketData(testPacketSerialized)
@@ -1456,7 +1456,7 @@ public class ControllerTest extends FloodlightTestCase
         byte[] testPacketSerialized = testPacket.serialize();
 
         // Build the PacketIn
-        OFPacketIn pi = ((OFPacketIn) new BasicFactory().getMessage(OFType.PACKET_IN))
+        OFPacketIn pi = ((OFPacketIn) BasicFactory.getInstance().getMessage(OFType.PACKET_IN))
                 .setBufferId(-1)
                 .setInPort((short) 1)
                 .setPacketData(testPacketSerialized)
@@ -1651,7 +1651,7 @@ public class ControllerTest extends FloodlightTestCase
                 byte[] testPacketSerialized = testPacket.serialize();
 
                 // Build the PacketIn
-                pi = ((OFPacketIn) new BasicFactory().getMessage(OFType.PACKET_IN))
+                pi = ((OFPacketIn) BasicFactory.getInstance().getMessage(OFType.PACKET_IN))
                         .setBufferId(-1)
                         .setInPort((short) 1)
                         .setPacketData(testPacketSerialized)
diff --git a/src/test/java/net/floodlightcontroller/core/internal/RoleChangerTest.java b/src/test/java/net/floodlightcontroller/core/internal/RoleChangerTest.java
index 40d1f7b59..7148b472f 100644
--- a/src/test/java/net/floodlightcontroller/core/internal/RoleChangerTest.java
+++ b/src/test/java/net/floodlightcontroller/core/internal/RoleChangerTest.java
@@ -62,7 +62,7 @@ public class RoleChangerTest {
     public void setUp() throws Exception {
         controller = createMock(Controller.class);
         roleChanger = new RoleChanger(controller);
-        BasicFactory factory = new BasicFactory();
+        BasicFactory factory = BasicFactory.getInstance();
         expect(controller.getOFMessageFactory()).andReturn(factory).anyTimes();
     }
     
diff --git a/src/test/java/net/floodlightcontroller/core/test/MockFloodlightProvider.java b/src/test/java/net/floodlightcontroller/core/test/MockFloodlightProvider.java
index 7b5cd6293..78139acfb 100644
--- a/src/test/java/net/floodlightcontroller/core/test/MockFloodlightProvider.java
+++ b/src/test/java/net/floodlightcontroller/core/test/MockFloodlightProvider.java
@@ -77,7 +77,7 @@ public class MockFloodlightProvider implements IFloodlightModule, IFloodlightPro
         switches = new ConcurrentHashMap<Long, IOFSwitch>();
         switchListeners = new CopyOnWriteArrayList<IOFSwitchListener>();
         haListeners = new CopyOnWriteArrayList<IHAListener>();
-        factory = new BasicFactory();
+        factory = BasicFactory.getInstance();
     }
 
     @Override
diff --git a/src/test/java/net/floodlightcontroller/core/test/PacketFactory.java b/src/test/java/net/floodlightcontroller/core/test/PacketFactory.java
index 99b1ecf25..07f8c3dd5 100644
--- a/src/test/java/net/floodlightcontroller/core/test/PacketFactory.java
+++ b/src/test/java/net/floodlightcontroller/core/test/PacketFactory.java
@@ -41,7 +41,7 @@ import org.openflow.protocol.factory.BasicFactory;
 public class PacketFactory {
     public static String broadcastMac = "ff:ff:ff:ff:ff:ff";
     public static String broadcastIp = "255.255.255.255";
-    protected static BasicFactory OFMessageFactory = new BasicFactory();
+    protected static BasicFactory OFMessageFactory = BasicFactory.getInstance();
     
     /**
      * Generates a DHCP request OFPacketIn.
diff --git a/src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkDiscoveryManagerTest.java b/src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkDiscoveryManagerTest.java
index b9397b603..566493a70 100644
--- a/src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkDiscoveryManagerTest.java
+++ b/src/test/java/net/floodlightcontroller/linkdiscovery/internal/LinkDiscoveryManagerTest.java
@@ -111,6 +111,7 @@ public class LinkDiscoveryManagerTest extends FloodlightTestCase {
         return mockSwitch;
     }
 
+    @Override
     @Before
     public void setUp() throws Exception {
         super.setUp();
@@ -525,7 +526,7 @@ public class LinkDiscoveryManagerTest extends FloodlightTestCase {
         byte[] testPacketSerialized = testPacket.serialize();
         OFPacketIn pi;
         // build out input packet
-        pi = ((OFPacketIn) new BasicFactory().getMessage(OFType.PACKET_IN))
+        pi = ((OFPacketIn) BasicFactory.getInstance().getMessage(OFType.PACKET_IN))
                 .setBufferId(-1)
                 .setInPort((short) 1)
                 .setPacketData(testPacketSerialized)
diff --git a/src/test/java/net/floodlightcontroller/util/OFMessageDamperTest.java b/src/test/java/net/floodlightcontroller/util/OFMessageDamperTest.java
index 27c6c4444..84db817f7 100644
--- a/src/test/java/net/floodlightcontroller/util/OFMessageDamperTest.java
+++ b/src/test/java/net/floodlightcontroller/util/OFMessageDamperTest.java
@@ -50,7 +50,7 @@ public class OFMessageDamperTest {
     
     @Before
     public void setUp() throws IOException {
-        factory = new BasicFactory();
+        factory = BasicFactory.getInstance();
         cntx = new FloodlightContext();
         
         sw1 = new OFMessageDamperMockSwitch();
diff --git a/src/test/java/org/openflow/protocol/BasicFactoryTest.java b/src/test/java/org/openflow/protocol/BasicFactoryTest.java
index 312fcd3f5..350d4be39 100644
--- a/src/test/java/org/openflow/protocol/BasicFactoryTest.java
+++ b/src/test/java/org/openflow/protocol/BasicFactoryTest.java
@@ -37,7 +37,7 @@ import org.openflow.util.U16;
 public class BasicFactoryTest extends TestCase {
 
     public void testCreateAndParse() throws MessageParseException {
-        BasicFactory factory = new BasicFactory();
+        BasicFactory factory = BasicFactory.getInstance();
         OFMessage m = factory.getMessage(OFType.HELLO);
         m.setVersion((byte) 1);
         m.setType(OFType.ECHO_REQUEST);
@@ -56,7 +56,7 @@ public class BasicFactoryTest extends TestCase {
     }
 
     public void testInvalidMsgParse() throws MessageParseException {
-        BasicFactory factory = new BasicFactory();
+        BasicFactory factory = BasicFactory.getInstance();
         OFMessage m = factory.getMessage(OFType.HELLO);
         m.setVersion((byte) 1);
         m.setType(OFType.ECHO_REQUEST);
@@ -69,7 +69,7 @@ public class BasicFactoryTest extends TestCase {
     }
 
     public void testCurrouptedMsgParse() throws MessageParseException {
-        BasicFactory factory = new BasicFactory();
+        BasicFactory factory = BasicFactory.getInstance();
         OFMessage m = factory.getMessage(OFType.HELLO);
         m.setVersion((byte) 1);
         m.setType(OFType.ERROR);
@@ -86,7 +86,7 @@ public class BasicFactoryTest extends TestCase {
     }
 
     public void testCustomVendorAction() throws MessageParseException {
-        BasicFactory factory = new BasicFactory();
+        BasicFactory factory = BasicFactory.getInstance();
         OFVendorActionRegistry.getInstance().register(
                 MockVendorAction.VENDOR_ID, new MockVendorActionFactory());
 
@@ -119,7 +119,7 @@ public class BasicFactoryTest extends TestCase {
                 0x05, 0x06, 0x07, 0x08               // pad
             };
 
-        BasicFactory factory = new BasicFactory();
+        BasicFactory factory = BasicFactory.getInstance();
         OFVendorActionRegistry.getInstance().register(
                 MockVendorAction.VENDOR_ID, new MockVendorActionFactory());
 
diff --git a/src/test/java/org/openflow/protocol/OFErrorTest.java b/src/test/java/org/openflow/protocol/OFErrorTest.java
index 45d52576e..f4a572698 100644
--- a/src/test/java/org/openflow/protocol/OFErrorTest.java
+++ b/src/test/java/org/openflow/protocol/OFErrorTest.java
@@ -34,14 +34,14 @@ public class OFErrorTest extends OFTestCase {
     public void testWriteRead() throws Exception {
         OFError msg = (OFError) messageFactory.getMessage(OFType.ERROR);
         msg.setMessageFactory(messageFactory);
-        msg.setErrorType((short) OFErrorType.OFPET_HELLO_FAILED.getValue());
+        msg.setErrorType(OFErrorType.OFPET_HELLO_FAILED.getValue());
         msg.setErrorCode((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
                 .ordinal());
         ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
         bb.clear();
         msg.writeTo(bb);
         msg.readFrom(bb);
-        TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.getValue(),
+        TestCase.assertEquals(OFErrorType.OFPET_HELLO_FAILED.getValue(),
                 msg.getErrorType());
         TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
                 .ordinal(), msg.getErrorType());
@@ -51,7 +51,7 @@ public class OFErrorTest extends OFTestCase {
         bb.clear();
         msg.writeTo(bb);
         msg.readFrom(bb);
-        TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.getValue(),
+        TestCase.assertEquals(OFErrorType.OFPET_HELLO_FAILED.getValue(),
                 msg.getErrorType());
         TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
                 .ordinal(), msg.getErrorType());
@@ -74,7 +74,7 @@ public class OFErrorTest extends OFTestCase {
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                 0x00 };
-        OFMessageFactory factory = new BasicFactory();
+        OFMessageFactory factory = BasicFactory.getInstance();
         ChannelBuffer oferrorBuf = 
                 ChannelBuffers.wrappedBuffer(oferrorRaw);
         List<OFMessage> msg = factory.parseMessage(oferrorBuf);
diff --git a/src/test/java/org/openflow/protocol/OFStatisticsReplyTest.java b/src/test/java/org/openflow/protocol/OFStatisticsReplyTest.java
index 50bac8f71..1885cae7b 100644
--- a/src/test/java/org/openflow/protocol/OFStatisticsReplyTest.java
+++ b/src/test/java/org/openflow/protocol/OFStatisticsReplyTest.java
@@ -64,7 +64,7 @@ public class OFStatisticsReplyTest extends OFTestCase {
                 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xc4, 0x00, 0x00, 0x00,
                 0x08, 0x00, 0x02, 0x00, 0x00 };
 
-        OFMessageFactory factory = new BasicFactory();
+        OFMessageFactory factory = BasicFactory.getInstance();
         ChannelBuffer packetBuf = ChannelBuffers.wrappedBuffer(packet);
         List<OFMessage> msg = factory.parseMessage(packetBuf);
         TestCase.assertNotNull(msg);
diff --git a/src/test/java/org/openflow/protocol/OFStatisticsRequestTest.java b/src/test/java/org/openflow/protocol/OFStatisticsRequestTest.java
index 74af6f41d..fceb8955e 100644
--- a/src/test/java/org/openflow/protocol/OFStatisticsRequestTest.java
+++ b/src/test/java/org/openflow/protocol/OFStatisticsRequestTest.java
@@ -40,7 +40,7 @@ public class OFStatisticsRequestTest extends OFTestCase {
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                 (byte) 0xff, 0x00, (byte) 0xff, (byte) 0xff };
 
-        OFMessageFactory factory = new BasicFactory();
+        OFMessageFactory factory = BasicFactory.getInstance();
         ChannelBuffer packetBuf = ChannelBuffers.wrappedBuffer(packet);
         List<OFMessage> msg = factory.parseMessage(packetBuf);
         TestCase.assertNotNull(msg);
@@ -64,7 +64,7 @@ public class OFStatisticsRequestTest extends OFTestCase {
                 0x00, 0x00, 0x00, 0x00, (byte) 0xff, 0x00, 0x00, 0x00,
                 (byte) 0xff, (byte) 0xff, 0x4e, 0x20 };
 
-        OFMessageFactory factory = new BasicFactory();
+        OFMessageFactory factory = BasicFactory.getInstance();
         ChannelBuffer packetBuf = ChannelBuffers.wrappedBuffer(packet);
         List<OFMessage> msg = factory.parseMessage(packetBuf);
         TestCase.assertNotNull(msg);
diff --git a/src/test/java/org/openflow/protocol/OFVendorTest.java b/src/test/java/org/openflow/protocol/OFVendorTest.java
index b85a915a9..c0385f441 100644
--- a/src/test/java/org/openflow/protocol/OFVendorTest.java
+++ b/src/test/java/org/openflow/protocol/OFVendorTest.java
@@ -38,14 +38,17 @@ public class OFVendorTest extends OFTestCase {
     static class AcmeVendorData implements OFVendorData {
         protected int dataType;
         
+        @Override
         public int getLength() {
             return 4;
         }
         
+        @Override
         public void readFrom(ChannelBuffer data, int length) {
             dataType = data.readInt();
         }
         
+        @Override
         public void writeTo(ChannelBuffer data) {
             data.writeInt(dataType);
         }
@@ -74,16 +77,19 @@ public class OFVendorTest extends OFTestCase {
             return value;
         }
         
+        @Override
         public int getLength() {
             return 8;
         }
         
+        @Override
         public void readFrom(ChannelBuffer data, int length) {
             super.readFrom(data, length);
             flags = data.readShort();
             value = data.readShort();
 
         }
+        @Override
         public void writeTo(ChannelBuffer data) {
             super.writeTo(data);
             data.writeShort(flags);
@@ -92,6 +98,7 @@ public class OFVendorTest extends OFTestCase {
         
         public static Instantiable<OFVendorData> getInstantiable() {
             return new Instantiable<OFVendorData>() {
+                @Override
                 public OFVendorData instantiate() {
                     return new AcmeVendorData1();
                 }
@@ -122,16 +129,19 @@ public class OFVendorTest extends OFTestCase {
             return subtype;
         }
         
+        @Override
         public int getLength() {
             return 12;
         }
         
+        @Override
         public void readFrom(ChannelBuffer data, int length) {
             super.readFrom(data, length);
             type = data.readShort();
             subtype = data.readShort();
 
         }
+        @Override
         public void writeTo(ChannelBuffer data) {
             super.writeTo(data);
             data.writeShort(type);
@@ -140,6 +150,7 @@ public class OFVendorTest extends OFTestCase {
         
         public static Instantiable<OFVendorData> getInstantiable() {
             return new Instantiable<OFVendorData>() {
+                @Override
                 public OFVendorData instantiate() {
                     return new AcmeVendorData2();
                 }
@@ -160,7 +171,7 @@ public class OFVendorTest extends OFTestCase {
     
     private OFVendor makeVendorMessage(int vendor) {
         OFVendor msg = (OFVendor) messageFactory.getMessage(OFType.VENDOR);
-        msg.setVendorDataFactory(new BasicFactory());
+        msg.setVendorDataFactory(BasicFactory.getInstance());
         msg.setVendor(vendor);
         return msg;
     }
diff --git a/src/test/java/org/openflow/util/OFTestCase.java b/src/test/java/org/openflow/util/OFTestCase.java
index 5132c2c70..07bee2d58 100644
--- a/src/test/java/org/openflow/util/OFTestCase.java
+++ b/src/test/java/org/openflow/util/OFTestCase.java
@@ -28,7 +28,7 @@ public class OFTestCase extends TestCase {
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-        messageFactory = new BasicFactory();
+        messageFactory = BasicFactory.getInstance();
     }
 
     public void test() throws Exception {
-- 
GitLab