blob: 6fa407e3346b76aca4655d534bd0a4c418c8d88a [file] [log] [blame]
slowrdb071b22017-07-07 11:10:25 -07001/*
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -07002 * Copyright 2017-present Open Networking Foundation
slowrdb071b22017-07-07 11:10:25 -07003 *
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 */
16package org.onosproject.artemis.impl;
17
slowrdb071b22017-07-07 11:10:25 -070018import com.google.common.collect.Sets;
19import org.onlab.packet.IpPrefix;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070020import org.onosproject.artemis.ArtemisPacketProcessor;
21import org.onosproject.artemis.Monitors;
22import org.onosproject.artemis.impl.monitors.ExaBgpMonitors;
23import org.onosproject.artemis.impl.monitors.RipeMonitors;
slowrdb071b22017-07-07 11:10:25 -070024
25import java.util.Map;
26import java.util.Objects;
27import java.util.Set;
28
29/**
30 * Handler of monitoring step for each different prefix.
31 * This class contains all the running monitors of the specified prefix.
32 */
33class PrefixHandler {
34
35 private IpPrefix prefix;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070036 private Set<Monitors> prefixMonitors = Sets.newHashSet();
slowrdb071b22017-07-07 11:10:25 -070037
38 /**
39 * Constructor that takes a CIDR-notation string and a list of monitors.
40 *
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070041 * @param prefix A CIDR-notation string, e.g. "192.168.0.1/24"
42 * @param monitors A map of strings to a set of string for monitors, e.g. "ripe", ["host1","host2",..]
43 * @param packetProcessor Packet processor
slowrdb071b22017-07-07 11:10:25 -070044 */
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070045 PrefixHandler(IpPrefix prefix, Map<String, Set<String>> monitors, ArtemisPacketProcessor packetProcessor) {
slowrdb071b22017-07-07 11:10:25 -070046 this.prefix = prefix;
47
48 monitors.forEach((type, values) -> {
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070049 if (Monitors.Types.getEnum(type).equals(Monitors.Types.RIPE)) {
50 values.forEach(host -> prefixMonitors.add(new RipeMonitors(prefix, host, packetProcessor)));
51 } else if (Monitors.Types.getEnum(type).equals(Monitors.Types.EXABGP)) {
52 values.forEach(host -> prefixMonitors.add(new ExaBgpMonitors(prefix, host, packetProcessor)));
slowrdb071b22017-07-07 11:10:25 -070053 }
54 });
55 }
56
57 /**
58 * Start all monitors for this prefix.
59 */
60 void startPrefixMonitors() {
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070061 prefixMonitors.forEach(Monitors::startMonitor);
slowrdb071b22017-07-07 11:10:25 -070062 }
63
64 /**
65 * Stop all monitors for this prefix.
66 */
67 void stopPrefixMonitors() {
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070068 prefixMonitors.forEach(Monitors::stopMonitor);
slowrdb071b22017-07-07 11:10:25 -070069 }
70
71 /**
72 * Return a CIDR-notation string of prefix.
73 *
74 * @return the prefix in CIDR-notation
75 */
76 IpPrefix getPrefix() {
77 return prefix;
78 }
79
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070080 @Override
81 public boolean equals(Object o) {
82 if (this == o) {
83 return true;
84 }
85 if (o == null || getClass() != o.getClass()) {
86 return false;
87 }
88 PrefixHandler that = (PrefixHandler) o;
89 return Objects.equals(prefix, that.prefix);
slowrdb071b22017-07-07 11:10:25 -070090 }
91
92 @Override
93 public int hashCode() {
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070094 return Objects.hash(prefix);
slowrdb071b22017-07-07 11:10:25 -070095 }
slowrdb071b22017-07-07 11:10:25 -070096}