slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 1 | /* |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 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 | package org.onosproject.artemis.impl; |
| 17 | |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
| 19 | import org.onlab.packet.IpPrefix; |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 20 | import org.onosproject.artemis.ArtemisPacketProcessor; |
| 21 | import org.onosproject.artemis.Monitors; |
| 22 | import org.onosproject.artemis.impl.monitors.ExaBgpMonitors; |
| 23 | import org.onosproject.artemis.impl.monitors.RipeMonitors; |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 24 | |
| 25 | import java.util.Map; |
| 26 | import java.util.Objects; |
| 27 | import 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 | */ |
| 33 | class PrefixHandler { |
| 34 | |
| 35 | private IpPrefix prefix; |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 36 | private Set<Monitors> prefixMonitors = Sets.newHashSet(); |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Constructor that takes a CIDR-notation string and a list of monitors. |
| 40 | * |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 41 | * @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 |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 44 | */ |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 45 | PrefixHandler(IpPrefix prefix, Map<String, Set<String>> monitors, ArtemisPacketProcessor packetProcessor) { |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 46 | this.prefix = prefix; |
| 47 | |
| 48 | monitors.forEach((type, values) -> { |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 49 | 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))); |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 53 | } |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Start all monitors for this prefix. |
| 59 | */ |
| 60 | void startPrefixMonitors() { |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 61 | prefixMonitors.forEach(Monitors::startMonitor); |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Stop all monitors for this prefix. |
| 66 | */ |
| 67 | void stopPrefixMonitors() { |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 68 | prefixMonitors.forEach(Monitors::stopMonitor); |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 69 | } |
| 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 Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 80 | @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); |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public int hashCode() { |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 94 | return Objects.hash(prefix); |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 95 | } |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 96 | } |