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