2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onosproject.pcepio.protocol.ver1;
19 import org.jboss.netty.buffer.ChannelBuffer;
20 import org.onosproject.pcepio.exceptions.PcepParseException;
21 import org.onosproject.pcepio.protocol.PcepFecObjectIPv4Adjacency;
22 import org.onosproject.pcepio.protocol.PcepVersion;
23 import org.onosproject.pcepio.types.PcepObjectHeader;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.google.common.base.MoreObjects;
30 * Provides PCEP fec Object IPv4 Adjacency object.
32 public class PcepFecObjectIPv4AdjacencyVer1 implements PcepFecObjectIPv4Adjacency {
35 * ref : draft-zhao-pce-pcep-extension-for-pce-controller-01 , section : 7.5
38 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
39 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 | Local IPv4 address |
41 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 | Remote IPv4 address |
43 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 FEC Object-Type is 3 IPv4 Adjacency
47 protected static final Logger log = LoggerFactory.getLogger(PcepFecObjectIPv4AdjacencyVer1.class);
49 public static final byte FEC_OBJ_TYPE = 3;
50 public static final byte FEC_OBJ_CLASS = 36; //to be defined
51 public static final byte FEC_OBJECT_VERSION = 1;
52 public static final short FEC_OBJ_MINIMUM_LENGTH = 12;
53 public static final int MINIMUM_COMMON_HEADER_LENGTH = 4;
55 static final PcepObjectHeader DEFAULT_FEC_OBJECT_HEADER = new PcepObjectHeader(FEC_OBJ_CLASS, FEC_OBJ_TYPE,
56 PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED, FEC_OBJ_MINIMUM_LENGTH);
58 private PcepObjectHeader fecObjHeader;
59 private int localIPv4Address;
60 private int remoteIPv4Address;
63 * Constructor to initialize parameters for PCEP fec object .
65 * @param fecObjHeader FEC Object header
66 * @param localIPv4Address Local IPv4 Address
67 * @param remoteIPv4Address Remote IPv4 Address
69 public PcepFecObjectIPv4AdjacencyVer1(PcepObjectHeader fecObjHeader, int localIPv4Address, int remoteIPv4Address) {
70 this.fecObjHeader = fecObjHeader;
71 this.localIPv4Address = localIPv4Address;
72 this.remoteIPv4Address = remoteIPv4Address;
78 * @param obj Pcep fec Object Header
80 public void setFecIpv4ObjHeader(PcepObjectHeader obj) {
81 this.fecObjHeader = obj;
85 public int getLocalIPv4Address() {
86 return this.localIPv4Address;
90 public void seLocalIPv4Address(int value) {
91 this.localIPv4Address = value;
95 public int getRemoteIPv4Address() {
96 return this.remoteIPv4Address;
100 public void seRemoteIPv4Address(int value) {
101 this.remoteIPv4Address = value;
105 * Reads from channel buffer and Returns object of PcepFecObjectIPv4Adjacency.
107 * @param cb of channel buffer.
108 * @return object of PcepFecObjectIPv4Adjacency
109 * @throws PcepParseException when fails to read from channel buffer
111 public static PcepFecObjectIPv4Adjacency read(ChannelBuffer cb) throws PcepParseException {
113 PcepObjectHeader fecObjHeader;
114 int localIPv4Address;
115 int remoteIPv4Address;
117 fecObjHeader = PcepObjectHeader.read(cb);
119 //take only FEC IPv4 Adjacency Object buffer.
120 ChannelBuffer tempCb = cb.readBytes(fecObjHeader.getObjLen() - MINIMUM_COMMON_HEADER_LENGTH);
121 localIPv4Address = tempCb.readInt();
122 remoteIPv4Address = tempCb.readInt();
124 return new PcepFecObjectIPv4AdjacencyVer1(fecObjHeader, localIPv4Address, remoteIPv4Address);
128 public int write(ChannelBuffer cb) throws PcepParseException {
130 int objStartIndex = cb.writerIndex();
132 //Write common header
133 int objLenIndex = fecObjHeader.write(cb);
134 cb.writeInt(localIPv4Address);
135 cb.writeInt(remoteIPv4Address);
137 //Now write FEC IPv4 Adjacency Object Length
138 cb.setShort(objLenIndex, (short) (cb.writerIndex() - objStartIndex));
139 return cb.writerIndex();
143 * Builder class for PCEP fec object IPv4 Adjacency.
145 public static class Builder implements PcepFecObjectIPv4Adjacency.Builder {
146 private boolean bIsHeaderSet = false;
147 private boolean bIsLocalIPv4Addressset = false;
148 private boolean bIsRemoteIPv4Addressset = false;
150 private PcepObjectHeader fecObjHeader;
151 int localIPv4Address;
152 int remoteIPv4Address;
154 private boolean bIsPFlagSet = false;
155 private boolean bPFlag;
157 private boolean bIsIFlagSet = false;
158 private boolean bIFlag;
161 public PcepFecObjectIPv4Adjacency build() throws PcepParseException {
162 PcepObjectHeader fecObjHeader = this.bIsHeaderSet ? this.fecObjHeader : DEFAULT_FEC_OBJECT_HEADER;
164 if (!this.bIsLocalIPv4Addressset) {
165 throw new PcepParseException(
166 "Local IPv4 Address not set while building PcepFecObjectIPv4Adjacency object.");
169 if (!this.bIsRemoteIPv4Addressset) {
170 throw new PcepParseException(
171 " Remote IPv4 Address not set while building PcepFecObjectIPv4Adjacency object.");
175 fecObjHeader.setPFlag(bPFlag);
179 fecObjHeader.setIFlag(bIFlag);
181 return new PcepFecObjectIPv4AdjacencyVer1(fecObjHeader, this.localIPv4Address, this.remoteIPv4Address);
185 public Builder setPFlag(boolean value) {
187 this.bIsPFlagSet = true;
192 public Builder setIFlag(boolean value) {
194 this.bIsIFlagSet = true;
199 public PcepObjectHeader getFecIpv4AdjacencyObjHeader() {
200 return this.fecObjHeader;
204 public Builder setFecIpv4AdjacencyObjHeader(PcepObjectHeader obj) {
205 this.fecObjHeader = obj;
206 this.bIsHeaderSet = true;
211 public int getLocalIPv4Address() {
212 return this.localIPv4Address;
216 public Builder seLocalIPv4Address(int value) {
217 this.localIPv4Address = value;
218 this.bIsLocalIPv4Addressset = true;
223 public int getRemoteIPv4Address() {
224 return this.remoteIPv4Address;
228 public Builder seRemoteIPv4Address(int value) {
229 this.remoteIPv4Address = value;
230 this.bIsRemoteIPv4Addressset = true;
237 public PcepVersion getVersion() {
238 return PcepVersion.PCEP_1;
242 public int getType() {
247 public String toString() {
248 return MoreObjects.toStringHelper(getClass())
249 .add("fecObjHeader", fecObjHeader)
250 .add("localIPv4Address", localIPv4Address)
251 .add("remoteIPv4Address", remoteIPv4Address).toString();