e1c327ae3ae68e53623f7502da12c9129b212d1c
[bgpcep.git] / pcep / ietf-stateful02 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful02 / Stateful02PCReplyMessageParser.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.impl.message.PCEPReplyMessageParser;
17 import org.opendaylight.protocol.pcep.spi.MessageUtil;
18 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.protocol.pcep.spi.VendorInformationObjectRegistry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Replies1;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Replies1Builder;
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.message.rev131007.Pcrep;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.Metrics;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.Replies;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.RepliesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.Result;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.FailureCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.FailureCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.SuccessCase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.SuccessCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.NoPath;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.success._case.SuccessBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.success._case.success.Paths;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.success._case.success.PathsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
41
42 /**
43  * Parser for {@link Pcrep}
44  */
45 public final class Stateful02PCReplyMessageParser extends PCEPReplyMessageParser {
46
47     public Stateful02PCReplyMessageParser(final ObjectRegistry registry, final VendorInformationObjectRegistry viReg) {
48         super(registry, viReg);
49     }
50
51     @Override
52     public void serializeMessage(final Message message, final ByteBuf out) {
53         Preconditions.checkArgument(message instanceof Pcrep, "Wrong instance of Message. Passed instance of %s. Need Pcrep.", message.getClass());
54         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessage repMsg = ((Pcrep) message).getPcrepMessage();
55         if (repMsg.getReplies() == null || repMsg.getReplies().isEmpty()) {
56             throw new IllegalArgumentException("Replies cannot be null or empty.");
57         }
58         ByteBuf buffer = Unpooled.buffer();
59         for (final Replies reply : repMsg.getReplies()) {
60             if (reply.getRp() == null) {
61                 throw new IllegalArgumentException("Reply must contain RP object.");
62             }
63             serializeObject(reply.getRp(), buffer);
64             if (reply.getAugmentation(Replies1.class) != null && reply.getAugmentation(Replies1.class).getLsp() != null) {
65                 serializeObject(reply.getAugmentation(Replies1.class).getLsp(), buffer);
66             }
67             if (reply.getResult() != null) {
68                 if (reply.getResult() instanceof FailureCase) {
69                     final FailureCase f = ((FailureCase) reply.getResult());
70                     serializeObject(f.getNoPath(), buffer);
71                     if (f.getLspa() != null) {
72                         serializeObject(f.getLspa(), buffer);
73                     }
74                     if (f.getBandwidth() != null) {
75                         serializeObject(f.getBandwidth(), buffer);
76                     }
77                     if (f.getMetrics() != null && !f.getMetrics().isEmpty()) {
78                         for (final Metrics m : f.getMetrics()) {
79                             serializeObject(m.getMetric(), buffer);
80                         }
81                     }
82                     if (f.getIro() != null) {
83                         serializeObject(f.getIro(), buffer);
84                     }
85                 } else {
86                     final SuccessCase s = (SuccessCase) reply.getResult();
87                     for (final Paths p : s.getSuccess().getPaths()) {
88                         serializeObject(p.getEro(), buffer);
89                         if (p.getLspa() != null) {
90                             serializeObject(p.getLspa(), buffer);
91                         }
92                         if (p.getOf() != null) {
93                             serializeObject(p.getOf(), buffer);
94                         }
95                         if (p.getBandwidth() != null) {
96                             serializeObject(p.getBandwidth(), buffer);
97                         }
98                         if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
99                             for (final Metrics m : p.getMetrics()) {
100                                 serializeObject(m.getMetric(), buffer);
101                             }
102                         }
103                         if (p.getIro() != null) {
104                             serializeObject(p.getIro(), buffer);
105                         }
106                     }
107                 }
108             }
109         }
110         MessageUtil.formatMessage(TYPE, buffer, out);
111     }
112
113     @Override
114     protected Replies getValidReply(final List<Object> objects, final List<Message> errors) {
115         if (!(objects.get(0) instanceof Rp)) {
116             errors.add(createErrorMsg(PCEPErrors.RP_MISSING, Optional.<Rp>absent()));
117             return null;
118         }
119         final Rp rp = (Rp) objects.get(0);
120         objects.remove(0);
121         Result res = null;
122         Lsp lsp = null;
123         if (objects.get(0) instanceof Lsp) {
124             lsp = (Lsp) objects.get(0);
125             objects.remove(0);
126         }
127         if (!objects.isEmpty()) {
128             if (objects.get(0) instanceof NoPath) {
129                 final NoPath noPath = (NoPath) objects.get(0);
130                 objects.remove(0);
131                 final FailureCaseBuilder builder = new FailureCaseBuilder();
132                 builder.setNoPath(noPath);
133                 while (!objects.isEmpty()) {
134                     parseAttributes(builder, objects);
135                 }
136                 res = builder.build();
137             } else if (objects.get(0) instanceof Ero) {
138                 final Ero ero = (Ero) objects.get(0);
139                 objects.remove(0);
140                 final SuccessBuilder builder = new SuccessBuilder();
141                 final List<Paths> paths = Lists.newArrayList();
142                 final PathsBuilder pBuilder = new PathsBuilder();
143                 pBuilder.setEro(ero);
144                 while (!objects.isEmpty()) {
145                     parsePath(pBuilder, objects);
146                     paths.add(pBuilder.build());
147                 }
148                 builder.setPaths(paths);
149                 res = new SuccessCaseBuilder().setSuccess(builder.build()).build();
150             }
151         }
152         return new RepliesBuilder().setRp(rp).addAugmentation(Replies1.class, new Replies1Builder().setLsp(lsp).build()).setResult(res).build();
153     }
154 }