BUG-113: Factor MessageRegistry out of BGPMessageFactory
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / BGPMessageFactoryImpl.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 java.util.List;
11
12 import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
13 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
14 import org.opendaylight.protocol.bgp.parser.spi.ProviderContext;
15 import org.opendaylight.protocol.framework.DeserializerException;
16 import org.opendaylight.protocol.framework.DocumentedException;
17 import org.opendaylight.yangtools.yang.binding.Notification;
18
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.Lists;
21
22 public final class BGPMessageFactoryImpl implements BGPMessageFactory {
23         private static final class Holder {
24                 private static final BGPMessageFactoryImpl INSTANCE;
25
26                 static {
27                         final ProviderContext pc = SingletonProviderContext.getInstance();
28
29                         new ActivatorImpl().start(pc);
30
31                         INSTANCE = new BGPMessageFactoryImpl(pc.getMessageRegistry());
32                 }
33         }
34
35         private final MessageRegistry registry;
36
37         private BGPMessageFactoryImpl(final MessageRegistry registry) {
38                 this.registry = Preconditions.checkNotNull(registry);
39         }
40
41         public static BGPMessageFactoryImpl getInstance() {
42                 return Holder.INSTANCE;
43         }
44
45         /*
46          * (non-Javadoc)
47          * @see org.opendaylight.protocol.bgp.parser.BGPMessageParser#parse(byte[])
48          */
49         @Override
50         public final List<Notification> parse(final byte[] bytes) throws DeserializerException, DocumentedException {
51                 return Lists.newArrayList(registry.parseMessage(bytes));
52         }
53
54         @Override
55         public final byte[] put(final Notification msg) {
56                 return registry.serializeMessage(msg);
57         }
58 }