Add new revision for pcep types model
[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 java.util.Optional;
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.rev181109.Pcupd;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.PcupdBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp.object.Lsp;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcupd.message.PcupdMessageBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcupd.message.pcupd.message.Updates;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcupd.message.pcupd.message.UpdatesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcupd.message.pcupd.message.updates.Path;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcupd.message.pcupd.message.updates.PathBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.srp.object.Srp;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.bandwidth.object.Bandwidth;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.Ero;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.include.route.object.Iro;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lsp.attributes.Metrics;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lsp.attributes.MetricsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lspa.object.Lspa;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.metric.object.Metric;
39
40 /**
41  * Parser for {@link Pcupd}
42  */
43 public class Stateful07PCUpdateRequestMessageParser extends AbstractMessageParser {
44
45     public static final int TYPE = 11;
46
47     public Stateful07PCUpdateRequestMessageParser(final ObjectRegistry registry) {
48         super(registry);
49     }
50
51     @Override
52     public void serializeMessage(final Message message, final ByteBuf out) {
53         Preconditions.checkArgument(message instanceof Pcupd, "Wrong instance of Message. Passed instance of %s. Need Pcupd.", message.getClass());
54         final Pcupd msg = (Pcupd) message;
55         final List<Updates> updates = msg.getPcupdMessage().getUpdates();
56         final ByteBuf buffer = Unpooled.buffer();
57         for (final Updates update : updates) {
58             serializeUpdate(update, buffer);
59         }
60         MessageUtil.formatMessage(TYPE, buffer, out);
61     }
62
63     protected void serializeUpdate(final Updates update, final ByteBuf buffer) {
64         serializeObject(update.getSrp(), buffer);
65         serializeObject(update.getLsp(), buffer);
66         final Path p = update.getPath();
67         if (p != null) {
68             serializeObject(p.getEro(), buffer);
69             serializeObject(p.getLspa(), buffer);
70             serializeObject(p.getBandwidth(), buffer);
71             serializeObject(p.getReoptimizationBandwidth(), buffer);
72             if (p.getMetrics() != null) {
73                 for (final Metrics m : p.getMetrics()) {
74                     serializeObject(m.getMetric(), buffer);
75                 }
76             }
77             serializeObject(p.getIro(), buffer);
78         }
79     }
80
81     @Override
82     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
83         Preconditions.checkArgument(objects != null, "Passed list can't be null.");
84         if (objects.isEmpty()) {
85             throw new PCEPDeserializerException("Pcup message cannot be empty.");
86         }
87
88         final List<Updates> updateRequests = Lists.newArrayList();
89
90         while (!objects.isEmpty()) {
91             final Updates upd = getValidUpdates(objects, errors);
92             if (upd != null) {
93                 updateRequests.add(upd);
94             }
95         }
96         if (!objects.isEmpty()) {
97             throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
98         }
99         return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
100     }
101
102     protected Updates getValidUpdates(final List<Object> objects, final List<Message> errors) {
103         final UpdatesBuilder builder = new UpdatesBuilder();
104
105         Object object = objects.remove(0);
106         if (object instanceof Srp) {
107             builder.setSrp((Srp) object);
108             if (objects.isEmpty()) {
109                 object = null;
110             } else {
111                 object = objects.remove(0);
112             }
113         } else {
114             errors.add(createErrorMsg(PCEPErrors.SRP_MISSING, Optional.empty()));
115         }
116
117         if (validateLsp(object, errors, builder)) {
118             if (!objects.isEmpty()) {
119                 if (!validatePath(objects, errors, builder)) {
120                     return null;
121                 }
122             }
123
124             return builder.build();
125         }
126         return null;
127     }
128
129     private static boolean validateLsp(final Object object, final List<Message> errors, final UpdatesBuilder builder) {
130         if (object instanceof Lsp) {
131             builder.setLsp((Lsp) object);
132         } else {
133             errors.add(createErrorMsg(PCEPErrors.LSP_MISSING, Optional.empty()));
134             return false;
135         }
136         return true;
137     }
138
139     private static boolean validatePath(final List<Object> objects, final List<Message> errors,
140             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.empty()));
147             return false;
148         }
149         parsePath(objects, pBuilder);
150         builder.setPath(pBuilder.build());
151         return true;
152     }
153
154     private static 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 static State insertObject(final State state, final Object obj, final PathBuilder pBuilder,
171             final List<Metrics> pathMetrics) {
172         switch (state) {
173         case INIT:
174             if (obj instanceof Lspa) {
175                 pBuilder.setLspa((Lspa) obj);
176                 return State.LSPA_IN;
177             }
178         case LSPA_IN:
179             if (obj instanceof Bandwidth) {
180                 pBuilder.setBandwidth((Bandwidth) obj);
181                 return State.LSPA_IN;
182             }
183             if (obj instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reoptimization.bandwidth.object.ReoptimizationBandwidth) {
184                 pBuilder.setReoptimizationBandwidth((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reoptimization.bandwidth.object.ReoptimizationBandwidth) obj);
185                 return State.LSPA_IN;
186             }
187         case BANDWIDTH_IN:
188             if (obj instanceof Metric) {
189                 pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
190                 return State.BANDWIDTH_IN;
191             }
192         case METRIC_IN:
193             if (obj instanceof Iro) {
194                 pBuilder.setIro((Iro) obj);
195                 return State.IRO_IN;
196             }
197         case IRO_IN:
198         case END:
199             return State.END;
200         default:
201             return state;
202         }
203     }
204
205     private enum State {
206         INIT, LSPA_IN, BANDWIDTH_IN, METRIC_IN, IRO_IN, END
207     }
208 }