2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.ietf.stateful07;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.opendaylight.protocol.pcep.impl.message.PCEPErrorMessageParser;
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.protocol.pcep.spi.UnknownObject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcerr.pcerr.message.error.type.StatefulCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcerr.pcerr.message.error.type.stateful._case.stateful.Srps;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcerr.pcerr.message.error.type.stateful._case.stateful.SrpsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.Srp;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcerrMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.Errors;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.RequestCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.RequestCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.SessionCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.SessionCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.request._case.RequestBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.request._case.request.Rps;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.request._case.request.RpsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.session._case.SessionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
46 * Parser for {@link PcerrMessage}
48 public final class Stateful07ErrorMessageParser extends PCEPErrorMessageParser {
50 public Stateful07ErrorMessageParser(final ObjectRegistry registry) {
55 public void serializeMessage(final Message message, final ByteBuf out) {
56 if (!(message instanceof PcerrMessage)) {
57 throw new IllegalArgumentException("Wrong instance of Message. Passed instance " + message.getClass()
58 + ". Nedded ErrorMessage.");
60 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessage err = ((PcerrMessage) message).getPcerrMessage();
62 if (err.getErrors() == null || err.getErrors().isEmpty()) {
63 throw new IllegalArgumentException("Errors should not be empty.");
65 ByteBuf buffer = Unpooled.buffer();
67 if (err.getErrorType() instanceof RequestCase) {
68 final List<Rps> rps = ((RequestCase) err.getErrorType()).getRequest().getRps();
69 for (final Rps r : rps) {
70 serializeObject(r.getRp(), buffer);
73 if (err.getErrorType() instanceof StatefulCase) {
74 final List<Srps> srps = ((StatefulCase) err.getErrorType()).getStateful().getSrps();
75 for (final Srps s : srps) {
76 serializeObject(s.getSrp(), buffer);
79 for (final Errors e : err.getErrors()) {
80 serializeObject(e.getErrorObject(), buffer);
82 if (err.getErrorType() instanceof SessionCase) {
83 serializeObject(((SessionCase) err.getErrorType()).getSession().getOpen(), buffer);
85 MessageUtil.formatMessage(TYPE, buffer, out);
89 protected PcerrMessage validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
90 if (objects == null) {
91 throw new IllegalArgumentException("Passed list can't be null.");
94 if (objects.isEmpty()) {
95 throw new PCEPDeserializerException("Error message is empty.");
98 final List<Rps> requestParameters = new ArrayList<>();
99 final List<Srps> srps = new ArrayList<>();
100 final List<Errors> errorObjects = new ArrayList<>();
101 final PcerrMessageBuilder b = new PcerrMessageBuilder();
104 State state = State.Init;
105 obj = objects.get(0);
107 if (obj instanceof ErrorObject) {
108 final ErrorObject o = (ErrorObject) obj;
109 errorObjects.add(new ErrorsBuilder().setErrorObject(o).build());
110 state = State.ErrorIn;
112 } else if (obj instanceof Rp) {
113 final Rp o = (Rp) obj;
114 if (o.isProcessingRule()) {
115 errors.add(createErrorMsg(PCEPErrors.P_FLAG_NOT_SET));
118 requestParameters.add(new RpsBuilder().setRp(o).build());
121 } else if (obj instanceof Srp) {
122 final Srp s = (Srp) obj;
123 srps.add(new SrpsBuilder().setSrp(s).build());
128 while (!objects.isEmpty()) {
129 obj = objects.get(0);
131 if (obj instanceof UnknownObject) {
132 return new PcerrBuilder().setPcerrMessage(b.setErrors(((UnknownObject) obj).getErrors()).build()).build();
138 if (obj instanceof ErrorObject) {
139 final ErrorObject o = (ErrorObject) obj;
140 errorObjects.add(new ErrorsBuilder().setErrorObject(o).build());
141 state = State.ErrorIn;
146 if (obj instanceof Rp) {
147 final Rp o = ((Rp) obj);
148 requestParameters.add(new RpsBuilder().setRp(o).build());
154 if (obj instanceof Srp) {
155 final Srp o = ((Srp) obj);
156 srps.add(new SrpsBuilder().setSrp(o).build());
161 state = State.OpenIn;
162 if (obj instanceof Open) {
163 b.setErrorType(new SessionCaseBuilder().setSession(new SessionBuilder().setOpen((Open) obj).build()).build());
167 state = State.OpenIn;
168 if (obj instanceof ErrorObject) {
169 final ErrorObject o = (ErrorObject) obj;
170 errorObjects.add(new ErrorsBuilder().setErrorObject(o).build());
182 if (!state.equals(State.End)) {
187 if (errorObjects.isEmpty() && errorObjects.isEmpty()) {
188 throw new PCEPDeserializerException("At least one PCEPErrorObject is mandatory.");
191 if (!objects.isEmpty()) {
192 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
194 if (requestParameters != null && !requestParameters.isEmpty()) {
195 b.setErrorType(new RequestCaseBuilder().setRequest(new RequestBuilder().setRps(requestParameters).build()).build());
198 return new PcerrBuilder().setPcerrMessage(b.setErrors(errorObjects).build()).build();
202 Init, ErrorIn, RpIn, SrpIn, Open, Error, OpenIn, End