2 * Copyright (c) 2013 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.deserialization.factories;
10 import static org.opendaylight.yangtools.yang.common.netty.ByteBufUtils.readUint32;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
14 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.BadActionCodeV10;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.BadRequestCodeV10;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ErrorTypeV10;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFailedCodeV10;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloFailedCodeV10;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortModFailedCodeV10;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueOpFailedCodeV10;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
24 import org.opendaylight.yangtools.yang.common.Uint16;
27 * Translates Error messages (OpenFlow v1.0).
29 * @author michal.polkorab
31 public class OF10ErrorMessageFactory implements OFDeserializer<ErrorMessage> {
33 private static final String UNKNOWN_TYPE = "UNKNOWN_TYPE";
34 private static final String UNKNOWN_CODE = "UNKNOWN_CODE";
37 public ErrorMessage deserialize(ByteBuf rawMessage) {
38 ErrorMessageBuilder builder = new ErrorMessageBuilder()
39 .setVersion(EncodeConstants.OF_VERSION_1_0)
40 .setXid(readUint32(rawMessage));
41 int type = rawMessage.readUnsignedShort();
42 ErrorTypeV10 errorType = ErrorTypeV10.forValue(type);
43 decodeType(builder, errorType, type);
44 decodeCode(rawMessage, builder, errorType);
45 int remainingBytes = rawMessage.readableBytes();
46 if (remainingBytes > 0) {
47 byte[] data = new byte[remainingBytes];
48 rawMessage.readBytes(data);
49 builder.setData(data);
51 return builder.build();
54 private static void decodeType(ErrorMessageBuilder builder, ErrorTypeV10 type, int readValue) {
56 builder.setType(Uint16.valueOf(type.getIntValue()));
57 builder.setTypeString(type.name());
59 builder.setType(Uint16.valueOf(readValue));
60 builder.setTypeString(UNKNOWN_TYPE);
64 private static void decodeCode(ByteBuf rawMessage, ErrorMessageBuilder builder,
66 int code = rawMessage.readUnsignedShort();
70 HelloFailedCodeV10 errorCode = HelloFailedCodeV10.forValue(code);
71 if (errorCode != null) {
72 setCode(builder, errorCode.getIntValue(), errorCode.name());
74 setUnknownCode(builder, code);
79 BadRequestCodeV10 errorCode = BadRequestCodeV10.forValue(code);
80 if (errorCode != null) {
81 setCode(builder, errorCode.getIntValue(), errorCode.name());
83 setUnknownCode(builder, code);
88 BadActionCodeV10 errorCode = BadActionCodeV10.forValue(code);
89 if (errorCode != null) {
90 setCode(builder, errorCode.getIntValue(), errorCode.name());
92 setUnknownCode(builder, code);
97 FlowModFailedCodeV10 errorCode = FlowModFailedCodeV10.forValue(code);
98 if (errorCode != null) {
99 setCode(builder, errorCode.getIntValue(), errorCode.name());
101 setUnknownCode(builder, code);
105 case PORTMODFAILED: {
106 PortModFailedCodeV10 errorCode = PortModFailedCodeV10.forValue(code);
107 if (errorCode != null) {
108 setCode(builder, errorCode.getIntValue(), errorCode.name());
110 setUnknownCode(builder, code);
114 case QUEUEOPFAILED: {
115 QueueOpFailedCodeV10 errorCode = QueueOpFailedCodeV10.forValue(code);
116 if (errorCode != null) {
117 setCode(builder, errorCode.getIntValue(), errorCode.name());
119 setUnknownCode(builder, code);
124 setUnknownCode(builder, code);
128 setUnknownCode(builder, code);
132 private static void setUnknownCode(ErrorMessageBuilder builder, int readValue) {
133 builder.setCode(Uint16.valueOf(readValue));
134 builder.setCodeString(UNKNOWN_CODE);
137 private static void setCode(ErrorMessageBuilder builder, int code, String codeString) {
138 builder.setCode(Uint16.valueOf(code));
139 builder.setCodeString(codeString);