blob: 74fe7072f7aaafaa1090f9db11251e7394129b4f [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.faultmanagement.impl;
17
18import static com.google.common.base.Strings.isNullOrEmpty;
19import java.util.Dictionary;
20import java.util.HashSet;
21import java.util.Map;
22import java.util.Set;
23import java.util.concurrent.ConcurrentHashMap;
24import java.util.concurrent.atomic.AtomicLong;
25import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Modified;
29import org.apache.felix.scr.annotations.Property;
30import static org.onlab.util.Tools.nullIsNotFound;
31
32import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
33import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
34import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEvent;
35import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
36import org.onosproject.incubator.net.faultmanagement.alarm.AlarmListener;
37import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService;
38import org.onosproject.net.ConnectPoint;
39import org.onosproject.net.DeviceId;
40import org.slf4j.Logger;
41import static org.slf4j.LoggerFactory.getLogger;
42import org.apache.felix.scr.annotations.Service;
43import static org.onlab.util.Tools.get;
44import org.onosproject.core.ApplicationId;
45import org.onosproject.core.IdGenerator;
46import org.onosproject.core.CoreService;
47import org.apache.felix.scr.annotations.Reference;
48import org.apache.felix.scr.annotations.ReferenceCardinality;
49import org.onlab.util.ItemNotFoundException;
50import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
51import org.osgi.service.component.ComponentContext;
52
53/**
54 * Implementation of the Alarm service.
55 */
56@Component(immediate = true)
57@Service
58public class AlarmsManager implements AlarmService {
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected CoreService coreService;
62 private final Logger log = getLogger(getClass());
63 private ApplicationId appId;
64 private IdGenerator idGenerator;
65
66
67 @Property(name = "fmDevices", value = "127.0.0.1", label = "Instance-specific configurations")
68 private String devConfigs;
69
70 private final Map<AlarmId, Alarm> alarms = new ConcurrentHashMap<>();
71
72
73 private final AtomicLong alarmIdGenerator = new AtomicLong(0);
74
75 @Override
76 public Alarm update(final Alarm replacement) {
77
78 final Alarm found = alarms.get(replacement.id());
79 if (found == null) {
80 throw new ItemNotFoundException("Alarm with id " + replacement.id() + " found");
81 }
82 final Alarm updated = new DefaultAlarm.Builder(found).
83 withAcknowledged(replacement.acknowledged()).
84 withAssignedUser(replacement.assignedUser()).build();
85 alarms.put(replacement.id(), updated);
86 return updated;
87 }
88
89 @Override
90 public int getActiveAlarmCount(final DeviceId deviceId) {
91 //TODO
92 throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
93 }
94 private static final String NOT_SUPPORTED_YET = "Not supported yet.";
95
96 @Override
97 public Alarm getAlarm(final AlarmId alarmId) {
98 return nullIsNotFound(
99 alarms.get(alarmId),
100 "Alarm is not found");
101 }
102
103 @Override
104 public Set<Alarm> getAlarms() {
105 //TODO
106 throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
107 }
108
109 @Override
110 public Set<Alarm> getActiveAlarms() {
111 // Enpty set if no values
112 return alarms.isEmpty() ? new HashSet<>() : new HashSet<>(alarms.values());
113
114 }
115
116 private static DefaultAlarm generateFake(final DeviceId deviceId, final AlarmId alarmId) {
117
118 return new DefaultAlarm.Builder(
119 alarmId, deviceId, "NE is not reachable", Alarm.SeverityLevel.MAJOR, System.currentTimeMillis()).
120 withTimeUpdated(System.currentTimeMillis()).
121 withServiceAffecting(true)
122 .withAcknowledged(true).
123 withManuallyClearable(true)
124 .withAssignedUser("user1").build();
125 }
126
127 @Override
128 public Set<Alarm> getAlarms(final Alarm.SeverityLevel severity) {
129 //TODO
130 throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
131 }
132
133 @Override
134 public Set<Alarm> getAlarms(final DeviceId deviceId) {
135 //TODO
136 throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
137 }
138
139 @Override
140 public Set<Alarm> getAlarms(final DeviceId deviceId, final AlarmEntityId source) {
141 //TODO
142 throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
143 }
144
145 @Override
146 public Set<Alarm> getAlarmsForLink(final ConnectPoint src, final ConnectPoint dst) {
147 //TODO
148 throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
149 }
150
151 @Override
152 public Set<Alarm> getAlarmsForFlow(final DeviceId deviceId, final long flowId) {
153 //TODO
154 throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
155 }
156
157 private void discoverAlarmsForDevice(final DeviceId deviceId) {
158 final AlarmId alarmId = new AlarmId(alarmIdGenerator.incrementAndGet());
159
160 // TODO In a new thread invoke SNMP Provider with DeviceId and device type and when done update our of alarms
161 //
162 alarms.put(alarmId, generateFake(deviceId, alarmId));
163
164 }
165
166 private class InternalAlarmListener implements AlarmListener {
167
168 @Override
169 public void event(final AlarmEvent event) {
170 // TODO
171 throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
172 }
173 }
174
175 @Activate
176 public void activate(final ComponentContext context) {
177 log.info("Activate ...");
178 appId = coreService.registerApplication("org.onos.faultmanagement.alarms");
179 idGenerator = coreService.getIdGenerator("alarm-ids");
180 log.info("Started with appId={} idGenerator={}", appId, idGenerator);
181
182 final boolean result = modified(context);
183 log.info("modified result = {}", result);
184
185 }
186
187 @Deactivate
188 public void deactivate(final ComponentContext context) {
189 log.info("Deactivate ...");
190 // cfgService.unregisterProperties(getClass(), false);
191
192 log.info("Stopped");
193 }
194
195 @Modified
196 public boolean modified(final ComponentContext context) {
197 log.info("context={}", context);
198 if (context == null) {
199 log.info("No configuration file");
200 return false;
201 }
202 final Dictionary<?, ?> properties = context.getProperties();
203 final String ipaddresses = get(properties, "fmDevices");
204 log.info("Settings: devConfigs={}", ipaddresses);
205 if (!isNullOrEmpty(ipaddresses)) {
206 discover(ipaddresses);
207
208 }
209 return true;
210 }
211
212 private void discover(final String ipaddresses) {
213 for (String deviceEntry : ipaddresses.split(",")) {
214 final DeviceId deviceId = DeviceId.deviceId(deviceEntry);
215 if (deviceId != null) {
216 log.info("Device {} needs to have its alarms refreshed!", deviceId);
217 discoverAlarmsForDevice(deviceId);
218 }
219 }
220 }
221
222}