blob: 71b9cbff4a08e60a427d41b6ef09ca92c1893d8f [file] [log] [blame]
Shashikanth VH95bd85e2015-10-27 14:56:33 +05301/*
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}