7787de14cf01f42b18999995ca15af6f6f34932e
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / 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.parser.subobject;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
18 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.SubobjectBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgSubobject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
28 import org.opendaylight.yangtools.yang.common.Uint8;
29 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
30
31 /**
32  * Parser for {@link SrlgCase}.
33  */
34 public class XROSrlgSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
35     public static final int TYPE = 34;
36
37     private static final int CONTENT_LENGTH = 6;
38
39     @Override
40     public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
41         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
42         if (buffer.readableBytes() != CONTENT_LENGTH) {
43             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
44                 + "; Expected: " + CONTENT_LENGTH + ".");
45         }
46         final SubobjectBuilder builder = new SubobjectBuilder()
47                 .setMandatory(mandatory)
48                 .setSubobjectType(new SrlgCaseBuilder()
49                     .setSrlg(new SrlgBuilder()
50                         .setSrlgId(new SrlgId(ByteBufUtils.readUint32(buffer)))
51                         .build())
52                     .build());
53         buffer.readByte();
54         return builder.setAttribute(ExcludeRouteSubobjects.Attribute.forValue(buffer.readUnsignedByte()))
55                 .build();
56     }
57
58     @Override
59     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
60         checkArgument(subobject.getSubobjectType() instanceof SrlgCase,
61             "Unknown subobject instance. Passed %s. Needed SrlgCase.", subobject.getSubobjectType().getClass());
62         final SrlgSubobject specObj = ((SrlgCase) subobject.getSubobjectType()).getSrlg();
63         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
64         checkArgument(specObj.getSrlgId() != null, "SrlgId is mandatory.");
65         writeUnsignedInt(specObj.getSrlgId().getValue(), body);
66         checkArgument(subobject.getAttribute() != null, "Attribute is mandatory.");
67         writeUnsignedByte((Uint8) null, body);
68         writeUnsignedByte(Uint8.valueOf(subobject.getAttribute().getIntValue()), body);
69         XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);
70     }
71 }