Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [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.pcepio.protocol; |
| 18 | |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 20 | import org.onosproject.pcepio.exceptions.PcepOutOfBoundMessageException; |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 21 | import org.onosproject.pcepio.exceptions.PcepParseException; |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 22 | import org.onosproject.pcepio.protocol.ver1.PcepFactoryVer1; |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
Phanendra Manda | 51fb9c2 | 2015-09-01 16:17:41 +0530 | [diff] [blame] | 26 | /** |
| 27 | * Abstraction to provide the version for PCEP. |
| 28 | */ |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 29 | public final class PcepFactories { |
| 30 | |
| 31 | protected static final Logger log = LoggerFactory.getLogger(PcepFactories.class); |
| 32 | |
| 33 | private static final GenericReader GENERIC_READER = new GenericReader(); |
| 34 | |
| 35 | public static final byte SHIFT_FLAG = 5; |
| 36 | |
| 37 | private PcepFactories() { |
| 38 | } |
| 39 | |
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame] | 40 | /** |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 41 | * Returns the instance of PCEP Version. |
| 42 | * |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 43 | * @param version PCEP version |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 44 | * @return PCEP version |
| 45 | */ |
| 46 | public static PcepFactory getFactory(PcepVersion version) { |
| 47 | switch (version) { |
| 48 | case PCEP_1: |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 49 | return PcepFactoryVer1.INSTANCE; |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 50 | default: |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 51 | throw new IllegalArgumentException("Unknown version: " + version); |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | private static class GenericReader implements PcepMessageReader<PcepMessage> { |
| 56 | |
| 57 | @Override |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame^] | 58 | public PcepMessage readFrom(ChannelBuffer bb) throws PcepParseException, PcepOutOfBoundMessageException { |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 59 | |
| 60 | if (!bb.readable()) { |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 61 | throw new PcepParseException("Empty message received"); |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * 0 1 2 3 |
| 66 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 67 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 68 | * | Ver | Flags | | |
| 69 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 70 | * |
| 71 | * Currently Version 1 is supported |
| 72 | * Currently no flags are used, it is all ignored |
| 73 | */ |
| 74 | |
| 75 | byte packetVersion = bb.getByte(bb.readerIndex()); |
| 76 | packetVersion = (byte) (packetVersion >> SHIFT_FLAG); |
| 77 | PcepFactory factory; |
| 78 | |
| 79 | switch (packetVersion) { |
| 80 | |
| 81 | case 1: |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 82 | factory = org.onosproject.pcepio.protocol.ver1.PcepFactoryVer1.INSTANCE; |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 83 | break; |
| 84 | default: |
bharat saraswal | e2e7a00 | 2015-08-21 22:47:30 +0530 | [diff] [blame] | 85 | throw new PcepParseException("Unknown Packet version: " + packetVersion); |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 86 | } |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 87 | return factory.getReader().readFrom(bb); |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame] | 91 | /** |
Sho SHIMIZU | 74361c1 | 2015-08-11 12:31:48 -0700 | [diff] [blame] | 92 | * Returns GENERIC_READER. |
| 93 | * |
| 94 | * @return GENERIC_READER |
| 95 | */ |
| 96 | public static PcepMessageReader<PcepMessage> getGenericReader() { |
| 97 | return GENERIC_READER; |
| 98 | } |
| 99 | } |