blob: 85416f986b0a71dac13bdaafb12329b6f9b8961f [file] [log] [blame]
Sho SHIMIZU74361c12015-08-11 12:31:48 -07001/*
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.pcepio.protocol;
18
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.onosproject.pcepio.exceptions.PcepParseException;
bharat saraswalf7364db2015-08-11 13:39:31 +053021import org.onosproject.pcepio.protocol.ver1.PcepFactoryVer1;
Sho SHIMIZU74361c12015-08-11 12:31:48 -070022import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
Phanendra Manda51fb9c22015-09-01 16:17:41 +053025/**
26 * Abstraction to provide the version for PCEP.
27 */
Sho SHIMIZU74361c12015-08-11 12:31:48 -070028public 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 saraswale1806302015-08-21 12:16:46 +053039 /**
Sho SHIMIZU74361c12015-08-11 12:31:48 -070040 * Returns the instance of PCEP Version.
41 *
bharat saraswalf7364db2015-08-11 13:39:31 +053042 * @param version PCEP version
Sho SHIMIZU74361c12015-08-11 12:31:48 -070043 * @return PCEP version
44 */
45 public static PcepFactory getFactory(PcepVersion version) {
46 switch (version) {
47 case PCEP_1:
bharat saraswalf7364db2015-08-11 13:39:31 +053048 return PcepFactoryVer1.INSTANCE;
Sho SHIMIZU74361c12015-08-11 12:31:48 -070049 default:
bharat saraswalf7364db2015-08-11 13:39:31 +053050 throw new IllegalArgumentException("Unknown version: " + version);
Sho SHIMIZU74361c12015-08-11 12:31:48 -070051 }
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 SHIMIZU74361c12015-08-11 12:31:48 -070060 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 saraswalf7364db2015-08-11 13:39:31 +053081 factory = org.onosproject.pcepio.protocol.ver1.PcepFactoryVer1.INSTANCE;
Sho SHIMIZU74361c12015-08-11 12:31:48 -070082 break;
83 default:
bharat saraswale2e7a002015-08-21 22:47:30 +053084 throw new PcepParseException("Unknown Packet version: " + packetVersion);
Sho SHIMIZU74361c12015-08-11 12:31:48 -070085 }
bharat saraswalf7364db2015-08-11 13:39:31 +053086 return factory.getReader().readFrom(bb);
Sho SHIMIZU74361c12015-08-11 12:31:48 -070087 }
88 }
89
bharat saraswale1806302015-08-21 12:16:46 +053090 /**
Sho SHIMIZU74361c12015-08-11 12:31:48 -070091 * Returns GENERIC_READER.
92 *
93 * @return GENERIC_READER
94 */
95 public static PcepMessageReader<PcepMessage> getGenericReader() {
96 return GENERIC_READER;
97 }
98}