2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl.message;
10 import io.netty.buffer.ByteBuf;
12 import java.util.List;
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
16 import org.opendaylight.protocol.pcep.PCEPErrors;
17 import org.opendaylight.protocol.pcep.UnknownObject;
18 import org.opendaylight.protocol.pcep.impl.AbstractMessageParser;
19 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcrpt;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcrptBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.Metrics;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.MetricsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.Lsp;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrpt.message.PcrptMessageBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrpt.message.pcrpt.message.Reports;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrpt.message.pcrpt.message.ReportsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrpt.message.pcrpt.message.reports.Path;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrpt.message.pcrpt.message.reports.PathBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.Rro;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.srp.object.Srp;
40 import com.google.common.collect.Lists;
43 * Parser for {@link Pcrpt}
45 public class PCEPReportMessageParser extends AbstractMessageParser {
47 public static final int TYPE = 10;
49 public PCEPReportMessageParser(final ObjectHandlerRegistry registry) {
54 public void serializeMessage(final Message message, final ByteBuf buffer) {
55 if (!(message instanceof Pcrpt)) {
56 throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
57 + ". Nedded PcrptMessage.");
59 final Pcrpt msg = (Pcrpt) message;
60 final List<Reports> reports = msg.getPcrptMessage().getReports();
61 for (final Reports report : reports) {
62 if (report.getSrp() != null) {
63 buffer.writeBytes(serializeObject(report.getSrp()));
65 buffer.writeBytes(serializeObject(report.getLsp()));
66 final Path p = report.getPath();
67 if (p.getEro() != null) {
68 buffer.writeBytes(serializeObject(p.getEro()));
70 if (p.getBandwidth() != null) {
71 buffer.writeBytes(serializeObject(p.getBandwidth()));
73 if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
74 for (final Metrics m : p.getMetrics()) {
75 buffer.writeBytes(serializeObject(m.getMetric()));
78 if (p.getIro() != null) {
79 buffer.writeBytes(serializeObject(p.getIro()));
81 if (p.getRro() != null) {
82 buffer.writeBytes(serializeObject(p.getRro()));
88 public Message parseMessage(final byte[] buffer) throws PCEPDeserializerException {
89 if (buffer == null || buffer.length == 0) {
90 throw new PCEPDeserializerException("Pcrpt message cannot be empty.");
93 final List<Object> objs = parseObjects(buffer);
94 return validate(objs);
95 } catch (final PCEPDocumentedException e) {
96 return createErrorMsg(e.getError());
100 public Pcrpt validate(final List<Object> objects) throws PCEPDeserializerException, PCEPDocumentedException {
101 if (objects == null) {
102 throw new IllegalArgumentException("Passed list can't be null.");
105 final List<Reports> reports = Lists.newArrayList();
107 while (!objects.isEmpty()) {
108 final Reports report = getValidReports(objects);
112 if (!objects.isEmpty()) {
113 if (objects.get(0) instanceof UnknownObject) {
114 throw new PCEPDocumentedException("Unknown object encountered", ((UnknownObject) objects.get(0)).getError());
116 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
118 return new PcrptBuilder().setPcrptMessage(new PcrptMessageBuilder().setReports(reports).build()).build();
121 private Reports getValidReports(final List<Object> objects) throws PCEPDocumentedException {
125 Lspa pathLspa = null;
126 Bandwidth pathBandwidth = null;
127 final List<Metrics> pathMetrics = Lists.newArrayList();
132 State state = State.Init;
133 while (!objects.isEmpty()) {
134 obj = objects.get(0);
135 if (obj instanceof UnknownObject) {
136 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
141 if (obj instanceof Srp) {
147 if (obj instanceof Lsp) {
152 state = State.LspaIn;
153 if (obj instanceof Ero) {
158 state = State.BandwidthIn;
159 if (obj instanceof Lspa) {
160 pathLspa = (Lspa) obj;
164 state = State.MetricIn;
165 if (obj instanceof Bandwidth) {
166 pathBandwidth = (Bandwidth) obj;
171 if (obj instanceof Metric) {
172 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
173 state = State.MetricIn;
178 if (obj instanceof Iro) {
184 if (obj instanceof Rro) {
191 if (obj instanceof UnknownObject) {
192 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
198 throw new PCEPDocumentedException("LSP object missing", PCEPErrors.LSP_MISSING);
201 final PathBuilder builder = new PathBuilder();
203 builder.setLspa(pathLspa);
204 builder.setBandwidth(pathBandwidth);
205 builder.setMetrics(pathMetrics);
207 builder.setRro(pathRro);
208 return new ReportsBuilder().setSrp(srp).setLsp(lsp).setPath(builder.build()).build();
212 Init, SrpIn, LspIn, EroIn, LspaIn, BandwidthIn, MetricIn, IroIn, RroIn, End
216 public int getMessageType() {