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 protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException, PCEPDocumentedException {
85 if (objects == null) {
86 throw new IllegalArgumentException("Passed list can't be null.");
88 if (objects.isEmpty()) {
89 throw new PCEPDeserializerException("Pcup message cannot be empty.");
92 final List<Updates> updateRequests = Lists.newArrayList();
94 while (!objects.isEmpty()) {
95 final Updates update = getValidUpdates(objects);
97 updateRequests.add(update);
100 if (!objects.isEmpty()) {
101 if (objects.get(0) instanceof UnknownObject) {
102 throw new PCEPDocumentedException("Unknown object encountered", ((UnknownObject) objects.get(0)).getError());
104 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
106 return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
109 private Updates getValidUpdates(final List<Object> objects) throws PCEPDocumentedException {
110 final UpdatesBuilder builder = new UpdatesBuilder();
111 if (objects.get(0) instanceof Srp) {
112 builder.setSrp((Srp) objects.get(0));
115 throw new PCEPDocumentedException("Srp object missing.", PCEPErrors.SRP_MISSING);
117 if (objects.get(0) instanceof Lsp) {
118 builder.setLsp((Lsp) objects.get(0));
121 throw new PCEPDocumentedException("Lsp object missing.", PCEPErrors.LSP_MISSING);
123 if (!objects.isEmpty()) {
124 final PathBuilder pBuilder = new PathBuilder();
125 if (objects.get(0) instanceof Ero) {
126 pBuilder.setEro((Ero) objects.get(0));
129 throw new PCEPDocumentedException("Ero object missing.", PCEPErrors.ERO_MISSING);
131 parsePath(objects, pBuilder);
132 builder.setPath(pBuilder.build());
134 return builder.build();
137 private void parsePath(final List<Object> objects, final PathBuilder pBuilder) throws PCEPDocumentedException {
138 final List<Metrics> pathMetrics = Lists.newArrayList();
140 State state = State.Init;
141 while (!objects.isEmpty() && !state.equals(State.End)) {
142 obj = objects.get(0);
145 state = State.LspaIn;
146 if (obj instanceof Lspa) {
147 pBuilder.setLspa((Lspa) obj);
151 state = State.BandwidthIn;
152 if (obj instanceof Bandwidth) {
153 pBuilder.setBandwidth((Bandwidth) obj);
157 state = State.MetricIn;
158 if (obj instanceof Metric) {
159 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
160 state = State.BandwidthIn;
165 if (obj instanceof Iro) {
166 pBuilder.setIro((Iro) obj);
175 if (obj instanceof UnknownObject) {
176 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
179 if (!state.equals(State.End)) {
183 if (!pathMetrics.isEmpty()) {
184 pBuilder.setMetrics(pathMetrics);
189 Init, LspaIn, BandwidthIn, MetricIn, IroIn, End
193 public int getMessageType() {