blob: c2ba8840ef8d9796d425a581dd9183bc7f9cbb58 [file] [log] [blame]
Sho SHIMIZU74361c12015-08-11 12:31:48 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZU74361c12015-08-11 12:31:48 -07003 *
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;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053020import org.onosproject.pcepio.exceptions.PcepOutOfBoundMessageException;
Sho SHIMIZU74361c12015-08-11 12:31:48 -070021import org.onosproject.pcepio.exceptions.PcepParseException;
bharat saraswalf7364db2015-08-11 13:39:31 +053022import org.onosproject.pcepio.protocol.ver1.PcepFactoryVer1;
Sho SHIMIZU74361c12015-08-11 12:31:48 -070023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
Phanendra Manda51fb9c22015-09-01 16:17:41 +053026/**
27 * Abstraction to provide the version for PCEP.
28 */
Sho SHIMIZU74361c12015-08-11 12:31:48 -070029public 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 saraswale1806302015-08-21 12:16:46 +053040 /**
Sho SHIMIZU74361c12015-08-11 12:31:48 -070041 * Returns the instance of PCEP Version.
42 *
bharat saraswalf7364db2015-08-11 13:39:31 +053043 * @param version PCEP version
Sho SHIMIZU74361c12015-08-11 12:31:48 -070044 * @return PCEP version
45 */
46 public static PcepFactory getFactory(PcepVersion version) {
47 switch (version) {
48 case PCEP_1:
bharat saraswalf7364db2015-08-11 13:39:31 +053049 return PcepFactoryVer1.INSTANCE;
Sho SHIMIZU74361c12015-08-11 12:31:48 -070050 default:
bharat saraswalf7364db2015-08-11 13:39:31 +053051 throw new IllegalArgumentException("Unknown version: " + version);
Sho SHIMIZU74361c12015-08-11 12:31:48 -070052 }
53 }
54
55 private static class GenericReader implements PcepMessageReader<PcepMessage> {
56
57 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053058 public PcepMessage readFrom(ChannelBuffer bb) throws PcepParseException, PcepOutOfBoundMessageException {
Sho SHIMIZU74361c12015-08-11 12:31:48 -070059
60 if (!bb.readable()) {
Sho SHIMIZU74361c12015-08-11 12:31:48 -070061 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 saraswalf7364db2015-08-11 13:39:31 +053082 factory = org.onosproject.pcepio.protocol.ver1.PcepFactoryVer1.INSTANCE;
Sho SHIMIZU74361c12015-08-11 12:31:48 -070083 break;
84 default:
bharat saraswale2e7a002015-08-21 22:47:30 +053085 throw new PcepParseException("Unknown Packet version: " + packetVersion);
Sho SHIMIZU74361c12015-08-11 12:31:48 -070086 }
bharat saraswalf7364db2015-08-11 13:39:31 +053087 return factory.getReader().readFrom(bb);
Sho SHIMIZU74361c12015-08-11 12:31:48 -070088 }
89 }
90
bharat saraswale1806302015-08-21 12:16:46 +053091 /**
Sho SHIMIZU74361c12015-08-11 12:31:48 -070092 * Returns GENERIC_READER.
93 *
94 * @return GENERIC_READER
95 */
96 public static PcepMessageReader<PcepMessage> getGenericReader() {
97 return GENERIC_READER;
98 }
99}