blob: 32f5c48f7a6e5d910c9b3fe79c8cf0df3c146997 [file] [log] [blame]
Madan Jampanic27b6b22016-02-05 11:36:31 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.bgpio.protocol;
18
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.onosproject.bgpio.exceptions.BgpParseException;
21import org.onosproject.bgpio.protocol.ver4.BgpFactoryVer4;
22import org.onosproject.bgpio.types.BgpHeader;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26/**
27 * Abstraction to provide the version for BGP.
28 */
29public final class BgpFactories {
30
31 protected static final Logger log = LoggerFactory.getLogger(BgpFactories.class);
32
33 private static final GenericReader GENERIC_READER = new GenericReader();
34
35 private BgpFactories() {
36 }
37
38 /**
39 * Returns the instance of BGP Version.
40 *
41 * @param version BGP version
42 * @return BGP version
43 */
44 public static BgpFactory getFactory(BgpVersion version) {
45 switch (version) {
46 case BGP_4:
47 return BgpFactoryVer4.INSTANCE;
48 default:
49 throw new IllegalArgumentException("[BgpFactory:]Unknown version: " + version);
50 }
51 }
52
53 /**
54 * Reader class for reading BGP messages from channel buffer.
55 *
56 */
57 private static class GenericReader implements BgpMessageReader<BgpMessage> {
58
59 @Override
60 public BgpMessage readFrom(ChannelBuffer bb, BgpHeader bgpHeader)
61 throws BgpParseException {
62 BgpFactory factory;
63
64 if (!bb.readable()) {
65 log.error("Empty message received");
66 throw new BgpParseException("Empty message received");
67 }
68 // TODO: Currently only BGP version 4 is supported
69 factory = org.onosproject.bgpio.protocol.ver4.BgpFactoryVer4.INSTANCE;
70 return factory.getReader().readFrom(bb, bgpHeader);
71 }
72 }
73
74 /**
75 * Returns BGP messsage generic reader.
76 *
77 * @return bgp message generic reader
78 */
79 public static BgpMessageReader<BgpMessage> getGenericReader() {
80 return GENERIC_READER;
81 }
82}