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