Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.net.link; |
| 17 | |
| 18 | import com.google.common.hash.HashFunction; |
| 19 | import com.google.common.hash.HashCode; |
| 20 | import com.google.common.hash.Hashing; |
| 21 | |
| 22 | import org.onosproject.cluster.ClusterMetadata; |
| 23 | |
| 24 | /** |
| 25 | * Abstraction for an entity that provides information about infrastructure |
| 26 | * links that are discovered or verified using probe messages. |
| 27 | */ |
| 28 | public interface ProbedLinkProvider extends LinkProvider { |
| 29 | |
| 30 | static final String DEFAULT_MAC = "DE:AD:BE:EF:BA:11"; |
| 31 | |
| 32 | static String defaultMac() { |
| 33 | return DEFAULT_MAC; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Build a stringified MAC address using the ClusterMetadata hash for uniqueness. |
| 38 | * Form of MAC is "02:eb" followed by four bytes of clusterMetadata hash. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 39 | * |
| 40 | * @param cm cluster metadata |
| 41 | * @return stringified mac address |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 42 | */ |
| 43 | static String fingerprintMac(ClusterMetadata cm) { |
| 44 | if (cm == null) { |
| 45 | return DEFAULT_MAC; |
| 46 | } |
| 47 | |
| 48 | HashFunction hf = Hashing.murmur3_32(); |
| 49 | HashCode hc = hf.newHasher().putObject(cm, ClusterMetadata.HASH_FUNNEL).hash(); |
| 50 | int unqf = hc.asInt(); |
| 51 | |
| 52 | StringBuilder sb = new StringBuilder(); |
| 53 | sb.append("02:eb"); |
| 54 | for (int i = 0; i < 4; i++) { |
| 55 | byte b = (byte) (unqf >> i * 8); |
| 56 | sb.append(String.format(":%02X", b)); |
| 57 | } |
| 58 | return sb.toString(); |
| 59 | } |
| 60 | } |