Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 1 | /*
|
| 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 |
|
| 17 | package org.onosproject.bgpio.protocol;
|
| 18 |
|
| 19 | import org.jboss.netty.buffer.ChannelBuffer;
|
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 20 | import org.onosproject.bgpio.exceptions.BgpParseException;
|
| 21 | import org.onosproject.bgpio.protocol.ver4.BgpFactoryVer4;
|
| 22 | import org.onosproject.bgpio.types.BgpHeader;
|
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 23 | import org.slf4j.Logger;
|
| 24 | import org.slf4j.LoggerFactory;
|
| 25 |
|
| 26 | /**
|
| 27 | * Abstraction to provide the version for BGP.
|
| 28 | */
|
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 29 | public final class BgpFactories {
|
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 30 |
|
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 31 | protected static final Logger log = LoggerFactory.getLogger(BgpFactories.class);
|
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 32 |
|
| 33 | private static final GenericReader GENERIC_READER = new GenericReader();
|
| 34 |
|
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 35 | private BgpFactories() {
|
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 36 | }
|
| 37 |
|
| 38 | /**
|
| 39 | * Returns the instance of BGP Version.
|
| 40 | *
|
| 41 | * @param version BGP version
|
| 42 | * @return BGP version
|
| 43 | */
|
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 44 | public static BgpFactory getFactory(BgpVersion version) {
|
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 45 | switch (version) {
|
| 46 | case BGP_4:
|
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 47 | return BgpFactoryVer4.INSTANCE;
|
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 48 | default:
|
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 49 | throw new IllegalArgumentException("[BgpFactory:]Unknown version: " + version);
|
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | /**
|
| 54 | * Reader class for reading BGP messages from channel buffer.
|
| 55 | *
|
| 56 | */
|
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 57 | private static class GenericReader implements BgpMessageReader<BgpMessage> {
|
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 58 |
|
| 59 | @Override
|
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 60 | public BgpMessage readFrom(ChannelBuffer bb, BgpHeader bgpHeader)
|
| 61 | throws BgpParseException {
|
| 62 | BgpFactory factory;
|
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 63 |
|
| 64 | if (!bb.readable()) {
|
| 65 | log.error("Empty message received");
|
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 66 | throw new BgpParseException("Empty message received");
|
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 67 | }
|
| 68 | // TODO: Currently only BGP version 4 is supported
|
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 69 | factory = org.onosproject.bgpio.protocol.ver4.BgpFactoryVer4.INSTANCE;
|
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 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 | */
|
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 79 | public static BgpMessageReader<BgpMessage> getGenericReader() {
|
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 80 | return GENERIC_READER;
|
| 81 | }
|
| 82 | }
|