Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPCloseObjectParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.protocol.pcep.impl.object;
9
10 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.PCEPObject;
12 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
13 import org.opendaylight.protocol.pcep.impl.PCEPTlvParser;
14 import org.opendaylight.protocol.pcep.object.PCEPCloseObject;
15 import org.opendaylight.protocol.pcep.object.PCEPCloseObject.Reason;
16 import org.opendaylight.protocol.util.ByteArray;
17
18 /**
19  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPCloseObject PCEPCloseObject}
20  */
21 public class PCEPCloseObjectParser implements PCEPObjectParser {
22
23         /*
24          * lengths of fields in bytes
25          */
26         public static final int FLAGS_F_LENGTH = 1;
27         public static final int REASON_F_LENGTH = 1;
28
29         /*
30          * offsets of fields in bytes
31          */
32         public static final int FLAGS_F_OFFSET = 2; // added reserved field of size 2 bytes
33         public static final int REASON_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
34
35         /*
36          * total size of object in bytes
37          */
38         public static final int TLVS_F_OFFSET = REASON_F_OFFSET + REASON_F_LENGTH;
39
40         @Override
41         public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException {
42                 if (bytes == null)
43                         throw new IllegalArgumentException("Byte array is mandatory.");
44
45                 if (bytes.length != TLVS_F_OFFSET)
46                         throw new PCEPDeserializerException("Size of byte array doesn't match defined size. Expected: " + TLVS_F_OFFSET + "; Passed: " + bytes.length);
47
48                 Reason reason;
49                 switch ((short) (bytes[REASON_F_OFFSET] & 0xFF)) {
50                         case 1:
51                                 reason = Reason.UNKNOWN;
52                                 break;
53                         case 2:
54                                 reason = Reason.EXP_DEADTIMER;
55                                 break;
56                         case 3:
57                                 reason = Reason.MALFORMED_MSG;
58                                 break;
59                         case 4:
60                                 reason = Reason.TOO_MANY_UNKNOWN_REQ_REP;
61                                 break;
62                         case 5:
63                                 reason = Reason.TOO_MANY_UNKNOWN_MSG;
64                                 break;
65                         default:
66                                 reason = Reason.UNKNOWN;
67                                 break;
68                 }
69
70                 return new PCEPCloseObject(reason, PCEPTlvParser.parse(ByteArray.cutBytes(bytes, TLVS_F_OFFSET)));
71         }
72
73         @Override
74         public byte[] put(PCEPObject obj) {
75                 if (!(obj instanceof PCEPCloseObject))
76                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPCloseObject.");
77
78                 final byte[] tlvs = PCEPTlvParser.put(((PCEPCloseObject) obj).getTlvs());
79                 final byte[] retBytes = new byte[TLVS_F_OFFSET + tlvs.length];
80                 ByteArray.copyWhole(tlvs, retBytes, TLVS_F_OFFSET);
81
82                 int reason;
83                 switch (((PCEPCloseObject) obj).getReason()) {
84                         case UNKNOWN:
85                                 reason = 1;
86                                 break;
87                         case EXP_DEADTIMER:
88                                 reason = 2;
89                                 break;
90                         case MALFORMED_MSG:
91                                 reason = 3;
92                                 break;
93                         case TOO_MANY_UNKNOWN_REQ_REP:
94                                 reason = 4;
95                                 break;
96                         case TOO_MANY_UNKNOWN_MSG:
97                                 reason = 5;
98                                 break;
99                         default:
100                                 reason = 1;
101                                 break;
102                 }
103
104                 retBytes[REASON_F_OFFSET] = (byte) reason;
105
106                 return retBytes;
107         }
108
109 }