Brian Stanke | ca93d9a | 2016-02-10 09:17:35 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 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.incubator.net.key.impl; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.onosproject.event.AbstractListenerManager; |
| 24 | import org.onosproject.incubator.net.key.DeviceKey; |
| 25 | import org.onosproject.incubator.net.key.DeviceKeyAdminService; |
| 26 | import org.onosproject.incubator.net.key.DeviceKeyEvent; |
| 27 | import org.onosproject.incubator.net.key.DeviceKeyId; |
| 28 | import org.onosproject.incubator.net.key.DeviceKeyListener; |
| 29 | import org.onosproject.incubator.net.key.DeviceKeyService; |
| 30 | import org.onosproject.incubator.net.key.DeviceKeyStore; |
| 31 | import org.onosproject.incubator.net.key.DeviceKeyStoreDelegate; |
| 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | import java.util.Collection; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | import static org.onosproject.security.AppGuard.checkPermission; |
| 38 | import static org.onosproject.security.AppPermission.Type.DEVICE_KEY_READ; |
| 39 | import static org.onosproject.security.AppPermission.Type.DEVICE_KEY_WRITE; |
| 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Implementation of device key services. |
| 44 | */ |
| 45 | public class DeviceKeyManager extends AbstractListenerManager<DeviceKeyEvent, DeviceKeyListener> |
| 46 | implements DeviceKeyService, DeviceKeyAdminService { |
| 47 | |
| 48 | private final Logger log = getLogger(getClass()); |
| 49 | |
| 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected DeviceKeyService deviceKeyService; |
| 52 | |
| 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) { |
| 74 | checkPermission(DEVICE_KEY_WRITE); |
| 75 | checkNotNull(deviceKey, "Device key cannot be null"); |
| 76 | store.createOrUpdateDeviceKey(deviceKey); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void removeKey(DeviceKeyId deviceKeyId) { |
| 81 | checkPermission(DEVICE_KEY_WRITE); |
| 82 | checkNotNull(deviceKeyId, "Device key identifier cannot be null"); |
| 83 | store.deleteDeviceKey(deviceKeyId); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Collection<DeviceKey> getDeviceKeys() { |
| 88 | checkPermission(DEVICE_KEY_READ); |
| 89 | return store.getDeviceKeys(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public DeviceKey getDeviceKey(DeviceKeyId deviceKeyId) { |
| 94 | checkPermission(DEVICE_KEY_READ); |
| 95 | checkNotNull(deviceKeyId, "Device key identifier cannot be null"); |
| 96 | return store.getDeviceKey(deviceKeyId); |
| 97 | } |
| 98 | } |
| 99 | |