blob: 57553b5596c6cbe48bf253c38ce79ee3ea1b03aa [file] [log] [blame]
slowrdb071b22017-07-07 11:10:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-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
18import com.google.common.collect.Maps;
19import com.google.common.collect.Sets;
20import org.onlab.packet.IpPrefix;
21import org.onosproject.artemis.impl.monitors.ExaBgpMonitor;
22import org.onosproject.artemis.impl.monitors.Monitor;
23import org.onosproject.artemis.impl.monitors.RipeMonitor;
24
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;
36 private Set<Monitor> prefixMonitors = Sets.newHashSet();
37
38 /**
39 * Constructor that takes a CIDR-notation string and a list of monitors.
40 *
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 */
44 PrefixHandler(IpPrefix prefix, Map<String, Set<String>> monitors) {
45 this.prefix = prefix;
46
47 monitors.forEach((type, values) -> {
48 if (type.equals(Monitor.Types.RIPE.toString())) {
49 values.forEach(host -> prefixMonitors.add(new RipeMonitor(prefix, host)));
50 } else if (type.equals(Monitor.Types.EXABGP.toString())) {
51 values.forEach(host -> prefixMonitors.add(new ExaBgpMonitor(prefix, host)));
52 }
53 });
54 }
55
56 /**
57 * Start all monitors for this prefix.
58 */
59 void startPrefixMonitors() {
60 prefixMonitors.forEach(Monitor::startMonitor);
61 }
62
63 /**
64 * Stop all monitors for this prefix.
65 */
66 void stopPrefixMonitors() {
67 prefixMonitors.forEach(Monitor::stopMonitor);
68 }
69
70 /**
71 * Return a CIDR-notation string of prefix.
72 *
73 * @return the prefix in CIDR-notation
74 */
75 IpPrefix getPrefix() {
76 return prefix;
77 }
78
79 /**
80 * Changes the monitors based on the new list given.
81 *
82 * @param newMonitors monitors to be added
83 */
84 void changeMonitors(Map<String, Set<String>> newMonitors) {
85 Set<String> newTypes = newMonitors.keySet();
86 Set<Monitor> monToRemove = Sets.newHashSet();
87 Map<String, Set<String>> monToAdd = Maps.newHashMap(newMonitors);
88
89 prefixMonitors.forEach(monitor -> {
90 String oldType = monitor.getType().toString();
91 if (newTypes.contains(oldType)) {
92 Set<String> newHosts = newMonitors.get(oldType);
93 String oldHost = monitor.getHost();
94 if (newHosts.contains(oldHost)) {
95 monToAdd.remove(oldHost, oldHost);
96 } else {
97 monToRemove.add(monitor);
98 }
99 } else {
100 monToRemove.add(monitor);
101 }
102 });
103
104 monToRemove.forEach(Monitor::stopMonitor);
105 prefixMonitors.removeAll(monToRemove);
106
107 //TODO
108 monToAdd.forEach((type, values) -> {
109 if (type.equals(Monitor.Types.RIPE.toString())) {
110 values.forEach(host -> prefixMonitors.add(new RipeMonitor(prefix, host)));
111 } else if (type.equals(Monitor.Types.EXABGP.toString())) {
112 values.forEach(host -> prefixMonitors.add(new ExaBgpMonitor(prefix, host)));
113 }
114 });
115
116 startPrefixMonitors();
117 }
118
119 @Override
120 public int hashCode() {
121 return Objects.hashCode(prefix);
122 }
123
124 @Override
125 public boolean equals(Object obj) {
126 if (this == obj) {
127 return true;
128 }
129 if (obj instanceof PrefixHandler) {
130 final PrefixHandler that = (PrefixHandler) obj;
131 return Objects.equals(this.prefix, that.prefix);
132 }
133 return false;
134 }
135
136}