Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPErrorObjectParser.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 java.util.HashMap;
11 import java.util.Map;
12 import java.util.NoSuchElementException;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.PCEPErrors;
20 import org.opendaylight.protocol.pcep.PCEPObject;
21 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
22 import org.opendaylight.protocol.pcep.impl.PCEPTlvParser;
23 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
24
25 /**
26  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPErrorObject PCEPErrorObject}
27  */
28 public class PCEPErrorObjectParser implements PCEPObjectParser {
29         /**
30          * Caret for combination of Error-type and Error-value
31          */
32         public static class PCEPErrorIdentifier {
33                 public final short type;
34                 public final short value;
35
36                 private PCEPErrorIdentifier(final short type, final short value) {
37                         this.type = type;
38                         this.value = value;
39                 }
40
41                 @Override
42                 public int hashCode() {
43                         final int prime = 31;
44                         int result = 1;
45                         result = prime * result + this.type;
46                         result = prime * result + this.value;
47                         return result;
48                 }
49
50                 @Override
51                 public boolean equals(final Object obj) {
52                         if (this == obj)
53                                 return true;
54                         if (obj == null)
55                                 return false;
56                         if (this.getClass() != obj.getClass())
57                                 return false;
58                         final PCEPErrorIdentifier other = (PCEPErrorIdentifier) obj;
59                         if (this.type != other.type)
60                                 return false;
61                         if (this.value != other.value)
62                                 return false;
63                         return true;
64                 }
65
66                 @Override
67                 public String toString() {
68                         return "type " + this.type + " value " + this.value;
69                 }
70         }
71
72         /**
73          * Bidirectional mapping of {@link org.opendaylight.protocol.pcep.PCEPErrors PCEPErrors}
74          * and
75          * {@link org.opendaylight.protocol.pcep.impl.object.PCEPErrorObjectParser.PCEPErrorIdentifier
76          * ErrorIdentifier}
77          */
78         public static class PCEPErrorsMaping {
79                 private static final PCEPErrorsMaping instance = new PCEPErrorsMaping();
80
81                 private final Map<PCEPErrors, PCEPErrorIdentifier> errorsMap = new HashMap<PCEPErrors, PCEPErrorIdentifier>();
82                 private final Map<PCEPErrorIdentifier, PCEPErrors> errorIdsMap = new HashMap<PCEPErrorIdentifier, PCEPErrors>();
83
84                 private PCEPErrorsMaping() {
85                         this.fillIn();
86                 }
87
88                 private void fillIn() {
89                         this.fillIn(new PCEPErrorIdentifier((short) 1, (short) 1), PCEPErrors.NON_OR_INVALID_OPEN_MSG);
90                         this.fillIn(new PCEPErrorIdentifier((short) 1, (short) 2), PCEPErrors.NO_OPEN_BEFORE_EXP_OPENWAIT);
91                         this.fillIn(new PCEPErrorIdentifier((short) 1, (short) 3), PCEPErrors.NON_ACC_NON_NEG_SESSION_CHAR);
92                         this.fillIn(new PCEPErrorIdentifier((short) 1, (short) 4), PCEPErrors.NON_ACC_NEG_SESSION_CHAR);
93                         this.fillIn(new PCEPErrorIdentifier((short) 1, (short) 5), PCEPErrors.SECOND_OPEN_MSG);
94                         this.fillIn(new PCEPErrorIdentifier((short) 1, (short) 6), PCEPErrors.PCERR_NON_ACC_SESSION_CHAR);
95                         this.fillIn(new PCEPErrorIdentifier((short) 1, (short) 7), PCEPErrors.NO_MSG_BEFORE_EXP_KEEPWAIT);
96                         this.fillIn(new PCEPErrorIdentifier((short) 1, (short) 8), PCEPErrors.PCEP_VERSION_NOT_SUPPORTED);
97
98                         this.fillIn(new PCEPErrorIdentifier((short) 2, (short) 0), PCEPErrors.CAPABILITY_NOT_SUPPORTED);
99
100                         this.fillIn(new PCEPErrorIdentifier((short) 3, (short) 1), PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
101                         this.fillIn(new PCEPErrorIdentifier((short) 3, (short) 2), PCEPErrors.UNRECOGNIZED_OBJ_TYPE);
102
103                         this.fillIn(new PCEPErrorIdentifier((short) 4, (short) 1), PCEPErrors.NOT_SUPPORTED_OBJ_CLASS);
104                         this.fillIn(new PCEPErrorIdentifier((short) 4, (short) 2), PCEPErrors.NOT_SUPPORTED_OBJ_TYPE);
105
106                         this.fillIn(new PCEPErrorIdentifier((short) 5, (short) 1), PCEPErrors.C_BIT_SET);
107                         this.fillIn(new PCEPErrorIdentifier((short) 5, (short) 2), PCEPErrors.O_BIT_SET);
108                         this.fillIn(new PCEPErrorIdentifier((short) 5, (short) 3), PCEPErrors.OF_NOT_ALLOWED);
109                         this.fillIn(new PCEPErrorIdentifier((short) 5, (short) 4), PCEPErrors.OF_BIT_SET);
110                         this.fillIn(new PCEPErrorIdentifier((short) 5, (short) 5), PCEPErrors.GCO_NOT_ALLOWED);
111                         this.fillIn(new PCEPErrorIdentifier((short) 5, (short) 7), PCEPErrors.P2MP_COMPUTATION_NOT_ALLOWED);
112
113                         this.fillIn(new PCEPErrorIdentifier((short) 6, (short) 1), PCEPErrors.RP_MISSING);
114                         this.fillIn(new PCEPErrorIdentifier((short) 6, (short) 2), PCEPErrors.RRO_MISSING);
115                         this.fillIn(new PCEPErrorIdentifier((short) 6, (short) 3), PCEPErrors.END_POINTS_MISSING);
116                         this.fillIn(new PCEPErrorIdentifier((short) 6, (short) 8), PCEPErrors.LSP_MISSING);
117                         this.fillIn(new PCEPErrorIdentifier((short) 6, (short) 9), PCEPErrors.ERO_MISSING);
118                         this.fillIn(new PCEPErrorIdentifier((short) 6, (short) 10), PCEPErrors.BANDWIDTH_MISSING);
119                         this.fillIn(new PCEPErrorIdentifier((short) 6, (short) 11), PCEPErrors.LSPA_MISSING);
120                         this.fillIn(new PCEPErrorIdentifier((short) 6, (short) 12), PCEPErrors.DB_VERSION_TLV_MISSING);
121
122                         this.fillIn(new PCEPErrorIdentifier((short) 6, (short) 13), PCEPErrors.LSP_CLEANUP_TLV_MISSING);
123                         this.fillIn(new PCEPErrorIdentifier((short) 6, (short) 14), PCEPErrors.SYMBOLIC_PATH_NAME_MISSING);
124
125                         this.fillIn(new PCEPErrorIdentifier((short) 7, (short) 0), PCEPErrors.SYNC_PATH_COMP_REQ_MISSING);
126
127                         this.fillIn(new PCEPErrorIdentifier((short) 8, (short) 0), PCEPErrors.UNKNOWN_REQ_REF);
128
129                         this.fillIn(new PCEPErrorIdentifier((short) 9, (short) 0), PCEPErrors.ATTEMPT_2ND_SESSION);
130
131                         this.fillIn(new PCEPErrorIdentifier((short) 10, (short) 1), PCEPErrors.P_FLAG_NOT_SET);
132
133                         this.fillIn(new PCEPErrorIdentifier((short) 12, (short) 1), PCEPErrors.UNSUPPORTED_CT);
134                         this.fillIn(new PCEPErrorIdentifier((short) 12, (short) 2), PCEPErrors.INVALID_CT);
135                         this.fillIn(new PCEPErrorIdentifier((short) 12, (short) 3), PCEPErrors.CT_AND_SETUP_PRIORITY_DO_NOT_FORM_TE_CLASS);
136
137                         this.fillIn(new PCEPErrorIdentifier((short) 15, (short) 1), PCEPErrors.INSUFFICIENT_MEMORY);
138                         this.fillIn(new PCEPErrorIdentifier((short) 15, (short) 2), PCEPErrors.GCO_NOT_SUPPORTED);
139
140                         this.fillIn(new PCEPErrorIdentifier((short) 16, (short) 1), PCEPErrors.CANNOT_SATISFY_P2MP_REQUEST_DUE_TO_INSUFFISIENT_MEMMORY);
141                         this.fillIn(new PCEPErrorIdentifier((short) 16, (short) 2), PCEPErrors.NOT_CAPPABLE_P2MP_COMPUTATION);
142
143                         this.fillIn(new PCEPErrorIdentifier((short) 17, (short) 1), PCEPErrors.P2MP_NOT_CAPPABLE_SATISFY_REQ_DUE_LT2);
144                         this.fillIn(new PCEPErrorIdentifier((short) 17, (short) 2), PCEPErrors.P2MP_NOT_CAPPABLE_SATISFY_REQ_DUE_LT3);
145                         this.fillIn(new PCEPErrorIdentifier((short) 17, (short) 3), PCEPErrors.P2MP_NOT_CAPPABLE_SATISFY_REQ_DUE_LT4);
146                         this.fillIn(new PCEPErrorIdentifier((short) 17, (short) 4), PCEPErrors.P2MP_NOT_CAPPABLE_SATISFY_REQ_DUE_INCONSISTENT_EP);
147
148                         this.fillIn(new PCEPErrorIdentifier((short) 18, (short) 1), PCEPErrors.P2MP_FRAGMENTATION_FAILRUE);
149
150                         this.fillIn(new PCEPErrorIdentifier((short) 19, (short) 1), PCEPErrors.UPDATE_REQ_FOR_NON_LSP);
151                         this.fillIn(new PCEPErrorIdentifier((short) 19, (short) 2), PCEPErrors.UPDATE_REQ_FOR_NO_STATEFUL);
152                         //TODO: value TBD
153                         this.fillIn(new PCEPErrorIdentifier((short) 19, (short) 3), PCEPErrors.LSP_LIMIT_REACHED);
154                         this.fillIn(new PCEPErrorIdentifier((short) 19, (short) 4), PCEPErrors.DELEGATION_NOT_REVOKED);
155
156                         this.fillIn(new PCEPErrorIdentifier((short) 20, (short) 1), PCEPErrors.CANNOT_PROCESS_STATE_REPORT);
157                         this.fillIn(new PCEPErrorIdentifier((short) 20, (short) 2), PCEPErrors.LSP_DB_VERSION_MISMATCH);
158                         this.fillIn(new PCEPErrorIdentifier((short) 20, (short) 3), PCEPErrors.DB_VERSION_TLV_MISSING_WHEN_SYNC_ALLOWED);
159
160                         this.fillIn(new PCEPErrorIdentifier((short) 23, (short) 1), PCEPErrors.USED_SYMBOLIC_PATH_NAME);
161                 }
162
163                 private void fillIn(final PCEPErrorIdentifier identifier, final PCEPErrors error) {
164                         this.errorsMap.put(error, identifier);
165                         this.errorIdsMap.put(identifier, error);
166                 }
167
168                 public PCEPErrorIdentifier getFromErrorsEnum(final PCEPErrors error) {
169                         final PCEPErrorIdentifier ei = this.errorsMap.get(error);
170                         if (ei == null) {
171                                 logger.debug("Unknown PCEPErrors type: {}.", error);
172                                 throw new NoSuchElementException("Unknown PCEPErrors type: " + error);
173                         }
174                         return ei;
175                 }
176
177                 public PCEPErrors getFromErrorIdentifier(final PCEPErrorIdentifier identifier) {
178                         final PCEPErrors e = this.errorIdsMap.get(identifier);
179                         if (e == null) {
180                                 logger.debug("Unknown error type/value combination: {}.", identifier);
181                                 throw new NoSuchElementException("Unknown error type/value combination: " + identifier);
182                         }
183                         return e;
184                 }
185
186                 public static PCEPErrorsMaping getInstance() {
187                         return instance;
188                 }
189         }
190
191         public static final int FLAGS_F_LENGTH = 1;
192         public static final int ET_F_LENGTH = 1;
193         public static final int EV_F_LENGTH = 1;
194
195         public static final int FLAGS_F_OFFSET = 1; //added reserved field of size 1 byte
196         public static final int ET_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
197         public static final int EV_F_OFFSET = ET_F_OFFSET + ET_F_LENGTH;
198         public static final int TLVS_OFFSET = EV_F_OFFSET + EV_F_LENGTH;
199
200         private final static Logger logger = LoggerFactory.getLogger(PCEPErrorObjectParser.class);
201
202         @Override
203         public PCEPObject parse(final byte[] bytes, final boolean processed, final boolean ignored) throws PCEPDeserializerException {
204                 if (bytes == null)
205                         throw new IllegalArgumentException("Array of bytes is mandatory.");
206
207                 if (bytes.length < TLVS_OFFSET)
208                         throw new PCEPDeserializerException("Wrong size of array of bytes. Passed: " + bytes.length + "; Expected: >=" + TLVS_OFFSET);
209
210                 final PCEPErrorIdentifier eid = new PCEPErrorIdentifier((short) (bytes[ET_F_OFFSET] & 0xFF), (short) (bytes[EV_F_OFFSET] & 0xFF));
211                 final PCEPErrors error;
212
213                 try {
214                         error = PCEPErrorsMaping.getInstance().getFromErrorIdentifier(eid);
215                 } catch (final NoSuchElementException e) {
216                         logger.debug("Failed to identify error {}", eid, e);
217                         throw new PCEPDeserializerException(e, "Error object has unknown identifier.");
218                 }
219
220                 return new PCEPErrorObject(error, PCEPTlvParser.parse(ByteArray.cutBytes(bytes, TLVS_OFFSET)));
221         }
222
223         @Override
224         public byte[] put(final PCEPObject obj) {
225                 if (!(obj instanceof PCEPErrorObject))
226                         throw new IllegalArgumentException("Unknown PCEPObject instance.");
227
228                 final PCEPErrorObject errObj = (PCEPErrorObject) obj;
229
230                 final byte[] tlvs = PCEPTlvParser.put(errObj.getTlvs());
231                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length];
232
233                 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
234
235                 final PCEPErrorIdentifier identifier = PCEPErrorsMaping.getInstance().getFromErrorsEnum(errObj.getError());
236
237                 retBytes[ET_F_OFFSET] = ByteArray.shortToBytes(identifier.type)[1];
238                 retBytes[EV_F_OFFSET] = ByteArray.shortToBytes(identifier.value)[1];
239
240                 return retBytes;
241         }
242
243 }