Merge "BUG-47: label model"
[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                         offset += addrLen;
71                 }
72
73                 return addresses;
74         }
75
76         public static void putAddresses(final byte[] destBytes, int offset, final List<IpAddress> addresses, final int addrLen) {
77                 for (final IpAddress address : addresses) {
78                         if (address.getIpv4Address() != null)
79                                 System.arraycopy(address.getIpv4Address().getValue().getBytes(), 0, destBytes, offset, addrLen);
80                         else
81                                 System.arraycopy(address.getIpv6Address().getValue().getBytes(), 0, destBytes, offset, addrLen);
82                         offset += addrLen;
83                 }
84         }
85
86         public static int getPadding(final int length, final int padding) {
87                 return (padding - (length % padding)) % padding;
88         }
89
90         public static Message createErrorMessage(final PCEPErrors e, final OpenObject t) {
91                 final PcerrBuilder errMessageBuilder = new PcerrBuilder();
92                 final PCEPErrorMapping mapping = PCEPErrorMapping.getInstance();
93                 final PCEPErrorIdentifier id = mapping.getFromErrorsEnum(e);
94                 final Errors err = new ErrorsBuilder().setType(id.type).setValue(id.value).build();
95                 if (t == null)
96                         return errMessageBuilder.setPcerrMessage(new PcerrMessageBuilder().setErrors(Arrays.asList(err)).build()).build();
97                 else {
98                         final ErrorType type = new SessionBuilder().setOpen((Open) t).build();
99                         return errMessageBuilder.setPcerrMessage(new PcerrMessageBuilder().setErrors(Arrays.asList(err)).setErrorType(type).build()).build();
100                 }
101         }
102 }