Use ListenableFutures when submitting RPCs
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / 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.impl.object;
9
10 import org.opendaylight.protocol.concepts.Ipv4Util;
11 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
13 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
14 import org.opendaylight.protocol.pcep.spi.UnknownObject;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.AddressFamily;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4Case;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObj;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Parser for IPv4 {@link EndpointsObj}
30  */
31 public class PCEPEndPointsIpv4ObjectParser extends AbstractObjectWithTlvsParser<EndpointsObjBuilder> {
32
33         private static final Logger LOG = LoggerFactory.getLogger(PCEPEndPointsIpv4ObjectParser.class);
34
35         public static final int CLASS = 4;
36         public static final int TYPE = 1;
37
38         /*
39          * fields lengths and offsets for IPv4 in bytes
40          */
41         private static final int SRC4_F_LENGTH = 4;
42         private static final int DEST4_F_LENGTH = 4;
43
44         private static final int SRC4_F_OFFSET = 0;
45         private static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
46
47         public PCEPEndPointsIpv4ObjectParser(final TlvHandlerRegistry tlvReg) {
48                 super(tlvReg);
49         }
50
51         @Override
52         public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
53                 if (bytes == null) {
54                         throw new IllegalArgumentException("Array of bytes is mandatory");
55                 }
56                 final EndpointsObjBuilder builder = new EndpointsObjBuilder();
57                 builder.setIgnore(header.isIgnore());
58                 builder.setProcessingRule(header.isProcessingRule());
59                 if (!builder.isProcessingRule()) {
60                         LOG.debug("Processed bit not set on Endpoints OBJECT, ignoring it.");
61                         return new UnknownObject(PCEPErrors.P_FLAG_NOT_SET, builder.build());
62                 }
63                 if (bytes.length != SRC4_F_LENGTH + DEST4_F_LENGTH) {
64                         throw new PCEPDeserializerException("Wrong length of array of bytes.");
65                 }
66                 final Ipv4Builder b = new Ipv4Builder();
67                 b.setSourceIpv4Address(Ipv4Util.addressForBytes(ByteArray.subByte(bytes, SRC4_F_OFFSET, SRC4_F_LENGTH)));
68                 b.setDestinationIpv4Address((Ipv4Util.addressForBytes(ByteArray.subByte(bytes, DEST4_F_OFFSET, DEST4_F_LENGTH))));
69                 builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(b.build()).build());
70                 return builder.build();
71         }
72
73         @Override
74         public void addTlv(final EndpointsObjBuilder builder, final Tlv tlv) {
75                 // No tlvs defined
76         }
77
78         @Override
79         public byte[] serializeObject(final Object object) {
80                 if (!(object instanceof EndpointsObj)) {
81                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed EndpointsObject.");
82                 }
83                 final EndpointsObj ePObj = (EndpointsObj) object;
84
85                 final AddressFamily afi = ePObj.getAddressFamily();
86
87                 if (!(afi instanceof Ipv4Case)) {
88                         throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + afi.getClass() + ". Needed IPv4");
89                 }
90                 final byte[] retBytes = new byte[SRC4_F_LENGTH + DEST4_F_LENGTH];
91                 ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getSourceIpv4Address()), retBytes, SRC4_F_OFFSET);
92                 ByteArray.copyWhole(Ipv4Util.bytesForAddress((((Ipv4Case) afi).getIpv4()).getDestinationIpv4Address()), retBytes, DEST4_F_OFFSET);
93                 return retBytes;
94         }
95
96         @Override
97         public int getObjectType() {
98                 return TYPE;
99         }
100
101         @Override
102         public int getObjectClass() {
103                 return CLASS;
104         }
105 }