7aaf328abe351c9c049f6368496156fdd351ed7a
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / SimpleBGPMessageFactory.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.parser.impl;
9
10 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
11 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
12 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
13 import org.opendaylight.protocol.concepts.HandlerRegistry;
14 import org.opendaylight.yangtools.yang.binding.DataContainer;
15 import org.opendaylight.yangtools.yang.binding.Notification;
16
17 public final class SimpleBGPMessageFactory extends AbstractMessageRegistry {
18         private static final class Holder {
19                 private static final SimpleBGPMessageFactory INSTANCE;
20
21                 static {
22                         final SimpleBGPMessageFactory f = new SimpleBGPMessageFactory();
23
24                         // Wrong order, but needed for circular dep.
25                         INSTANCE = f;
26                         try {
27                                 new ActivatorImpl().start(SingletonProviderContext.getInstance());
28                         } catch (Exception e) {
29                                 throw new ExceptionInInitializerError(e);
30                         }
31                 }
32         }
33
34         private final HandlerRegistry<DataContainer, MessageParser, MessageSerializer> handlers = new HandlerRegistry<>();
35
36         private SimpleBGPMessageFactory() {
37
38         }
39
40         public static SimpleBGPMessageFactory getInstance() {
41                 return Holder.INSTANCE;
42         }
43
44         @Override
45         protected Notification parseBody(final int type, final byte[] body, final int messageLength) throws BGPDocumentedException {
46                 final MessageParser parser = handlers.getParser(type);
47                 if (parser == null) {
48                         return null;
49                 }
50
51                 return parser.parseMessageBody(body, messageLength);
52         }
53
54         @Override
55         protected byte[] serializeMessageImpl(final Notification message) {
56                 final MessageSerializer serializer = handlers.getSerializer(message.getImplementedInterface());
57                 if (serializer == null) {
58                         return null;
59                 }
60
61                 return serializer.serializeMessage(message);
62         }
63
64         @Override
65         public AutoCloseable registerMessageParser(final int messageType, final MessageParser parser) {
66                 return handlers.registerParser(messageType, parser);
67         }
68
69         @Override
70         public AutoCloseable registerMessageSerializer(final Class<? extends Notification> messageClass, final MessageSerializer serializer) {
71                 return handlers.registerSerializer(messageClass, serializer);
72         }
73 }