blob: 3a9635cb9b24e07bba90d9938e3bc8eb44af6120 [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;
47
48/**
49 * Default implementation of the Network Troubleshooting Core Service.
50 *
51 * It is simply modularized at present.
52 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070053@Component(immediate = true, service = NetworkDiagnosticService.class)
maojianwei42e23442016-02-15 10:40:48 +080054public class NetworkDiagnosticManager implements NetworkDiagnosticService {
55
56 /**
57 * Name of Network Troubleshooting Application.
58 */
59 public static final String NTS_APP_NAME =
60 "org.onosproject.FNL.Network-Troubleshoot";
61
62 private static final String PROPERTY_AUTO_REGISTER_DIAG =
63 "autoRegisterDefaultDiagnostics";
64
65 private final Logger log = LoggerFactory.getLogger(getClass());
66
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY)
maojianwei42e23442016-02-15 10:40:48 +080068 protected CoreService coreService;
69
Ray Milkeyd84f89b2018-08-17 14:54:17 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY)
maojianwei42e23442016-02-15 10:40:48 +080071 protected ComponentConfigService cfgService;
72
73
74 // ------ service below is for auto register ------
75
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY)
maojianwei42e23442016-02-15 10:40:48 +080077 protected DeviceService ds;
78
Ray Milkeyd84f89b2018-08-17 14:54:17 -070079 @Reference(cardinality = ReferenceCardinality.MANDATORY)
maojianwei42e23442016-02-15 10:40:48 +080080 protected HostService hs;
81
Ray Milkeyd84f89b2018-08-17 14:54:17 -070082 @Reference(cardinality = ReferenceCardinality.MANDATORY)
maojianwei42e23442016-02-15 10:40:48 +080083 protected FlowRuleService frs;
84
Ray Milkeyd84f89b2018-08-17 14:54:17 -070085 @Reference(cardinality = ReferenceCardinality.MANDATORY)
maojianwei42e23442016-02-15 10:40:48 +080086 protected LinkService ls;
87
88
Ray Milkeyd84f89b2018-08-17 14:54:17 -070089 //@Property(name = PROPERTY_AUTO_REGISTER_DIAG, boolValue = true,
90 // label = "Automatically register all of default diagnostic modules.")
maojianwei42e23442016-02-15 10:40:48 +080091 private boolean autoRegister = true;
92
93
94 private ApplicationId appId;
95
96 private Map<Type, NetworkDiagnostic> diagnostics = new ConcurrentHashMap<>();
97
98
99 @Activate
100 protected void activate(ComponentContext context) {
101 appId = coreService.registerApplication(NTS_APP_NAME);
102
103 cfgService.registerProperties(getClass());
104 readConfiguration(context);
105
106 autoRegisterDiagnostics();
107
108 log.info("Started");
109 }
110
111 @Deactivate
112 protected void deactivate() {
113 unregisterDiagnostics();
114 log.info("Stopped");
115 }
116
117 @Modified
118 protected void modified(ComponentContext context) {
119
120 readConfiguration(context);
121
122 // We should not register default diagnostics automatically
123 // in the Modify(), because that will erase the result of
124 // dynamic extension, run-time and custom diagnostics.
125 //
126 // And, using this modified() is to avoid deactivate-activate procedure.
127
128 log.info("Modified");
129 }
130
131 private void readConfiguration(ComponentContext context) {
132 Dictionary<?, ?> properties = context.getProperties();
133
134 Boolean autoRegisterEnabled =
135 Tools.isPropertyEnabled(properties, PROPERTY_AUTO_REGISTER_DIAG);
136 if (autoRegisterEnabled == null) {
137 log.warn("Auto Register is not configured, " +
138 "using current value of {}", autoRegister);
139 } else {
140 autoRegister = autoRegisterEnabled;
141 log.info("Configured. Auto Register is {}",
142 autoRegister ? "enabled" : "disabled");
143 }
144 }
145
146 private void autoRegisterDiagnostics() {
147 if (!autoRegister) {
148 return;
149 }
150
151 // TODO: 10/26/16 future new default diagnostics should be added here.
152 register(new DefaultCheckLoop(ds, hs, frs, ls));
153 }
154
155 private void unregisterDiagnostics() {
156 // TODO: 10/27/16 improve this for parallel.
157 diagnostics.clear();
158 }
159
160 @Override
161 public Set<NetworkAnomaly> findAnomalies() {
162 Set<NetworkAnomaly> allAnomalies = new HashSet<>();
163
164 diagnostics.forEach((type, diag) ->
165 allAnomalies.addAll(diag.findAnomalies()));
166
167 return allAnomalies;
168 }
169
170 @Override
171 public Set<NetworkAnomaly> findAnomalies(Type type) {
172 checkNotNull(type, "NetworkAnomaly Type cannot be null");
173
174 Set<NetworkAnomaly> anomalies = new HashSet<>();
175
176 NetworkDiagnostic diag = diagnostics.get(type);
177 if (diag == null) {
178 log.warn("no anomalies of type {} found", type);
179 return anomalies;
180 }
181
182 anomalies.addAll(diag.findAnomalies());
183
184 return anomalies;
185 }
186
187 @Override
188 public void register(NetworkDiagnostic diag) {
189 checkNotNull(diag, "Diagnostic cannot be null");
190
191 NetworkDiagnostic oldDiag = diagnostics.put(diag.type(), diag);
192
193 if (oldDiag != null) {
194 log.warn("previous diagnostic {} is replaced by {},",
195 oldDiag.getClass(), diag.getClass());
196 } else {
197 log.info("Register {} type module: {}",
198 diag.type(), diag.getClass().getName());
199 }
200 }
201
202 @Override
203 public boolean unregister(NetworkDiagnostic diag) {
204 checkNotNull(diag, "Diagnostic cannot be null");
205
206 return diagnostics.remove(diag.type(), diag);
207 }
208}