Remove trailing whitespace
[openflowjava.git] / 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
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
25 /**
26  * Translates Error messages (OpenFlow v1.0)
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         if (rawMessage.readableBytes() > 0) {
44             builder.setData(rawMessage.readBytes(rawMessage.readableBytes()).array());
45         }
46         return builder.build();
47     }
48
49     private static void decodeType(ErrorMessageBuilder builder, ErrorTypeV10 type, int readValue) {
50         if (type != null) {
51             builder.setType(type.getIntValue());
52             builder.setTypeString(type.name());
53         } else {
54             builder.setType(readValue);
55             builder.setTypeString(UNKNOWN_TYPE);
56         }
57     }
58
59     private static void decodeCode(ByteBuf rawMessage, ErrorMessageBuilder builder,
60             ErrorTypeV10 type) {
61         int code = rawMessage.readUnsignedShort();
62         if (type != null) {
63                 switch (type) {
64                 case HELLOFAILED:
65                 {
66                         HelloFailedCodeV10 errorCode = HelloFailedCodeV10.forValue(code);
67                         if (errorCode != null) {
68                                 setCode(builder, errorCode.getIntValue(), errorCode.name());
69                         } else {
70                                 setUnknownCode(builder, code);
71                         }
72                         break;
73                 }
74                 case BADREQUEST:
75                 {
76                         BadRequestCodeV10 errorCode = BadRequestCodeV10.forValue(code);
77                         if (errorCode != null) {
78                                 setCode(builder, errorCode.getIntValue(), errorCode.name());
79                         } else {
80                                 setUnknownCode(builder, code);
81                         }
82                         break;
83                 }
84                 case BADACTION:
85                 {
86                         BadActionCodeV10 errorCode = BadActionCodeV10.forValue(code);
87                         if (errorCode != null) {
88                                 setCode(builder, errorCode.getIntValue(), errorCode.name());
89                         } else {
90                                 setUnknownCode(builder, code);
91                         }
92                         break;
93                 }
94                 case FLOWMODFAILED:
95                 {
96                         FlowModFailedCodeV10 errorCode = FlowModFailedCodeV10.forValue(code);
97                         if (errorCode != null) {
98                                 setCode(builder, errorCode.getIntValue(), errorCode.name());
99                         } else {
100                                 setUnknownCode(builder, code);
101                         }
102                         break;
103                 }
104                 case PORTMODFAILED:
105                 {
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                 {
116                         QueueOpFailedCodeV10 errorCode = QueueOpFailedCodeV10.forValue(code);
117                         if (errorCode != null) {
118                                 setCode(builder, errorCode.getIntValue(), errorCode.name());
119                         } else {
120                                 setUnknownCode(builder, code);
121                         }
122                         break;
123                 }
124                 default:
125                         setUnknownCode(builder, code);
126                         break;
127                 }
128         } else {
129                 setUnknownCode(builder, code);
130         }
131     }
132
133     private static void setUnknownCode(ErrorMessageBuilder builder, int readValue) {
134         builder.setCode(readValue);
135                 builder.setCodeString(UNKNOWN_CODE);
136     }
137
138     private static void setCode(ErrorMessageBuilder builder, int code, String codeString) {
139         builder.setCode(code);
140                 builder.setCodeString(codeString);
141     }
142
143 }