BUG 2230: RSVP ERO Subobjects
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / ero / EROPathKey32SubobjectParser.java
1 /*
2  * Copyright (c) 2015 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.rsvp.parser.impl.subobject.ero;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectParser;
13 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectSerializer;
14 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectUtil;
15 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
16 import org.opendaylight.protocol.rsvp.parser.spi.subobjects.CommonPathKeyParser;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCase;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainer;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainerBuilder;
21
22 /**
23  * Parser for {PathKey}
24  */
25 public class EROPathKey32SubobjectParser extends CommonPathKeyParser implements EROSubobjectParser,
26     EROSubobjectSerializer {
27
28     public static final int TYPE = 64;
29
30     private static final int PCE_ID_F_LENGTH = 4;
31
32     private static final int CONTENT_LENGTH = 2 + PCE_ID_F_LENGTH;
33
34     @Override
35     public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean loose) throws RSVPParsingException {
36         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
37         if (buffer.readableBytes() != CONTENT_LENGTH) {
38             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
39                 + CONTENT_LENGTH + ".");
40         }
41
42         final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
43         builder.setLoose(loose);
44         builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(parsePathKey(PCE_ID_F_LENGTH, buffer)).build());
45         return builder.build();
46     }
47
48     @Override
49     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
50         Preconditions.checkArgument(subobject.getSubobjectType() instanceof PathKeyCase, "Unknown subobject instance. Passed %s. Needed PathKey.", subobject.getSubobjectType().getClass());
51         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
52         final ByteBuf body = serializePathKey(pk);
53         if (pk.getPceId().getBinary().length == PCE_ID_F_LENGTH) {
54             EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
55         } else if (pk.getPceId().getBinary().length == EROPathKey128SubobjectParser.PCE128_ID_F_LENGTH) {
56             EROSubobjectUtil.formatSubobject(EROPathKey128SubobjectParser.TYPE, subobject.isLoose(), body, buffer);
57         }
58     }
59 }