adfafe630c587709d6600be38aad39a2237eefa7
[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
9 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
13 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.BadActionCodeV10;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.BadRequestCodeV10;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ErrorTypeV10;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFailedCodeV10;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloFailedCodeV10;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortModFailedCodeV10;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueOpFailedCodeV10;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
23
24 /**
25  * Translates Error messages (OpenFlow v1.0).
26  *
27  * @author michal.polkorab
28  */
29 public class OF10ErrorMessageFactory implements OFDeserializer<ErrorMessage> {
30
31     private static final String UNKNOWN_TYPE = "UNKNOWN_TYPE";
32     private static final String UNKNOWN_CODE = "UNKNOWN_CODE";
33
34     @Override
35     public ErrorMessage deserialize(ByteBuf rawMessage) {
36         ErrorMessageBuilder builder = new ErrorMessageBuilder();
37         builder.setVersion((short) EncodeConstants.OF10_VERSION_ID);
38         builder.setXid(rawMessage.readUnsignedInt());
39         int type = rawMessage.readUnsignedShort();
40         ErrorTypeV10 errorType = ErrorTypeV10.forValue(type);
41         decodeType(builder, errorType, type);
42         decodeCode(rawMessage, builder, errorType);
43         int remainingBytes = rawMessage.readableBytes();
44         if (remainingBytes > 0) {
45             byte[] data = new byte[remainingBytes];
46             rawMessage.readBytes(data);
47             builder.setData(data);
48         }
49         return builder.build();
50     }
51
52     private static void decodeType(ErrorMessageBuilder builder, ErrorTypeV10 type, int readValue) {
53         if (type != null) {
54             builder.setType(type.getIntValue());
55             builder.setTypeString(type.name());
56         } else {
57             builder.setType(readValue);
58             builder.setTypeString(UNKNOWN_TYPE);
59         }
60     }
61
62     private static void decodeCode(ByteBuf rawMessage, ErrorMessageBuilder builder,
63             ErrorTypeV10 type) {
64         int code = rawMessage.readUnsignedShort();
65         if (type != null) {
66             switch (type) {
67                 case HELLOFAILED:
68                 {
69                     HelloFailedCodeV10 errorCode = HelloFailedCodeV10.forValue(code);
70                     if (errorCode != null) {
71                         setCode(builder, errorCode.getIntValue(), errorCode.name());
72                     } else {
73                         setUnknownCode(builder, code);
74                     }
75                     break;
76                 }
77                 case BADREQUEST:
78                 {
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                 {
89                     BadActionCodeV10 errorCode = BadActionCodeV10.forValue(code);
90                     if (errorCode != null) {
91                         setCode(builder, errorCode.getIntValue(), errorCode.name());
92                     } else {
93                         setUnknownCode(builder, code);
94                     }
95                     break;
96                 }
97                 case FLOWMODFAILED:
98                 {
99                     FlowModFailedCodeV10 errorCode = FlowModFailedCodeV10.forValue(code);
100                     if (errorCode != null) {
101                         setCode(builder, errorCode.getIntValue(), errorCode.name());
102                     } else {
103                         setUnknownCode(builder, code);
104                     }
105                     break;
106                 }
107                 case PORTMODFAILED:
108                 {
109                     PortModFailedCodeV10 errorCode = PortModFailedCodeV10.forValue(code);
110                     if (errorCode != null) {
111                         setCode(builder, errorCode.getIntValue(), errorCode.name());
112                     } else {
113                         setUnknownCode(builder, code);
114                     }
115                     break;
116                 }
117                 case QUEUEOPFAILED:
118                 {
119                     QueueOpFailedCodeV10 errorCode = QueueOpFailedCodeV10.forValue(code);
120                     if (errorCode != null) {
121                         setCode(builder, errorCode.getIntValue(), errorCode.name());
122                     } else {
123                         setUnknownCode(builder, code);
124                     }
125                     break;
126                 }
127                 default:
128                     setUnknownCode(builder, code);
129                     break;
130             }
131         } else {
132             setUnknownCode(builder, code);
133         }
134     }
135
136     private static void setUnknownCode(ErrorMessageBuilder builder, int readValue) {
137         builder.setCode(readValue);
138         builder.setCodeString(UNKNOWN_CODE);
139     }
140
141     private static void setCode(ErrorMessageBuilder builder, int code, String codeString) {
142         builder.setCode(code);
143         builder.setCodeString(codeString);
144     }
145
146 }