Merge "Fixed stateful07 message parsers/serializers"
[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 com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
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 /**
40  * Parser for {@link Pcupd}
41  */
42 public class Stateful07PCUpdateRequestMessageParser extends AbstractMessageParser {
43
44     public static final int TYPE = 11;
45
46     public Stateful07PCUpdateRequestMessageParser(final ObjectRegistry registry) {
47         super(registry);
48     }
49
50     @Override
51     public void serializeMessage(final Message message, final ByteBuf out) {
52         Preconditions.checkArgument(message instanceof Pcupd, "Wrong instance of Message. Passed instance of %s. Need Pcupd.", message.getClass());
53         final Pcupd msg = (Pcupd) message;
54         final List<Updates> updates = msg.getPcupdMessage().getUpdates();
55         ByteBuf buffer = Unpooled.buffer();
56         for (final Updates update : updates) {
57             serializeUpdate(update, buffer);
58         }
59         MessageUtil.formatMessage(TYPE, buffer, out);
60     }
61
62     protected void serializeUpdate(final Updates update, final ByteBuf buffer) {
63         serializeObject(update.getSrp(), buffer);
64         serializeObject(update.getLsp(), buffer);
65         final Path p = update.getPath();
66         if (p != null) {
67             serializeObject(p.getEro(), buffer);
68             if (p.getLspa() != null) {
69                 serializeObject(p.getLspa(), buffer);
70             }
71             if (p.getBandwidth() != null) {
72                 serializeObject(p.getBandwidth(), buffer);
73             }
74             if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
75                 for (final Metrics m : p.getMetrics()) {
76                     serializeObject(m.getMetric(), buffer);
77                 }
78             }
79             if (p.getIro() != null) {
80                 serializeObject(p.getIro(), buffer);
81             }
82         }
83     }
84
85     @Override
86     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
87         if (objects == null) {
88             throw new IllegalArgumentException("Passed list can't be null.");
89         }
90         if (objects.isEmpty()) {
91             throw new PCEPDeserializerException("Pcup message cannot be empty.");
92         }
93
94         final List<Updates> updateRequests = Lists.newArrayList();
95
96         while (!objects.isEmpty()) {
97             final Updates upd = getValidUpdates(objects, errors);
98             if(upd != null) {
99                 updateRequests.add(upd);
100             }
101         }
102         if (!objects.isEmpty()) {
103             throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
104         }
105         return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
106     }
107
108     protected Updates getValidUpdates(final List<Object> objects, final List<Message> errors) {
109         boolean isValid = true;
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             isValid = false;
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             isValid = false;
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                 isValid = false;
133             }
134             parsePath(objects, pBuilder);
135             builder.setPath(pBuilder.build());
136         }
137         if(isValid) {
138             return builder.build();
139         }
140         return null;
141     }
142
143     private void parsePath(final List<Object> objects, final PathBuilder pBuilder) {
144         final List<Metrics> pathMetrics = Lists.newArrayList();
145         Object obj;
146         State state = State.Init;
147         while (!objects.isEmpty() && !state.equals(State.End)) {
148             obj = objects.get(0);
149             switch (state) {
150             case Init:
151                 state = State.LspaIn;
152                 if (obj instanceof Lspa) {
153                     pBuilder.setLspa((Lspa) obj);
154                     break;
155                 }
156             case LspaIn:
157                 state = State.BandwidthIn;
158                 if (obj instanceof Bandwidth) {
159                     pBuilder.setBandwidth((Bandwidth) obj);
160                     break;
161                 }
162             case BandwidthIn:
163                 state = State.MetricIn;
164                 if (obj instanceof Metric) {
165                     pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
166                     state = State.BandwidthIn;
167                     break;
168                 }
169             case MetricIn:
170                 state = State.IroIn;
171                 if (obj instanceof Iro) {
172                     pBuilder.setIro((Iro) obj);
173                     break;
174                 }
175             case IroIn:
176                 state = State.End;
177                 break;
178             case End:
179                 break;
180             }
181             if (!state.equals(State.End)) {
182                 objects.remove(0);
183             }
184         }
185         if (!pathMetrics.isEmpty()) {
186             pBuilder.setMetrics(pathMetrics);
187         }
188     }
189
190     private enum State {
191         Init, LspaIn, BandwidthIn, MetricIn, IroIn, End
192     }
193 }