Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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.net.key.impl; |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 18 | |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 19 | import org.onosproject.event.AbstractListenerManager; |
Claudine Chiu | 31ad527 | 2016-02-17 20:56:24 +0000 | [diff] [blame] | 20 | import org.onosproject.net.key.DeviceKey; |
| 21 | import org.onosproject.net.key.DeviceKeyAdminService; |
| 22 | import org.onosproject.net.key.DeviceKeyEvent; |
| 23 | import org.onosproject.net.key.DeviceKeyId; |
| 24 | import org.onosproject.net.key.DeviceKeyListener; |
| 25 | import org.onosproject.net.key.DeviceKeyService; |
| 26 | import org.onosproject.net.key.DeviceKeyStore; |
| 27 | import org.onosproject.net.key.DeviceKeyStoreDelegate; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 28 | import org.osgi.service.component.annotations.Activate; |
| 29 | import org.osgi.service.component.annotations.Component; |
| 30 | import org.osgi.service.component.annotations.Deactivate; |
| 31 | import org.osgi.service.component.annotations.Reference; |
| 32 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.util.Collection; |
| 36 | |
| 37 | import static com.google.common.base.Preconditions.checkNotNull; |
| 38 | import static org.onosproject.security.AppGuard.checkPermission; |
| 39 | import static org.onosproject.security.AppPermission.Type.DEVICE_KEY_READ; |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Implementation of device key services. |
| 44 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 45 | @Component(immediate = true, service = { DeviceKeyService.class, DeviceKeyAdminService.class }) |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 46 | public class DeviceKeyManager extends AbstractListenerManager<DeviceKeyEvent, DeviceKeyListener> |
| 47 | implements DeviceKeyService, DeviceKeyAdminService { |
| 48 | |
| 49 | private final Logger log = getLogger(getClass()); |
| 50 | |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 51 | private DeviceKeyStoreDelegate delegate = this::post; |
| 52 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 54 | protected DeviceKeyStore store; |
| 55 | |
| 56 | @Activate |
| 57 | public void activate() { |
| 58 | store.setDelegate(delegate); |
| 59 | eventDispatcher.addSink(DeviceKeyEvent.class, listenerRegistry); |
| 60 | log.info("Started"); |
| 61 | } |
| 62 | |
| 63 | @Deactivate |
| 64 | public void deactivate() { |
| 65 | store.unsetDelegate(delegate); |
| 66 | eventDispatcher.removeSink(DeviceKeyEvent.class); |
| 67 | log.info("Stopped"); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void addKey(DeviceKey deviceKey) { |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 72 | checkNotNull(deviceKey, "Device key cannot be null"); |
| 73 | store.createOrUpdateDeviceKey(deviceKey); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void removeKey(DeviceKeyId deviceKeyId) { |
Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame] | 78 | checkNotNull(deviceKeyId, "Device key identifier cannot be null"); |
| 79 | store.deleteDeviceKey(deviceKeyId); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public Collection<DeviceKey> getDeviceKeys() { |
| 84 | checkPermission(DEVICE_KEY_READ); |
| 85 | return store.getDeviceKeys(); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public DeviceKey getDeviceKey(DeviceKeyId deviceKeyId) { |
| 90 | checkPermission(DEVICE_KEY_READ); |
| 91 | checkNotNull(deviceKeyId, "Device key identifier cannot be null"); |
| 92 | return store.getDeviceKey(deviceKeyId); |
| 93 | } |
| 94 | } |
| 95 | |