blob: b787447bbe6714300bf01d29ba821750e1a25c16 [file] [log] [blame]
maojianwei42e23442016-02-15 10:40:48 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
maojianwei42e23442016-02-15 10:40:48 +08003 *
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.fnl.impl;
17
maojianwei42e23442016-02-15 10:40:48 +080018import org.onlab.util.Tools;
19import org.onosproject.cfg.ComponentConfigService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.onosproject.core.ApplicationId;
21import org.onosproject.core.CoreService;
maojianwei42e23442016-02-15 10:40:48 +080022import org.onosproject.fnl.intf.NetworkAnomaly;
23import org.onosproject.fnl.intf.NetworkDiagnostic;
24import org.onosproject.fnl.intf.NetworkDiagnostic.Type;
25import org.onosproject.fnl.intf.NetworkDiagnosticService;
maojianwei42e23442016-02-15 10:40:48 +080026import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.flow.FlowRuleService;
28import org.onosproject.net.host.HostService;
29import org.onosproject.net.link.LinkService;
30import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031import org.osgi.service.component.annotations.Activate;
32import org.osgi.service.component.annotations.Component;
33import org.osgi.service.component.annotations.Deactivate;
34import org.osgi.service.component.annotations.Modified;
35import org.osgi.service.component.annotations.Reference;
36import org.osgi.service.component.annotations.ReferenceCardinality;
maojianwei42e23442016-02-15 10:40:48 +080037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
40import java.util.Dictionary;
41import java.util.HashSet;
42import java.util.Map;
43import java.util.Set;
44import java.util.concurrent.ConcurrentHashMap;
45
46import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkey4694e062018-10-31 13:17:18 -070047import static org.onosproject.fnl.impl.OsgiPropertyConstants.AUTO_REGISTER_DEFAULT_DIAGNOSTICS;
48import static org.onosproject.fnl.impl.OsgiPropertyConstants.AUTO_REGISTER_DEFAULT_DIAGNOSTICS_DEFAULT;
maojianwei42e23442016-02-15 10:40:48 +080049
50/**
51 * Default implementation of the Network Troubleshooting Core Service.
52 *
53 * It is simply modularized at present.
54 */
Ray Milkey4694e062018-10-31 13:17:18 -070055@Component(
56 immediate = true,
57 service = NetworkDiagnosticService.class,
58 property = {
59 AUTO_REGISTER_DEFAULT_DIAGNOSTICS + ":Boolean=" + AUTO_REGISTER_DEFAULT_DIAGNOSTICS_DEFAULT
60 }
61)
maojianwei42e23442016-02-15 10:40:48 +080062public 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
maojianwei42e23442016-02-15 10:40:48 +080070 private final Logger log = LoggerFactory.getLogger(getClass());
71
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 @Reference(cardinality = ReferenceCardinality.MANDATORY)
maojianwei42e23442016-02-15 10:40:48 +080073 protected CoreService coreService;
74
Ray Milkeyd84f89b2018-08-17 14:54:17 -070075 @Reference(cardinality = ReferenceCardinality.MANDATORY)
maojianwei42e23442016-02-15 10:40:48 +080076 protected ComponentConfigService cfgService;
77
78
79 // ------ service below is for auto register ------
80
Ray Milkeyd84f89b2018-08-17 14:54:17 -070081 @Reference(cardinality = ReferenceCardinality.MANDATORY)
maojianwei42e23442016-02-15 10:40:48 +080082 protected DeviceService ds;
83
Ray Milkeyd84f89b2018-08-17 14:54:17 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY)
maojianwei42e23442016-02-15 10:40:48 +080085 protected HostService hs;
86
Ray Milkeyd84f89b2018-08-17 14:54:17 -070087 @Reference(cardinality = ReferenceCardinality.MANDATORY)
maojianwei42e23442016-02-15 10:40:48 +080088 protected FlowRuleService frs;
89
Ray Milkeyd84f89b2018-08-17 14:54:17 -070090 @Reference(cardinality = ReferenceCardinality.MANDATORY)
maojianwei42e23442016-02-15 10:40:48 +080091 protected LinkService ls;
92
93
Ray Milkey4694e062018-10-31 13:17:18 -070094 /** Automatically register all of default diagnostic modules. */
95 private boolean autoRegisterDefaultDiagnostics = AUTO_REGISTER_DEFAULT_DIAGNOSTICS_DEFAULT;
maojianwei42e23442016-02-15 10:40:48 +080096
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 Milkey4694e062018-10-31 13:17:18 -0700139 Tools.isPropertyEnabled(properties, AUTO_REGISTER_DEFAULT_DIAGNOSTICS);
maojianwei42e23442016-02-15 10:40:48 +0800140 if (autoRegisterEnabled == null) {
141 log.warn("Auto Register is not configured, " +
Ray Milkey4694e062018-10-31 13:17:18 -0700142 "using current value of {}", autoRegisterDefaultDiagnostics);
maojianwei42e23442016-02-15 10:40:48 +0800143 } else {
Ray Milkey4694e062018-10-31 13:17:18 -0700144 autoRegisterDefaultDiagnostics = autoRegisterEnabled;
maojianwei42e23442016-02-15 10:40:48 +0800145 log.info("Configured. Auto Register is {}",
Ray Milkey4694e062018-10-31 13:17:18 -0700146 autoRegisterDefaultDiagnostics ? "enabled" : "disabled");
maojianwei42e23442016-02-15 10:40:48 +0800147 }
148 }
149
150 private void autoRegisterDiagnostics() {
Ray Milkey4694e062018-10-31 13:17:18 -0700151 if (!autoRegisterDefaultDiagnostics) {
maojianwei42e23442016-02-15 10:40:48 +0800152 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}