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