blob: 5076e1355751b6a22bbae0790c9fd98702ee4b5c [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()) &&
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 Hart9a426f82015-09-03 15:43:13 +020064 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}