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.PCEPDocumentedException;
15 import org.opendaylight.protocol.pcep.PCEPErrors;
16 import org.opendaylight.protocol.pcep.UnknownObject;
17 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcrep;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcrepBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcrepMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.Metrics;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.MetricsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.Of;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessageBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.Replies;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.RepliesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.Result;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.Failure;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.FailureBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.Success;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.SuccessBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure.NoPath;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.success.Paths;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.success.PathsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
44 import com.google.common.collect.Lists;
47 * Parser for {@link PcrepMessage}
49 public class PCEPReplyMessageParser extends AbstractMessageParser {
51 public static final int TYPE = 4;
53 public PCEPReplyMessageParser(final ObjectHandlerRegistry registry) {
58 public void serializeMessage(final Message message, final ByteBuf buffer) {
59 if (!(message instanceof Pcrep)) {
60 throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
61 + ". Nedded PcrepMessage.");
63 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessage repMsg = ((Pcrep) message).getPcrepMessage();
64 if (repMsg.getReplies() == null || repMsg.getReplies().isEmpty()) {
65 throw new IllegalArgumentException("Replies cannot be null or empty.");
67 for (final Replies reply : repMsg.getReplies()) {
68 if (reply.getRp() == null) {
69 throw new IllegalArgumentException("Reply must contain RP object.");
71 buffer.writeBytes(serializeObject(reply.getRp()));
72 if (reply.getResult() != null) {
73 if (reply.getResult() instanceof Failure) {
74 final Failure f = (Failure) reply.getResult();
75 buffer.writeBytes(serializeObject(f.getNoPath()));
76 if (f.getLspa() != null) {
77 buffer.writeBytes(serializeObject(f.getLspa()));
79 if (f.getBandwidth() != null) {
80 buffer.writeBytes(serializeObject(f.getBandwidth()));
82 if (f.getMetrics() != null && !f.getMetrics().isEmpty()) {
83 for (final Metrics m : f.getMetrics()) {
84 buffer.writeBytes(serializeObject(m.getMetric()));
87 if (f.getIro() != null) {
88 buffer.writeBytes(serializeObject(f.getIro()));
91 final Success s = (Success) reply.getResult();
92 for (final Paths p : s.getPaths()) {
93 buffer.writeBytes(serializeObject(p.getEro()));
94 if (p.getLspa() != null) {
95 buffer.writeBytes(serializeObject(p.getLspa()));
97 if (p.getOf() != null) {
98 buffer.writeBytes(serializeObject(p.getOf()));
100 if (p.getBandwidth() != null) {
101 buffer.writeBytes(serializeObject(p.getBandwidth()));
103 if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
104 for (final Metrics m : p.getMetrics()) {
105 buffer.writeBytes(serializeObject(m.getMetric()));
108 if (p.getIro() != null) {
109 buffer.writeBytes(serializeObject(p.getIro()));
118 protected Pcrep validate(final List<Object> objects, final List<Message> errors) throws PCEPDocumentedException {
119 final List<Replies> replies = Lists.newArrayList();
120 while (!objects.isEmpty()) {
121 replies.add(this.getValidReply(objects));
123 return new PcrepBuilder().setPcrepMessage(new PcrepMessageBuilder().setReplies(replies).build()).build();
126 private Replies getValidReply(final List<Object> objects) throws PCEPDocumentedException {
127 if (!(objects.get(0) instanceof Rp)) {
128 throw new PCEPDocumentedException("Pcrep message must contain at least one RP object.", PCEPErrors.RP_MISSING);
130 final Rp rp = (Rp) objects.get(0);
133 if (!objects.isEmpty()) {
134 if (objects.get(0) instanceof NoPath) {
135 final NoPath noPath = (NoPath) objects.get(0);
137 final FailureBuilder builder = new FailureBuilder();
138 builder.setNoPath(noPath);
139 while (!objects.isEmpty()) {
140 this.parseAttributes(builder, objects);
142 res = builder.build();
143 } else if (objects.get(0) instanceof Ero) {
144 final Ero ero = (Ero) objects.get(0);
146 final SuccessBuilder builder = new SuccessBuilder();
147 final List<Paths> paths = Lists.newArrayList();
148 final PathsBuilder pBuilder = new PathsBuilder();
149 pBuilder.setEro(ero);
150 while (!objects.isEmpty()) {
151 this.parsePath(pBuilder, objects);
152 paths.add(pBuilder.build());
154 builder.setPaths(paths);
155 res = builder.build();
158 return new RepliesBuilder().setRp(rp).setResult(res).build();
161 private void parseAttributes(final FailureBuilder builder, final List<Object> objects) throws PCEPDocumentedException {
162 final List<Metrics> pathMetrics = Lists.newArrayList();
165 State state = State.Init;
166 while (!objects.isEmpty() && !state.equals(State.End)) {
167 obj = objects.get(0);
171 state = State.LspaIn;
172 if (obj instanceof Lspa) {
173 builder.setLspa((Lspa) obj);
177 state = State.BandwidthIn;
178 if (obj instanceof Bandwidth) {
179 builder.setBandwidth((Bandwidth) obj);
183 state = State.MetricIn;
184 if (obj instanceof Metric) {
185 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
186 state = State.MetricIn;
191 if (obj instanceof Iro) {
192 builder.setIro((Iro) obj);
201 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
203 if (!state.equals(State.End)) {
207 builder.setMetrics(pathMetrics);
210 private void parsePath(final PathsBuilder builder, final List<Object> objects) throws PCEPDocumentedException {
211 final List<Metrics> pathMetrics = Lists.newArrayList();
214 State state = State.Init;
215 while (!objects.isEmpty() && !state.equals(State.End)) {
216 obj = objects.get(0);
220 state = State.LspaIn;
221 if (obj instanceof Lspa) {
222 builder.setLspa((Lspa) obj);
227 if (obj instanceof Of) {
228 builder.setOf((Of) obj);
232 state = State.BandwidthIn;
233 if (obj instanceof Bandwidth) {
234 builder.setBandwidth((Bandwidth) obj);
238 state = State.MetricIn;
239 if (obj instanceof Metric) {
240 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
241 state = State.BandwidthIn;
246 if (obj instanceof Iro) {
247 builder.setIro((Iro) obj);
256 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
258 if (!state.equals(State.End)) {
262 builder.setMetrics(pathMetrics);
266 Init, LspaIn, OfIn, BandwidthIn, MetricIn, IroIn, End
270 public int getMessageType() {