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