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