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