Merge "BUG-1287: migrate tests to new APIs"
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / 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.impl.object;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
19 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
20 import org.opendaylight.protocol.pcep.spi.UnknownObject;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.protocol.util.Ipv6Util;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.AddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Case;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6CaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Parser for IPv6 {@link EndpointsObj}
37  */
38 public class PCEPEndPointsIpv6ObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
39
40     private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv6ObjectParser.class);
41
42     public static final int CLASS = 4;
43
44     public static final int TYPE = 2;
45
46     public PCEPEndPointsIpv6ObjectParser(final TlvRegistry tlvReg) {
47         super(tlvReg);
48     }
49
50     @Override
51     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
52         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
53         final EndpointsObjBuilder builder = new EndpointsObjBuilder();
54         if (!header.isProcessingRule()) {
55             LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
56             return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
57         }
58         if (bytes.readableBytes() != Ipv6Util.IPV6_LENGTH * 2) {
59             throw new PCEPDeserializerException("Wrong length of array of bytes.");
60         }
61         builder.setIgnore(header.isIgnore());
62         builder.setProcessingRule(header.isProcessingRule());
63         final Ipv6Builder b = new Ipv6Builder();
64         b.setSourceIpv6Address(Ipv6Util.addressForBytes(ByteArray.readBytes(bytes, Ipv6Util.IPV6_LENGTH)));
65         b.setDestinationIpv6Address((Ipv6Util.addressForBytes(ByteArray.readBytes(bytes, Ipv6Util.IPV6_LENGTH))));
66         builder.setAddressFamily(new Ipv6CaseBuilder().setIpv6(b.build()).build());
67         return builder.build();
68     }
69
70     @Override
71     public void serializeObject(final Object object, final ByteBuf buffer) {
72         Preconditions.checkArgument(object instanceof EndpointsObj, "Wrong instance of PCEPObject. Passed %s. Needed EndpointsObject.", object.getClass());
73         final EndpointsObj ePObj = (EndpointsObj) object;
74         final AddressFamily afi = ePObj.getAddressFamily();
75         Preconditions.checkArgument(afi instanceof Ipv6Case, "Wrong instance of NetworkAddress. Passed %s. Needed IPv6", afi.getClass());
76         final ByteBuf body = Unpooled.buffer(Ipv6Util.IPV6_LENGTH + Ipv6Util.IPV6_LENGTH);
77         final Ipv6 ipv6 = ((Ipv6Case) afi).getIpv6();
78         Preconditions.checkArgument(ipv6.getSourceIpv6Address() != null, "SourceIpv6Address is mandatory.");
79         writeIpv6Address(ipv6.getSourceIpv6Address(), body);
80         Preconditions.checkArgument(ipv6.getDestinationIpv6Address() != null, "DestinationIpv6Address is mandatory.");
81         writeIpv6Address(ipv6.getDestinationIpv6Address(), body);
82         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
83     }
84 }