Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / ietf-stateful / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful / StatefulPCUpdateRequestMessageParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.ietf.stateful;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
41
42 /**
43  * Parser for {@link Pcupd}.
44  */
45 public class StatefulPCUpdateRequestMessageParser extends AbstractMessageParser {
46
47     public static final int TYPE = 11;
48
49     public StatefulPCUpdateRequestMessageParser(final ObjectRegistry registry) {
50         super(registry);
51     }
52
53     @Override
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.",
56             message.getClass());
57         final Pcupd msg = (Pcupd) message;
58         final ByteBuf buffer = Unpooled.buffer();
59         for (final Updates update : msg.getPcupdMessage().nonnullUpdates()) {
60             serializeUpdate(update, buffer);
61         }
62         MessageUtil.formatMessage(TYPE, buffer, out);
63     }
64
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();
69         if (p != null) {
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);
76             }
77             serializeObject(p.getIro(), buffer);
78         }
79     }
80
81     @Override
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.");
87         }
88
89         final var updateRequests = new ArrayList<Updates>();
90         while (!objects.isEmpty()) {
91             final Updates upd = getValidUpdates(objects, errors);
92             if (upd != null) {
93                 updateRequests.add(upd);
94             }
95         }
96         if (!objects.isEmpty()) {
97             throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
98         }
99         return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
100     }
101
102     protected Updates getValidUpdates(final Queue<Object> objects, final List<Message> errors) {
103         final UpdatesBuilder builder = new UpdatesBuilder();
104
105         Object object = objects.remove();
106         if (object instanceof Srp srp) {
107             builder.setSrp(srp);
108             object = objects.poll();
109         } else {
110             errors.add(createErrorMsg(PCEPErrors.SRP_MISSING, Optional.empty()));
111         }
112
113         if (validateLsp(object, errors, builder)) {
114             if (!objects.isEmpty()) {
115                 if (!validatePath(objects, errors, builder)) {
116                     return null;
117                 }
118             }
119
120             return builder.build();
121         }
122         return null;
123     }
124
125     private static boolean validateLsp(final Object object, final List<Message> errors, final UpdatesBuilder builder) {
126         if (object instanceof Lsp lsp) {
127             builder.setLsp(lsp);
128         } else {
129             errors.add(createErrorMsg(PCEPErrors.LSP_MISSING, Optional.empty()));
130             return false;
131         }
132         return true;
133     }
134
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);
141         } else {
142             errors.add(createErrorMsg(PCEPErrors.ERO_MISSING, Optional.empty()));
143             return false;
144         }
145         parsePath(objects, pBuilder);
146         builder.setPath(pBuilder.build());
147         return true;
148     }
149
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) {
156                 break;
157             }
158
159             objects.remove();
160         }
161
162         if (!pathMetrics.isEmpty()) {
163             pathBuilder.setMetrics(pathMetrics);
164         }
165     }
166
167     private static State insertObject(final State state, final Object obj, final PathBuilder pathBuilder,
168             final List<Metrics> pathMetrics) {
169         switch (state) {
170             case INIT:
171                 if (obj instanceof Lspa lspa) {
172                     pathBuilder.setLspa(lspa);
173                     return State.LSPA_IN;
174                 }
175                 // fall through
176             case LSPA_IN:
177                 if (obj instanceof Bandwidth bandwidth) {
178                     pathBuilder.setBandwidth(bandwidth);
179                     return State.LSPA_IN;
180                 }
181                 if (obj
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;
186                 }
187                 // fall through
188             case BANDWIDTH_IN:
189                 if (obj instanceof Metric metric) {
190                     pathMetrics.add(new MetricsBuilder().setMetric(metric).build());
191                     return State.BANDWIDTH_IN;
192                 }
193                 // fall through
194             case METRIC_IN:
195                 if (obj instanceof Iro iro) {
196                     pathBuilder.setIro(iro);
197                     return State.IRO_IN;
198                 }
199                 // fall through
200             case IRO_IN:
201             case END:
202                 return State.END;
203             default:
204                 return state;
205         }
206     }
207
208     private enum State {
209         INIT, LSPA_IN, BANDWIDTH_IN, METRIC_IN, IRO_IN, END
210     }
211 }