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.spi.ObjectHandlerRegistry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcupd;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcupdBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
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.lsp.object.Lsp;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.PcupdMessageBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.Updates;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.UpdatesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.updates.Path;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.updates.PathBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.srp.object.Srp;
38 import com.google.common.collect.Lists;
41 * Parser for {@link Pcupd}
43 public class PCEPUpdateRequestMessageParser extends AbstractMessageParser {
45 public static final int TYPE = 11;
47 public PCEPUpdateRequestMessageParser(final ObjectHandlerRegistry registry) {
52 public void serializeMessage(final Message message, final ByteBuf buffer) {
53 if (!(message instanceof Pcupd)) {
54 throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance of " + message.getClass()
55 + ". Nedded PcupdMessage.");
57 final Pcupd msg = (Pcupd) message;
58 final List<Updates> updates = msg.getPcupdMessage().getUpdates();
59 for (final Updates update : updates) {
60 buffer.writeBytes(serializeObject(update.getSrp()));
61 buffer.writeBytes(serializeObject(update.getLsp()));
62 final Path p = update.getPath();
64 buffer.writeBytes(serializeObject(p.getEro()));
65 if (p.getLspa() != null) {
66 buffer.writeBytes(serializeObject(p.getLspa()));
68 if (p.getBandwidth() != null) {
69 buffer.writeBytes(serializeObject(p.getBandwidth()));
71 if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
72 for (final Metrics m : p.getMetrics()) {
73 buffer.writeBytes(serializeObject(m.getMetric()));
76 if (p.getIro() != null) {
77 buffer.writeBytes(serializeObject(p.getIro()));
84 public Message parseMessage(final byte[] buffer) throws PCEPDeserializerException {
85 if (buffer == null || buffer.length == 0) {
86 throw new PCEPDeserializerException("Pcup message cannot be empty.");
89 final List<Object> objs = parseObjects(buffer);
90 return validate(objs);
91 } catch (final PCEPDocumentedException e) {
92 return createErrorMsg(e.getError());
96 public Message validate(final List<Object> objects) throws PCEPDeserializerException, PCEPDocumentedException {
97 if (objects == null) {
98 throw new IllegalArgumentException("Passed list can't be null.");
100 final List<Updates> updateRequests = Lists.newArrayList();
102 while (!objects.isEmpty()) {
103 final Updates update = getValidUpdates(objects);
104 if (update != null) {
105 updateRequests.add(update);
108 if (!objects.isEmpty()) {
109 if (objects.get(0) instanceof UnknownObject) {
110 throw new PCEPDocumentedException("Unknown object encountered", ((UnknownObject) objects.get(0)).getError());
112 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
114 return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
117 private Updates getValidUpdates(final List<Object> objects) throws PCEPDocumentedException {
118 final UpdatesBuilder builder = new UpdatesBuilder();
119 if (objects.get(0) instanceof Srp) {
120 builder.setSrp((Srp) objects.get(0));
123 throw new PCEPDocumentedException("Srp object missing.", PCEPErrors.SRP_MISSING);
125 if (objects.get(0) instanceof Lsp) {
126 builder.setLsp((Lsp) objects.get(0));
129 throw new PCEPDocumentedException("Lsp object missing.", PCEPErrors.LSP_MISSING);
131 if (!objects.isEmpty()) {
132 final PathBuilder pBuilder = new PathBuilder();
133 if (objects.get(0) instanceof Ero) {
134 pBuilder.setEro((Ero) objects.get(0));
137 throw new PCEPDocumentedException("Ero object missing.", PCEPErrors.ERO_MISSING);
139 parsePath(objects, pBuilder);
140 builder.setPath(pBuilder.build());
142 return builder.build();
145 private void parsePath(final List<Object> objects, final PathBuilder pBuilder) throws PCEPDocumentedException {
146 final List<Metrics> pathMetrics = Lists.newArrayList();
148 State state = State.Init;
149 while (!objects.isEmpty() && !state.equals(State.End)) {
150 obj = objects.get(0);
153 state = State.LspaIn;
154 if (obj instanceof Lspa) {
155 pBuilder.setLspa((Lspa) obj);
159 state = State.BandwidthIn;
160 if (obj instanceof Bandwidth) {
161 pBuilder.setBandwidth((Bandwidth) obj);
165 state = State.MetricIn;
166 if (obj instanceof Metric) {
167 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
168 state = State.BandwidthIn;
173 if (obj instanceof Iro) {
174 pBuilder.setIro((Iro) obj);
183 if (obj instanceof UnknownObject) {
184 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
187 if (!state.equals(State.End)) {
191 if (!pathMetrics.isEmpty()) {
192 pBuilder.setMetrics(pathMetrics);
197 Init, LspaIn, BandwidthIn, MetricIn, IroIn, End
201 public int getMessageType() {