Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPNotificationObjectParser.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.PCEPNotificationObject;
15 import org.opendaylight.protocol.util.ByteArray;
16
17 /**
18  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPNotificationObject
19  * PCEPNotificationObject}
20  */
21 public class PCEPNotificationObjectParser implements PCEPObjectParser {
22
23         /*
24          * lengths of fields
25          */
26         public static final int FLAGS_F_LENGTH = 1;
27         public static final int NT_F_LENGTH = 1;
28         public static final int NV_F_LENGTH = 1;
29
30         /*
31          * offsets of fields
32          */
33         public static final int FLAGS_F_OFFSET = 1; //added reserved filed of size 1
34         public static final int NT_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
35         public static final int NV_F_OFFSET = NT_F_OFFSET + NT_F_LENGTH;
36         public static final int TLVS_OFFSET = NV_F_OFFSET + NV_F_LENGTH;
37
38         @Override
39         public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException {
40                 if (bytes == null || bytes.length == 0)
41                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
42
43                 if (bytes.length < TLVS_OFFSET)
44                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + TLVS_OFFSET + ".");
45
46                 return new PCEPNotificationObject((short) (bytes[NT_F_OFFSET] & 0xFF), (short) (bytes[NV_F_OFFSET] & 0xFF), PCEPTlvParser.parse(ByteArray.cutBytes(
47                                 bytes, TLVS_OFFSET)));
48         }
49
50         @Override
51         public byte[] put(PCEPObject obj) {
52                 if (!(obj instanceof PCEPNotificationObject))
53                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPNotificationObject.");
54
55                 final PCEPNotificationObject notObj = (PCEPNotificationObject) obj;
56
57                 final byte[] tlvs = PCEPTlvParser.put(notObj.getTlvs());
58                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length];
59
60                 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
61
62                 retBytes[NT_F_OFFSET] = ByteArray.shortToBytes(notObj.getType())[1];
63                 retBytes[NV_F_OFFSET] = ByteArray.shortToBytes(notObj.getValue())[1];
64
65                 return retBytes;
66         }
67
68 }