blob: 863de12af9e4a4219b24119d0d642c6bfe97797e [file] [log] [blame]
Jonathan Hart9a426f82015-09-03 15:43:13 +02001/*
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
17package org.onosproject.sdnip;
18
19import org.onosproject.net.intent.Intent;
20import org.onosproject.net.intent.MultiPointToSinglePointIntent;
21import org.onosproject.net.intent.PointToPointIntent;
Luca Prete6a4b39d2015-11-18 17:04:46 -080022import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Jonathan Hart9a426f82015-09-03 15:43:13 +020023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import java.util.Objects;
27
Jonathan Hart9a426f82015-09-03 15:43:13 +020028/**
29 * Utilities for dealing with intents.
30 */
31public final class IntentUtils {
32
33 private static final Logger log = LoggerFactory.getLogger(IntentUtils.class);
34
35 private IntentUtils() {
36
37 }
38
39 /**
40 * Checks if two intents represent the same value.
41 *
42 * <p>({@link Intent#equals(Object)} only checks ID equality)</p>
43 *
44 * <p>Both intents must be of the same type.</p>
45 *
46 * @param one first intent
47 * @param two second intent
48 * @return true if the two intents represent the same value, otherwise false
49 */
50 public static boolean equals(Intent one, Intent two) {
Brian O'Connore1fb43b2015-11-18 18:34:27 -080051 if (one.getClass() != two.getClass()) {
52 return false;
53 }
Jonathan Hart9a426f82015-09-03 15:43:13 +020054
55 if (!(Objects.equals(one.appId(), two.appId()) &&
56 Objects.equals(one.key(), two.key()))) {
57 return false;
58 }
59
Luca Prete6a4b39d2015-11-18 17:04:46 -080060 if (one instanceof SinglePointToMultiPointIntent) {
61 SinglePointToMultiPointIntent intent1 = (SinglePointToMultiPointIntent) one;
62 SinglePointToMultiPointIntent intent2 = (SinglePointToMultiPointIntent) two;
63
64 return Objects.equals(intent1.selector(), intent2.selector()) &&
65 Objects.equals(intent1.treatment(), intent2.treatment()) &&
66 Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) &&
67 Objects.equals(intent1.egressPoints(), intent2.egressPoints());
68 } else if (one instanceof MultiPointToSinglePointIntent) {
Jonathan Hart9a426f82015-09-03 15:43:13 +020069 MultiPointToSinglePointIntent intent1 = (MultiPointToSinglePointIntent) one;
70 MultiPointToSinglePointIntent intent2 = (MultiPointToSinglePointIntent) two;
71
72 return Objects.equals(intent1.selector(), intent2.selector()) &&
73 Objects.equals(intent1.treatment(), intent2.treatment()) &&
74 Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) &&
75 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
76 } else if (one instanceof PointToPointIntent) {
77 PointToPointIntent intent1 = (PointToPointIntent) one;
78 PointToPointIntent intent2 = (PointToPointIntent) two;
79
80 return Objects.equals(intent1.selector(), intent2.selector()) &&
81 Objects.equals(intent1.treatment(), intent2.treatment()) &&
82 Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) &&
83 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
84 } else {
85 log.error("Unimplemented intent type");
86 return false;
87 }
88 }
89}