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.stateful;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.Queue;
18 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
20 import org.opendaylight.protocol.pcep.spi.MessageUtil;
21 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
22 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.Pcupd;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.PcupdBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.lsp.object.Lsp;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.pcupd.message.PcupdMessageBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.pcupd.message.pcupd.message.Updates;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.pcupd.message.pcupd.message.UpdatesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.pcupd.message.pcupd.message.updates.Path;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.pcupd.message.pcupd.message.updates.PathBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.srp.object.Srp;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.bandwidth.object.Bandwidth;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.Ero;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.include.route.object.Iro;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lsp.attributes.Metrics;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lsp.attributes.MetricsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lspa.object.Lspa;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.metric.object.Metric;
43 * Parser for {@link Pcupd}.
45 public class StatefulPCUpdateRequestMessageParser extends AbstractMessageParser {
47 public static final int TYPE = 11;
49 public StatefulPCUpdateRequestMessageParser(final ObjectRegistry registry) {
54 public void serializeMessage(final Message message, final ByteBuf out) {
55 checkArgument(message instanceof Pcupd, "Wrong instance of Message. Passed instance of %s. Need Pcupd.",
57 final Pcupd msg = (Pcupd) message;
58 final ByteBuf buffer = Unpooled.buffer();
59 for (final Updates update : msg.getPcupdMessage().nonnullUpdates()) {
60 serializeUpdate(update, buffer);
62 MessageUtil.formatMessage(TYPE, buffer, out);
65 protected void serializeUpdate(final Updates update, final ByteBuf buffer) {
66 serializeObject(update.getSrp(), buffer);
67 serializeObject(update.getLsp(), buffer);
68 final Path p = update.getPath();
70 serializeObject(p.getEro(), buffer);
71 serializeObject(p.getLspa(), buffer);
72 serializeObject(p.getBandwidth(), buffer);
73 serializeObject(p.getReoptimizationBandwidth(), buffer);
74 for (final Metrics m : p.nonnullMetrics()) {
75 serializeObject(m.getMetric(), buffer);
77 serializeObject(p.getIro(), buffer);
82 protected Message validate(final Queue<Object> objects, final List<Message> errors)
83 throws PCEPDeserializerException {
84 checkArgument(objects != null, "Passed list can't be null.");
85 if (objects.isEmpty()) {
86 throw new PCEPDeserializerException("Pcup message cannot be empty.");
89 final var updateRequests = new ArrayList<Updates>();
90 while (!objects.isEmpty()) {
91 final Updates upd = getValidUpdates(objects, errors);
93 updateRequests.add(upd);
96 if (!objects.isEmpty()) {
97 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
99 return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
102 protected Updates getValidUpdates(final Queue<Object> objects, final List<Message> errors) {
103 final UpdatesBuilder builder = new UpdatesBuilder();
105 Object object = objects.remove();
106 if (object instanceof Srp srp) {
108 object = objects.poll();
110 errors.add(createErrorMsg(PCEPErrors.SRP_MISSING, Optional.empty()));
113 if (validateLsp(object, errors, builder)) {
114 if (!objects.isEmpty()) {
115 if (!validatePath(objects, errors, builder)) {
120 return builder.build();
125 private static boolean validateLsp(final Object object, final List<Message> errors, final UpdatesBuilder builder) {
126 if (object instanceof Lsp lsp) {
129 errors.add(createErrorMsg(PCEPErrors.LSP_MISSING, Optional.empty()));
135 private static boolean validatePath(final Queue<Object> objects, final List<Message> errors,
136 final UpdatesBuilder builder) {
137 final PathBuilder pBuilder = new PathBuilder();
138 Object object = objects.remove();
139 if (object instanceof Ero ero) {
140 pBuilder.setEro(ero);
142 errors.add(createErrorMsg(PCEPErrors.ERO_MISSING, Optional.empty()));
145 parsePath(objects, pBuilder);
146 builder.setPath(pBuilder.build());
150 private static void parsePath(final Queue<Object> objects, final PathBuilder pathBuilder) {
151 final var pathMetrics = new ArrayList<Metrics>();
152 State state = State.INIT;
153 for (Object obj = objects.peek(); obj != null; obj = objects.peek()) {
154 state = insertObject(state, obj, pathBuilder, pathMetrics);
155 if (state == State.END) {
162 if (!pathMetrics.isEmpty()) {
163 pathBuilder.setMetrics(pathMetrics);
167 private static State insertObject(final State state, final Object obj, final PathBuilder pathBuilder,
168 final List<Metrics> pathMetrics) {
171 if (obj instanceof Lspa lspa) {
172 pathBuilder.setLspa(lspa);
173 return State.LSPA_IN;
177 if (obj instanceof Bandwidth bandwidth) {
178 pathBuilder.setBandwidth(bandwidth);
179 return State.LSPA_IN;
182 instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
183 .reoptimization.bandwidth.object.ReoptimizationBandwidth reoptBandwidth) {
184 pathBuilder.setReoptimizationBandwidth(reoptBandwidth);
185 return State.LSPA_IN;
189 if (obj instanceof Metric metric) {
190 pathMetrics.add(new MetricsBuilder().setMetric(metric).build());
191 return State.BANDWIDTH_IN;
195 if (obj instanceof Iro iro) {
196 pathBuilder.setIro(iro);
209 INIT, LSPA_IN, BANDWIDTH_IN, METRIC_IN, IRO_IN, END