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