blob: eb18c7d1056f9925ab875bc5b6f50eb11ba492ad [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;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053020import org.onosproject.bgpio.exceptions.BgpParseException;
21import org.onosproject.bgpio.protocol.ver4.BgpFactoryVer4;
22import org.onosproject.bgpio.types.BgpHeader;
Shashikanth VH95bd85e2015-10-27 14:56:33 +053023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26/**
27 * Abstraction to provide the version for BGP.
28 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053029public final class BgpFactories {
Shashikanth VH95bd85e2015-10-27 14:56:33 +053030
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053031 protected static final Logger log = LoggerFactory.getLogger(BgpFactories.class);
Shashikanth VH95bd85e2015-10-27 14:56:33 +053032
33 private static final GenericReader GENERIC_READER = new GenericReader();
34
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053035 private BgpFactories() {
Shashikanth VH95bd85e2015-10-27 14:56:33 +053036 }
37
38 /**
39 * Returns the instance of BGP Version.
40 *
41 * @param version BGP version
42 * @return BGP version
43 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053044 public static BgpFactory getFactory(BgpVersion version) {
Shashikanth VH95bd85e2015-10-27 14:56:33 +053045 switch (version) {
46 case BGP_4:
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053047 return BgpFactoryVer4.INSTANCE;
Shashikanth VH95bd85e2015-10-27 14:56:33 +053048 default:
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053049 throw new IllegalArgumentException("[BgpFactory:]Unknown version: " + version);
Shashikanth VH95bd85e2015-10-27 14:56:33 +053050 }
51 }
52
53 /**
54 * Reader class for reading BGP messages from channel buffer.
55 *
56 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053057 private static class GenericReader implements BgpMessageReader<BgpMessage> {
Shashikanth VH95bd85e2015-10-27 14:56:33 +053058
59 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053060 public BgpMessage readFrom(ChannelBuffer bb, BgpHeader bgpHeader)
61 throws BgpParseException {
62 BgpFactory factory;
Shashikanth VH95bd85e2015-10-27 14:56:33 +053063
64 if (!bb.readable()) {
65 log.error("Empty message received");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053066 throw new BgpParseException("Empty message received");
Shashikanth VH95bd85e2015-10-27 14:56:33 +053067 }
68 // TODO: Currently only BGP version 4 is supported
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053069 factory = org.onosproject.bgpio.protocol.ver4.BgpFactoryVer4.INSTANCE;
Shashikanth VH95bd85e2015-10-27 14:56:33 +053070 return factory.getReader().readFrom(bb, bgpHeader);
71 }
72 }
73
74 /**
75 * Returns BGP messsage generic reader.
76 *
77 * @return bgp message generic reader
78 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053079 public static BgpMessageReader<BgpMessage> getGenericReader() {
Shashikanth VH95bd85e2015-10-27 14:56:33 +053080 return GENERIC_READER;
81 }
82}