blob: 9ddb1747912c1bf257f51b1bcbe9b82847866d3d [file] [log] [blame]
Jonathan Hart9a426f82015-09-03 15:43:13 +02001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hart9a426f82015-09-03 15:43:13 +02003 *
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 Prete86ac7d12015-12-02 23:36:49 -080017package org.onosproject.net.intent;
Jonathan Hart9a426f82015-09-03 15:43:13 +020018
Jonathan Hart9a426f82015-09-03 15:43:13 +020019import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
21
22import java.util.Objects;
23
Jonathan Hart9a426f82015-09-03 15:43:13 +020024/**
25 * Utilities for dealing with intents.
26 */
27public final class IntentUtils {
28
29 private static final Logger log = LoggerFactory.getLogger(IntentUtils.class);
30
31 private IntentUtils() {
Jonathan Hart9a426f82015-09-03 15:43:13 +020032 }
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 Milkey4fd3ceb2015-12-10 14:43:08 -080045 public static boolean intentsAreEqual(Intent one, Intent two) {
Brian O'Connore1fb43b2015-11-18 18:34:27 -080046 if (one.getClass() != two.getClass()) {
47 return false;
48 }
Jonathan Hart9a426f82015-09-03 15:43:13 +020049
50 if (!(Objects.equals(one.appId(), two.appId()) &&
51 Objects.equals(one.key(), two.key()))) {
52 return false;
53 }
54
Luca Prete6a4b39d2015-11-18 17:04:46 -080055 if (one instanceof SinglePointToMultiPointIntent) {
56 SinglePointToMultiPointIntent intent1 = (SinglePointToMultiPointIntent) one;
57 SinglePointToMultiPointIntent intent2 = (SinglePointToMultiPointIntent) two;
58
59 return Objects.equals(intent1.selector(), intent2.selector()) &&
Yi Tseng2a81c9d2016-09-14 10:14:24 -070060 Objects.equals(intent1.treatment(), intent2.treatment()) &&
61 Objects.equals(intent1.filteredIngressPoint(), intent2.filteredIngressPoint()) &&
62 Objects.equals(intent1.filteredEgressPoints(), intent2.filteredEgressPoints()) &&
63 Objects.equals(intent1.constraints(), intent2.constraints());
Luca Prete6a4b39d2015-11-18 17:04:46 -080064 } else if (one instanceof MultiPointToSinglePointIntent) {
Jonathan Hart9a426f82015-09-03 15:43:13 +020065 MultiPointToSinglePointIntent intent1 = (MultiPointToSinglePointIntent) one;
66 MultiPointToSinglePointIntent intent2 = (MultiPointToSinglePointIntent) two;
67
68 return Objects.equals(intent1.selector(), intent2.selector()) &&
Yi Tseng2a81c9d2016-09-14 10:14:24 -070069 Objects.equals(intent1.filteredIngressPoints(), intent2.filteredIngressPoints()) &&
70 Objects.equals(intent1.filteredEgressPoint(), intent2.filteredEgressPoint()) &&
71 Objects.equals(intent1.treatment(), intent2.treatment()) &&
72 Objects.equals(intent1.constraints(), intent2.constraints());
Jonathan Hart9a426f82015-09-03 15:43:13 +020073 } else if (one instanceof PointToPointIntent) {
74 PointToPointIntent intent1 = (PointToPointIntent) one;
75 PointToPointIntent intent2 = (PointToPointIntent) two;
76
77 return Objects.equals(intent1.selector(), intent2.selector()) &&
78 Objects.equals(intent1.treatment(), intent2.treatment()) &&
Pier Ventre91a99122016-07-09 11:10:47 +020079 Objects.equals(intent1.constraints(), intent2.constraints()) &&
Jonathan Hart9a426f82015-09-03 15:43:13 +020080 Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) &&
81 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
82 } else {
83 log.error("Unimplemented intent type");
84 return false;
85 }
86 }
87}