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.impl.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.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;
36 * Parser for IPv4 {@link EndpointsObj}
38 public class PCEPEndPointsIpv4ObjectParser implements ObjectParser, ObjectSerializer {
40 private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
42 public static final int CLASS = 4;
44 public static final int TYPE = 1;
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());
54 if (bytes.readableBytes() != Ipv4Util.IP4_LENGTH * 2) {
55 throw new PCEPDeserializerException("Wrong length of array of bytes.");
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();
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);
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);