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