Copyright update
[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.impl.deserialization.OFDeserializer;
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  * @author michal.polkorab
27  */
28 public class OF10ErrorMessageFactory implements OFDeserializer<ErrorMessage> {
29
30         private static final String UNKNOWN_TYPE = "UNKNOWN_TYPE";
31     private static final String UNKNOWN_CODE = "UNKNOWN_CODE";
32     
33     private static OF10ErrorMessageFactory instance;
34     
35     private OF10ErrorMessageFactory() {
36         // do nothing, just singleton
37     }
38     
39     /**
40      * @return singleton factory
41      */
42     public static synchronized OF10ErrorMessageFactory getInstance() {
43         if (instance == null) {
44             instance = new OF10ErrorMessageFactory();
45         }
46         return instance;
47     }
48     
49     @Override
50     public ErrorMessage bufferToMessage(ByteBuf rawMessage, short version) {
51         ErrorMessageBuilder builder = new ErrorMessageBuilder();
52         builder.setVersion(version);
53         builder.setXid(rawMessage.readUnsignedInt());
54         int type = rawMessage.readUnsignedShort();
55         ErrorTypeV10 errorType = ErrorTypeV10.forValue(type);
56         decodeType(builder, errorType, type);
57         decodeCode(rawMessage, builder, errorType);
58         if (rawMessage.readableBytes() > 0) {
59             builder.setData(rawMessage.readBytes(rawMessage.readableBytes()).array());
60         }
61         return builder.build();
62     }
63     
64     private static void decodeType(ErrorMessageBuilder builder, ErrorTypeV10 type, int readValue) {
65         if (type != null) {
66             builder.setType(type.getIntValue());
67             builder.setTypeString(type.name());
68         } else {
69             builder.setType(readValue);
70             builder.setTypeString(UNKNOWN_TYPE);
71         }
72     }
73
74     private static void decodeCode(ByteBuf rawMessage, ErrorMessageBuilder builder,
75             ErrorTypeV10 type) {
76         int code = rawMessage.readUnsignedShort();
77         if (type != null) {
78                 switch (type) {
79                 case HELLOFAILED:
80                 {
81                         HelloFailedCodeV10 errorCode = HelloFailedCodeV10.forValue(code);
82                         if (errorCode != null) {
83                                 setCode(builder, errorCode.getIntValue(), errorCode.name());
84                         } else {
85                                 setUnknownCode(builder, code);
86                         }
87                         break;
88                 }
89                 case BADREQUEST:
90                 {
91                         BadRequestCodeV10 errorCode = BadRequestCodeV10.forValue(code);
92                         if (errorCode != null) {
93                                 setCode(builder, errorCode.getIntValue(), errorCode.name());
94                         } else {
95                                 setUnknownCode(builder, code);
96                         }
97                         break;
98                 }
99                 case BADACTION:
100                 {
101                         BadActionCodeV10 errorCode = BadActionCodeV10.forValue(code);
102                         if (errorCode != null) {
103                                 setCode(builder, errorCode.getIntValue(), errorCode.name());
104                         } else {
105                                 setUnknownCode(builder, code);
106                         }
107                         break;
108                 }
109                 case FLOWMODFAILED:
110                 {
111                         FlowModFailedCodeV10 errorCode = FlowModFailedCodeV10.forValue(code);
112                         if (errorCode != null) {
113                                 setCode(builder, errorCode.getIntValue(), errorCode.name());
114                         } else {
115                                 setUnknownCode(builder, code);
116                         }
117                         break;
118                 }
119                 case PORTMODFAILED:
120                 {
121                         PortModFailedCodeV10 errorCode = PortModFailedCodeV10.forValue(code);
122                         if (errorCode != null) {
123                                 setCode(builder, errorCode.getIntValue(), errorCode.name());
124                         } else {
125                                 setUnknownCode(builder, code);
126                         }
127                         break;
128                 }
129                 case QUEUEOPFAILED:
130                 {
131                         QueueOpFailedCodeV10 errorCode = QueueOpFailedCodeV10.forValue(code);
132                         if (errorCode != null) {
133                                 setCode(builder, errorCode.getIntValue(), errorCode.name());
134                         } else {
135                                 setUnknownCode(builder, code);
136                         }
137                         break;
138                 }
139                 default:
140                         setUnknownCode(builder, code);
141                         break;
142                 }
143         } else {
144                 setUnknownCode(builder, code);
145         }
146     }
147     
148     private static void setUnknownCode(ErrorMessageBuilder builder, int readValue) {
149         builder.setCode(readValue);
150                 builder.setCodeString(UNKNOWN_CODE);
151     }
152     
153     private static void setCode(ErrorMessageBuilder builder, int code, String codeString) {
154         builder.setCode(code);
155                 builder.setCodeString(codeString);
156     }
157
158 }