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