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.Arrays;
13 import java.util.List;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
17 import org.opendaylight.protocol.pcep.PCEPErrorMapping;
18 import org.opendaylight.protocol.pcep.PCEPErrors;
19 import org.opendaylight.protocol.pcep.UnknownObject;
20 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
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.PcrepMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.Metrics;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.MetricsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.Of;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
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.Failure;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.FailureBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.Success;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.SuccessBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure.NoPath;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.success.Paths;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.success.PathsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
51 import com.google.common.collect.Lists;
54 * Parser for {@link PcrepMessage}
56 public class PCEPReplyMessageParser extends AbstractMessageParser {
58 public static final int TYPE = 4;
60 public PCEPReplyMessageParser(final ObjectHandlerRegistry registry) {
65 public void serializeMessage(final Message message, final ByteBuf buffer) {
66 if (!(message instanceof Pcrep)) {
67 throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
68 + ". Nedded PcrepMessage.");
70 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessage repMsg = ((Pcrep) message).getPcrepMessage();
71 if (repMsg.getReplies() == null || repMsg.getReplies().isEmpty()) {
72 throw new IllegalArgumentException("Replies cannot be null or empty.");
74 for (final Replies reply : repMsg.getReplies()) {
75 if (reply.getRp() == null) {
76 throw new IllegalArgumentException("Reply must contain RP object.");
78 buffer.writeBytes(serializeObject(reply.getRp()));
79 if (reply.getResult() != null) {
80 if (reply.getResult() instanceof Failure) {
81 final Failure f = (Failure) reply.getResult();
82 buffer.writeBytes(serializeObject(f.getNoPath()));
83 if (f.getLspa() != null) {
84 buffer.writeBytes(serializeObject(f.getLspa()));
86 if (f.getBandwidth() != null) {
87 buffer.writeBytes(serializeObject(f.getBandwidth()));
89 if (f.getMetrics() != null && !f.getMetrics().isEmpty()) {
90 for (final Metrics m : f.getMetrics()) {
91 buffer.writeBytes(serializeObject(m.getMetric()));
94 if (f.getIro() != null) {
95 buffer.writeBytes(serializeObject(f.getIro()));
98 final Success s = (Success) reply.getResult();
99 for (final Paths p : s.getPaths()) {
100 buffer.writeBytes(serializeObject(p.getEro()));
101 if (p.getLspa() != null) {
102 buffer.writeBytes(serializeObject(p.getLspa()));
104 if (p.getOf() != null) {
105 buffer.writeBytes(serializeObject(p.getOf()));
107 if (p.getBandwidth() != null) {
108 buffer.writeBytes(serializeObject(p.getBandwidth()));
110 if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
111 for (final Metrics m : p.getMetrics()) {
112 buffer.writeBytes(serializeObject(m.getMetric()));
115 if (p.getIro() != null) {
116 buffer.writeBytes(serializeObject(p.getIro()));
125 public Message parseMessage(final byte[] buffer) throws PCEPDeserializerException, PCEPDocumentedException {
126 if (buffer == null || buffer.length == 0) {
127 throw new PCEPDeserializerException("Error message is empty.");
129 final List<Object> objs = parseObjects(buffer);
130 PcrepMessage m = null;
133 } catch (final PCEPDocumentedException e) {
134 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
135 return new PcerrBuilder().setPcerrMessage(
136 new PcerrMessageBuilder().setErrors(
137 Arrays.asList(new ErrorsBuilder().setErrorObject(
138 new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e.getError()).type).setValue(
139 maping.getFromErrorsEnum(e.getError()).value).build()).build())).build()).build();
144 private Pcrep validate(final List<Object> objects) throws PCEPDocumentedException {
145 final List<Replies> replies = Lists.newArrayList();
146 while (!objects.isEmpty()) {
147 replies.add(this.getValidReply(objects));
149 return new PcrepBuilder().setPcrepMessage(new PcrepMessageBuilder().setReplies(replies).build()).build();
152 private Replies getValidReply(final List<Object> objects) throws PCEPDocumentedException {
153 if (!(objects.get(0) instanceof Rp)) {
154 throw new PCEPDocumentedException("Pcrep message must contain at least one RP object.", PCEPErrors.RP_MISSING);
156 final Rp rp = (Rp) objects.get(0);
159 if (!objects.isEmpty()) {
160 if (objects.get(0) instanceof NoPath) {
161 final NoPath noPath = (NoPath) objects.get(0);
163 final FailureBuilder builder = new FailureBuilder();
164 builder.setNoPath(noPath);
165 while (!objects.isEmpty()) {
166 this.parseAttributes(builder, objects);
168 res = builder.build();
169 } else if (objects.get(0) instanceof Ero) {
170 final Ero ero = (Ero) objects.get(0);
172 final SuccessBuilder builder = new SuccessBuilder();
173 final List<Paths> paths = Lists.newArrayList();
174 final PathsBuilder pBuilder = new PathsBuilder();
175 pBuilder.setEro(ero);
176 while (!objects.isEmpty()) {
177 this.parsePath(pBuilder, objects);
178 paths.add(pBuilder.build());
180 builder.setPaths(paths);
181 res = builder.build();
184 return new RepliesBuilder().setRp(rp).setResult(res).build();
187 private void parseAttributes(final FailureBuilder builder, final List<Object> objects) throws PCEPDocumentedException {
188 final List<Metrics> pathMetrics = Lists.newArrayList();
191 State state = State.Init;
192 while (!objects.isEmpty() && !state.equals(State.End)) {
193 obj = objects.get(0);
197 state = State.LspaIn;
198 if (obj instanceof Lspa) {
199 builder.setLspa((Lspa) obj);
203 state = State.BandwidthIn;
204 if (obj instanceof Bandwidth) {
205 builder.setBandwidth((Bandwidth) obj);
209 state = State.MetricIn;
210 if (obj instanceof Metric) {
211 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
212 state = State.MetricIn;
217 if (obj instanceof Iro) {
218 builder.setIro((Iro) obj);
227 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
229 if (!state.equals(State.End)) {
233 builder.setMetrics(pathMetrics);
236 private void parsePath(final PathsBuilder builder, final List<Object> objects) throws PCEPDocumentedException {
237 final List<Metrics> pathMetrics = Lists.newArrayList();
240 State state = State.Init;
241 while (!objects.isEmpty() && !state.equals(State.End)) {
242 obj = objects.get(0);
246 state = State.LspaIn;
247 if (obj instanceof Lspa) {
248 builder.setLspa((Lspa) obj);
253 if (obj instanceof Of) {
254 builder.setOf((Of) obj);
258 state = State.BandwidthIn;
259 if (obj instanceof Bandwidth) {
260 builder.setBandwidth((Bandwidth) obj);
264 state = State.MetricIn;
265 if (obj instanceof Metric) {
266 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
267 state = State.BandwidthIn;
272 if (obj instanceof Iro) {
273 builder.setIro((Iro) obj);
282 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
284 if (!state.equals(State.End)) {
288 builder.setMetrics(pathMetrics);
292 Init, LspaIn, OfIn, BandwidthIn, MetricIn, IroIn, End
296 public int getMessageType() {