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.Arrays;
11 import java.util.HashMap;
12 import java.util.List;
14 import org.opendaylight.protocol.concepts.Ipv4Util;
15 import org.opendaylight.protocol.concepts.Ipv6Util;
16 import org.opendaylight.protocol.pcep.PCEPErrorMapping;
17 import org.opendaylight.protocol.pcep.PCEPErrorMapping.PCEPErrorIdentifier;
18 import org.opendaylight.protocol.pcep.PCEPErrors;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenObject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.AddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.address.family.Ipv4;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.Errors;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.SessionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.session.Open;
33 import com.google.common.collect.Lists;
36 * Utilities used in pcep-impl
38 public final class Util {
43 public static class BiParsersMap<K, KV, V> {
44 private final HashMap<K, KV> kToKv = new HashMap<K, KV>();
46 private final HashMap<KV, V> kvToV = new HashMap<KV, V>();
48 public void put(final K key, final KV keyValue, final V value) {
49 this.kToKv.put(key, keyValue);
50 this.kvToV.put(keyValue, value);
53 public KV getKeyValueFromKey(final K key) {
54 return this.kToKv.get(key);
57 public V getValueFromKeyValue(final KV keyValue) {
58 return this.kvToV.get(keyValue);
62 public static List<IpAddress> parseAddresses(final byte[] bytes, int offset, final AddressFamily family, final int addrLen) {
63 final List<IpAddress> addresses = Lists.newArrayList();
65 while (bytes.length > offset) {
66 if (family instanceof Ipv4) {
67 addresses.add(new IpAddress(Ipv4Util.addressForBytes(ByteArray.subByte(bytes, offset, addrLen))));
69 addresses.add(new IpAddress(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, offset, addrLen))));
77 public static void putAddresses(final byte[] destBytes, int offset, final List<IpAddress> addresses, final int addrLen) {
78 for (final IpAddress address : addresses) {
79 if (address.getIpv4Address() != null) {
80 System.arraycopy(address.getIpv4Address().getValue().getBytes(), 0, destBytes, offset, addrLen);
82 System.arraycopy(address.getIpv6Address().getValue().getBytes(), 0, destBytes, offset, addrLen);
88 public static Message createErrorMessage(final PCEPErrors e, final OpenObject t) {
89 final PcerrBuilder errMessageBuilder = new PcerrBuilder();
90 final PCEPErrorMapping mapping = PCEPErrorMapping.getInstance();
91 final PCEPErrorIdentifier id = mapping.getFromErrorsEnum(e);
92 final Errors err = new ErrorsBuilder().setType(id.type).setValue(id.value).build();
94 return errMessageBuilder.setPcerrMessage(new PcerrMessageBuilder().setErrors(Arrays.asList(err)).build()).build();
96 final ErrorType type = new SessionBuilder().setOpen((Open) t).build();
97 return errMessageBuilder.setPcerrMessage(new PcerrMessageBuilder().setErrors(Arrays.asList(err)).setErrorType(type).build()).build();