Fixed some java8 non-compliant javadocs.
[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 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.crabbe.stateful._02.rev140110.Pcupd;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PcupdBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.Lsp;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.PcupdMessageBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.Updates;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.UpdatesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.updates.Path;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.pcupd.message.pcupd.message.updates.PathBuilder;
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 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
39
40 /**
41  * Parser for {@link Pcupd}
42  */
43 @Deprecated
44 public final class Stateful02PCUpdateRequestMessageParser extends AbstractMessageParser {
45
46     public static final int TYPE = 11;
47
48     public Stateful02PCUpdateRequestMessageParser(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             serializeObject(update.getLsp(), buffer);
60             final Path p = update.getPath();
61             if (p != null) {
62                 serializeObject(p.getEro(), buffer);
63                 serializeObject(p.getLspa(), buffer);
64                 serializeObject(p.getBandwidth(), buffer);
65                 if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
66                     for (final Metrics m : p.getMetrics()) {
67                         serializeObject(m.getMetric(), buffer);
68                     }
69                 }
70                 serializeObject(p.getIro(), buffer);
71             }
72         }
73         MessageUtil.formatMessage(TYPE, buffer, out);
74     }
75
76     @Override
77     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
78         Preconditions.checkArgument(objects != null, "Passed list can't be null.");
79         if (objects.isEmpty()) {
80             throw new PCEPDeserializerException("Pcup message cannot be empty.");
81         }
82
83         final List<Updates> updateRequests = Lists.newArrayList();
84
85         while (!objects.isEmpty()) {
86             final Updates update = getValidUpdates(objects, errors);
87             if (update != null) {
88                 updateRequests.add(update);
89             }
90         }
91         if (!objects.isEmpty()) {
92             throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
93         }
94         return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
95     }
96
97     private Updates getValidUpdates(final List<Object> objects, final List<Message> errors) {
98         final UpdatesBuilder builder = new UpdatesBuilder();
99         if (objects.get(0) instanceof Lsp) {
100             builder.setLsp((Lsp) objects.get(0));
101             objects.remove(0);
102         } else {
103             errors.add(createErrorMsg(PCEPErrors.LSP_MISSING, Optional.<Rp>absent()));
104             return null;
105         }
106         if (!objects.isEmpty()) {
107             final PathBuilder pBuilder = new PathBuilder();
108             if (objects.get(0) instanceof Ero) {
109                 pBuilder.setEro((Ero) objects.get(0));
110                 objects.remove(0);
111             } else {
112                 errors.add(createErrorMsg(PCEPErrors.ERO_MISSING, Optional.<Rp>absent()));
113                 return null;
114             }
115             parsePath(objects, pBuilder);
116             builder.setPath(pBuilder.build());
117         }
118         return builder.build();
119     }
120
121     private void parsePath(final List<Object> objects, final PathBuilder pBuilder) {
122         final List<Metrics> pathMetrics = Lists.newArrayList();
123         Object obj;
124         State state = State.Init;
125         while (!objects.isEmpty() && !state.equals(State.End)) {
126             obj = objects.get(0);
127             switch (state) {
128             case Init:
129                 state = State.LspaIn;
130                 if (obj instanceof Lspa) {
131                     pBuilder.setLspa((Lspa) obj);
132                     break;
133                 }
134             case LspaIn:
135                 state = State.BandwidthIn;
136                 if (obj instanceof Bandwidth) {
137                     pBuilder.setBandwidth((Bandwidth) obj);
138                     break;
139                 }
140             case BandwidthIn:
141                 state = State.MetricIn;
142                 if (obj instanceof Metric) {
143                     pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
144                     state = State.BandwidthIn;
145                     break;
146                 }
147             case MetricIn:
148                 state = State.IroIn;
149                 if (obj instanceof Iro) {
150                     pBuilder.setIro((Iro) obj);
151                     break;
152                 }
153             case IroIn:
154                 state = State.End;
155                 break;
156             case End:
157                 break;
158             }
159             if (!state.equals(State.End)) {
160                 objects.remove(0);
161             }
162         }
163         if (!pathMetrics.isEmpty()) {
164             pBuilder.setMetrics(pathMetrics);
165         }
166     }
167
168     private enum State {
169         Init, LspaIn, BandwidthIn, MetricIn, IroIn, End
170     }
171 }