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