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