BUG-47 : PCEP migration to generated DTOs.
[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.AbstractObjectParser;
13 import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
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 AbstractObjectParser<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 HandlerRegistry registry) {
42                 super(registry);
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                 final OfBuilder builder = new OfBuilder();
51
52                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
53
54                 builder.setIgnore(header.isIgnore());
55                 builder.setProcessingRule(header.isProcessingRule());
56                 builder.setCode(new OfId(ByteArray.bytesToInt(ByteArray.subByte(bytes, OF_CODE_F_OFFSET, OF_CODE_F_LENGTH)) & 0xFFFF));
57
58                 return builder.build();
59         }
60
61         @Override
62         public void addTlv(final OfBuilder builder, final Tlv tlv) {
63                 // No tlvs defined
64         }
65
66         @Override
67         public byte[] serializeObject(final Object object) {
68                 if (!(object instanceof OfObject))
69                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
70                                         + ". Needed PCEPObjectiveFunction.");
71
72                 final OfObject specObj = (OfObject) object;
73                 // FIXME
74                 // final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
75                 final byte[] retBytes = new byte[TLVS_OFFSET + 0];
76
77                 // ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
78
79                 ByteArray.copyWhole(ByteArray.shortToBytes(specObj.getCode().getValue().shortValue()), retBytes, OF_CODE_F_OFFSET);
80
81                 return retBytes;
82         }
83
84         @Override
85         public int getObjectType() {
86                 return TYPE;
87         }
88
89         @Override
90         public int getObjectClass() {
91                 return CLASS;
92         }
93 }