Rework parser infrastructure to support partial message processing
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPRequestMessageParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.pcep.impl.message;
9
10 import io.netty.buffer.ByteBuf;
11
12 import java.util.List;
13
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcrepMessage;
18
19 /**
20  * Parser for {@link PcrepMessage}
21  */
22 // FIXME finish
23 public class PCEPRequestMessageParser extends AbstractMessageParser {
24
25         public static final int TYPE = 3;
26
27         public PCEPRequestMessageParser(final ObjectHandlerRegistry registry) {
28                 super(registry);
29         }
30
31         @Override
32         public void serializeMessage(final Message message, final ByteBuf buffer) {
33                 if (!(message instanceof PcrepMessage)) {
34                         throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance of " + message.getClass()
35                                         + ". Needed PcrepMessage.");
36                 }
37         }
38
39         @Override
40         protected Message validate(
41                         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object> objects,
42                         final List<Message> errors) throws PCEPDeserializerException {
43                 // if (objects == null)
44                 // throw new IllegalArgumentException("Passed list can't be null.");
45                 //
46                 // final List<Message> msgs = Lists.newArrayList();
47                 // final List<CompositeRequestSvecObject> svecList = new ArrayList<CompositeRequestSvecObject>();
48                 //
49                 // CompositeRequestSvecObject svecComp;
50                 // while (!objects.isEmpty()) {
51                 // try {
52                 // if ((svecComp = getValidSvecComposite(objects)) == null)
53                 // break;
54                 // } catch (final PCEPDocumentedException e) {
55                 // msgs.add(new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
56                 // return msgs;
57                 // }
58                 //
59                 // svecList.add(svecComp);
60                 // }
61                 //
62                 // while (!objects.isEmpty()) {
63                 // final List<CompositeRequestObject> requests = new ArrayList<CompositeRequestObject>();
64                 // PCEPRequestParameterObject rpObj = null;
65                 // boolean requestRejected = false;
66                 //
67                 // if (objects.get(0) instanceof PCEPRequestParameterObject) {
68                 // rpObj = (PCEPRequestParameterObject) objects.get(0);
69                 // objects.remove(rpObj);
70                 // if (!rpObj.isProcessed()) {
71                 // msgs.add(new PCEPErrorMessage(new CompositeErrorObject(rpObj, new PCEPErrorObject(PCEPErrors.P_FLAG_NOT_SET))));
72                 // requestRejected = true;
73                 // }
74                 //
75                 // } else {
76                 // // if RP obj is missing return error only;
77                 // msgs.clear();
78                 // msgs.add(new PCEPErrorMessage(new PCEPErrorObject(PCEPErrors.RP_MISSING)));
79                 // return msgs;
80                 // }
81                 //
82                 // PCEPEndPointsObject<?> endPoints = null;
83                 // if (objects.get(0) instanceof PCEPEndPointsObject<?>) {
84                 // endPoints = (PCEPEndPointsObject<?>) objects.get(0);
85                 // objects.remove(0);
86                 // if (!endPoints.isProcessed()) {
87                 // msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(rpObj, false), new
88                 // PCEPErrorObject(PCEPErrors.P_FLAG_NOT_SET))));
89                 // requestRejected = true;
90                 // }
91                 // } else {
92                 // msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(rpObj, false), new
93                 // PCEPErrorObject(PCEPErrors.END_POINTS_MISSING))));
94                 // requestRejected = true;
95                 // }
96                 //
97                 // // ignore all continual end-points objects
98                 // while (!objects.isEmpty() && objects.get(0) instanceof PCEPEndPointsObject<?>) {
99                 // objects.remove(0);
100                 // }
101                 //
102                 // PCEPClassTypeObject classType = null;
103                 // PCEPLspObject lsp = null;
104                 // PCEPLspaObject lspa = null;
105                 // PCEPRequestedPathBandwidthObject bandwidth = null;
106                 // final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
107                 // PCEPReportedRouteObject rro = null;
108                 // PCEPExistingPathBandwidthObject rroBandwidth = null;
109                 // PCEPIncludeRouteObject iro = null;
110                 // PCEPLoadBalancingObject loadBalancing = null;
111                 //
112                 // int state = 1;
113                 // while (!objects.isEmpty()) {
114                 // final Object obj = objects.get(0);
115                 // if (obj instanceof UnknownObject) {
116                 // if (((UnknownObject) obj).isProcessingRule()) {
117                 // msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(rpObj, false), new PCEPErrorObject(((UnknownObject)
118                 // obj).getError()))));
119                 // requestRejected = true;
120                 // }
121                 //
122                 // objects.remove(0);
123                 // continue;
124                 // }
125                 // switch (state) {
126                 // case 1:
127                 // state = 2;
128                 // if (obj instanceof PCEPClassTypeObject) {
129                 // classType = (PCEPClassTypeObject) obj;
130                 // if (!classType.isProcessed()) {
131                 // msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(rpObj, false), new
132                 // PCEPErrorObject(PCEPErrors.P_FLAG_NOT_SET))));
133                 // requestRejected = true;
134                 // }
135                 // break;
136                 // }
137                 // case 2:
138                 // state = 3;
139                 // if (obj instanceof PCEPLspObject) {
140                 // lsp = (PCEPLspObject) obj;
141                 // break;
142                 // }
143                 // case 3:
144                 // state = 4;
145                 // if (obj instanceof PCEPLspaObject) {
146                 // lspa = (PCEPLspaObject) obj;
147                 // break;
148                 // }
149                 // case 4:
150                 // state = 5;
151                 // if (obj instanceof PCEPRequestedPathBandwidthObject) {
152                 // bandwidth = (PCEPRequestedPathBandwidthObject) obj;
153                 // break;
154                 // }
155                 // case 5:
156                 // state = 6;
157                 // if (obj instanceof PCEPMetricObject) {
158                 // metrics.add((PCEPMetricObject) obj);
159                 // state = 5;
160                 //
161                 // break;
162                 // }
163                 // case 6:
164                 // state = 8;
165                 // if (obj instanceof PCEPReportedRouteObject) {
166                 // rro = (PCEPReportedRouteObject) obj;
167                 // state = 7;
168                 // break;
169                 // }
170                 // case 7:
171                 // state = 8;
172                 // if (obj instanceof PCEPExistingPathBandwidthObject) {
173                 // rroBandwidth = (PCEPExistingPathBandwidthObject) obj;
174                 // break;
175                 // }
176                 // case 8:
177                 // state = 9;
178                 // if (obj instanceof PCEPIncludeRouteObject) {
179                 // iro = (PCEPIncludeRouteObject) obj;
180                 // break;
181                 // }
182                 // case 9:
183                 // if (obj instanceof PCEPLoadBalancingObject) {
184                 // loadBalancing = (PCEPLoadBalancingObject) obj;
185                 // break;
186                 // }
187                 // state = 10;
188                 // }
189                 //
190                 // if (state == 10) {
191                 // break;
192                 // }
193                 //
194                 // objects.remove(obj);
195                 // }
196                 //
197                 // if (rpObj.isReoptimized() && bandwidth != null && bandwidth.getBandwidth() != new Bandwidth(new byte[] { 0 }) &&
198                 // rro == null) {
199                 // msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(rpObj, false), new
200                 // PCEPErrorObject(PCEPErrors.RRO_MISSING))));
201                 // requestRejected = true;
202                 // }
203                 //
204                 // if (!requestRejected) {
205                 // requests.add(new CompositeRequestObject(rpObj, endPoints, classType, lsp, lspa, bandwidth, metrics, rro,
206                 // rroBandwidth, iro, loadBalancing));
207                 // msgs.add(new PCEPRequestMessage(Collections.unmodifiableList(svecList), Collections.unmodifiableList(requests)));
208                 // }
209                 // }
210                 //
211                 // return msgs;
212                 // }
213                 //
214                 // private static CompositeRequestSvecObject getValidSvecComposite(final List<Object> objects) throws
215                 // PCEPDocumentedException {
216                 // if (objects == null || objects.isEmpty()) {
217                 // throw new IllegalArgumentException("List cannot be null or empty.");
218                 // }
219                 //
220                 // PCEPSvecObject svec = null;
221                 // if (objects.get(0) instanceof PCEPSvecObject) {
222                 // svec = (PCEPSvecObject) objects.get(0);
223                 // objects.remove(svec);
224                 // } else
225                 // return null;
226                 //
227                 // PCEPObjectiveFunctionObject of = null;
228                 // PCEPGlobalConstraintsObject gc = null;
229                 // PCEPExcludeRouteObject xro = null;
230                 // final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
231                 //
232                 // int state = 1;
233                 // while (!objects.isEmpty()) {
234                 // final Object obj = objects.get(0);
235                 //
236                 // if (obj instanceof UnknownObject && ((UnknownObject) obj).isProcessingRule()) {
237                 // throw new PCEPDocumentedException("Unknown object in SVEC list.", ((UnknownObject) obj).getError());
238                 // }
239                 //
240                 // switch (state) {
241                 // case 1:
242                 // state = 2;
243                 // if (obj instanceof PCEPObjectiveFunctionObject) {
244                 // of = (PCEPObjectiveFunctionObject) obj;
245                 // break;
246                 // }
247                 // case 2:
248                 // state = 3;
249                 // if (obj instanceof PCEPGlobalConstraintsObject) {
250                 // gc = (PCEPGlobalConstraintsObject) obj;
251                 // break;
252                 // }
253                 // case 3:
254                 // state = 4;
255                 // if (obj instanceof PCEPExcludeRouteObject) {
256                 // xro = (PCEPExcludeRouteObject) obj;
257                 // break;
258                 // }
259                 // case 4:
260                 // state = 5;
261                 // if (obj instanceof PCEPMetricObject) {
262                 // metrics.add((PCEPMetricObject) obj);
263                 // state = 4;
264                 //
265                 // break;
266                 // }
267                 // }
268                 //
269                 // if (state == 5)
270                 // break;
271                 //
272                 // objects.remove(obj);
273                 // }
274                 //
275                 // return new CompositeRequestSvecObject(svec, of, gc, xro, metrics);
276                 // }
277                 //
278                 // private static PCEPRequestParameterObject copyRP(final PCEPRequestParameterObject origRp, final boolean
279                 // processed) {
280                 // return new PCEPRequestParameterObject(origRp.isLoose(), origRp.isBidirectional(), origRp.isReoptimized(),
281                 // origRp.isMakeBeforeBreak(), origRp.isReportRequestOrder(), origRp.isSuplyOFOnResponse(),
282                 // origRp.isFragmentation(), origRp.isP2mp(), origRp.isEroCompression(), origRp.getPriority(),
283                 // origRp.getRequestID(), origRp.getTlvs(), processed, origRp.isIgnored());
284                 // }
285
286                 return null;
287         }
288
289         @Override
290         public int getMessageType() {
291                 return TYPE;
292         }
293 }