BUG-47 : 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 org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
12 import org.opendaylight.protocol.pcep.impl.Util;
13 import org.opendaylight.protocol.pcep.spi.AbstractObjectParser;
14 import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcepErrorObject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ReqMissingTlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.Tlvs;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.TlvsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.tlvs.ReqMissingBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.Errors;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
26
27 /**
28  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPErrorObject PCEPErrorObject}
29  */
30 public class PCEPErrorObjectParser extends AbstractObjectParser<ErrorsBuilder> {
31
32         public static final int CLASS = 13;
33
34         public static final int TYPE = 1;
35
36         public static final int FLAGS_F_LENGTH = 1;
37         public static final int ET_F_LENGTH = 1;
38         public static final int EV_F_LENGTH = 1;
39
40         public static final int FLAGS_F_OFFSET = 1; // added reserved field of size 1 byte
41         public static final int ET_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
42         public static final int EV_F_OFFSET = ET_F_OFFSET + ET_F_LENGTH;
43         public static final int TLVS_OFFSET = EV_F_OFFSET + EV_F_LENGTH;
44
45         public PCEPErrorObjectParser(final HandlerRegistry registry) {
46                 super(registry);
47         }
48
49         @Override
50         public PcepErrorObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
51                         PCEPDocumentedException {
52                 if (bytes == null)
53                         throw new IllegalArgumentException("Array of bytes is mandatory.");
54
55                 final ErrorsBuilder builder = new ErrorsBuilder();
56
57                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
58
59                 builder.setIgnore(header.isIgnore());
60                 builder.setProcessingRule(header.isProcessingRule());
61
62                 builder.setType((short) (bytes[ET_F_OFFSET] & 0xFF));
63                 builder.setValue((short) (bytes[EV_F_OFFSET] & 0xFF));
64
65                 return builder.build();
66         }
67
68         @Override
69         public void addTlv(final ErrorsBuilder builder, final Tlv tlv) {
70                 if (tlv instanceof ReqMissingTlv && builder.getType() == 7)
71                         builder.setTlvs(new TlvsBuilder().setReqMissing(
72                                         new ReqMissingBuilder().setRequestId(((ReqMissingTlv) tlv).getRequestId()).build()).build());
73         }
74
75         @Override
76         public byte[] serializeObject(final Object object) {
77                 if (!(object instanceof PcepErrorObject))
78                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PcepErrorObject.");
79
80                 final PcepErrorObject errObj = (PcepErrorObject) object;
81
82                 final byte[] tlvs = serializeTlvs(((Errors) errObj).getTlvs());
83                 int tlvsLength = 0;
84                 if (tlvs != null)
85                         tlvsLength = tlvs.length;
86                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvsLength + Util.getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
87
88                 if (tlvs != null)
89                         ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
90
91                 retBytes[ET_F_OFFSET] = ByteArray.shortToBytes(errObj.getType())[1];
92                 retBytes[EV_F_OFFSET] = ByteArray.shortToBytes(errObj.getValue())[1];
93
94                 return retBytes;
95         }
96
97         public byte[] serializeTlvs(final Tlvs tlvs) {
98                 if (tlvs.getReqMissing() != null) {
99                         return serializeTlv(new ReqMissingBuilder().setRequestId(tlvs.getReqMissing().getRequestId()).build());
100                 }
101                 return null;
102         }
103
104         @Override
105         public int getObjectType() {
106                 return TYPE;
107         }
108
109         @Override
110         public int getObjectClass() {
111                 return CLASS;
112         }
113 }