Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [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 | |
| 17 | package org.onosproject.faultmanagement.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 20 | import org.onosproject.faultmanagement.api.AlarmStore; |
| 21 | import org.onosproject.faultmanagement.api.AlarmStoreDelegate; |
| 22 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 23 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId; |
| 24 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEvent; |
| 25 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
| 26 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.store.AbstractStore; |
| 29 | import org.onosproject.store.serializers.KryoNamespaces; |
| 30 | import org.onosproject.store.service.ConsistentMap; |
| 31 | import org.onosproject.store.service.MapEvent; |
| 32 | import org.onosproject.store.service.MapEventListener; |
| 33 | import org.onosproject.store.service.Serializer; |
| 34 | import org.onosproject.store.service.StorageService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 35 | import org.osgi.service.component.annotations.Activate; |
| 36 | import org.osgi.service.component.annotations.Component; |
| 37 | import org.osgi.service.component.annotations.Deactivate; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 38 | import org.osgi.service.component.annotations.Reference; |
| 39 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
| 42 | import java.util.Collection; |
| 43 | import java.util.Map; |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 44 | import java.util.Objects; |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 45 | import java.util.stream.Collectors; |
| 46 | |
| 47 | import static org.slf4j.LoggerFactory.getLogger; |
| 48 | |
| 49 | /** |
| 50 | * Manages information of alarms using gossip protocol to distribute |
| 51 | * information. |
| 52 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 53 | @Component(immediate = true, service = AlarmStore.class) |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 54 | public class DistributedAlarmStore |
| 55 | extends AbstractStore<AlarmEvent, AlarmStoreDelegate> |
| 56 | implements AlarmStore { |
| 57 | |
| 58 | private final Logger log = getLogger(getClass()); |
| 59 | private ConsistentMap<AlarmId, Alarm> alarms; |
| 60 | private Map<AlarmId, Alarm> alarmsMap; |
| 61 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 63 | protected StorageService storageService; |
| 64 | |
| 65 | private final MapEventListener<AlarmId, Alarm> listener = new InternalListener(); |
| 66 | |
| 67 | @Activate |
| 68 | public void activate() { |
| 69 | log.info("Started"); |
| 70 | alarms = storageService.<AlarmId, Alarm>consistentMapBuilder() |
| 71 | .withName("onos-alarm-table") |
| 72 | .withSerializer(Serializer.using(KryoNamespaces.API, |
| 73 | Alarm.class, |
| 74 | DefaultAlarm.class, |
| 75 | AlarmId.class, |
| 76 | AlarmEvent.Type.class, |
| 77 | Alarm.SeverityLevel.class, |
| 78 | AlarmEntityId.class)) |
| 79 | .build(); |
| 80 | alarms.addListener(listener); |
| 81 | alarmsMap = alarms.asJavaMap(); |
| 82 | } |
| 83 | |
| 84 | @Deactivate |
| 85 | public void deactivate() { |
| 86 | alarms.removeListener(listener); |
| 87 | log.info("Stopped"); |
| 88 | } |
| 89 | |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 90 | @Override |
| 91 | public Alarm getAlarm(AlarmId alarmId) { |
| 92 | return alarmsMap.get(alarmId); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Collection<Alarm> getAlarms() { |
| 97 | return ImmutableSet.copyOf(alarmsMap.values()); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Collection<Alarm> getAlarms(DeviceId deviceId) { |
| 102 | //FIXME: this is expensive, need refactoring when core maps provide different indexes. |
| 103 | return ImmutableSet.copyOf(alarmsMap.values().stream() |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 104 | .filter(alarm -> alarm.deviceId().equals(deviceId)) |
| 105 | .collect(Collectors.toSet())); |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 109 | public void createOrUpdateAlarm(Alarm alarm) { |
| 110 | Alarm existing = alarmsMap.get(alarm.id()); |
| 111 | if (Objects.equals(existing, alarm)) { |
fahadnaeemkhan | 26072c6 | 2017-11-09 20:19:31 -0800 | [diff] [blame] | 112 | log.debug("Received identical alarm, no operation needed on {}", alarm.id()); |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 113 | } else { |
| 114 | alarms.put(alarm.id(), alarm); |
| 115 | } |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void removeAlarm(AlarmId alarmId) { |
| 120 | alarms.remove(alarmId); |
| 121 | } |
| 122 | |
| 123 | //Event listener to notify delegates about Map events. |
| 124 | private class InternalListener implements MapEventListener<AlarmId, Alarm> { |
| 125 | |
| 126 | @Override |
| 127 | public void event(MapEvent<AlarmId, Alarm> mapEvent) { |
| 128 | final AlarmEvent.Type type; |
| 129 | final Alarm alarm; |
| 130 | switch (mapEvent.type()) { |
| 131 | case INSERT: |
| 132 | type = AlarmEvent.Type.CREATED; |
| 133 | alarm = mapEvent.newValue().value(); |
| 134 | break; |
| 135 | case UPDATE: |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 136 | type = AlarmEvent.Type.UPDATED; |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 137 | alarm = mapEvent.newValue().value(); |
| 138 | break; |
| 139 | case REMOVE: |
| 140 | type = AlarmEvent.Type.REMOVED; |
| 141 | alarm = mapEvent.oldValue().value(); |
| 142 | break; |
| 143 | default: |
| 144 | throw new IllegalArgumentException("Wrong event type " + mapEvent.type()); |
| 145 | } |
| 146 | notifyDelegate(new AlarmEvent(type, alarm)); |
| 147 | } |
| 148 | } |
| 149 | } |