bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [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.ver1;
|
| 18 |
|
| 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;
|
| 25 |
|
| 26 | import com.google.common.base.MoreObjects;
|
| 27 |
|
| 28 | public class PcepEndPointsObjectVer1 implements PcepEndPointsObject {
|
| 29 |
|
| 30 | /*
|
| 31 | * RFC : 5440 , section : 7.6
|
| 32 | * An End point is defined as follows:
|
| 33 | END-POINTS Object-Class is 4.
|
| 34 |
|
| 35 | END-POINTS Object-Type is 1 for IPv4 and 2 for IPv6.
|
| 36 | 0 1 2 3
|
| 37 | 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
|
| 38 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 39 | | Object-Class | OT |Res|P|I| Object Length (bytes) |
|
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 41 | | Source IPv4 address |
|
| 42 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 43 | | Destination IPv4 address |
|
| 44 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
| 45 |
|
| 46 | */
|
| 47 | protected static final Logger log = LoggerFactory.getLogger(PcepEndPointsObjectVer1.class);
|
| 48 |
|
| 49 | static final byte END_POINTS_OBJ_TYPE = 1;
|
| 50 | static final byte END_POINTS_OBJ_CLASS = 4;
|
| 51 | static final byte END_POINTS_OBJECT_VERSION = 1;
|
| 52 | static final short END_POINTS_OBJ_MINIMUM_LENGTH = 12;
|
| 53 | public static byte endPointObjType;
|
| 54 |
|
| 55 | static final PcepObjectHeader DEFAULT_END_POINTS_OBJECT_HEADER = new PcepObjectHeader(END_POINTS_OBJ_CLASS,
|
| 56 | END_POINTS_OBJ_TYPE, PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED,
|
| 57 | END_POINTS_OBJ_MINIMUM_LENGTH);
|
| 58 |
|
| 59 | private PcepObjectHeader endPointsObjHeader;
|
| 60 | public int sourceIpAddress;
|
| 61 | public int destIpAddress;
|
| 62 |
|
| 63 | /**
|
| 64 | * Constructor to initialize all variables.
|
| 65 | *
|
| 66 | * @param endPointsObjHeader end points object header
|
| 67 | * @param sourceIpAddress source IP address
|
| 68 | * @param destIpAddress destination IP address
|
| 69 | */
|
| 70 | public PcepEndPointsObjectVer1(PcepObjectHeader endPointsObjHeader, int sourceIpAddress, int destIpAddress) {
|
| 71 |
|
| 72 | this.endPointsObjHeader = endPointsObjHeader;
|
| 73 | this.sourceIpAddress = sourceIpAddress;
|
| 74 | this.destIpAddress = destIpAddress;
|
| 75 | }
|
| 76 |
|
| 77 | /**
|
| 78 | * Sets End Points Object Header.
|
| 79 | *
|
| 80 | * @param obj of PcepObjectHeader
|
| 81 | */
|
| 82 | public void setEndPointsObjHeader(PcepObjectHeader obj) {
|
| 83 | this.endPointsObjHeader = obj;
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public void setSourceIpAddress(int sourceIpAddress) {
|
| 88 | this.sourceIpAddress = sourceIpAddress;
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public void setDestIpAddress(int destIpAddress) {
|
| 93 | this.destIpAddress = destIpAddress;
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public int getSourceIpAddress() {
|
| 98 | return this.sourceIpAddress;
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public int getDestIpAddress() {
|
| 103 | return this.destIpAddress;
|
| 104 | }
|
| 105 |
|
| 106 | /**
|
| 107 | * Reads from channel buffer and returns object of PcepEndPointsObject.
|
| 108 | *
|
| 109 | * @param cb of channel buffer
|
| 110 | * @return object of PcepEndPointsObject
|
| 111 | * @throws PcepParseException while parsing channel buffer
|
| 112 | */
|
| 113 | public static PcepEndPointsObject read(ChannelBuffer cb) throws PcepParseException {
|
| 114 |
|
| 115 | PcepObjectHeader endPointsObjHeader;
|
| 116 | int sourceIpAddress;
|
| 117 | int destIpAddress;
|
| 118 |
|
| 119 | endPointsObjHeader = PcepObjectHeader.read(cb);
|
| 120 | if (END_POINTS_OBJ_TYPE == endPointsObjHeader.getObjType()
|
| 121 | && END_POINTS_OBJ_CLASS == endPointsObjHeader.getObjClass()) {
|
| 122 | sourceIpAddress = cb.readInt();
|
| 123 | destIpAddress = cb.readInt();
|
| 124 | } else {
|
| 125 | throw new PcepParseException("Expected PcepEndPointsObject.");
|
| 126 | }
|
| 127 | return new PcepEndPointsObjectVer1(endPointsObjHeader, sourceIpAddress, destIpAddress);
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public int write(ChannelBuffer cb) throws PcepParseException {
|
| 132 |
|
| 133 | int objStartIndex = cb.writerIndex();
|
| 134 | //write common header
|
| 135 | int objLenIndex = endPointsObjHeader.write(cb);
|
| 136 |
|
| 137 | //write source IPv4 IP
|
| 138 | cb.writeInt(sourceIpAddress);
|
| 139 | //write destination IPv4 IP
|
| 140 | cb.writeInt(destIpAddress);
|
| 141 |
|
| 142 | int length = cb.writerIndex() - objStartIndex;
|
| 143 | //now write EndPoints Object Length
|
| 144 | cb.setShort(objLenIndex, (short) length);
|
| 145 | //will be helpful during print().
|
| 146 | endPointsObjHeader.setObjLen((short) length);
|
| 147 |
|
| 148 | return cb.writerIndex();
|
| 149 |
|
| 150 | }
|
| 151 |
|
| 152 | /**
|
| 153 | * Builder class for PCEP end points objects.
|
| 154 | */
|
| 155 | public static class Builder implements PcepEndPointsObject.Builder {
|
| 156 |
|
| 157 | private boolean bIsHeaderSet = false;
|
| 158 | private boolean bIsSourceIpAddressset = false;
|
| 159 | private boolean bIsDestIpAddressset = false;
|
| 160 | private PcepObjectHeader endpointsObjHeader;
|
| 161 | private int sourceIpAddress;
|
| 162 | private int destIpAddress;
|
| 163 |
|
| 164 | private boolean bIsPFlagSet = false;
|
| 165 | private boolean bPFlag;
|
| 166 |
|
| 167 | private boolean bIsIFlagSet = false;
|
| 168 | private boolean bIFlag;
|
| 169 |
|
| 170 | @Override
|
| 171 | public PcepEndPointsObject build() throws PcepParseException {
|
| 172 |
|
| 173 | PcepObjectHeader endpointsObjHeader = this.bIsHeaderSet ? this.endpointsObjHeader
|
| 174 | : DEFAULT_END_POINTS_OBJECT_HEADER;
|
| 175 |
|
| 176 | if (bIsPFlagSet) {
|
| 177 | endpointsObjHeader.setPFlag(bPFlag);
|
| 178 | }
|
| 179 |
|
| 180 | if (bIsIFlagSet) {
|
| 181 | endpointsObjHeader.setIFlag(bIFlag);
|
| 182 | }
|
| 183 |
|
| 184 | if (!this.bIsSourceIpAddressset) {
|
| 185 | throw new PcepParseException("SourceIpAddress not set while building EndPoints object");
|
| 186 | }
|
| 187 |
|
| 188 | if (!this.bIsDestIpAddressset) {
|
| 189 | throw new PcepParseException("DestIpAddress not set while building EndPoints object");
|
| 190 | }
|
| 191 |
|
| 192 | return new PcepEndPointsObjectVer1(endpointsObjHeader, this.sourceIpAddress, this.destIpAddress);
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public PcepObjectHeader getEndPointsObjHeader() {
|
| 197 | return this.endpointsObjHeader;
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public Builder setEndPointsObjHeader(PcepObjectHeader obj) {
|
| 202 | this.endpointsObjHeader = obj;
|
| 203 | this.bIsHeaderSet = true;
|
| 204 | return this;
|
| 205 | }
|
| 206 |
|
| 207 | @Override
|
| 208 | public int getSourceIpAddress() {
|
| 209 | return this.sourceIpAddress;
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public Builder setSourceIpAddress(int sourceIpAddress) {
|
| 214 | this.sourceIpAddress = sourceIpAddress;
|
| 215 | this.bIsSourceIpAddressset = true;
|
| 216 | return this;
|
| 217 | }
|
| 218 |
|
| 219 | @Override
|
| 220 | public int getDestIpAddress() {
|
| 221 | return this.destIpAddress;
|
| 222 | }
|
| 223 |
|
| 224 | @Override
|
| 225 | public Builder setDestIpAddress(int destIpAddress) {
|
| 226 | this.destIpAddress = destIpAddress;
|
| 227 | this.bIsDestIpAddressset = true;
|
| 228 | return this;
|
| 229 | }
|
| 230 |
|
| 231 | @Override
|
| 232 | public Builder setPFlag(boolean value) {
|
| 233 | this.bPFlag = value;
|
| 234 | this.bIsPFlagSet = true;
|
| 235 | return this;
|
| 236 | }
|
| 237 |
|
| 238 | @Override
|
| 239 | public Builder setIFlag(boolean value) {
|
| 240 | this.bIFlag = value;
|
| 241 | this.bIsIFlagSet = true;
|
| 242 | return this;
|
| 243 | }
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 244 | }
|
| 245 |
|
| 246 | @Override
|
| 247 | public String toString() {
|
bharat saraswal | e180630 | 2015-08-21 12:16:46 +0530 | [diff] [blame] | 248 | return MoreObjects.toStringHelper(getClass()).add("sourceIpAddress", sourceIpAddress)
|
| 249 | .add("destIpAddress", destIpAddress).toString();
|
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 250 | }
|
| 251 |
|
| 252 | }
|