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