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;
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.crabbe.stateful._02.rev140110.Pcupd;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PcupdBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.Lsp;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.PcupdMessageBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.Updates;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.UpdatesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.updates.Path;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.updates.PathBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.Metrics;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.MetricsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
38 import com.google.common.collect.Lists;
41 * Parser for {@link Pcupd}
43 public final class Stateful02PCUpdateRequestMessageParser extends AbstractMessageParser {
45 public static final int TYPE = 11;
47 public Stateful02PCUpdateRequestMessageParser(final ObjectRegistry registry) {
52 public void serializeMessage(final Message message, final ByteBuf out) {
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 ByteBuf buffer = Unpooled.buffer();
60 for (final Updates update : updates) {
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()));
81 MessageUtil.formatMessage(TYPE, buffer, out);
85 protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
86 if (objects == null) {
87 throw new IllegalArgumentException("Passed list can't be null.");
89 if (objects.isEmpty()) {
90 throw new PCEPDeserializerException("Pcup message cannot be empty.");
93 final List<Updates> updateRequests = Lists.newArrayList();
95 while (!objects.isEmpty()) {
96 final Updates update = getValidUpdates(objects, errors);
98 updateRequests.add(update);
101 if (!objects.isEmpty()) {
102 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
104 return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
107 private Updates getValidUpdates(final List<Object> objects, final List<Message> errors) {
108 final UpdatesBuilder builder = new UpdatesBuilder();
109 if (objects.get(0) instanceof Lsp) {
110 builder.setLsp((Lsp) objects.get(0));
113 errors.add(createErrorMsg(PCEPErrors.LSP_MISSING));
116 if (!objects.isEmpty()) {
117 final PathBuilder pBuilder = new PathBuilder();
118 if (objects.get(0) instanceof Ero) {
119 pBuilder.setEro((Ero) objects.get(0));
122 errors.add(createErrorMsg(PCEPErrors.ERO_MISSING));
125 parsePath(objects, pBuilder);
126 builder.setPath(pBuilder.build());
128 return builder.build();
131 private void parsePath(final List<Object> objects, final PathBuilder pBuilder) {
132 final List<Metrics> pathMetrics = Lists.newArrayList();
134 State state = State.Init;
135 while (!objects.isEmpty() && !state.equals(State.End)) {
136 obj = objects.get(0);
139 state = State.LspaIn;
140 if (obj instanceof Lspa) {
141 pBuilder.setLspa((Lspa) obj);
145 state = State.BandwidthIn;
146 if (obj instanceof Bandwidth) {
147 pBuilder.setBandwidth((Bandwidth) obj);
151 state = State.MetricIn;
152 if (obj instanceof Metric) {
153 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
154 state = State.BandwidthIn;
159 if (obj instanceof Iro) {
160 pBuilder.setIro((Iro) obj);
169 if (!state.equals(State.End)) {
173 if (!pathMetrics.isEmpty()) {
174 pBuilder.setMetrics(pathMetrics);
179 Init, LspaIn, BandwidthIn, MetricIn, IroIn, End
183 public int getMessageType() {