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;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.protocol.concepts.AddressFamily;
16 import org.opendaylight.protocol.concepts.NetworkAddress;
19 * Utilities used in pcep-impl
21 public final class Util {
26 public static class BiParsersMap<K, KV, V> {
27 private final HashMap<K, KV> kToKv = new HashMap<K, KV>();
29 private final HashMap<KV, V> kvToV = new HashMap<KV, V>();
31 public void put(K key, KV keyValue, V value) {
32 this.kToKv.put(key, keyValue);
33 this.kvToV.put(keyValue, value);
36 public KV getKeyValueFromKey(K key) {
37 return this.kToKv.get(key);
40 public V getValueFromKeyValue(KV keyValue) {
41 return this.kvToV.get(keyValue);
45 public static int getPadding(int length, int padding) {
46 return (padding - (length % padding)) % padding;
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>();
52 while (bytes.length > offset) {
53 addresses.add(family.addressForBytes(ByteArray.subByte(bytes, offset, addrLen)));
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);