Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
Luca Prete | 86ac7d1 | 2015-12-02 23:36:49 -0800 | [diff] [blame] | 17 | package org.onosproject.net.intent; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 18 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 24 | /** |
| 25 | * Utilities for dealing with intents. |
| 26 | */ |
| 27 | public final class IntentUtils { |
| 28 | |
| 29 | private static final Logger log = LoggerFactory.getLogger(IntentUtils.class); |
| 30 | |
| 31 | private IntentUtils() { |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Checks if two intents represent the same value. |
| 36 | * |
| 37 | * <p>({@link Intent#equals(Object)} only checks ID equality)</p> |
| 38 | * |
| 39 | * <p>Both intents must be of the same type.</p> |
| 40 | * |
| 41 | * @param one first intent |
| 42 | * @param two second intent |
| 43 | * @return true if the two intents represent the same value, otherwise false |
| 44 | */ |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 45 | public static boolean intentsAreEqual(Intent one, Intent two) { |
Brian O'Connor | e1fb43b | 2015-11-18 18:34:27 -0800 | [diff] [blame] | 46 | if (one.getClass() != two.getClass()) { |
| 47 | return false; |
| 48 | } |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 49 | |
| 50 | if (!(Objects.equals(one.appId(), two.appId()) && |
| 51 | Objects.equals(one.key(), two.key()))) { |
| 52 | return false; |
| 53 | } |
| 54 | |
Luca Prete | 6a4b39d | 2015-11-18 17:04:46 -0800 | [diff] [blame] | 55 | if (one instanceof SinglePointToMultiPointIntent) { |
| 56 | SinglePointToMultiPointIntent intent1 = (SinglePointToMultiPointIntent) one; |
| 57 | SinglePointToMultiPointIntent intent2 = (SinglePointToMultiPointIntent) two; |
| 58 | |
| 59 | return Objects.equals(intent1.selector(), intent2.selector()) && |
| 60 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 61 | Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) && |
| 62 | Objects.equals(intent1.egressPoints(), intent2.egressPoints()); |
| 63 | } else if (one instanceof MultiPointToSinglePointIntent) { |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 64 | MultiPointToSinglePointIntent intent1 = (MultiPointToSinglePointIntent) one; |
| 65 | MultiPointToSinglePointIntent intent2 = (MultiPointToSinglePointIntent) two; |
| 66 | |
| 67 | return Objects.equals(intent1.selector(), intent2.selector()) && |
| 68 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 69 | Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) && |
| 70 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 71 | } else if (one instanceof PointToPointIntent) { |
| 72 | PointToPointIntent intent1 = (PointToPointIntent) one; |
| 73 | PointToPointIntent intent2 = (PointToPointIntent) two; |
| 74 | |
| 75 | return Objects.equals(intent1.selector(), intent2.selector()) && |
| 76 | Objects.equals(intent1.treatment(), intent2.treatment()) && |
| 77 | Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) && |
| 78 | Objects.equals(intent1.egressPoint(), intent2.egressPoint()); |
| 79 | } else { |
| 80 | log.error("Unimplemented intent type"); |
| 81 | return false; |
| 82 | } |
| 83 | } |
| 84 | } |