Bug 611 - BGP Update message serialization
[bgpcep.git] / bgp / parser-mock / src / main / java / org / opendaylight / protocol / bgp / parser / mock / BGPMessageParserMock.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.mock;
9
10 import io.netty.buffer.ByteBuf;
11
12 import java.util.Map;
13
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
16 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
17 import org.opendaylight.yangtools.yang.binding.Notification;
18
19 /**
20  * Mock implementation of {@link BGPMessageParser}. It implements the required interface by having two internal maps,
21  * each used in one of the methods. It looks up the key provided to the method and returns whatever value is stored in
22  * the map.
23  */
24 public class BGPMessageParserMock implements MessageRegistry {
25     private final Map<ByteBuf, Notification> messages;
26
27     /**
28      * @param updateMessages Map<byte[], BGPUpdateEvent>
29      */
30     public BGPMessageParserMock(final Map<ByteBuf, Notification> messages) {
31         this.messages = messages;
32     }
33
34     @Override
35     public Notification parseMessage(final ByteBuf buffer) throws BGPParsingException, BGPDocumentedException {
36         final Notification ret = this.messages.get(buffer);
37         if (ret == null) {
38             throw new IllegalArgumentException("Undefined message encountered");
39         }
40         return ret;
41     }
42
43     @Override
44     public void serializeMessage(final Notification msg, final ByteBuf buffer) {
45         // no action needed, it's a mock for parsing, not serializing
46         return;
47     }
48 }