Move pcep base parser Activator to its own bundle
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPEndPointsIpv4ObjectParser.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;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
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.ObjectParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.protocol.pcep.spi.UnknownObject;
21 import org.opendaylight.protocol.util.Ipv4Util;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.AddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4Case;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Case;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
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 IPv4 {@link EndpointsObj}
37  */
38 public class PCEPEndPointsIpv4ObjectParser implements ObjectParser, ObjectSerializer {
39
40     private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
41
42     public static final int CLASS = 4;
43
44     public static final int TYPE = 1;
45
46     @Override
47     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
48         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
49         final EndpointsObjBuilder builder = new EndpointsObjBuilder();
50         if (!header.isProcessingRule()) {
51             LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
52             return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
53         }
54         if (bytes.readableBytes() != Ipv4Util.IP4_LENGTH * 2) {
55             throw new PCEPDeserializerException("Wrong length of array of bytes.");
56         }
57         builder.setIgnore(header.isIgnore());
58         builder.setProcessingRule(header.isProcessingRule());
59         final Ipv4Builder b = new Ipv4Builder();
60         b.setSourceIpv4Address(Ipv4Util.addressForByteBuf(bytes));
61         b.setDestinationIpv4Address((Ipv4Util.addressForByteBuf(bytes)));
62         builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(b.build()).build());
63         return builder.build();
64     }
65
66     @Override
67     public void serializeObject(final Object object, final ByteBuf buffer) {
68         Preconditions.checkArgument(object instanceof EndpointsObj, "Wrong instance of PCEPObject. Passed %s. Needed EndpointsObject.", object.getClass());
69         final EndpointsObj ePObj = (EndpointsObj) object;
70         final AddressFamily afi = ePObj.getAddressFamily();
71         if(afi instanceof Ipv6Case) {
72             PCEPEndPointsIpv6ObjectParser.serializeObject(object,buffer);
73         }
74         Preconditions.checkArgument(afi instanceof Ipv4Case, "Wrong instance of NetworkAddress. Passed %s. Needed IPv4", afi.getClass());
75         final Ipv4 ipv4 = ((Ipv4Case) afi).getIpv4();
76         final ByteBuf body = Unpooled.buffer(Ipv4Util.IP4_LENGTH + Ipv4Util.IP4_LENGTH);
77         Preconditions.checkArgument(ipv4.getSourceIpv4Address() != null, "SourceIpv4Address is mandatory.");
78         writeIpv4Address(ipv4.getSourceIpv4Address(), body);
79         Preconditions.checkArgument(ipv4.getDestinationIpv4Address() != null, "DestinationIpv4Address is mandatory.");
80         writeIpv4Address(ipv4.getDestinationIpv4Address(), body);
81         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
82     }
83 }