25d1cdb2760f77d660a1311adb22cb14ec1adc96
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / OF10ErrorMessageFactory.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.deserialization.factories;
9
10 import static org.opendaylight.yangtools.yang.common.netty.ByteBufUtils.readUint32;
11
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;
25
26 /**
27  * Translates Error messages (OpenFlow v1.0).
28  *
29  * @author michal.polkorab
30  */
31 public class OF10ErrorMessageFactory implements OFDeserializer<ErrorMessage> {
32
33     private static final String UNKNOWN_TYPE = "UNKNOWN_TYPE";
34     private static final String UNKNOWN_CODE = "UNKNOWN_CODE";
35
36     @Override
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);
50         }
51         return builder.build();
52     }
53
54     private static void decodeType(ErrorMessageBuilder builder, ErrorTypeV10 type, int readValue) {
55         if (type != null) {
56             builder.setType(Uint16.valueOf(type.getIntValue()));
57             builder.setTypeString(type.name());
58         } else {
59             builder.setType(Uint16.valueOf(readValue));
60             builder.setTypeString(UNKNOWN_TYPE);
61         }
62     }
63
64     private static void decodeCode(ByteBuf rawMessage, ErrorMessageBuilder builder,
65             ErrorTypeV10 type) {
66         int code = rawMessage.readUnsignedShort();
67         if (type != null) {
68             switch (type) {
69                 case HELLOFAILED: {
70                     HelloFailedCodeV10 errorCode = HelloFailedCodeV10.forValue(code);
71                     if (errorCode != null) {
72                         setCode(builder, errorCode.getIntValue(), errorCode.name());
73                     } else {
74                         setUnknownCode(builder, code);
75                     }
76                     break;
77                 }
78                 case BADREQUEST: {
79                     BadRequestCodeV10 errorCode = BadRequestCodeV10.forValue(code);
80                     if (errorCode != null) {
81                         setCode(builder, errorCode.getIntValue(), errorCode.name());
82                     } else {
83                         setUnknownCode(builder, code);
84                     }
85                     break;
86                 }
87                 case BADACTION: {
88                     BadActionCodeV10 errorCode = BadActionCodeV10.forValue(code);
89                     if (errorCode != null) {
90                         setCode(builder, errorCode.getIntValue(), errorCode.name());
91                     } else {
92                         setUnknownCode(builder, code);
93                     }
94                     break;
95                 }
96                 case FLOWMODFAILED: {
97                     FlowModFailedCodeV10 errorCode = FlowModFailedCodeV10.forValue(code);
98                     if (errorCode != null) {
99                         setCode(builder, errorCode.getIntValue(), errorCode.name());
100                     } else {
101                         setUnknownCode(builder, code);
102                     }
103                     break;
104                 }
105                 case PORTMODFAILED: {
106                     PortModFailedCodeV10 errorCode = PortModFailedCodeV10.forValue(code);
107                     if (errorCode != null) {
108                         setCode(builder, errorCode.getIntValue(), errorCode.name());
109                     } else {
110                         setUnknownCode(builder, code);
111                     }
112                     break;
113                 }
114                 case QUEUEOPFAILED: {
115                     QueueOpFailedCodeV10 errorCode = QueueOpFailedCodeV10.forValue(code);
116                     if (errorCode != null) {
117                         setCode(builder, errorCode.getIntValue(), errorCode.name());
118                     } else {
119                         setUnknownCode(builder, code);
120                     }
121                     break;
122                 }
123                 default:
124                     setUnknownCode(builder, code);
125                     break;
126             }
127         } else {
128             setUnknownCode(builder, code);
129         }
130     }
131
132     private static void setUnknownCode(ErrorMessageBuilder builder, int readValue) {
133         builder.setCode(Uint16.valueOf(readValue));
134         builder.setCodeString(UNKNOWN_CODE);
135     }
136
137     private static void setCode(ErrorMessageBuilder builder, int code, String codeString) {
138         builder.setCode(Uint16.valueOf(code));
139         builder.setCodeString(codeString);
140     }
141
142 }