Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [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 | |
Claudine Chiu | e014b3a | 2016-02-18 19:56:42 +0000 | [diff] [blame] | 17 | package org.onosproject.store.key.impl; |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Claudine Chiu | 31ad527 | 2016-02-17 20:56:24 +0000 | [diff] [blame] | 25 | import org.onosproject.net.key.DeviceKey; |
| 26 | import org.onosproject.net.key.DeviceKeyEvent; |
| 27 | import org.onosproject.net.key.DeviceKeyId; |
| 28 | import org.onosproject.net.key.DeviceKeyStore; |
| 29 | import org.onosproject.net.key.DeviceKeyStoreDelegate; |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 30 | import org.onosproject.store.AbstractStore; |
| 31 | import org.onosproject.store.serializers.KryoNamespaces; |
| 32 | import org.onosproject.store.service.ConsistentMap; |
| 33 | import org.onosproject.store.service.MapEvent; |
| 34 | import org.onosproject.store.service.MapEventListener; |
| 35 | import org.onosproject.store.service.Serializer; |
| 36 | import org.onosproject.store.service.StorageService; |
| 37 | import org.slf4j.Logger; |
| 38 | |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 39 | import java.util.Arrays; |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 40 | import java.util.Collection; |
| 41 | import java.util.Map; |
| 42 | |
| 43 | import static com.google.common.base.Preconditions.checkNotNull; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
| 46 | /** |
| 47 | * A distributed device key store implementation, device keys are stored consistently |
| 48 | * across the cluster. |
| 49 | */ |
| 50 | @Component(immediate = true) |
| 51 | @Service |
| 52 | public class DistributedDeviceKeyStore |
| 53 | extends AbstractStore<DeviceKeyEvent, DeviceKeyStoreDelegate> |
| 54 | implements DeviceKeyStore { |
| 55 | |
| 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected StorageService storageService; |
| 60 | |
| 61 | private ConsistentMap<DeviceKeyId, DeviceKey> deviceKeys; |
| 62 | private Map<DeviceKeyId, DeviceKey> deviceKeysMap; |
| 63 | |
| 64 | private final MapEventListener<DeviceKeyId, DeviceKey> listener = new InternalMapListener(); |
| 65 | |
| 66 | /** |
| 67 | * Activate the distributed device key store. |
| 68 | */ |
| 69 | @Activate |
| 70 | public void activate() { |
| 71 | deviceKeys = storageService.<DeviceKeyId, DeviceKey>consistentMapBuilder() |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 72 | .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API), |
| 73 | DeviceKey.class, |
| 74 | DeviceKeyId.class, |
| 75 | DeviceKey.Type.class)) |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 76 | .withName("onos-device-keys") |
| 77 | .withRelaxedReadConsistency() |
| 78 | .build(); |
| 79 | deviceKeys.addListener(listener); |
| 80 | deviceKeysMap = deviceKeys.asJavaMap(); |
| 81 | |
| 82 | log.info("Started"); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Deactivate the distributed device key store. |
| 87 | */ |
| 88 | @Deactivate |
| 89 | public void deactivate() { |
| 90 | deviceKeys.removeListener(listener); |
| 91 | log.info("Stopped"); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void createOrUpdateDeviceKey(DeviceKey deviceKey) { |
| 96 | |
| 97 | // Add the device key to the store, if the device key already exists |
| 98 | // then it will be replaced with the new one. |
| 99 | deviceKeys.put(deviceKey.deviceKeyId(), deviceKey); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void deleteDeviceKey(DeviceKeyId deviceKeyId) { |
| 104 | // Remove the device key from the store if the device key identifier exists. |
| 105 | deviceKeys.remove(deviceKeyId); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public Collection<DeviceKey> getDeviceKeys() { |
| 110 | return deviceKeysMap.values(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public DeviceKey getDeviceKey(DeviceKeyId deviceKeyId) { |
| 115 | return deviceKeysMap.get(deviceKeyId); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Listener class to map listener events to the device key events. |
| 120 | */ |
| 121 | private class InternalMapListener implements MapEventListener<DeviceKeyId, DeviceKey> { |
| 122 | @Override |
| 123 | public void event(MapEvent<DeviceKeyId, DeviceKey> event) { |
| 124 | DeviceKey deviceKey = null; |
| 125 | |
| 126 | DeviceKeyEvent.Type type = null; |
| 127 | switch (event.type()) { |
| 128 | case INSERT: |
| 129 | type = DeviceKeyEvent.Type.DEVICE_KEY_ADDED; |
| 130 | deviceKey = checkNotNull(event.newValue().value()); |
| 131 | break; |
| 132 | case UPDATE: |
| 133 | type = DeviceKeyEvent.Type.DEVICE_KEY_UPDATED; |
| 134 | deviceKey = checkNotNull(event.newValue().value()); |
| 135 | break; |
| 136 | case REMOVE: |
| 137 | type = DeviceKeyEvent.Type.DEVICE_KEY_REMOVED; |
| 138 | deviceKey = checkNotNull(event.oldValue().value()); |
| 139 | break; |
| 140 | default: |
| 141 | log.error("Unsupported event type: " + event.type()); |
| 142 | } |
| 143 | notifyDelegate(new DeviceKeyEvent(type, deviceKey)); |
| 144 | } |
| 145 | } |
| 146 | } |