Merge "Added tests for bgp-rib-impl-config modules, fixed validations and RIBImplModule."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPObjectiveFunctionObjectParser.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.spi.TlvHandlerRegistry;
12 import org.opendaylight.protocol.util.ByteArray;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.Of;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.OfBuilder;
19
20 /**
21  * Parser for {@link Of}
22  */
23 public class PCEPObjectiveFunctionObjectParser extends AbstractObjectWithTlvsParser<OfBuilder> {
24
25         public static final int CLASS = 21;
26
27         public static final int TYPE = 1;
28         /*
29          * lengths of fields
30          */
31         private static final int OF_CODE_F_LENGTH = 2;
32
33         /*
34          * offsets of fields
35          */
36         private static final int OF_CODE_F_OFFSET = 0;
37         private static final int TLVS_OFFSET = OF_CODE_F_OFFSET + OF_CODE_F_LENGTH + 2;
38
39         public PCEPObjectiveFunctionObjectParser(final TlvHandlerRegistry tlvReg) {
40                 super(tlvReg);
41         }
42
43         @Override
44         public Of parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
45                 if (bytes == null || bytes.length == 0) {
46                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
47                 }
48                 final OfBuilder builder = new OfBuilder();
49                 builder.setIgnore(header.isIgnore());
50                 builder.setProcessingRule(header.isProcessingRule());
51                 builder.setCode(new OfId(ByteArray.bytesToInt(ByteArray.subByte(bytes, OF_CODE_F_OFFSET, OF_CODE_F_LENGTH))));
52                 return builder.build();
53         }
54
55         @Override
56         public void addTlv(final OfBuilder builder, final Tlv tlv) {
57                 // No tlvs defined
58         }
59
60         @Override
61         public byte[] serializeObject(final Object object) {
62                 if (!(object instanceof Of)) {
63                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
64                                         + ". Needed PCEPObjectiveFunction.");
65                 }
66                 final Of specObj = (Of) object;
67                 final byte[] retBytes = new byte[TLVS_OFFSET + 0];
68                 ByteArray.copyWhole(ByteArray.shortToBytes(specObj.getCode().getValue().shortValue()), retBytes, OF_CODE_F_OFFSET);
69                 return retBytes;
70         }
71
72         @Override
73         public int getObjectType() {
74                 return TYPE;
75         }
76
77         @Override
78         public int getObjectClass() {
79                 return CLASS;
80         }
81 }