49c738fc54bda4a7dfbd164268faf91ecca82cb8
[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 package org.onosproject.pcepio.protocol.ver1;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.onosproject.pcepio.exceptions.PcepParseException;
20 import org.onosproject.pcepio.protocol.PcepAttribute;
21 import org.onosproject.pcepio.protocol.PcepEroObject;
22 import org.onosproject.pcepio.protocol.PcepMsgPath;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.MoreObjects;
27
28 /**
29  * Provides PCEP Message PAth for update message.
30  * Reference :PCE extensions for stateful draft-ietf-pce-stateful-pce-10.
31  */
32 public class PcepMsgPathVer1 implements PcepMsgPath {
33
34     /*
35      *  <path>         ::= <ERO><attribute-list>
36      */
37
38     protected static final Logger log = LoggerFactory.getLogger(PcepMsgPathVer1.class);
39     //PcepEroObject
40     private PcepEroObject eroObj;
41     private boolean isEroObjectSet;
42     // PcepAttribute
43     private PcepAttribute attrList;
44     private boolean isAttributeListSet;
45
46     /**
47      * constructor to initialize objects.
48      */
49     public PcepMsgPathVer1() {
50         eroObj = null;
51         attrList = null;
52         isEroObjectSet = false;
53         isAttributeListSet = false;
54     }
55
56     @Override
57     public PcepEroObject getEroObject() {
58         return eroObj;
59     }
60
61     @Override
62     public PcepAttribute getPcepAttribute() {
63         return attrList;
64     }
65
66     @Override
67     public void setEroObject(PcepEroObject eroObj) {
68         this.eroObj = eroObj;
69     }
70
71     @Override
72     public void setPcepAttribute(PcepAttribute attrList) {
73         this.attrList = attrList;
74     }
75
76     /**
77      * constructor to initialize member variables.
78      *
79      * @param eroObj pcep ero object
80      * @param attrList pcep attribute
81      */
82     public PcepMsgPathVer1(PcepEroObject eroObj, PcepAttribute attrList) {
83         this.eroObj = eroObj;
84         isEroObjectSet = true;
85         this.attrList = attrList;
86         if (attrList == null) {
87             isAttributeListSet = false;
88         } else {
89             isAttributeListSet = true;
90         }
91     }
92
93     @Override
94     public PcepMsgPath read(ChannelBuffer cb) throws PcepParseException {
95         PcepEroObject eroObj;
96         PcepAttribute attrList;
97
98         eroObj = PcepEroObjectVer1.read(cb);
99         attrList = PcepAttributeVer1.read(cb);
100
101         return new PcepMsgPathVer1(eroObj, attrList);
102     }
103
104     @Override
105     public int write(ChannelBuffer cb) throws PcepParseException {
106         int iLenStartIndex = cb.writerIndex();
107
108         //write Object header
109         if (this.isEroObjectSet) {
110             this.eroObj.write(cb);
111         }
112         if (this.isAttributeListSet) {
113             attrList.write(cb);
114         }
115
116         return cb.writerIndex() - iLenStartIndex;
117     }
118
119     /**
120      * Builder class for PCEP Message path.
121      */
122     public static class Builder implements PcepMsgPath.Builder {
123
124         private boolean bIsEROObjectSet = false;
125         private boolean bIsPcepAttributeSet = false;
126
127         //PCEP ERO Object
128         private PcepEroObject eroObject;
129         //PCEP Attribute list
130         private PcepAttribute pcepAttribute;
131
132         @Override
133         public PcepMsgPath build() throws PcepParseException {
134
135             //PCEP ERO Object
136             PcepEroObject eroObject = null;
137             //PCEP Attribute list
138             PcepAttribute pcepAttribute = null;
139
140             if (!this.bIsEROObjectSet) {
141                 throw new PcepParseException("ERO Object NOT Set while building PcepMsgPath.");
142             } else {
143                 eroObject = this.eroObject;
144             }
145             if (!this.bIsPcepAttributeSet) {
146                 throw new PcepParseException("Pcep Attributes NOT Set while building PcepMsgPath.");
147             } else {
148                 pcepAttribute = this.pcepAttribute;
149             }
150
151             return new PcepMsgPathVer1(eroObject, pcepAttribute);
152         }
153
154         @Override
155         public PcepEroObject getEroObject() {
156             return this.eroObject;
157         }
158
159         @Override
160         public PcepAttribute getPcepAttribute() {
161             return this.pcepAttribute;
162         }
163
164         @Override
165         public Builder setEroObject(PcepEroObject eroObject) {
166             this.eroObject = eroObject;
167             this.bIsEROObjectSet = true;
168             return this;
169         }
170
171         @Override
172         public Builder setPcepAttribute(PcepAttribute pcepAttribute) {
173             this.pcepAttribute = pcepAttribute;
174             this.bIsPcepAttributeSet = true;
175             return this;
176         }
177
178     }
179
180     @Override
181     public String toString() {
182         return MoreObjects.toStringHelper(getClass())
183                 .add("EroObject", eroObj)
184                 .add("AttributeList", attrList)
185                 .toString();
186     }
187 }