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