blob: c2bda5b470cbbd1ef4e3d60ce03f3aaaf13ad206 [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
28import static com.google.common.base.Preconditions.checkArgument;
29
30/**
31 * Utilities for dealing with intents.
32 */
33public final class IntentUtils {
34
35 private static final Logger log = LoggerFactory.getLogger(IntentUtils.class);
36
37 private IntentUtils() {
38
39 }
40
41 /**
42 * Checks if two intents represent the same value.
43 *
44 * <p>({@link Intent#equals(Object)} only checks ID equality)</p>
45 *
46 * <p>Both intents must be of the same type.</p>
47 *
48 * @param one first intent
49 * @param two second intent
50 * @return true if the two intents represent the same value, otherwise false
51 */
52 public static boolean equals(Intent one, Intent two) {
53 checkArgument(one.getClass() == two.getClass(),
54 "Intents are not the same type");
55
56 if (!(Objects.equals(one.appId(), two.appId()) &&
57 Objects.equals(one.key(), two.key()))) {
58 return false;
59 }
60
Luca Prete6a4b39d2015-11-18 17:04:46 -080061 if (one instanceof SinglePointToMultiPointIntent) {
62 SinglePointToMultiPointIntent intent1 = (SinglePointToMultiPointIntent) one;
63 SinglePointToMultiPointIntent intent2 = (SinglePointToMultiPointIntent) two;
64
65 return Objects.equals(intent1.selector(), intent2.selector()) &&
66 Objects.equals(intent1.treatment(), intent2.treatment()) &&
67 Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) &&
68 Objects.equals(intent1.egressPoints(), intent2.egressPoints());
69 } else if (one instanceof MultiPointToSinglePointIntent) {
Jonathan Hart9a426f82015-09-03 15:43:13 +020070 MultiPointToSinglePointIntent intent1 = (MultiPointToSinglePointIntent) one;
71 MultiPointToSinglePointIntent intent2 = (MultiPointToSinglePointIntent) two;
72
73 return Objects.equals(intent1.selector(), intent2.selector()) &&
74 Objects.equals(intent1.treatment(), intent2.treatment()) &&
75 Objects.equals(intent1.ingressPoints(), intent2.ingressPoints()) &&
76 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
77 } else if (one instanceof PointToPointIntent) {
78 PointToPointIntent intent1 = (PointToPointIntent) one;
79 PointToPointIntent intent2 = (PointToPointIntent) two;
80
81 return Objects.equals(intent1.selector(), intent2.selector()) &&
82 Objects.equals(intent1.treatment(), intent2.treatment()) &&
83 Objects.equals(intent1.ingressPoint(), intent2.ingressPoint()) &&
84 Objects.equals(intent1.egressPoint(), intent2.egressPoint());
85 } else {
86 log.error("Unimplemented intent type");
87 return false;
88 }
89 }
90}