BUG-47 : moved Ipv4 and Ipv6 to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPEROSubobjectParser.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;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.impl.subobject.EROExplicitExclusionRouteSubobjectParser;
15 import org.opendaylight.protocol.pcep.impl.subobject.EROLabelSubobjectParser;
16 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKeyWith128PCEIDSubobjectParser;
17 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKeyWith32PCEIDSubobjectParser;
18 import org.opendaylight.protocol.pcep.impl.subobject.EROUnnumberedInterfaceSubobjectParser;
19 import org.opendaylight.protocol.pcep.subobject.EROAsNumberSubobject;
20 import org.opendaylight.protocol.pcep.subobject.EROExplicitExclusionRouteSubobject;
21 import org.opendaylight.protocol.pcep.subobject.EROLabelSubobject;
22 import org.opendaylight.protocol.pcep.subobject.EROPathKeyWith128PCEIDSubobject;
23 import org.opendaylight.protocol.pcep.subobject.EROPathKeyWith32PCEIDSubobject;
24 import org.opendaylight.protocol.pcep.subobject.EROUnnumberedInterfaceSubobject;
25 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Parser for {@link org.opendaylight.protocol.pcep.PCEPSubobject PCEPSubobject}
32  */
33 public class PCEPEROSubobjectParser {
34
35         private static final Logger logger = LoggerFactory.getLogger(PCEPEROSubobjectParser.class);
36
37         /**
38          * Type identifier for {@link org.opendaylight.protocol.pcep.PCEPSubobject PCEPSubobject}
39          */
40         public enum PCEPSubobjectType {
41                 IPv4_PREFIX(1), IPv6_PREFIX(2), LABEL(3), UNNUMBERED_INTERFACE_ID(4), AS_NUMBER(32), EXRS(33), PROTECTION(37), PK_32(64), PK_128(65);
42
43                 private final int indicator;
44
45                 PCEPSubobjectType(final int indicator) {
46                         this.indicator = indicator;
47                 }
48
49                 public int getIndicator() {
50                         return this.indicator;
51                 }
52
53                 public static PCEPSubobjectType getFromInt(final int type) throws PCEPDeserializerException {
54
55                         for (final PCEPSubobjectType type_e : PCEPSubobjectType.values()) {
56                                 if (type_e.getIndicator() == type)
57                                         return type_e;
58                         }
59
60                         throw new PCEPDeserializerException("Unknown Subobject type. Passed: " + type + "; Known: " + PCEPSubobjectType.values() + ".");
61                 }
62         }
63
64         /*
65          * Fields lengths in Bytes
66          */
67         public static final int TYPE_FLAG_F_LENGTH = 1;
68         public static final int LENGTH_F_LENGTH = 1;
69
70         /*
71          * Fields offsets in Bytes
72          */
73         public static final int TYPE_FLAG_F_OFFSET = 0;
74         public static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + TYPE_FLAG_F_LENGTH;
75         public static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + LENGTH_F_LENGTH;
76
77         public static List<ExplicitRouteSubobject> parse(final byte[] bytes) throws PCEPDeserializerException {
78                 if (bytes == null)
79                         throw new IllegalArgumentException("Byte array is mandatory.");
80
81                 final List<ExplicitRouteSubobject> subobjsList = new ArrayList<ExplicitRouteSubobject>();
82                 boolean loose_flag;
83                 PCEPSubobjectType type;
84                 byte[] soContentsBytes;
85                 int length;
86                 int offset = 0;
87
88                 while (offset < bytes.length) {
89
90                         loose_flag = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
91                         length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, LENGTH_F_LENGTH));
92
93                         type = PCEPSubobjectType.getFromInt((bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7));
94
95                         if (length > bytes.length - offset)
96                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
97                                                 + (bytes.length - offset));
98
99                         soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
100                         System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
101
102                         logger.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
103                         final ExplicitRouteSubobject subObj = parseSpecificSubobject(type, soContentsBytes, loose_flag);
104                         logger.debug("Subobject was parsed. {}", subObj);
105
106                         subobjsList.add(subObj);
107
108                         offset += length;
109                 }
110
111                 return subobjsList;
112         }
113
114         public static byte[] put(final List<ExplicitRouteSubobject> objsToSerialize) {
115                 final List<byte[]> bytesList = new ArrayList<byte[]>(objsToSerialize.size());
116
117                 int length = 0;
118                 for (final ExplicitRouteSubobject obj : objsToSerialize) {
119                         final byte[] bytes = put(obj);
120                         length += bytes.length;
121                         bytesList.add(bytes);
122                 }
123
124                 final byte[] retBytes = new byte[length];
125
126                 int offset = 0;
127                 for (final byte[] bytes : bytesList) {
128                         System.arraycopy(bytes, 0, retBytes, offset, bytes.length);
129                         offset += bytes.length;
130                 }
131
132                 return retBytes;
133         }
134
135         public static byte[] put(final ExplicitRouteSubobject objToSerialize) {
136                 int typeIndicator = 0;
137
138                 byte[] soContentsBytes = null;
139
140                 // if (objToSerialize instanceof EROIPPrefixSubobject<?>
141                 // && ((EROIPPrefixSubobject<?>) objToSerialize).getPrefix() instanceof IPv4Prefix) {
142                 // typeIndicator = PCEPSubobjectType.IPv4_PREFIX.getIndicator();
143                 // // soContentsBytes = EROIpPrefixSubobjectParser.put(objToSerialize);
144                 // } else if (objToSerialize instanceof EROIPPrefixSubobject<?>
145                 // && ((EROIPPrefixSubobject<?>) objToSerialize).getPrefix() instanceof IPv6Prefix) {
146                 // typeIndicator = PCEPSubobjectType.IPv6_PREFIX.getIndicator();
147                 // // soContentsBytes = EROIPv6PrefixSubobjectParser.put(objToSerialize);
148                 // } else
149                 if (objToSerialize instanceof EROAsNumberSubobject) {
150                         typeIndicator = PCEPSubobjectType.AS_NUMBER.getIndicator();
151                         // soContentsBytes = EROAsNumberSubobjectParser.put(objToSerialize);
152                 } else if (objToSerialize instanceof EROUnnumberedInterfaceSubobject) {
153                         typeIndicator = PCEPSubobjectType.UNNUMBERED_INTERFACE_ID.getIndicator();
154                         soContentsBytes = EROUnnumberedInterfaceSubobjectParser.put(objToSerialize);
155                 } else if (objToSerialize instanceof EROLabelSubobject) {
156                         typeIndicator = PCEPSubobjectType.LABEL.getIndicator();
157                         soContentsBytes = EROLabelSubobjectParser.put((EROLabelSubobject) objToSerialize);
158                 } else if (objToSerialize instanceof EROExplicitExclusionRouteSubobject) {
159                         typeIndicator = PCEPSubobjectType.EXRS.getIndicator();
160                         soContentsBytes = EROExplicitExclusionRouteSubobjectParser.put((EROExplicitExclusionRouteSubobject) objToSerialize);
161                 } else if (objToSerialize instanceof EROPathKeyWith32PCEIDSubobject) {
162                         typeIndicator = PCEPSubobjectType.PK_32.getIndicator();
163                         soContentsBytes = EROPathKeyWith32PCEIDSubobjectParser.put((EROPathKeyWith32PCEIDSubobject) objToSerialize);
164                 } else if (objToSerialize instanceof EROPathKeyWith128PCEIDSubobject) {
165                         typeIndicator = PCEPSubobjectType.PK_128.getIndicator();
166                         soContentsBytes = EROPathKeyWith128PCEIDSubobjectParser.put((EROPathKeyWith128PCEIDSubobject) objToSerialize);
167                 } else
168                         throw new IllegalArgumentException("Unknown instance of PCEPSubobject. Passed: " + objToSerialize.getClass() + ".");
169
170                 final byte[] bytes = new byte[SO_CONTENTS_OFFSET + soContentsBytes.length];
171
172                 bytes[TYPE_FLAG_F_OFFSET] = (byte) (ByteArray.cutBytes(ByteArray.intToBytes(typeIndicator), (Integer.SIZE / 8) - TYPE_FLAG_F_LENGTH)[0] | (objToSerialize.isLoose() ? 1 << 7
173                                 : 0));
174                 bytes[LENGTH_F_OFFSET] = ByteArray.cutBytes(ByteArray.intToBytes(soContentsBytes.length + SO_CONTENTS_OFFSET), (Integer.SIZE / 8)
175                                 - LENGTH_F_LENGTH)[0];
176
177                 System.arraycopy(soContentsBytes, 0, bytes, SO_CONTENTS_OFFSET, soContentsBytes.length);
178
179                 return bytes;
180         }
181
182         private static ExplicitRouteSubobject parseSpecificSubobject(final PCEPSubobjectType type, final byte[] soContentsBytes,
183                         final boolean loose_flag) throws PCEPDeserializerException {
184
185                 switch (type) {
186                 case IPv4_PREFIX:
187                         // return EROIpPrefixSubobjectParser.parse(soContentsBytes, loose_flag);
188                 case IPv6_PREFIX:
189                         // return EROIPv6PrefixSubobjectParser.parse(soContentsBytes, loose_flag);
190                 case UNNUMBERED_INTERFACE_ID:
191                         return EROUnnumberedInterfaceSubobjectParser.parse(soContentsBytes, loose_flag);
192                 case AS_NUMBER:
193                         // return EROAsNumberSubobjectParser.parse(soContentsBytes, loose_flag);
194                 case LABEL:
195                         return EROLabelSubobjectParser.parse(soContentsBytes, loose_flag);
196                 case EXRS:
197                         return EROExplicitExclusionRouteSubobjectParser.parse(soContentsBytes, loose_flag);
198                 case PK_32:
199                         return EROPathKeyWith32PCEIDSubobjectParser.parse(soContentsBytes, loose_flag);
200                 case PK_128:
201                         return EROPathKeyWith128PCEIDSubobjectParser.parse(soContentsBytes, loose_flag);
202                 default:
203                         throw new PCEPDeserializerException("Unknown Subobject type. Passed: " + type + ".");
204                 }
205         }
206 }