f8ea786948abd11e3ef90bbd6c1790045624ff1d
[onosfw.git] /
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.PcepFecObjectIPv6Adjacency;
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 Adjacency object.
31  */
32 public class PcepFecObjectIPv6AdjacencyVer1 implements PcepFecObjectIPv6Adjacency {
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            //              Local IPv6 address (16 bytes)                   //
42            |                                                               |
43            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44            |                                                               |
45            //              Remote IPv6 address (16 bytes)                  //
46            |                                                               |
47            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48
49                       FEC Object-Type is 4 IPv6 Adjacency
50      */
51     protected static final Logger log = LoggerFactory.getLogger(PcepFecObjectIPv6AdjacencyVer1.class);
52
53     public static final byte FEC_OBJ_TYPE = 4;
54     public static final byte FEC_OBJ_CLASS = 63; //to be defined
55     public static final byte FEC_OBJECT_VERSION = 1;
56     public static final short FEC_OBJ_MINIMUM_LENGTH = 36;
57     public static final int MINIMUM_COMMON_HEADER_LENGTH = 4;
58     public static final int IPV6_ADDRESS_LENGTH = 16;
59
60     static final PcepObjectHeader DEFAULT_FEC_OBJECT_HEADER = new PcepObjectHeader(FEC_OBJ_CLASS, FEC_OBJ_TYPE,
61             PcepObjectHeader.REQ_OBJ_OPTIONAL_PROCESS, PcepObjectHeader.RSP_OBJ_PROCESSED, FEC_OBJ_MINIMUM_LENGTH);
62
63     private PcepObjectHeader fecObjHeader;
64     private byte[] localIPv6Address = new byte[IPV6_ADDRESS_LENGTH];
65     private byte[] remoteIPv6Address = new byte[IPV6_ADDRESS_LENGTH];
66
67     /**
68      * Constructor to initialize parameters for PCEP fec object.
69      *
70      * @param fecObjHeader fec object header
71      * @param localIPv6Address local IPv6 address
72      * @param remoteIPv6Address remote IPv6 address
73      */
74     public PcepFecObjectIPv6AdjacencyVer1(PcepObjectHeader fecObjHeader, byte[] localIPv6Address,
75             byte[] remoteIPv6Address) {
76         this.fecObjHeader = fecObjHeader;
77         this.localIPv6Address = localIPv6Address;
78         this.remoteIPv6Address = remoteIPv6Address;
79     }
80
81     /**
82      * Sets Object Header.
83      *
84      * @param obj object header
85      */
86     public void setFecIpv4ObjHeader(PcepObjectHeader obj) {
87         this.fecObjHeader = obj;
88     }
89
90     @Override
91     public byte[] getLocalIPv6Address() {
92         return this.localIPv6Address;
93     }
94
95     @Override
96     public void seLocalIPv6Address(byte[] value) {
97         this.localIPv6Address = value;
98     }
99
100     @Override
101     public byte[] getRemoteIPv6Address() {
102         return this.remoteIPv6Address;
103     }
104
105     @Override
106     public void seRemoteIPv6Address(byte[] value) {
107         this.remoteIPv6Address = value;
108     }
109
110     /**
111      * Reads channel buffer and Returns object of PcepFecObjectIPv6Adjacency.
112      *
113      * @param cb of channel buffer
114      * @return object of PcepFecObjectIPv6Adjacency
115      * @throws PcepParseException when fails tp read from channel buffer
116      */
117     public static PcepFecObjectIPv6Adjacency read(ChannelBuffer cb) throws PcepParseException {
118
119         PcepObjectHeader fecObjHeader;
120         byte[] localIPv6Address = new byte[IPV6_ADDRESS_LENGTH];
121         byte[] remoteIPv6Address = new byte[IPV6_ADDRESS_LENGTH];
122         fecObjHeader = PcepObjectHeader.read(cb);
123         cb.readBytes(localIPv6Address, 0, IPV6_ADDRESS_LENGTH);
124         cb.readBytes(remoteIPv6Address, 0, IPV6_ADDRESS_LENGTH);
125         return new PcepFecObjectIPv6AdjacencyVer1(fecObjHeader, localIPv6Address, remoteIPv6Address);
126     }
127
128     @Override
129     public int write(ChannelBuffer cb) throws PcepParseException {
130
131         int objStartIndex = cb.writerIndex();
132
133         //write common header
134         int objLenIndex = fecObjHeader.write(cb);
135         cb.writeBytes(localIPv6Address);
136         cb.writeBytes(remoteIPv6Address);
137         //now write FEC IPv6 Adjacency Object Length
138         cb.setShort(objLenIndex, (short) (cb.writerIndex() - objStartIndex));
139         return cb.writerIndex();
140     }
141
142     /**
143      * Builder class for PCEP fec object IPv6 Adjacency.
144      */
145     public static class Builder implements PcepFecObjectIPv6Adjacency.Builder {
146         private boolean bIsHeaderSet = false;
147         private boolean bIsLocalIPv6Addressset = false;
148         private boolean bIsRemoteIPv6Addressset = false;
149
150         private PcepObjectHeader fecObjHeader;
151         byte[] localIPv6Address = new byte[IPV6_ADDRESS_LENGTH];
152         byte[] remoteIPv6Address = new byte[IPV6_ADDRESS_LENGTH];
153
154         private boolean bIsPFlagSet = false;
155         private boolean bPFlag;
156
157         private boolean bIsIFlagSet = false;
158         private boolean bIFlag;
159
160         @Override
161         public PcepFecObjectIPv6Adjacency build() throws PcepParseException {
162             PcepObjectHeader fecObjHeader = this.bIsHeaderSet ? this.fecObjHeader : DEFAULT_FEC_OBJECT_HEADER;
163
164             if (!this.bIsLocalIPv6Addressset) {
165                 throw new PcepParseException(
166                         "Local IPv6 Address not set while building PcepFecObjectIPv6Adjacency object.");
167             }
168             if (!this.bIsRemoteIPv6Addressset) {
169                 throw new PcepParseException(
170                         "Remote IPv6 Address not set while building PcepFecObjectIPv6Adjacency object.");
171             }
172             if (bIsPFlagSet) {
173                 fecObjHeader.setPFlag(bPFlag);
174             }
175             if (bIsIFlagSet) {
176                 fecObjHeader.setIFlag(bIFlag);
177             }
178             return new PcepFecObjectIPv6AdjacencyVer1(fecObjHeader, this.localIPv6Address, this.remoteIPv6Address);
179         }
180
181         @Override
182         public Builder setPFlag(boolean value) {
183             this.bPFlag = value;
184             this.bIsPFlagSet = true;
185             return this;
186         }
187
188         @Override
189         public Builder setIFlag(boolean value) {
190             this.bIFlag = value;
191             this.bIsIFlagSet = true;
192             return this;
193         }
194
195         @Override
196         public PcepObjectHeader getFecIpv6AdjacencyObjHeader() {
197             return this.fecObjHeader;
198         }
199
200         @Override
201         public Builder setFecIpv6AdjacencyObjHeader(PcepObjectHeader obj) {
202             this.fecObjHeader = obj;
203             this.bIsHeaderSet = true;
204             return this;
205         }
206
207         @Override
208         public byte[] getLocalIPv6Address() {
209             return this.localIPv6Address;
210         }
211
212         @Override
213         public Builder setLocalIPv6Address(byte[] value) {
214             this.localIPv6Address = value;
215             this.bIsLocalIPv6Addressset = true;
216             return this;
217         }
218
219         @Override
220         public byte[] getRemoteIPv6Address() {
221             return this.remoteIPv6Address;
222         }
223
224         @Override
225         public Builder setRemoteIPv6Address(byte[] value) {
226             this.remoteIPv6Address = value;
227             this.bIsRemoteIPv6Addressset = true;
228             return this;
229         }
230     }
231
232     @Override
233     public PcepVersion getVersion() {
234         return PcepVersion.PCEP_1;
235     }
236
237     @Override
238     public int getType() {
239         return FEC_OBJ_TYPE;
240     }
241
242     @Override
243     public String toString() {
244         return MoreObjects.toStringHelper(getClass())
245                 .add("localIPv6Address", localIPv6Address)
246                 .add("remoteIPv6Address: ", remoteIPv6Address)
247                 .toString();
248     }
249 }