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