0dcd4174dfac077203384a06c86565eacd4bf12d
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / end / points / PCEPEndPointsIpv6ObjectParser.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.object.end.points;
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.spi.CommonObjectParser;
15 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
18 import org.opendaylight.protocol.pcep.spi.UnknownObject;
19 import org.opendaylight.protocol.util.Ipv6Util;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.Ipv6CaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.ipv6._case.Ipv6;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.ipv6._case.Ipv6Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.object.EndpointsObj;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.object.EndpointsObjBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Parser for IPv6 {@link EndpointsObj}.
32  */
33 public final class PCEPEndPointsIpv6ObjectParser extends CommonObjectParser {
34
35     private static final int CLASS = 4;
36     private static final int TYPE = 2;
37     private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv6ObjectParser.class);
38
39     public PCEPEndPointsIpv6ObjectParser() {
40         super(CLASS, TYPE);
41     }
42
43     public static void serializeObject(
44         final Boolean processing,
45         final Boolean ignore,
46         final Ipv6 ipv6,
47         final ByteBuf buffer) {
48         final ByteBuf body = Unpooled.buffer(Ipv6Util.IPV6_LENGTH + Ipv6Util.IPV6_LENGTH);
49         checkArgument(ipv6.getSourceIpv6Address() != null, "SourceIpv6Address is mandatory.");
50         Ipv6Util.writeIpv6Address(ipv6.getSourceIpv6Address(), body);
51         checkArgument(ipv6.getDestinationIpv6Address() != null, "DestinationIpv6Address is mandatory.");
52         Ipv6Util.writeIpv6Address(ipv6.getDestinationIpv6Address(), body);
53         ObjectUtil.formatSubobject(TYPE, CLASS, processing, ignore, body, buffer);
54     }
55
56     @Override
57     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
58         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
59         final EndpointsObjBuilder builder = new EndpointsObjBuilder();
60         if (!header.isProcessingRule()) {
61             LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
62             return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
63         }
64         if (bytes.readableBytes() != Ipv6Util.IPV6_LENGTH * 2) {
65             throw new PCEPDeserializerException("Wrong length of array of bytes.");
66         }
67         final Ipv6Builder ipv6bldr = new Ipv6Builder()
68                 .setSourceIpv6Address(Ipv6Util.addressForByteBuf(bytes))
69                 .setDestinationIpv6Address(Ipv6Util.addressForByteBuf(bytes));
70         return builder.setIgnore(header.isIgnore())
71                 .setProcessingRule(header.isProcessingRule())
72                 .setAddressFamily(new Ipv6CaseBuilder().setIpv6(ipv6bldr.build()).build())
73                 .build();
74     }
75 }