Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 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.PcepFecObjectIPv4; |
| 22 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 23 | import org.onosproject.pcepio.types.PcepObjectHeader; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import com.google.common.base.MoreObjects; |
| 28 | |
| 29 | /** |
| 30 | * Provides Pcep Fec Object IPv4 object. |
| 31 | */ |
| 32 | public class PcepFecObjectIPv4Ver1 implements PcepFecObjectIPv4 { |
| 33 | |
| 34 | /* |
| 35 | * ref : draft-zhao-pce-pcep-extension-for-pce-controller-01 , section : 7.5 |
| 36 | * |
| 37 | 0 1 2 3 |
| 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 | | IPv4 Node ID | |
| 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 42 | |
| 43 | FEC Object-Type is 1 IPv4 Node ID |
| 44 | */ |
| 45 | protected static final Logger log = LoggerFactory.getLogger(PcepFecObjectIPv4Ver1.class); |
| 46 | |
| 47 | public static final byte FEC_OBJ_TYPE = 1; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 48 | public static final byte FEC_OBJ_CLASS = (byte) 226; |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 49 | public static final byte FEC_OBJECT_VERSION = 1; |
| 50 | public static final short FEC_OBJ_MINIMUM_LENGTH = 8; |
| 51 | public static final int MINIMUM_COMMON_HEADER_LENGTH = 4; |
| 52 | |
| 53 | static final PcepObjectHeader DEFAULT_FEC_OBJECT_HEADER = new PcepObjectHeader(FEC_OBJ_CLASS, FEC_OBJ_TYPE, |
| 54 | PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED, FEC_OBJ_MINIMUM_LENGTH); |
| 55 | |
| 56 | private PcepObjectHeader fecObjHeader; |
| 57 | private int nodeID; |
| 58 | |
| 59 | /** |
| 60 | * Constructor to initialize parameters for PCEP fec object. |
| 61 | * |
| 62 | * @param fecObjHeader fec object header |
| 63 | * @param nodeID node id |
| 64 | */ |
| 65 | public PcepFecObjectIPv4Ver1(PcepObjectHeader fecObjHeader, int nodeID) { |
| 66 | this.fecObjHeader = fecObjHeader; |
| 67 | this.nodeID = nodeID; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Sets the Object Header. |
| 72 | * |
| 73 | * @param obj object header |
| 74 | */ |
| 75 | public void setFecIpv4ObjHeader(PcepObjectHeader obj) { |
| 76 | this.fecObjHeader = obj; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void setNodeID(int nodeID) { |
| 81 | this.nodeID = nodeID; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns Object Header. |
| 86 | * |
| 87 | * @return fecObjHeader fec object header |
| 88 | */ |
| 89 | public PcepObjectHeader getFecIpv4ObjHeader() { |
| 90 | return this.fecObjHeader; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public int getNodeID() { |
| 95 | return this.nodeID; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Reads from channel buffer and returns object of PcepFecObjectIPv4. |
| 100 | * |
| 101 | * @param cb of channel buffer |
| 102 | * @return object of PcepFecObjectIPv4 |
| 103 | * @throws PcepParseException when fails to read from channel buffer |
| 104 | */ |
| 105 | public static PcepFecObjectIPv4 read(ChannelBuffer cb) throws PcepParseException { |
| 106 | |
| 107 | PcepObjectHeader fecObjHeader; |
| 108 | int nodeID; |
| 109 | fecObjHeader = PcepObjectHeader.read(cb); |
| 110 | nodeID = cb.readInt(); |
| 111 | return new PcepFecObjectIPv4Ver1(fecObjHeader, nodeID); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int write(ChannelBuffer cb) throws PcepParseException { |
| 116 | |
| 117 | int objStartIndex = cb.writerIndex(); |
| 118 | |
| 119 | //write common header |
| 120 | int objLenIndex = fecObjHeader.write(cb); |
| 121 | cb.writeInt(nodeID); |
| 122 | |
| 123 | //now write FEC IPv4 Object Length |
| 124 | cb.setShort(objLenIndex, (short) (cb.writerIndex() - objStartIndex)); |
| 125 | return cb.writerIndex(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Builder class for PCEP fec pobject IPv4. |
| 130 | */ |
| 131 | public static class Builder implements PcepFecObjectIPv4.Builder { |
| 132 | private boolean bIsHeaderSet = false; |
| 133 | private boolean bIsNodeIdset = false; |
| 134 | |
| 135 | private PcepObjectHeader fecObjHeader; |
| 136 | private int nodeID; |
| 137 | |
| 138 | private boolean bIsPFlagSet = false; |
| 139 | private boolean bPFlag; |
| 140 | |
| 141 | private boolean bIsIFlagSet = false; |
| 142 | private boolean bIFlag; |
| 143 | |
| 144 | @Override |
| 145 | public PcepFecObjectIPv4 build() throws PcepParseException { |
| 146 | PcepObjectHeader fecObjHeader = this.bIsHeaderSet ? this.fecObjHeader : DEFAULT_FEC_OBJECT_HEADER; |
| 147 | |
| 148 | if (!this.bIsNodeIdset) { |
| 149 | throw new PcepParseException("NodeID not set while building PcepFecObjectIPv4 object."); |
| 150 | } |
| 151 | if (bIsPFlagSet) { |
| 152 | fecObjHeader.setPFlag(bPFlag); |
| 153 | } |
| 154 | if (bIsIFlagSet) { |
| 155 | fecObjHeader.setIFlag(bIFlag); |
| 156 | } |
| 157 | return new PcepFecObjectIPv4Ver1(fecObjHeader, this.nodeID); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Builder setPFlag(boolean value) { |
| 162 | this.bPFlag = value; |
| 163 | this.bIsPFlagSet = true; |
| 164 | return this; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public Builder setIFlag(boolean value) { |
| 169 | this.bIFlag = value; |
| 170 | this.bIsIFlagSet = true; |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public PcepObjectHeader getFecIpv4ObjHeader() { |
| 176 | return this.fecObjHeader; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public Builder setFecIpv4ObjHeader(PcepObjectHeader obj) { |
| 181 | this.fecObjHeader = obj; |
| 182 | this.bIsHeaderSet = true; |
| 183 | return this; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public int getNodeID() { |
| 188 | return this.nodeID; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public Builder setNodeID(int value) { |
| 193 | this.nodeID = value; |
| 194 | this.bIsNodeIdset = true; |
| 195 | return this; |
| 196 | } |
| 197 | |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public PcepVersion getVersion() { |
| 202 | return PcepVersion.PCEP_1; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public int getType() { |
| 207 | return FEC_OBJ_TYPE; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public String toString() { |
Sho SHIMIZU | 7cdbcf7 | 2015-09-03 14:43:05 -0700 | [diff] [blame] | 212 | return MoreObjects.toStringHelper(getClass()) |
| 213 | .add("fecObjHeader", fecObjHeader) |
| 214 | .add("nodeID: ", nodeID) |
Sho SHIMIZU | e81e4db | 2015-09-03 09:44:38 -0700 | [diff] [blame] | 215 | .toString(); |
| 216 | } |
| 217 | } |