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