32c617792dcb0b721d4b5a766a626c79dec5ce83
[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         public Message parseMessage(final byte[] buffer) throws PCEPDeserializerException {
85                 if (buffer == null || buffer.length == 0) {
86                         throw new PCEPDeserializerException("Pcup message cannot be empty.");
87                 }
88                 try {
89                         final List<Object> objs = parseObjects(buffer);
90                         return validate(objs);
91                 } catch (final PCEPDocumentedException e) {
92                         return createErrorMsg(e.getError());
93                 }
94         }
95
96         public Message validate(final List<Object> objects) throws PCEPDeserializerException, PCEPDocumentedException {
97                 if (objects == null) {
98                         throw new IllegalArgumentException("Passed list can't be null.");
99                 }
100                 final List<Updates> updateRequests = Lists.newArrayList();
101
102                 while (!objects.isEmpty()) {
103                         final Updates update = getValidUpdates(objects);
104                         if (update != null) {
105                                 updateRequests.add(update);
106                         }
107                 }
108                 if (!objects.isEmpty()) {
109                         if (objects.get(0) instanceof UnknownObject) {
110                                 throw new PCEPDocumentedException("Unknown object encountered", ((UnknownObject) objects.get(0)).getError());
111                         }
112                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
113                 }
114                 return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
115         }
116
117         private Updates getValidUpdates(final List<Object> objects) throws PCEPDocumentedException {
118                 final UpdatesBuilder builder = new UpdatesBuilder();
119                 if (objects.get(0) instanceof Srp) {
120                         builder.setSrp((Srp) objects.get(0));
121                         objects.remove(0);
122                 } else {
123                         throw new PCEPDocumentedException("Srp object missing.", PCEPErrors.SRP_MISSING);
124                 }
125                 if (objects.get(0) instanceof Lsp) {
126                         builder.setLsp((Lsp) objects.get(0));
127                         objects.remove(0);
128                 } else {
129                         throw new PCEPDocumentedException("Lsp object missing.", PCEPErrors.LSP_MISSING);
130                 }
131                 if (!objects.isEmpty()) {
132                         final PathBuilder pBuilder = new PathBuilder();
133                         if (objects.get(0) instanceof Ero) {
134                                 pBuilder.setEro((Ero) objects.get(0));
135                                 objects.remove(0);
136                         } else {
137                                 throw new PCEPDocumentedException("Ero object missing.", PCEPErrors.ERO_MISSING);
138                         }
139                         parsePath(objects, pBuilder);
140                         builder.setPath(pBuilder.build());
141                 }
142                 return builder.build();
143         }
144
145         private void parsePath(final List<Object> objects, final PathBuilder pBuilder) throws PCEPDocumentedException {
146                 final List<Metrics> pathMetrics = Lists.newArrayList();
147                 Object obj;
148                 State state = State.Init;
149                 while (!objects.isEmpty() && !state.equals(State.End)) {
150                         obj = objects.get(0);
151                         switch (state) {
152                         case Init:
153                                 state = State.LspaIn;
154                                 if (obj instanceof Lspa) {
155                                         pBuilder.setLspa((Lspa) obj);
156                                         break;
157                                 }
158                         case LspaIn:
159                                 state = State.BandwidthIn;
160                                 if (obj instanceof Bandwidth) {
161                                         pBuilder.setBandwidth((Bandwidth) obj);
162                                         break;
163                                 }
164                         case BandwidthIn:
165                                 state = State.MetricIn;
166                                 if (obj instanceof Metric) {
167                                         pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
168                                         state = State.BandwidthIn;
169                                         break;
170                                 }
171                         case MetricIn:
172                                 state = State.IroIn;
173                                 if (obj instanceof Iro) {
174                                         pBuilder.setIro((Iro) obj);
175                                         break;
176                                 }
177                         case IroIn:
178                                 state = State.End;
179                                 break;
180                         case End:
181                                 break;
182                         default:
183                                 if (obj instanceof UnknownObject) {
184                                         throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
185                                 }
186                         }
187                         if (!state.equals(State.End)) {
188                                 objects.remove(0);
189                         }
190                 }
191                 if (!pathMetrics.isEmpty()) {
192                         pBuilder.setMetrics(pathMetrics);
193                 }
194         }
195
196         private enum State {
197                 Init, LspaIn, BandwidthIn, MetricIn, IroIn, End
198         }
199
200         @Override
201         public int getMessageType() {
202                 return TYPE;
203         }
204 }