Rename lists such that they follow IETF conventions
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / XROSRLGSubobjectParser.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.subobject;
9
10 import org.opendaylight.protocol.pcep.impl.object.XROSubobjectUtil;
11 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
13 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgSubobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.SrlgCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
23
24 import com.google.common.primitives.UnsignedBytes;
25
26 /**
27  * Parser for {@link SrlgCase}
28  */
29 public class XROSRLGSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
30
31         public static final int TYPE = 34;
32
33         private static final int SRLG_ID_NUMBER_LENGTH = 4;
34         private static final int ATTRIBUTE_LENGTH = 1;
35
36         private static final int SRLG_ID_NUMBER_OFFSET = 0;
37         private static final int ATTRIBUTE_OFFSET = SRLG_ID_NUMBER_OFFSET + SRLG_ID_NUMBER_LENGTH;
38
39         private static final int CONTENT_LENGTH = SRLG_ID_NUMBER_LENGTH + ATTRIBUTE_LENGTH;
40
41         @Override
42         public Subobject parseSubobject(final byte[] buffer, final boolean mandatory) throws PCEPDeserializerException {
43                 if (buffer == null || buffer.length == 0) {
44                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
45                 }
46                 if (buffer.length != CONTENT_LENGTH) {
47                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
48                                         + CONTENT_LENGTH + ".");
49                 }
50
51                 final SubobjectBuilder builder = new SubobjectBuilder();
52                 builder.setMandatory(mandatory);
53                 builder.setAttribute(Attribute.Srlg);
54                 builder.setSubobjectType(new SrlgCaseBuilder().setSrlg(
55                                 new SrlgBuilder().setSrlgId(
56                                                 new SrlgId(ByteArray.bytesToLong(ByteArray.subByte(buffer, SRLG_ID_NUMBER_OFFSET, SRLG_ID_NUMBER_LENGTH)))).build()).build());
57                 return builder.build();
58         }
59
60         @Override
61         public byte[] serializeSubobject(final Subobject subobject) {
62                 if (!(subobject.getSubobjectType() instanceof SrlgCase)) {
63                         throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
64                                         + ". Needed SrlgCase.");
65                 }
66
67                 byte[] retBytes;
68                 retBytes = new byte[CONTENT_LENGTH];
69                 final SrlgSubobject specObj = ((SrlgCase) subobject.getSubobjectType()).getSrlg();
70
71                 ByteArray.copyWhole(ByteArray.longToBytes(specObj.getSrlgId().getValue(), SRLG_ID_NUMBER_LENGTH), retBytes, SRLG_ID_NUMBER_OFFSET);
72                 retBytes[ATTRIBUTE_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
73
74                 return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
75         }
76
77         @Override
78         public int getType() {
79                 return TYPE;
80         }
81 }