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