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