Bug-731: Fixed few major Sonar warnings
[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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Lists;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.List;
16 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
17 import org.opendaylight.protocol.pcep.spi.MessageUtil;
18 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
21 import org.opendaylight.protocol.pcep.spi.VendorInformationObjectRegistry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcreq;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcreqBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.classtype.object.ClassType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.gc.object.Gc;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.load.balancing.object.LoadBalancing;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.Metrics;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.MetricsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.Of;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.PathKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.PcreqMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.PcreqMessageBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.Requests;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.RequestsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.Svec;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.SvecBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.requests.PathKeyExpansionBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.requests.SegmentComputation;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.requests.SegmentComputationBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.requests.segment.computation.P2p;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.requests.segment.computation.P2pBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.requests.segment.computation.p2p.ReportedRoute;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.requests.segment.computation.p2p.ReportedRouteBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.Rro;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.objects.VendorInformationObject;
55
56 /**
57  * Parser for {@link Pcreq}
58  */
59 public class PCEPRequestMessageParser extends AbstractMessageParser {
60
61     public static final int TYPE = 3;
62
63     public PCEPRequestMessageParser(final ObjectRegistry registry, final VendorInformationObjectRegistry viRegistry) {
64         super(registry, viRegistry);
65     }
66
67     @Override
68     public void serializeMessage(final Message message, final ByteBuf out) {
69         Preconditions.checkArgument(message instanceof Pcreq, "Wrong instance of Message. Passed instance of %s. Need Pcreq.", message.getClass());
70         final PcreqMessage msg = ((Pcreq) message).getPcreqMessage();
71         if (msg.getRequests() == null || msg.getRequests().isEmpty()) {
72             throw new IllegalArgumentException("Requests cannot be null or empty.");
73         }
74         final ByteBuf buffer = Unpooled.buffer();
75         for (final Requests req : msg.getRequests()) {
76             serializeObject(req.getRp(), buffer);
77             serializeVendorInformationObjects(req.getVendorInformationObject(), buffer);
78             if (req.getPathKeyExpansion() != null) {
79                 serializeObject(req.getPathKeyExpansion().getPathKey(), buffer);
80             }
81             if (req.getSegmentComputation() != null) {
82                 final SegmentComputation sc = req.getSegmentComputation();
83                 if (sc.getP2p() != null) {
84                     serializeP2P(buffer, sc.getP2p());
85                 }
86             }
87         }
88         if (msg.getSvec() != null) {
89             for (final Svec s : msg.getSvec()) {
90                 serializeObject(s.getSvec(), buffer);
91                 if (s.getOf() != null) {
92                     serializeObject(s.getOf(), buffer);
93                 }
94                 if (s.getGc() != null) {
95                     serializeObject(s.getGc(), buffer);
96                 }
97                 if (s.getXro() != null) {
98                     serializeObject(s.getXro(), buffer);
99                 }
100                 if (s.getMetric() != null) {
101                     for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.svec.Metric m : s.getMetric()) {
102                         serializeObject(m.getMetric(), buffer);
103                     }
104                 }
105                 serializeVendorInformationObjects(s.getVendorInformationObject(), buffer);
106             }
107         }
108         MessageUtil.formatMessage(TYPE, buffer, out);
109     }
110
111     protected void serializeP2P(final ByteBuf buffer, final P2p p2p) {
112         if (p2p.getEndpointsObj() != null) {
113             serializeObject(p2p.getEndpointsObj(), buffer);
114         }
115         serializeVendorInformationObjects(p2p.getVendorInformationObject(), buffer);
116         if (p2p.getReportedRoute() != null) {
117             final ReportedRoute rr = p2p.getReportedRoute();
118             if (rr.getRro() != null) {
119                 serializeObject(rr.getRro(), buffer);
120             }
121             if (rr.getBandwidth() != null) {
122                 serializeObject(rr.getBandwidth(), buffer);
123             }
124         }
125         if (p2p.getLoadBalancing() != null) {
126             serializeObject(p2p.getLoadBalancing(), buffer);
127         }
128         if (p2p.getLspa() != null) {
129             serializeObject(p2p.getLspa(), buffer);
130         }
131         if (p2p.getBandwidth() != null) {
132             serializeObject(p2p.getBandwidth(), buffer);
133         }
134         if (p2p.getMetrics() != null) {
135             for (final Metrics m : p2p.getMetrics()) {
136                 serializeObject(m.getMetric(), buffer);
137             }
138         }
139         if (p2p.getIro() != null) {
140             serializeObject(p2p.getIro(), buffer);
141         }
142         if (p2p.getRro() != null) {
143             serializeObject(p2p.getRro(), buffer);
144         }
145         if (p2p.getXro() != null) {
146             serializeObject(p2p.getXro(), buffer);
147         }
148         if (p2p.getOf() != null) {
149             serializeObject(p2p.getOf(), buffer);
150         }
151         if (p2p.getClassType() != null) {
152             serializeObject(p2p.getClassType(), buffer);
153         }
154     }
155
156     @Override
157     protected Message validate(
158             final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object> objects,
159             final List<Message> errors) throws PCEPDeserializerException {
160         if (objects == null) {
161             throw new IllegalArgumentException("Passed list can't be null.");
162         }
163
164         final List<Requests> requests = Lists.newArrayList();
165         final List<Svec> svecList = Lists.newArrayList();
166         while (!objects.isEmpty()) {
167             final RequestsBuilder rBuilder = new RequestsBuilder();
168             Rp rpObj = null;
169             if (objects.get(0) instanceof Rp) {
170                 rpObj = (Rp) objects.get(0);
171                 objects.remove(0);
172                 if (!rpObj.isProcessingRule()) {
173                     errors.add(createErrorMsg(PCEPErrors.P_FLAG_NOT_SET, Optional.<Rp>absent()));
174                 } else {
175                     rBuilder.setRp(rpObj);
176                 }
177             } else {
178                 // if RP obj is missing return error only
179                 errors.add(createErrorMsg(PCEPErrors.RP_MISSING, Optional.<Rp>absent()));
180                 return null;
181             }
182             final List<VendorInformationObject> vendorInfo = addVendorInformationObjects(objects);
183             if (!vendorInfo.isEmpty()) {
184                 rBuilder.setVendorInformationObject(vendorInfo);
185             }
186             // expansion
187             if (rpObj.isPathKey()) {
188                 if (objects.get(0) instanceof PathKey) {
189                     rBuilder.setPathKeyExpansion(new PathKeyExpansionBuilder().setPathKey((PathKey) objects.get(0)).build());
190                 }
191                 continue;
192             }
193
194             final P2pBuilder p2pBuilder = new P2pBuilder();
195
196             if (objects.get(0) instanceof EndpointsObj) {
197                 final EndpointsObj ep = (EndpointsObj) objects.get(0);
198                 objects.remove(0);
199                 if (!ep.isProcessingRule()) {
200                     errors.add(createErrorMsg(PCEPErrors.P_FLAG_NOT_SET, Optional.of(rpObj)));
201                 } else {
202                     p2pBuilder.setEndpointsObj(ep);
203                 }
204             } else {
205                 errors.add(createErrorMsg(PCEPErrors.END_POINTS_MISSING, Optional.of(rpObj)));
206                 return null;
207             }
208             // p2p
209             if (!rpObj.isP2mp()) {
210                 final SegmentComputation segm = getSegmentComputation(p2pBuilder, objects, errors, rpObj);
211                 if (segm != null) {
212                     rBuilder.setSegmentComputation(segm);
213                 }
214             }
215             while (!objects.isEmpty()) {
216                 final SvecBuilder sBuilder = new SvecBuilder();
217                 final Svec svecComp = getValidSvec(sBuilder, objects);
218                 if (svecComp == null) {
219                     break;
220                 }
221                 svecList.add(svecComp);
222             }
223             requests.add(rBuilder.build());
224         }
225
226         final PcreqMessageBuilder mBuilder = new PcreqMessageBuilder();
227         mBuilder.setRequests(requests);
228         if (!svecList.isEmpty()) {
229             mBuilder.setSvec(svecList);
230         }
231         return new PcreqBuilder().setPcreqMessage(mBuilder.build()).build();
232     }
233
234     protected SegmentComputation getSegmentComputation(final P2pBuilder builder, final List<Object> objects, final List<Message> errors,
235             final Rp rp) {
236         final List<Metrics> metrics = Lists.newArrayList();
237         final List<VendorInformationObject> viObjects = Lists.newArrayList();
238
239         State state = State.Init;
240         while (!objects.isEmpty() && state != State.End) {
241             Object obj = objects.get(0);
242
243             switch (state) {
244             case Init:
245                 state = State.ReportedIn;
246                 if (obj instanceof Rro) {
247                     final ReportedRouteBuilder rrBuilder = new ReportedRouteBuilder();
248                     rrBuilder.setRro((Rro) obj);
249                     objects.remove(0);
250                     obj = objects.get(0);
251                     if (obj instanceof Bandwidth) {
252                         rrBuilder.setBandwidth((Bandwidth) obj);
253                     }
254                     break;
255                 }
256             case ReportedIn:
257                 state = State.VendorInfoList;
258                 if (obj instanceof VendorInformationObject) {
259                     viObjects.add((VendorInformationObject) obj);
260                     state = State.ReportedIn;
261                     break;
262                 }
263             case VendorInfoList:
264                 state = State.LoadBIn;
265                 if (obj instanceof LoadBalancing) {
266                     builder.setLoadBalancing((LoadBalancing) obj);
267                     break;
268                 }
269             case LoadBIn:
270                 state = State.LspaIn;
271                 if (obj instanceof Lspa) {
272                     builder.setLspa((Lspa) obj);
273                     break;
274                 }
275             case LspaIn:
276                 state = State.BandwidthIn;
277                 if (obj instanceof Bandwidth) {
278                     builder.setBandwidth((Bandwidth) obj);
279                     break;
280                 }
281             case BandwidthIn:
282                 state = State.MetricIn;
283                 if (obj instanceof Metric) {
284                     metrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
285                     state = State.BandwidthIn;
286                     break;
287                 }
288             case MetricIn:
289                 state = State.IroIn;
290                 if (obj instanceof Iro) {
291                     builder.setIro((Iro) obj);
292                     break;
293                 }
294             case IroIn:
295                 state = State.RroIn;
296                 if (obj instanceof Rro) {
297                     builder.setRro((Rro) obj);
298                     break;
299                 }
300             case RroIn:
301                 state = State.XroIn;
302                 if (obj instanceof Xro) {
303                     builder.setXro((Xro) obj);
304                     break;
305                 }
306             case XroIn:
307                 state = State.OfIn;
308                 if (obj instanceof Of) {
309                     builder.setOf((Of) obj);
310                     break;
311                 }
312             case OfIn:
313                 state = State.CtIn;
314                 if (obj instanceof ClassType) {
315                     final ClassType classType = (ClassType) obj;
316                     if (!classType.isProcessingRule()) {
317                         errors.add(createErrorMsg(PCEPErrors.P_FLAG_NOT_SET, Optional.of(rp)));
318                     } else {
319                         builder.setClassType(classType);
320                     }
321                     break;
322                 }
323             case CtIn:
324                 state = State.End;
325                 break;
326             case End:
327                 break;
328             }
329             if (!state.equals(State.End)) {
330                 objects.remove(0);
331             }
332         }
333         if (!metrics.isEmpty()) {
334             builder.setMetrics(metrics);
335         }
336         if (!viObjects.isEmpty()) {
337             builder.setVendorInformationObject(viObjects);
338         }
339
340         if (rp.isReoptimization()
341                 && builder.getBandwidth() != null
342                 && !builder.getReportedRoute().getBandwidth().getBandwidth().equals(
343                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth(new byte[] { 0 }))
344                 && builder.getReportedRoute().getRro() == null) {
345             errors.add(createErrorMsg(PCEPErrors.RRO_MISSING, Optional.of(rp)));
346             return null;
347         }
348         return new SegmentComputationBuilder().setP2p(builder.build()).build();
349     }
350
351     private enum State {
352         Init, ReportedIn, VendorInfoList, LoadBIn, LspaIn, BandwidthIn, MetricIn, IroIn, RroIn, XroIn, OfIn, CtIn, End
353     }
354
355     private Svec getValidSvec(final SvecBuilder builder, final List<Object> objects) {
356         if (objects == null || objects.isEmpty()) {
357             throw new IllegalArgumentException("List cannot be null or empty.");
358         }
359
360         if (objects.get(0) instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.svec.object.Svec) {
361             builder.setSvec((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.svec.object.Svec) objects.get(0));
362             objects.remove(0);
363         } else {
364             return null;
365         }
366
367         final List<Metrics> metrics = Lists.newArrayList();
368         final List<VendorInformationObject> viObjects = Lists.newArrayList();
369
370         Object obj = null;
371         SvecState state = SvecState.Init;
372         while (!objects.isEmpty() && !state.equals(SvecState.End)) {
373             obj = objects.get(0);
374
375             switch (state) {
376             case Init:
377                 state = SvecState.OfIn;
378                 if (obj instanceof Of) {
379                     builder.setOf((Of) obj);
380                     break;
381                 }
382             case OfIn:
383                 state = SvecState.GcIn;
384                 if (obj instanceof Gc) {
385                     builder.setGc((Gc) obj);
386                     break;
387                 }
388             case GcIn:
389                 state = SvecState.XroIn;
390                 if (obj instanceof Xro) {
391                     builder.setXro((Xro) obj);
392                     break;
393                 }
394             case XroIn:
395                 state = SvecState.MetricIn;
396                 if (obj instanceof Metric) {
397                     metrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
398                     state = SvecState.XroIn;
399                     break;
400                 }
401             case MetricIn:
402                 state = SvecState.VendorInfo;
403                 if (obj instanceof VendorInformationObject) {
404                     viObjects.add((VendorInformationObject) obj);
405                     state = SvecState.MetricIn;
406                     break;
407                 }
408             case VendorInfo:
409                 state = SvecState.End;
410                 break;
411             case End:
412                 break;
413             }
414             if (!state.equals(SvecState.End)) {
415                 objects.remove(0);
416             }
417         }
418         if (!viObjects.isEmpty()) {
419             builder.setVendorInformationObject(viObjects);
420         }
421         return builder.build();
422     }
423
424     private enum SvecState {
425         Init, OfIn, GcIn, XroIn, MetricIn, VendorInfo, End
426     }
427 }