maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [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.fnl.impl; |
| 17 | |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 18 | import org.onlab.util.Tools; |
| 19 | import org.onosproject.cfg.ComponentConfigService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.core.CoreService; |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 22 | import org.onosproject.fnl.intf.NetworkAnomaly; |
| 23 | import org.onosproject.fnl.intf.NetworkDiagnostic; |
| 24 | import org.onosproject.fnl.intf.NetworkDiagnostic.Type; |
| 25 | import org.onosproject.fnl.intf.NetworkDiagnosticService; |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.flow.FlowRuleService; |
| 28 | import org.onosproject.net.host.HostService; |
| 29 | import org.onosproject.net.link.LinkService; |
| 30 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | import org.osgi.service.component.annotations.Activate; |
| 32 | import org.osgi.service.component.annotations.Component; |
| 33 | import org.osgi.service.component.annotations.Deactivate; |
| 34 | import org.osgi.service.component.annotations.Modified; |
| 35 | import org.osgi.service.component.annotations.Reference; |
| 36 | import org.osgi.service.component.annotations.ReferenceCardinality; |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import java.util.Dictionary; |
| 41 | import java.util.HashSet; |
| 42 | import java.util.Map; |
| 43 | import java.util.Set; |
| 44 | import java.util.concurrent.ConcurrentHashMap; |
| 45 | |
| 46 | import static com.google.common.base.Preconditions.checkNotNull; |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 47 | import static org.onosproject.fnl.impl.OsgiPropertyConstants.AUTO_REGISTER_DEFAULT_DIAGNOSTICS; |
| 48 | import static org.onosproject.fnl.impl.OsgiPropertyConstants.AUTO_REGISTER_DEFAULT_DIAGNOSTICS_DEFAULT; |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Default implementation of the Network Troubleshooting Core Service. |
| 52 | * |
| 53 | * It is simply modularized at present. |
| 54 | */ |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 55 | @Component( |
| 56 | immediate = true, |
| 57 | service = NetworkDiagnosticService.class, |
| 58 | property = { |
| 59 | AUTO_REGISTER_DEFAULT_DIAGNOSTICS + ":Boolean=" + AUTO_REGISTER_DEFAULT_DIAGNOSTICS_DEFAULT |
| 60 | } |
| 61 | ) |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 62 | public class NetworkDiagnosticManager implements NetworkDiagnosticService { |
| 63 | |
| 64 | /** |
| 65 | * Name of Network Troubleshooting Application. |
| 66 | */ |
| 67 | public static final String NTS_APP_NAME = |
| 68 | "org.onosproject.FNL.Network-Troubleshoot"; |
| 69 | |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 70 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 71 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 73 | protected CoreService coreService; |
| 74 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 76 | protected ComponentConfigService cfgService; |
| 77 | |
| 78 | |
| 79 | // ------ service below is for auto register ------ |
| 80 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 82 | protected DeviceService ds; |
| 83 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 85 | protected HostService hs; |
| 86 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 88 | protected FlowRuleService frs; |
| 89 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 91 | protected LinkService ls; |
| 92 | |
| 93 | |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 94 | /** Automatically register all of default diagnostic modules. */ |
| 95 | private boolean autoRegisterDefaultDiagnostics = AUTO_REGISTER_DEFAULT_DIAGNOSTICS_DEFAULT; |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 96 | |
| 97 | |
| 98 | private ApplicationId appId; |
| 99 | |
| 100 | private Map<Type, NetworkDiagnostic> diagnostics = new ConcurrentHashMap<>(); |
| 101 | |
| 102 | |
| 103 | @Activate |
| 104 | protected void activate(ComponentContext context) { |
| 105 | appId = coreService.registerApplication(NTS_APP_NAME); |
| 106 | |
| 107 | cfgService.registerProperties(getClass()); |
| 108 | readConfiguration(context); |
| 109 | |
| 110 | autoRegisterDiagnostics(); |
| 111 | |
| 112 | log.info("Started"); |
| 113 | } |
| 114 | |
| 115 | @Deactivate |
| 116 | protected void deactivate() { |
| 117 | unregisterDiagnostics(); |
| 118 | log.info("Stopped"); |
| 119 | } |
| 120 | |
| 121 | @Modified |
| 122 | protected void modified(ComponentContext context) { |
| 123 | |
| 124 | readConfiguration(context); |
| 125 | |
| 126 | // We should not register default diagnostics automatically |
| 127 | // in the Modify(), because that will erase the result of |
| 128 | // dynamic extension, run-time and custom diagnostics. |
| 129 | // |
| 130 | // And, using this modified() is to avoid deactivate-activate procedure. |
| 131 | |
| 132 | log.info("Modified"); |
| 133 | } |
| 134 | |
| 135 | private void readConfiguration(ComponentContext context) { |
| 136 | Dictionary<?, ?> properties = context.getProperties(); |
| 137 | |
| 138 | Boolean autoRegisterEnabled = |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 139 | Tools.isPropertyEnabled(properties, AUTO_REGISTER_DEFAULT_DIAGNOSTICS); |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 140 | if (autoRegisterEnabled == null) { |
| 141 | log.warn("Auto Register is not configured, " + |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 142 | "using current value of {}", autoRegisterDefaultDiagnostics); |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 143 | } else { |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 144 | autoRegisterDefaultDiagnostics = autoRegisterEnabled; |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 145 | log.info("Configured. Auto Register is {}", |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 146 | autoRegisterDefaultDiagnostics ? "enabled" : "disabled"); |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | private void autoRegisterDiagnostics() { |
Ray Milkey | 4694e06 | 2018-10-31 13:17:18 -0700 | [diff] [blame] | 151 | if (!autoRegisterDefaultDiagnostics) { |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
| 155 | // TODO: 10/26/16 future new default diagnostics should be added here. |
| 156 | register(new DefaultCheckLoop(ds, hs, frs, ls)); |
| 157 | } |
| 158 | |
| 159 | private void unregisterDiagnostics() { |
| 160 | // TODO: 10/27/16 improve this for parallel. |
| 161 | diagnostics.clear(); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Set<NetworkAnomaly> findAnomalies() { |
| 166 | Set<NetworkAnomaly> allAnomalies = new HashSet<>(); |
| 167 | |
| 168 | diagnostics.forEach((type, diag) -> |
| 169 | allAnomalies.addAll(diag.findAnomalies())); |
| 170 | |
| 171 | return allAnomalies; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public Set<NetworkAnomaly> findAnomalies(Type type) { |
| 176 | checkNotNull(type, "NetworkAnomaly Type cannot be null"); |
| 177 | |
| 178 | Set<NetworkAnomaly> anomalies = new HashSet<>(); |
| 179 | |
| 180 | NetworkDiagnostic diag = diagnostics.get(type); |
| 181 | if (diag == null) { |
| 182 | log.warn("no anomalies of type {} found", type); |
| 183 | return anomalies; |
| 184 | } |
| 185 | |
| 186 | anomalies.addAll(diag.findAnomalies()); |
| 187 | |
| 188 | return anomalies; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void register(NetworkDiagnostic diag) { |
| 193 | checkNotNull(diag, "Diagnostic cannot be null"); |
| 194 | |
| 195 | NetworkDiagnostic oldDiag = diagnostics.put(diag.type(), diag); |
| 196 | |
| 197 | if (oldDiag != null) { |
| 198 | log.warn("previous diagnostic {} is replaced by {},", |
| 199 | oldDiag.getClass(), diag.getClass()); |
| 200 | } else { |
| 201 | log.info("Register {} type module: {}", |
| 202 | diag.type(), diag.getClass().getName()); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public boolean unregister(NetworkDiagnostic diag) { |
| 208 | checkNotNull(diag, "Diagnostic cannot be null"); |
| 209 | |
| 210 | return diagnostics.remove(diag.type(), diag); |
| 211 | } |
| 212 | } |