Code clean up
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / message / PCEPReplyMessageParser.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.parser.message;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.protocol.pcep.parser.util.Util;
17 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
18 import org.opendaylight.protocol.pcep.spi.MessageUtil;
19 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcrep;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcrepBuilder;
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.explicit.route.object.Ero;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.Metrics;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.MetricsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.metrics.MetricPce;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.object.Monitoring;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.Of;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcc.id.req.object.PccIdReq;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pce.id.object.PceId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessageBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.Replies;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.RepliesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.Result;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.FailureCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.FailureCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.SuccessCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.SuccessCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.NoPath;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.success._case.SuccessBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.success._case.success.Paths;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.success._case.success.PathsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.objects.VendorInformationObject;
52
53 /**
54  * Parser for {@link Pcrep}
55  */
56 public class PCEPReplyMessageParser extends AbstractMessageParser {
57
58     public static final int TYPE = 4;
59
60     public PCEPReplyMessageParser(final ObjectRegistry registry) {
61         super(registry);
62     }
63
64     @Override
65     public void serializeMessage(final Message message, final ByteBuf out) {
66         Preconditions.checkArgument(message instanceof Pcrep, "Wrong instance of Message. Passed instance of %s. Need Pcrep.", message.getClass());
67         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessage repMsg = ((Pcrep) message).getPcrepMessage();
68         Preconditions.checkArgument(repMsg.getReplies() != null && !repMsg.getReplies().isEmpty(), "Replies cannot be null or empty.");
69         final ByteBuf buffer = Unpooled.buffer();
70         for (final Replies reply : repMsg.getReplies()) {
71             Preconditions.checkArgument(reply.getRp() != null, "Reply must contain RP object.");
72             serializeReply(reply, buffer);
73         }
74         MessageUtil.formatMessage(TYPE, buffer, out);
75     }
76
77     protected void serializeReply(final Replies reply, final ByteBuf buffer) {
78         serializeObject(reply.getRp(), buffer);
79         serializeMonitoring(reply, buffer);
80         serializeVendorInformationObjects(reply.getVendorInformationObject(), buffer);
81         if (reply.getResult() == null) {
82             return;
83         }
84         if (reply.getResult() instanceof FailureCase) {
85             final FailureCase f = ((FailureCase) reply.getResult());
86             serializeFailure(f, buffer);
87             return;
88         }
89         final SuccessCase s = (SuccessCase) reply.getResult();
90         serializeSuccess(s, buffer);
91         serializeMonitoringMetrics(reply, buffer);
92     }
93
94     private void serializeFailure(final FailureCase f, final ByteBuf buffer) {
95         if (f == null) {
96             return;
97         }
98         serializeObject(f.getNoPath(), buffer);
99         serializeObject(f.getLspa(), buffer);
100         serializeObject(f.getBandwidth(), buffer);
101         if (f.getMetrics() != null) {
102             for (final Metrics m : f.getMetrics()) {
103                 serializeObject(m.getMetric(), buffer);
104             }
105         }
106         serializeObject(f.getIro(), buffer);
107     }
108
109     private void serializeSuccess(final SuccessCase s, final ByteBuf buffer) {
110         if (s == null || s.getSuccess() == null) {
111             return;
112         }
113         for (final Paths p : s.getSuccess().getPaths()) {
114             serializeObject(p.getEro(), buffer);
115             serializeObject(p.getLspa(), buffer);
116             serializeObject(p.getOf(), buffer);
117             serializeObject(p.getBandwidth(), buffer);
118             if (p.getMetrics() != null) {
119                 for (final Metrics m : p.getMetrics()) {
120                     serializeObject(m.getMetric(), buffer);
121                 }
122             }
123             serializeObject(p.getIro(), buffer);
124         }
125         serializeVendorInformationObjects(s.getSuccess().getVendorInformationObject(), buffer);
126     }
127
128     private void serializeMonitoring(final Replies reply, final ByteBuf buffer) {
129         serializeObject(reply.getMonitoring(), buffer);
130         serializeObject(reply.getPccIdReq(), buffer);
131     }
132
133     private void serializeMonitoringMetrics(final Replies reply, final ByteBuf buffer) {
134         if (reply.getMetricPce() != null) {
135             for (final MetricPce metricPce : reply.getMetricPce()) {
136                 serializeMetricPce(metricPce, buffer);
137             }
138         }
139     }
140
141     protected void serializeMetricPce(final MetricPce metricPce, final ByteBuf buffer) {
142         Preconditions.checkArgument(metricPce.getPceId() != null, "PCE-ID must be present.");
143         serializeObject(metricPce.getPceId(), buffer);
144         serializeObject(metricPce.getProcTime(), buffer);
145         serializeObject(metricPce.getOverload(), buffer);
146     }
147
148     @Override
149     protected Pcrep validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
150         Preconditions.checkArgument(objects != null, "Passed list can't be null.");
151         if (objects.isEmpty()) {
152             throw new PCEPDeserializerException("Pcrep message cannot be empty.");
153         }
154         final List<Replies> replies = new ArrayList<>();
155         while (!objects.isEmpty()) {
156             final Replies r = this.getValidReply(objects, errors);
157             if (r != null) {
158                 replies.add(r);
159             }
160         }
161         if (!objects.isEmpty()) {
162             throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
163         }
164         return new PcrepBuilder().setPcrepMessage(new PcrepMessageBuilder().setReplies(replies).build()).build();
165     }
166
167     protected Replies getValidReply(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
168         Object object = objects.remove(0);
169         if (!(object instanceof Rp)) {
170             errors.add(createErrorMsg(PCEPErrors.RP_MISSING, Optional.absent()));
171             return null;
172         }
173         final Rp rp = (Rp) object;
174         final RepliesBuilder repliesBuilder = new RepliesBuilder();
175         if (!objects.isEmpty() && objects.get(0) instanceof Monitoring) {
176             repliesBuilder.setMonitoring((Monitoring) objects.get(0));
177             objects.remove(0);
178         }
179         if (!objects.isEmpty() && objects.get(0) instanceof PccIdReq) {
180             repliesBuilder.setPccIdReq((PccIdReq) objects.get(0));
181             objects.remove(0);
182         }
183         final List<VendorInformationObject> vendorInfo = addVendorInformationObjects(objects);
184         Result res = null;
185         if (!objects.isEmpty()) {
186             if (objects.get(0) instanceof NoPath) {
187                 res = handleNoPath((NoPath) objects.get(0), objects);
188             } else if (objects.get(0) instanceof Ero) {
189                 res = handleEro((Ero) objects.get(0), objects);
190             }
191         }
192         final List<MetricPce> metricPceList = new ArrayList<>();
193         if (!objects.isEmpty() && objects.get(0) instanceof PceId) {
194             while (!objects.isEmpty()) {
195                 metricPceList.add(Util.validateMonitoringMetrics(objects));
196             }
197         }
198         if (!vendorInfo.isEmpty()) {
199             repliesBuilder.setVendorInformationObject(vendorInfo);
200         }
201         if (!metricPceList.isEmpty()) {
202             repliesBuilder.setMetricPce(metricPceList);
203         }
204         return repliesBuilder.setRp(rp).setResult(res).build();
205     }
206
207     private Result handleNoPath(final NoPath noPath, final List<Object> objects) {
208         objects.remove(0);
209         final FailureCaseBuilder builder = new FailureCaseBuilder();
210         builder.setNoPath(noPath);
211         while (!objects.isEmpty() && !(objects.get(0) instanceof PceId)) {
212             this.parseAttributes(builder, objects);
213         }
214         return builder.build();
215     }
216
217     private Result handleEro(final Ero ero, final List<Object> objects) {
218         objects.remove(0);
219         final SuccessBuilder builder = new SuccessBuilder();
220         final List<Paths> paths = new ArrayList<>();
221         final PathsBuilder pBuilder = new PathsBuilder();
222         pBuilder.setEro(ero);
223         while (!objects.isEmpty() && !(objects.get(0) instanceof PceId)) {
224             final List<VendorInformationObject> vendorInfoObjects = addVendorInformationObjects(objects);
225             if (!vendorInfoObjects.isEmpty()) {
226                 builder.setVendorInformationObject(vendorInfoObjects);
227             }
228             this.parsePath(pBuilder, objects);
229             paths.add(pBuilder.build());
230         }
231         builder.setPaths(paths);
232         return new SuccessCaseBuilder().setSuccess(builder.build()).build();
233     }
234
235     protected void parseAttributes(final FailureCaseBuilder builder, final List<Object> objects) {
236         final List<Metrics> pathMetrics = new ArrayList<>();
237
238         Object obj;
239         State state = State.INIT;
240         while (!objects.isEmpty() && !state.equals(State.END)) {
241             obj = objects.get(0);
242             state = insertObject(state, obj, builder, pathMetrics);
243             if (!state.equals(State.END)) {
244                 objects.remove(0);
245             }
246         }
247         if (!pathMetrics.isEmpty()) {
248             builder.setMetrics(pathMetrics);
249         }
250     }
251
252     private State insertObject(final State state, final Object obj, final FailureCaseBuilder builder, final List<Metrics> pathMetrics) {
253         switch (state) {
254         case INIT:
255             if (obj instanceof Lspa) {
256                 builder.setLspa((Lspa) obj);
257                 return State.LSPA_IN;
258             }
259         case LSPA_IN:
260             if (obj instanceof Bandwidth) {
261                 builder.setBandwidth((Bandwidth) obj);
262                 return State.BANDWIDTH_IN;
263             }
264         case BANDWIDTH_IN:
265             if (obj instanceof Metric) {
266                 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
267                 return State.BANDWIDTH_IN;
268             }
269         case METRIC_IN:
270             if (obj instanceof Iro) {
271                 builder.setIro((Iro) obj);
272                 return State.IRO_IN;
273             }
274         case IRO_IN:
275         case END:
276             return State.END;
277         default:
278             return state;
279         }
280     }
281
282     protected void parsePath(final PathsBuilder builder, final List<Object> objects) {
283         final List<Metrics> pathMetrics = new ArrayList<>();
284
285         Object obj;
286         State state = State.INIT;
287         while (!objects.isEmpty() && !state.equals(State.END)) {
288             obj = objects.get(0);
289             state = insertObject(state, obj, builder, pathMetrics);
290             if (!state.equals(State.END)) {
291                 objects.remove(0);
292             }
293         }
294         if (!pathMetrics.isEmpty()) {
295             builder.setMetrics(pathMetrics);
296         }
297     }
298
299     private State insertObject(final State state, final Object obj, final PathsBuilder builder, final List<Metrics> pathMetrics) {
300         switch (state) {
301         case INIT:
302             if (obj instanceof Lspa) {
303                 builder.setLspa((Lspa) obj);
304                 return State.LSPA_IN;
305             }
306         case LSPA_IN:
307             if (obj instanceof Of) {
308                 builder.setOf((Of) obj);
309                 return State.OF_IN;
310             }
311         case OF_IN:
312             if (obj instanceof Bandwidth) {
313                 builder.setBandwidth((Bandwidth) obj);
314                 return State.BANDWIDTH_IN;
315             }
316         case BANDWIDTH_IN:
317             if (obj instanceof Metric) {
318                 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
319                 return State.BANDWIDTH_IN;
320             }
321         case METRIC_IN:
322             if (obj instanceof Iro) {
323                 builder.setIro((Iro) obj);
324                 return State.IRO_IN;
325             }
326         case IRO_IN:
327         case END:
328             return State.END;
329         default:
330             return state;
331         }
332     }
333
334     private enum State {
335         INIT, LSPA_IN, OF_IN, BANDWIDTH_IN, METRIC_IN, IRO_IN, END
336     }
337 }