Rework parser infrastructure to support partial message processing
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPUpdateRequestMessageParser.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.impl.message;
9
10 import io.netty.buffer.ByteBuf;
11
12 import java.util.List;
13
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
16 import org.opendaylight.protocol.pcep.PCEPErrors;
17 import org.opendaylight.protocol.pcep.UnknownObject;
18 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcupd;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcupdBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.Metrics;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.MetricsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.Lsp;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.PcupdMessageBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.Updates;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.UpdatesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.updates.Path;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.updates.PathBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.srp.object.Srp;
37
38 import com.google.common.collect.Lists;
39
40 /**
41  * Parser for {@link Pcupd}
42  */
43 public class PCEPUpdateRequestMessageParser extends AbstractMessageParser {
44
45         public static final int TYPE = 11;
46
47         public PCEPUpdateRequestMessageParser(final ObjectHandlerRegistry registry) {
48                 super(registry);
49         }
50
51         @Override
52         public void serializeMessage(final Message message, final ByteBuf buffer) {
53                 if (!(message instanceof Pcupd)) {
54                         throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance of " + message.getClass()
55                                         + ". Nedded PcupdMessage.");
56                 }
57                 final Pcupd msg = (Pcupd) message;
58                 final List<Updates> updates = msg.getPcupdMessage().getUpdates();
59                 for (final Updates update : updates) {
60                         buffer.writeBytes(serializeObject(update.getSrp()));
61                         buffer.writeBytes(serializeObject(update.getLsp()));
62                         final Path p = update.getPath();
63                         if (p != null) {
64                                 buffer.writeBytes(serializeObject(p.getEro()));
65                                 if (p.getLspa() != null) {
66                                         buffer.writeBytes(serializeObject(p.getLspa()));
67                                 }
68                                 if (p.getBandwidth() != null) {
69                                         buffer.writeBytes(serializeObject(p.getBandwidth()));
70                                 }
71                                 if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
72                                         for (final Metrics m : p.getMetrics()) {
73                                                 buffer.writeBytes(serializeObject(m.getMetric()));
74                                         }
75                                 }
76                                 if (p.getIro() != null) {
77                                         buffer.writeBytes(serializeObject(p.getIro()));
78                                 }
79                         }
80                 }
81         }
82
83         @Override
84         protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException, PCEPDocumentedException {
85                 if (objects == null) {
86                         throw new IllegalArgumentException("Passed list can't be null.");
87                 }
88                 if (objects.isEmpty()) {
89                         throw new PCEPDeserializerException("Pcup message cannot be empty.");
90                 }
91
92                 final List<Updates> updateRequests = Lists.newArrayList();
93
94                 while (!objects.isEmpty()) {
95                         final Updates update = getValidUpdates(objects);
96                         if (update != null) {
97                                 updateRequests.add(update);
98                         }
99                 }
100                 if (!objects.isEmpty()) {
101                         if (objects.get(0) instanceof UnknownObject) {
102                                 throw new PCEPDocumentedException("Unknown object encountered", ((UnknownObject) objects.get(0)).getError());
103                         }
104                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
105                 }
106                 return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
107         }
108
109         private Updates getValidUpdates(final List<Object> objects) throws PCEPDocumentedException {
110                 final UpdatesBuilder builder = new UpdatesBuilder();
111                 if (objects.get(0) instanceof Srp) {
112                         builder.setSrp((Srp) objects.get(0));
113                         objects.remove(0);
114                 } else {
115                         throw new PCEPDocumentedException("Srp object missing.", PCEPErrors.SRP_MISSING);
116                 }
117                 if (objects.get(0) instanceof Lsp) {
118                         builder.setLsp((Lsp) objects.get(0));
119                         objects.remove(0);
120                 } else {
121                         throw new PCEPDocumentedException("Lsp object missing.", PCEPErrors.LSP_MISSING);
122                 }
123                 if (!objects.isEmpty()) {
124                         final PathBuilder pBuilder = new PathBuilder();
125                         if (objects.get(0) instanceof Ero) {
126                                 pBuilder.setEro((Ero) objects.get(0));
127                                 objects.remove(0);
128                         } else {
129                                 throw new PCEPDocumentedException("Ero object missing.", PCEPErrors.ERO_MISSING);
130                         }
131                         parsePath(objects, pBuilder);
132                         builder.setPath(pBuilder.build());
133                 }
134                 return builder.build();
135         }
136
137         private void parsePath(final List<Object> objects, final PathBuilder pBuilder) throws PCEPDocumentedException {
138                 final List<Metrics> pathMetrics = Lists.newArrayList();
139                 Object obj;
140                 State state = State.Init;
141                 while (!objects.isEmpty() && !state.equals(State.End)) {
142                         obj = objects.get(0);
143                         switch (state) {
144                         case Init:
145                                 state = State.LspaIn;
146                                 if (obj instanceof Lspa) {
147                                         pBuilder.setLspa((Lspa) obj);
148                                         break;
149                                 }
150                         case LspaIn:
151                                 state = State.BandwidthIn;
152                                 if (obj instanceof Bandwidth) {
153                                         pBuilder.setBandwidth((Bandwidth) obj);
154                                         break;
155                                 }
156                         case BandwidthIn:
157                                 state = State.MetricIn;
158                                 if (obj instanceof Metric) {
159                                         pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
160                                         state = State.BandwidthIn;
161                                         break;
162                                 }
163                         case MetricIn:
164                                 state = State.IroIn;
165                                 if (obj instanceof Iro) {
166                                         pBuilder.setIro((Iro) obj);
167                                         break;
168                                 }
169                         case IroIn:
170                                 state = State.End;
171                                 break;
172                         case End:
173                                 break;
174                         default:
175                                 if (obj instanceof UnknownObject) {
176                                         throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
177                                 }
178                         }
179                         if (!state.equals(State.End)) {
180                                 objects.remove(0);
181                         }
182                 }
183                 if (!pathMetrics.isEmpty()) {
184                         pBuilder.setMetrics(pathMetrics);
185                 }
186         }
187
188         private enum State {
189                 Init, LspaIn, BandwidthIn, MetricIn, IroIn, End
190         }
191
192         @Override
193         public int getMessageType() {
194                 return TYPE;
195         }
196 }