BUG-113: initialize registries lazily
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / SimpleBGPMessageFactory.java
index f9f8eb5ab4ef7af9100b7e8e866915fbdba08467..6a49e33eb3f0b2811445b0174aa44168cc6ed96f 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.protocol.bgp.parser.impl;
 
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
-import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
 import org.opendaylight.protocol.bgp.parser.impl.message.BGPKeepAliveMessageParser;
 import org.opendaylight.protocol.bgp.parser.impl.message.BGPNotificationMessageParser;
 import org.opendaylight.protocol.bgp.parser.impl.message.BGPOpenMessageParser;
@@ -23,22 +22,23 @@ import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.Notification;
 
 public final class SimpleBGPMessageFactory extends AbstractMessageRegistry {
+       private static final class Holder {
+               private static final SimpleBGPMessageFactory INSTANCE;
 
-       public static final BGPMessageFactory INSTANCE;
+               static {
+                       final SimpleBGPMessageFactory reg = new SimpleBGPMessageFactory();
 
-       static {
-               final SimpleBGPMessageFactory reg = new SimpleBGPMessageFactory();
+                       reg.registerMessageParser(BGPOpenMessageParser.TYPE, BGPOpenMessageParser.PARSER);
+                       reg.registerMessageSerializer(Open.class, BGPOpenMessageParser.SERIALIZER);
+                       reg.registerMessageParser(BGPUpdateMessageParser.TYPE, BGPUpdateMessageParser.PARSER);
+                       // Serialization of Update message is not supported
+                       reg.registerMessageParser(BGPNotificationMessageParser.TYPE, BGPNotificationMessageParser.PARSER);
+                       reg.registerMessageSerializer(Notify.class, BGPNotificationMessageParser.SERIALIZER);
+                       reg.registerMessageParser(BGPKeepAliveMessageParser.TYPE, BGPKeepAliveMessageParser.PARSER);
+                       reg.registerMessageSerializer(Keepalive.class, BGPKeepAliveMessageParser.SERIALIZER);
 
-               reg.registerMessageParser(BGPOpenMessageParser.TYPE, BGPOpenMessageParser.PARSER);
-               reg.registerMessageSerializer(Open.class, BGPOpenMessageParser.SERIALIZER);
-               reg.registerMessageParser(BGPUpdateMessageParser.TYPE, BGPUpdateMessageParser.PARSER);
-               // Serialization of Update message is not supported
-               reg.registerMessageParser(BGPNotificationMessageParser.TYPE, BGPNotificationMessageParser.PARSER);
-               reg.registerMessageSerializer(Notify.class, BGPNotificationMessageParser.SERIALIZER);
-               reg.registerMessageParser(BGPKeepAliveMessageParser.TYPE, BGPKeepAliveMessageParser.PARSER);
-               reg.registerMessageSerializer(Keepalive.class, BGPKeepAliveMessageParser.SERIALIZER);
-
-               INSTANCE = reg;
+                       INSTANCE = reg;
+               }
        }
 
        private final HandlerRegistry<DataContainer, MessageParser, MessageSerializer> handlers = new HandlerRegistry<>();
@@ -47,6 +47,10 @@ public final class SimpleBGPMessageFactory extends AbstractMessageRegistry {
 
        }
 
+       public static SimpleBGPMessageFactory getInstance() {
+               return Holder.INSTANCE;
+       }
+
        @Override
        protected Notification parseBody(final int type, final byte[] body, final int messageLength) throws BGPDocumentedException {
                final MessageParser parser = handlers.getParser(type);