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