Merge "Fix junit dependencies in poms. Reuse existing from parent, add missing ones."
[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.Arrays;
11 import java.util.HashMap;
12 import java.util.List;
13
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;
32
33 import com.google.common.collect.Lists;
34
35 /**
36  * Utilities used in pcep-impl
37  */
38 public final class Util {
39
40         private Util() {
41         }
42
43         public static class BiParsersMap<K, KV, V> {
44                 private final HashMap<K, KV> kToKv = new HashMap<K, KV>();
45
46                 private final HashMap<KV, V> kvToV = new HashMap<KV, V>();
47
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);
51                 }
52
53                 public KV getKeyValueFromKey(final K key) {
54                         return this.kToKv.get(key);
55                 }
56
57                 public V getValueFromKeyValue(final KV keyValue) {
58                         return this.kvToV.get(keyValue);
59                 }
60         }
61
62         public static List<IpAddress> parseAddresses(final byte[] bytes, int offset, final AddressFamily family, final int addrLen) {
63                 final List<IpAddress> addresses = Lists.newArrayList();
64
65                 while (bytes.length > offset) {
66                         if (family instanceof Ipv4) {
67                                 addresses.add(new IpAddress(Ipv4Util.addressForBytes(ByteArray.subByte(bytes, offset, addrLen))));
68                         } else {
69                                 addresses.add(new IpAddress(Ipv6Util.addressForBytes(ByteArray.subByte(bytes, offset, addrLen))));
70                         }
71                         offset += addrLen;
72                 }
73
74                 return addresses;
75         }
76
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);
81                         } else {
82                                 System.arraycopy(address.getIpv6Address().getValue().getBytes(), 0, destBytes, offset, addrLen);
83                         }
84                         offset += addrLen;
85                 }
86         }
87
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();
93                 if (t == null) {
94                         return errMessageBuilder.setPcerrMessage(new PcerrMessageBuilder().setErrors(Arrays.asList(err)).build()).build();
95                 } else {
96                         final ErrorType type = new SessionBuilder().setOpen((Open) t).build();
97                         return errMessageBuilder.setPcerrMessage(new PcerrMessageBuilder().setErrors(Arrays.asList(err)).setErrorType(type).build()).build();
98                 }
99         }
100 }