Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / Util.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.HashMap;
12 import java.util.List;
13
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.protocol.concepts.AddressFamily;
16 import org.opendaylight.protocol.concepts.NetworkAddress;
17
18 /**
19  * Utilities used in pcep-impl
20  */
21 public final class Util {
22
23         private Util() {
24         }
25
26         public static class BiParsersMap<K, KV, V> {
27                 private final HashMap<K, KV> kToKv = new HashMap<K, KV>();
28
29                 private final HashMap<KV, V> kvToV = new HashMap<KV, V>();
30
31                 public void put(K key, KV keyValue, V value) {
32                         this.kToKv.put(key, keyValue);
33                         this.kvToV.put(keyValue, value);
34                 }
35
36                 public KV getKeyValueFromKey(K key) {
37                         return this.kToKv.get(key);
38                 }
39
40                 public V getValueFromKeyValue(KV keyValue) {
41                         return this.kvToV.get(keyValue);
42                 }
43         }
44
45         public static int getPadding(int length, int padding) {
46                 return (padding - (length % padding)) % padding;
47         }
48
49         public static <T extends NetworkAddress<T>> List<T> parseAddresses(byte[] bytes, int offset, AddressFamily<T> family, int addrLen) {
50                 final List<T> addresses = new ArrayList<T>();
51
52                 while (bytes.length > offset) {
53                         addresses.add(family.addressForBytes(ByteArray.subByte(bytes, offset, addrLen)));
54                         offset += addrLen;
55                 }
56
57                 return addresses;
58         }
59
60         public static <T extends NetworkAddress<T>> void putAddresses(byte[] destBytes, int offset, List<T> addresses, int addrLen) {
61                 for (final T address : addresses) {
62                         System.arraycopy(address.getAddress(), 0, destBytes, offset, addrLen);
63                         offset += addrLen;
64                 }
65         }
66 }