2 * Copyright (c) 2014 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.ietf.stateful02;
10 import io.netty.buffer.ByteBuf;
12 import java.util.List;
14 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
15 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Pcupd;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PcupdBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.Lsp;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.PcupdMessageBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.Updates;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.UpdatesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.updates.Path;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.updates.PathBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.Metrics;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.MetricsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
36 import com.google.common.collect.Lists;
39 * Parser for {@link Pcupd}
41 public final class Stateful02PCUpdateRequestMessageParser extends AbstractMessageParser {
43 public static final int TYPE = 11;
45 public Stateful02PCUpdateRequestMessageParser(final ObjectHandlerRegistry registry) {
50 public void serializeMessage(final Message message, final ByteBuf buffer) {
51 if (!(message instanceof Pcupd)) {
52 throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance of " + message.getClass()
53 + ". Nedded PcupdMessage.");
55 final Pcupd msg = (Pcupd) message;
56 final List<Updates> updates = msg.getPcupdMessage().getUpdates();
57 for (final Updates update : updates) {
58 buffer.writeBytes(serializeObject(update.getLsp()));
59 final Path p = update.getPath();
61 buffer.writeBytes(serializeObject(p.getEro()));
62 if (p.getLspa() != null) {
63 buffer.writeBytes(serializeObject(p.getLspa()));
65 if (p.getBandwidth() != null) {
66 buffer.writeBytes(serializeObject(p.getBandwidth()));
68 if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
69 for (final Metrics m : p.getMetrics()) {
70 buffer.writeBytes(serializeObject(m.getMetric()));
73 if (p.getIro() != null) {
74 buffer.writeBytes(serializeObject(p.getIro()));
81 protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
82 if (objects == null) {
83 throw new IllegalArgumentException("Passed list can't be null.");
85 if (objects.isEmpty()) {
86 throw new PCEPDeserializerException("Pcup message cannot be empty.");
89 final List<Updates> updateRequests = Lists.newArrayList();
91 while (!objects.isEmpty()) {
92 final Updates update = getValidUpdates(objects, errors);
94 updateRequests.add(update);
97 if (!objects.isEmpty()) {
98 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
100 return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
103 private Updates getValidUpdates(final List<Object> objects, final List<Message> errors) {
104 final UpdatesBuilder builder = new UpdatesBuilder();
105 if (objects.get(0) instanceof Lsp) {
106 builder.setLsp((Lsp) objects.get(0));
109 errors.add(createErrorMsg(PCEPErrors.LSP_MISSING));
112 if (!objects.isEmpty()) {
113 final PathBuilder pBuilder = new PathBuilder();
114 if (objects.get(0) instanceof Ero) {
115 pBuilder.setEro((Ero) objects.get(0));
118 errors.add(createErrorMsg(PCEPErrors.ERO_MISSING));
121 parsePath(objects, pBuilder);
122 builder.setPath(pBuilder.build());
124 return builder.build();
127 private void parsePath(final List<Object> objects, final PathBuilder pBuilder) {
128 final List<Metrics> pathMetrics = Lists.newArrayList();
130 State state = State.Init;
131 while (!objects.isEmpty() && !state.equals(State.End)) {
132 obj = objects.get(0);
135 state = State.LspaIn;
136 if (obj instanceof Lspa) {
137 pBuilder.setLspa((Lspa) obj);
141 state = State.BandwidthIn;
142 if (obj instanceof Bandwidth) {
143 pBuilder.setBandwidth((Bandwidth) obj);
147 state = State.MetricIn;
148 if (obj instanceof Metric) {
149 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
150 state = State.BandwidthIn;
155 if (obj instanceof Iro) {
156 pBuilder.setIro((Iro) obj);
165 if (!state.equals(State.End)) {
169 if (!pathMetrics.isEmpty()) {
170 pBuilder.setMetrics(pathMetrics);
175 Init, LspaIn, BandwidthIn, MetricIn, IroIn, End
179 public int getMessageType() {