Code clean up
[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.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Lists;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.List;
16 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
17 import org.opendaylight.protocol.pcep.spi.MessageUtil;
18 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcupd;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PcupdBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.PcupdMessageBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.Updates;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.UpdatesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.updates.Path;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.updates.PathBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.Srp;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.Metrics;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.MetricsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
40
41 /**
42  * Parser for {@link Pcupd}
43  */
44 public 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         Preconditions.checkArgument(message instanceof Pcupd, "Wrong instance of Message. Passed instance of %s. Need Pcupd.", message.getClass());
55         final Pcupd msg = (Pcupd) message;
56         final List<Updates> updates = msg.getPcupdMessage().getUpdates();
57         final ByteBuf buffer = Unpooled.buffer();
58         for (final Updates update : updates) {
59             serializeUpdate(update, buffer);
60         }
61         MessageUtil.formatMessage(TYPE, buffer, out);
62     }
63
64     protected void serializeUpdate(final Updates update, final ByteBuf buffer) {
65         serializeObject(update.getSrp(), buffer);
66         serializeObject(update.getLsp(), buffer);
67         final Path p = update.getPath();
68         if (p != null) {
69             serializeObject(p.getEro(), buffer);
70             serializeObject(p.getLspa(), buffer);
71             serializeObject(p.getBandwidth(), buffer);
72             serializeObject(p.getReoptimizationBandwidth(), buffer);
73             if (p.getMetrics() != null) {
74                 for (final Metrics m : p.getMetrics()) {
75                     serializeObject(m.getMetric(), buffer);
76                 }
77             }
78             serializeObject(p.getIro(), buffer);
79         }
80     }
81
82     @Override
83     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
84         Preconditions.checkArgument(objects != null, "Passed list can't be null.");
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 upd = getValidUpdates(objects, errors);
93             if (upd != null) {
94                 updateRequests.add(upd);
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     protected Updates getValidUpdates(final List<Object> objects, final List<Message> errors) {
104         final UpdatesBuilder builder = new UpdatesBuilder();
105
106         Object object = objects.remove(0);
107         if (object instanceof Srp) {
108             builder.setSrp((Srp) object);
109             if (objects.isEmpty()) {
110                 object = null;
111             } else {
112                 object = objects.remove(0);
113             }
114         } else {
115             errors.add(createErrorMsg(PCEPErrors.SRP_MISSING, Optional.absent()));
116         }
117
118         if (validateLsp(object, errors, builder)) {
119             if (!objects.isEmpty()) {
120                 if (!validatePath(objects, errors, builder)) {
121                     return null;
122                 }
123             }
124
125             return builder.build();
126         }
127         return null;
128     }
129
130     private boolean validateLsp(final Object object, final List<Message> errors, final UpdatesBuilder builder) {
131         if (object instanceof Lsp) {
132             builder.setLsp((Lsp) object);
133         } else {
134             errors.add(createErrorMsg(PCEPErrors.LSP_MISSING, Optional.absent()));
135             return false;
136         }
137         return true;
138     }
139
140     private boolean validatePath(final List<Object> objects, final List<Message> errors, final UpdatesBuilder builder) {
141         final PathBuilder pBuilder = new PathBuilder();
142         Object object = objects.remove(0);
143         if (object instanceof Ero) {
144             pBuilder.setEro((Ero) object);
145         } else {
146             errors.add(createErrorMsg(PCEPErrors.ERO_MISSING, Optional.absent()));
147             return false;
148         }
149         parsePath(objects, pBuilder);
150         builder.setPath(pBuilder.build());
151         return true;
152     }
153
154     private void parsePath(final List<Object> objects, final PathBuilder pBuilder) {
155         final List<Metrics> pathMetrics = Lists.newArrayList();
156         Object obj;
157         State state = State.INIT;
158         while (!objects.isEmpty() && !state.equals(State.END)) {
159             obj = objects.get(0);
160             state = insertObject(state,obj, pBuilder, pathMetrics);
161             if (!state.equals(State.END)) {
162                 objects.remove(0);
163             }
164         }
165         if (!pathMetrics.isEmpty()) {
166             pBuilder.setMetrics(pathMetrics);
167         }
168     }
169
170     private State insertObject(final State state, final Object obj, final PathBuilder pBuilder, final List<Metrics> pathMetrics) {
171         switch (state) {
172         case INIT:
173             if (obj instanceof Lspa) {
174                 pBuilder.setLspa((Lspa) obj);
175                 return State.LSPA_IN;
176             }
177         case LSPA_IN:
178             if (obj instanceof Bandwidth) {
179                 pBuilder.setBandwidth((Bandwidth) obj);
180                 return State.LSPA_IN;
181             }
182             if (obj instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reoptimization.bandwidth.object.ReoptimizationBandwidth) {
183                 pBuilder.setReoptimizationBandwidth((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reoptimization.bandwidth.object.ReoptimizationBandwidth) obj);
184                 return State.LSPA_IN;
185             }
186         case BANDWIDTH_IN:
187             if (obj instanceof Metric) {
188                 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
189                 return State.BANDWIDTH_IN;
190             }
191         case METRIC_IN:
192             if (obj instanceof Iro) {
193                 pBuilder.setIro((Iro) obj);
194                 return State.IRO_IN;
195             }
196         case IRO_IN:
197         case END:
198             return State.END;
199         default:
200             return state;
201         }
202     }
203
204     private enum State {
205         INIT, LSPA_IN, BANDWIDTH_IN, METRIC_IN, IRO_IN, END
206     }
207 }