Merge "Revoke delegation sent as Pcinitiate message."
[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         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             if (p.getLspa() != null) {
71                 serializeObject(p.getLspa(), buffer);
72             }
73             if (p.getBandwidth() != null) {
74                 serializeObject(p.getBandwidth(), buffer);
75             }
76             if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
77                 for (final Metrics m : p.getMetrics()) {
78                     serializeObject(m.getMetric(), buffer);
79                 }
80             }
81             if (p.getIro() != null) {
82                 serializeObject(p.getIro(), buffer);
83             }
84         }
85     }
86
87     @Override
88     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
89         if (objects == null) {
90             throw new IllegalArgumentException("Passed list can't be null.");
91         }
92         if (objects.isEmpty()) {
93             throw new PCEPDeserializerException("Pcup message cannot be empty.");
94         }
95
96         final List<Updates> updateRequests = Lists.newArrayList();
97
98         while (!objects.isEmpty()) {
99             final Updates upd = getValidUpdates(objects, errors);
100             if(upd != null) {
101                 updateRequests.add(upd);
102             }
103         }
104         if (!objects.isEmpty()) {
105             throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
106         }
107         return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
108     }
109
110     protected Updates getValidUpdates(final List<Object> objects, final List<Message> errors) {
111         boolean isValid = true;
112         final UpdatesBuilder builder = new UpdatesBuilder();
113         if (objects.get(0) instanceof Srp) {
114             builder.setSrp((Srp) objects.get(0));
115             objects.remove(0);
116         } else {
117             errors.add(createErrorMsg(PCEPErrors.SRP_MISSING, Optional.<Rp>absent()));
118             isValid = false;
119         }
120         if (objects.get(0) instanceof Lsp) {
121             builder.setLsp((Lsp) objects.get(0));
122             objects.remove(0);
123         } else {
124             errors.add(createErrorMsg(PCEPErrors.LSP_MISSING, Optional.<Rp>absent()));
125             isValid = false;
126         }
127         if (!objects.isEmpty()) {
128             final PathBuilder pBuilder = new PathBuilder();
129             if (objects.get(0) instanceof Ero) {
130                 pBuilder.setEro((Ero) objects.get(0));
131                 objects.remove(0);
132             } else {
133                 errors.add(createErrorMsg(PCEPErrors.ERO_MISSING, Optional.<Rp>absent()));
134                 isValid = false;
135             }
136             parsePath(objects, pBuilder);
137             builder.setPath(pBuilder.build());
138         }
139         if(isValid) {
140             return builder.build();
141         }
142         return null;
143     }
144
145     private void parsePath(final List<Object> objects, final PathBuilder pBuilder) {
146         final List<Metrics> pathMetrics = Lists.newArrayList();
147         Object obj;
148         State state = State.Init;
149         while (!objects.isEmpty() && !state.equals(State.End)) {
150             obj = objects.get(0);
151             switch (state) {
152             case Init:
153                 state = State.LspaIn;
154                 if (obj instanceof Lspa) {
155                     pBuilder.setLspa((Lspa) obj);
156                     break;
157                 }
158             case LspaIn:
159                 state = State.BandwidthIn;
160                 if (obj instanceof Bandwidth) {
161                     pBuilder.setBandwidth((Bandwidth) obj);
162                     break;
163                 }
164             case BandwidthIn:
165                 state = State.MetricIn;
166                 if (obj instanceof Metric) {
167                     pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
168                     state = State.BandwidthIn;
169                     break;
170                 }
171             case MetricIn:
172                 state = State.IroIn;
173                 if (obj instanceof Iro) {
174                     pBuilder.setIro((Iro) obj);
175                     break;
176                 }
177             case IroIn:
178                 state = State.End;
179                 break;
180             case End:
181                 break;
182             }
183             if (!state.equals(State.End)) {
184                 objects.remove(0);
185             }
186         }
187         if (!pathMetrics.isEmpty()) {
188             pBuilder.setMetrics(pathMetrics);
189         }
190     }
191
192     private enum State {
193         Init, LspaIn, BandwidthIn, MetricIn, IroIn, End
194     }
195 }