Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 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; |
| 46 | import java.util.stream.Collectors; |
| 47 | |
| 48 | import static org.slf4j.LoggerFactory.getLogger; |
| 49 | |
| 50 | /** |
| 51 | * Manages information of alarms using gossip protocol to distribute |
| 52 | * information. |
| 53 | */ |
| 54 | @Component(immediate = true) |
| 55 | @Service |
| 56 | public class DistributedAlarmStore |
| 57 | extends AbstractStore<AlarmEvent, AlarmStoreDelegate> |
| 58 | implements AlarmStore { |
| 59 | |
| 60 | private final Logger log = getLogger(getClass()); |
| 61 | private ConsistentMap<AlarmId, Alarm> alarms; |
| 62 | private Map<AlarmId, Alarm> alarmsMap; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected StorageService storageService; |
| 66 | |
| 67 | private final MapEventListener<AlarmId, Alarm> listener = new InternalListener(); |
| 68 | |
| 69 | @Activate |
| 70 | public void activate() { |
| 71 | log.info("Started"); |
| 72 | alarms = storageService.<AlarmId, Alarm>consistentMapBuilder() |
| 73 | .withName("onos-alarm-table") |
| 74 | .withSerializer(Serializer.using(KryoNamespaces.API, |
| 75 | Alarm.class, |
| 76 | DefaultAlarm.class, |
| 77 | AlarmId.class, |
| 78 | AlarmEvent.Type.class, |
| 79 | Alarm.SeverityLevel.class, |
| 80 | AlarmEntityId.class)) |
| 81 | .build(); |
| 82 | alarms.addListener(listener); |
| 83 | alarmsMap = alarms.asJavaMap(); |
| 84 | } |
| 85 | |
| 86 | @Deactivate |
| 87 | public void deactivate() { |
| 88 | alarms.removeListener(listener); |
| 89 | log.info("Stopped"); |
| 90 | } |
| 91 | |
| 92 | @Modified |
| 93 | public boolean modified() { |
| 94 | log.info("Modified"); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Alarm getAlarm(AlarmId alarmId) { |
| 100 | return alarmsMap.get(alarmId); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Collection<Alarm> getAlarms() { |
| 105 | return ImmutableSet.copyOf(alarmsMap.values()); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public Collection<Alarm> getAlarms(DeviceId deviceId) { |
| 110 | //FIXME: this is expensive, need refactoring when core maps provide different indexes. |
| 111 | return ImmutableSet.copyOf(alarmsMap.values().stream() |
| 112 | .filter(alarm -> alarm.deviceId().equals(deviceId)) |
| 113 | .collect(Collectors.toSet())); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void setAlarm(Alarm alarm) { |
| 118 | alarms.put(alarm.id(), alarm); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void removeAlarm(AlarmId alarmId) { |
| 123 | alarms.remove(alarmId); |
| 124 | } |
| 125 | |
| 126 | //Event listener to notify delegates about Map events. |
| 127 | private class InternalListener implements MapEventListener<AlarmId, Alarm> { |
| 128 | |
| 129 | @Override |
| 130 | public void event(MapEvent<AlarmId, Alarm> mapEvent) { |
| 131 | final AlarmEvent.Type type; |
| 132 | final Alarm alarm; |
| 133 | switch (mapEvent.type()) { |
| 134 | case INSERT: |
| 135 | type = AlarmEvent.Type.CREATED; |
| 136 | alarm = mapEvent.newValue().value(); |
| 137 | break; |
| 138 | case UPDATE: |
| 139 | type = AlarmEvent.Type.CREATED; |
| 140 | alarm = mapEvent.newValue().value(); |
| 141 | break; |
| 142 | case REMOVE: |
| 143 | type = AlarmEvent.Type.REMOVED; |
| 144 | alarm = mapEvent.oldValue().value(); |
| 145 | break; |
| 146 | default: |
| 147 | throw new IllegalArgumentException("Wrong event type " + mapEvent.type()); |
| 148 | } |
| 149 | notifyDelegate(new AlarmEvent(type, alarm)); |
| 150 | } |
| 151 | } |
| 152 | } |