OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[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                     HelloFailedCodeV10 errorCode = HelloFailedCodeV10.forValue(code);
69                     if (errorCode != null) {
70                         setCode(builder, errorCode.getIntValue(), errorCode.name());
71                     } else {
72                         setUnknownCode(builder, code);
73                     }
74                     break;
75                 }
76                 case BADREQUEST: {
77                     BadRequestCodeV10 errorCode = BadRequestCodeV10.forValue(code);
78                     if (errorCode != null) {
79                         setCode(builder, errorCode.getIntValue(), errorCode.name());
80                     } else {
81                         setUnknownCode(builder, code);
82                     }
83                     break;
84                 }
85                 case BADACTION: {
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                     FlowModFailedCodeV10 errorCode = FlowModFailedCodeV10.forValue(code);
96                     if (errorCode != null) {
97                         setCode(builder, errorCode.getIntValue(), errorCode.name());
98                     } else {
99                         setUnknownCode(builder, code);
100                     }
101                     break;
102                 }
103                 case PORTMODFAILED: {
104                     PortModFailedCodeV10 errorCode = PortModFailedCodeV10.forValue(code);
105                     if (errorCode != null) {
106                         setCode(builder, errorCode.getIntValue(), errorCode.name());
107                     } else {
108                         setUnknownCode(builder, code);
109                     }
110                     break;
111                 }
112                 case QUEUEOPFAILED: {
113                     QueueOpFailedCodeV10 errorCode = QueueOpFailedCodeV10.forValue(code);
114                     if (errorCode != null) {
115                         setCode(builder, errorCode.getIntValue(), errorCode.name());
116                     } else {
117                         setUnknownCode(builder, code);
118                     }
119                     break;
120                 }
121                 default:
122                     setUnknownCode(builder, code);
123                     break;
124             }
125         } else {
126             setUnknownCode(builder, code);
127         }
128     }
129
130     private static void setUnknownCode(ErrorMessageBuilder builder, int readValue) {
131         builder.setCode(readValue);
132         builder.setCodeString(UNKNOWN_CODE);
133     }
134
135     private static void setCode(ErrorMessageBuilder builder, int code, String codeString) {
136         builder.setCode(code);
137         builder.setCodeString(codeString);
138     }
139
140 }