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.ietf.stateful07;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
13 import java.util.List;
15 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
16 import org.opendaylight.protocol.pcep.spi.MessageUtil;
17 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcupd;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PcupdBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.PcupdMessageBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.Updates;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.UpdatesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.updates.Path;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.updates.PathBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.Srp;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.Metrics;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.MetricsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
39 import com.google.common.collect.Lists;
42 * Parser for {@link Pcupd}
44 public final class Stateful07PCUpdateRequestMessageParser extends AbstractMessageParser {
46 public static final int TYPE = 11;
48 public Stateful07PCUpdateRequestMessageParser(final ObjectRegistry registry) {
53 public void serializeMessage(final Message message, final ByteBuf out) {
54 if (!(message instanceof Pcupd)) {
55 throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance of " + message.getClass()
56 + ". Nedded PcupdMessage.");
58 final Pcupd msg = (Pcupd) message;
59 final List<Updates> updates = msg.getPcupdMessage().getUpdates();
60 ByteBuf buffer = Unpooled.buffer();
61 for (final Updates update : updates) {
62 buffer.writeBytes(serializeObject(update.getSrp()));
63 buffer.writeBytes(serializeObject(update.getLsp()));
64 final Path p = update.getPath();
66 buffer.writeBytes(serializeObject(p.getEro()));
67 if (p.getLspa() != null) {
68 buffer.writeBytes(serializeObject(p.getLspa()));
70 if (p.getBandwidth() != null) {
71 buffer.writeBytes(serializeObject(p.getBandwidth()));
73 if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
74 for (final Metrics m : p.getMetrics()) {
75 buffer.writeBytes(serializeObject(m.getMetric()));
78 if (p.getIro() != null) {
79 buffer.writeBytes(serializeObject(p.getIro()));
83 MessageUtil.formatMessage(TYPE, buffer, out);
87 protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
88 if (objects == null) {
89 throw new IllegalArgumentException("Passed list can't be null.");
91 if (objects.isEmpty()) {
92 throw new PCEPDeserializerException("Pcup message cannot be empty.");
95 final List<Updates> updateRequests = Lists.newArrayList();
97 while (!objects.isEmpty()) {
98 final Updates update = getValidUpdates(objects, errors);
100 updateRequests.add(update);
103 if (!objects.isEmpty()) {
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, final List<Message> errors) {
110 final UpdatesBuilder builder = new UpdatesBuilder();
111 if (objects.get(0) instanceof Srp) {
112 builder.setSrp((Srp) objects.get(0));
115 errors.add(createErrorMsg(PCEPErrors.SRP_MISSING));
118 if (objects.get(0) instanceof Lsp) {
119 builder.setLsp((Lsp) objects.get(0));
122 errors.add(createErrorMsg(PCEPErrors.LSP_MISSING));
125 if (!objects.isEmpty()) {
126 final PathBuilder pBuilder = new PathBuilder();
127 if (objects.get(0) instanceof Ero) {
128 pBuilder.setEro((Ero) objects.get(0));
131 errors.add(createErrorMsg(PCEPErrors.ERO_MISSING));
134 parsePath(objects, pBuilder);
135 builder.setPath(pBuilder.build());
137 return builder.build();
140 private void parsePath(final List<Object> objects, final PathBuilder pBuilder) {
141 final List<Metrics> pathMetrics = Lists.newArrayList();
143 State state = State.Init;
144 while (!objects.isEmpty() && !state.equals(State.End)) {
145 obj = objects.get(0);
148 state = State.LspaIn;
149 if (obj instanceof Lspa) {
150 pBuilder.setLspa((Lspa) obj);
154 state = State.BandwidthIn;
155 if (obj instanceof Bandwidth) {
156 pBuilder.setBandwidth((Bandwidth) obj);
160 state = State.MetricIn;
161 if (obj instanceof Metric) {
162 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
163 state = State.BandwidthIn;
168 if (obj instanceof Iro) {
169 pBuilder.setIro((Iro) obj);
178 if (!state.equals(State.End)) {
182 if (!pathMetrics.isEmpty()) {
183 pBuilder.setMetrics(pathMetrics);
188 Init, LspaIn, BandwidthIn, MetricIn, IroIn, End
192 public int getMessageType() {