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