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.PcepEndPointsObject;
22 import org.onosproject.pcepio.types.PcepObjectHeader;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.common.base.MoreObjects;
29 * Provides PCEP Endpoints Object.
31 public class PcepEndPointsObjectVer1 implements PcepEndPointsObject {
34 * RFC : 5440 , section : 7.6
35 * An End point is defined as follows:
36 END-POINTS Object-Class is 4.
38 END-POINTS Object-Type is 1 for IPv4 and 2 for IPv6.
40 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
41 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 | Object-Class | OT |Res|P|I| Object Length (bytes) |
43 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 | Source IPv4 address |
45 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 | Destination IPv4 address |
47 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 protected static final Logger log = LoggerFactory.getLogger(PcepEndPointsObjectVer1.class);
52 static final byte END_POINTS_OBJ_TYPE = 1;
53 static final byte END_POINTS_OBJ_CLASS = 4;
54 static final byte END_POINTS_OBJECT_VERSION = 1;
55 static final short END_POINTS_OBJ_MINIMUM_LENGTH = 12;
56 public static byte endPointObjType;
58 static final PcepObjectHeader DEFAULT_END_POINTS_OBJECT_HEADER = new PcepObjectHeader(END_POINTS_OBJ_CLASS,
59 END_POINTS_OBJ_TYPE, PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED,
60 END_POINTS_OBJ_MINIMUM_LENGTH);
62 private PcepObjectHeader endPointsObjHeader;
63 public int sourceIpAddress;
64 public int destIpAddress;
67 * Constructor to initialize all variables.
69 * @param endPointsObjHeader end points object header
70 * @param sourceIpAddress source IP address
71 * @param destIpAddress destination IP address
73 public PcepEndPointsObjectVer1(PcepObjectHeader endPointsObjHeader, int sourceIpAddress, int destIpAddress) {
75 this.endPointsObjHeader = endPointsObjHeader;
76 this.sourceIpAddress = sourceIpAddress;
77 this.destIpAddress = destIpAddress;
81 * Sets End Points Object Header.
83 * @param obj of PcepObjectHeader
85 public void setEndPointsObjHeader(PcepObjectHeader obj) {
86 this.endPointsObjHeader = obj;
90 public void setSourceIpAddress(int sourceIpAddress) {
91 this.sourceIpAddress = sourceIpAddress;
95 public void setDestIpAddress(int destIpAddress) {
96 this.destIpAddress = destIpAddress;
100 public int getSourceIpAddress() {
101 return this.sourceIpAddress;
105 public int getDestIpAddress() {
106 return this.destIpAddress;
110 * Reads from channel buffer and returns object of PcepEndPointsObject.
112 * @param cb of channel buffer
113 * @return object of PcepEndPointsObject
114 * @throws PcepParseException while parsing channel buffer
116 public static PcepEndPointsObject read(ChannelBuffer cb) throws PcepParseException {
118 PcepObjectHeader endPointsObjHeader;
122 endPointsObjHeader = PcepObjectHeader.read(cb);
123 if (endPointsObjHeader.getObjType() == END_POINTS_OBJ_TYPE
124 && endPointsObjHeader.getObjClass() == END_POINTS_OBJ_CLASS) {
125 sourceIpAddress = cb.readInt();
126 destIpAddress = cb.readInt();
128 throw new PcepParseException("Expected PcepEndPointsObject.");
130 return new PcepEndPointsObjectVer1(endPointsObjHeader, sourceIpAddress, destIpAddress);
134 public int write(ChannelBuffer cb) throws PcepParseException {
136 int objStartIndex = cb.writerIndex();
137 //write common header
138 int objLenIndex = endPointsObjHeader.write(cb);
140 //write source IPv4 IP
141 cb.writeInt(sourceIpAddress);
142 //write destination IPv4 IP
143 cb.writeInt(destIpAddress);
145 int length = cb.writerIndex() - objStartIndex;
146 //now write EndPoints Object Length
147 cb.setShort(objLenIndex, (short) length);
148 //will be helpful during print().
149 endPointsObjHeader.setObjLen((short) length);
151 return cb.writerIndex();
156 * Builder class for PCEP end points objects.
158 public static class Builder implements PcepEndPointsObject.Builder {
160 private boolean bIsHeaderSet = false;
161 private boolean bIsSourceIpAddressset = false;
162 private boolean bIsDestIpAddressset = false;
163 private PcepObjectHeader endpointsObjHeader;
164 private int sourceIpAddress;
165 private int destIpAddress;
167 private boolean bIsPFlagSet = false;
168 private boolean bPFlag;
170 private boolean bIsIFlagSet = false;
171 private boolean bIFlag;
174 public PcepEndPointsObject build() throws PcepParseException {
176 PcepObjectHeader endpointsObjHeader = this.bIsHeaderSet ? this.endpointsObjHeader
177 : DEFAULT_END_POINTS_OBJECT_HEADER;
180 endpointsObjHeader.setPFlag(bPFlag);
184 endpointsObjHeader.setIFlag(bIFlag);
187 if (!this.bIsSourceIpAddressset) {
188 throw new PcepParseException("SourceIpAddress not set while building EndPoints object");
191 if (!this.bIsDestIpAddressset) {
192 throw new PcepParseException("DestIpAddress not set while building EndPoints object");
195 return new PcepEndPointsObjectVer1(endpointsObjHeader, this.sourceIpAddress, this.destIpAddress);
199 public PcepObjectHeader getEndPointsObjHeader() {
200 return this.endpointsObjHeader;
204 public Builder setEndPointsObjHeader(PcepObjectHeader obj) {
205 this.endpointsObjHeader = obj;
206 this.bIsHeaderSet = true;
211 public int getSourceIpAddress() {
212 return this.sourceIpAddress;
216 public Builder setSourceIpAddress(int sourceIpAddress) {
217 this.sourceIpAddress = sourceIpAddress;
218 this.bIsSourceIpAddressset = true;
223 public int getDestIpAddress() {
224 return this.destIpAddress;
228 public Builder setDestIpAddress(int destIpAddress) {
229 this.destIpAddress = destIpAddress;
230 this.bIsDestIpAddressset = true;
235 public Builder setPFlag(boolean value) {
237 this.bIsPFlagSet = true;
242 public Builder setIFlag(boolean value) {
244 this.bIsIFlagSet = true;
250 public String toString() {
251 return MoreObjects.toStringHelper(getClass())
252 .add("sourceIpAddress", sourceIpAddress)
253 .add("destIpAddress", destIpAddress).toString();