kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.faultmanagement.impl; |
| 17 | |
| 18 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 19 | import java.util.Dictionary; |
| 20 | import java.util.HashSet; |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | import java.util.concurrent.atomic.AtomicLong; |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Modified; |
| 29 | import org.apache.felix.scr.annotations.Property; |
| 30 | import static org.onlab.util.Tools.nullIsNotFound; |
| 31 | |
| 32 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 33 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId; |
| 34 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEvent; |
| 35 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
| 36 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmListener; |
| 37 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService; |
| 38 | import org.onosproject.net.ConnectPoint; |
| 39 | import org.onosproject.net.DeviceId; |
| 40 | import org.slf4j.Logger; |
| 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | import org.apache.felix.scr.annotations.Service; |
| 43 | import static org.onlab.util.Tools.get; |
| 44 | import org.onosproject.core.ApplicationId; |
| 45 | import org.onosproject.core.IdGenerator; |
| 46 | import org.onosproject.core.CoreService; |
| 47 | import org.apache.felix.scr.annotations.Reference; |
| 48 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 49 | import org.onlab.util.ItemNotFoundException; |
| 50 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 51 | import org.osgi.service.component.ComponentContext; |
| 52 | |
| 53 | /** |
| 54 | * Implementation of the Alarm service. |
| 55 | */ |
| 56 | @Component(immediate = true) |
| 57 | @Service |
| 58 | public 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 |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 76 | public Alarm update(Alarm replacement) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 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 |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 90 | public int getActiveAlarmCount(DeviceId deviceId) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 91 | //TODO |
| 92 | throw new UnsupportedOperationException(NOT_SUPPORTED_YET); |
| 93 | } |
| 94 | private static final String NOT_SUPPORTED_YET = "Not supported yet."; |
| 95 | |
| 96 | @Override |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 97 | public Alarm getAlarm(AlarmId alarmId) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 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 | |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 116 | private static DefaultAlarm generateFake(DeviceId deviceId, AlarmId alarmId) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 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 |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 128 | public Set<Alarm> getAlarms(Alarm.SeverityLevel severity) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 129 | //TODO |
| 130 | throw new UnsupportedOperationException(NOT_SUPPORTED_YET); |
| 131 | } |
| 132 | |
| 133 | @Override |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 134 | public Set<Alarm> getAlarms(DeviceId deviceId) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 135 | //TODO |
| 136 | throw new UnsupportedOperationException(NOT_SUPPORTED_YET); |
| 137 | } |
| 138 | |
| 139 | @Override |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 140 | public Set<Alarm> getAlarms(DeviceId deviceId, AlarmEntityId source) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 141 | //TODO |
| 142 | throw new UnsupportedOperationException(NOT_SUPPORTED_YET); |
| 143 | } |
| 144 | |
| 145 | @Override |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 146 | public Set<Alarm> getAlarmsForLink(ConnectPoint src, ConnectPoint dst) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 147 | //TODO |
| 148 | throw new UnsupportedOperationException(NOT_SUPPORTED_YET); |
| 149 | } |
| 150 | |
| 151 | @Override |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 152 | public Set<Alarm> getAlarmsForFlow(DeviceId deviceId, long flowId) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 153 | //TODO |
| 154 | throw new UnsupportedOperationException(NOT_SUPPORTED_YET); |
| 155 | } |
| 156 | |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 157 | private void discoverAlarmsForDevice(DeviceId deviceId) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 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 |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 169 | public void event(AlarmEvent event) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 170 | // TODO |
| 171 | throw new UnsupportedOperationException(NOT_SUPPORTED_YET); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | @Activate |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 176 | public void activate(ComponentContext context) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 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 |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 188 | public void deactivate(ComponentContext context) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 189 | log.info("Deactivate ..."); |
| 190 | // cfgService.unregisterProperties(getClass(), false); |
| 191 | |
| 192 | log.info("Stopped"); |
| 193 | } |
| 194 | |
| 195 | @Modified |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 196 | public boolean modified(ComponentContext context) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 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 | |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame^] | 212 | private void discover(String ipaddresses) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 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 | } |