2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.parser.object;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
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.CommonObjectParser;
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.rev181109.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.AddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.Ipv4Case;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.Ipv4CaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.Ipv6Case;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.ipv4._case.Ipv4;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.ipv4._case.Ipv4Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.object.EndpointsObj;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.object.EndpointsObjBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Parser for IPv4 {@link EndpointsObj}
38 public class PCEPEndPointsIpv4ObjectParser extends CommonObjectParser implements ObjectSerializer {
40 private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
42 private static final int CLASS = 4;
43 private static final int TYPE = 1;
45 public PCEPEndPointsIpv4ObjectParser() {
50 public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
51 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
52 final EndpointsObjBuilder builder = new EndpointsObjBuilder();
53 if (!header.isProcessingRule()) {
54 LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
55 return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
57 if (bytes.readableBytes() != Ipv4Util.IP4_LENGTH * 2) {
58 throw new PCEPDeserializerException("Wrong length of array of bytes.");
60 builder.setIgnore(header.isIgnore());
61 builder.setProcessingRule(header.isProcessingRule());
62 final Ipv4Builder b = new Ipv4Builder();
63 b.setSourceIpv4Address(Ipv4Util.noZoneAddressForByteBuf(bytes));
64 b.setDestinationIpv4Address(Ipv4Util.noZoneAddressForByteBuf(bytes));
65 builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(b.build()).build());
66 return builder.build();
70 public void serializeObject(final Object object, final ByteBuf buffer) {
71 Preconditions.checkArgument(object instanceof EndpointsObj, "Wrong instance of PCEPObject. Passed %s. Needed EndpointsObject.", object.getClass());
72 final EndpointsObj ePObj = (EndpointsObj) object;
73 final AddressFamily afi = ePObj.getAddressFamily();
74 if(afi instanceof Ipv6Case) {
75 PCEPEndPointsIpv6ObjectParser.serializeObject(object,buffer);
77 Preconditions.checkArgument(afi instanceof Ipv4Case, "Wrong instance of NetworkAddress. Passed %s. Needed IPv4", afi.getClass());
78 final Ipv4 ipv4 = ((Ipv4Case) afi).getIpv4();
79 final ByteBuf body = Unpooled.buffer(Ipv4Util.IP4_LENGTH + Ipv4Util.IP4_LENGTH);
80 Preconditions.checkArgument(ipv4.getSourceIpv4Address() != null, "SourceIpv4Address is mandatory.");
81 writeIpv4Address(ipv4.getSourceIpv4Address(), body);
82 Preconditions.checkArgument(ipv4.getDestinationIpv4Address() != null, "DestinationIpv4Address is mandatory.");
83 writeIpv4Address(ipv4.getDestinationIpv4Address(), body);
84 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);