blob: 91d1364e69ef0fe435293e802ee5473cb99e4633 [file] [log] [blame]
Brian Stankeca93d9a2016-02-10 09:17:35 -05001/*
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
17package org.onosproject.incubator.net.key.impl;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.onosproject.event.AbstractListenerManager;
24import org.onosproject.incubator.net.key.DeviceKey;
25import org.onosproject.incubator.net.key.DeviceKeyAdminService;
26import org.onosproject.incubator.net.key.DeviceKeyEvent;
27import org.onosproject.incubator.net.key.DeviceKeyId;
28import org.onosproject.incubator.net.key.DeviceKeyListener;
29import org.onosproject.incubator.net.key.DeviceKeyService;
30import org.onosproject.incubator.net.key.DeviceKeyStore;
31import org.onosproject.incubator.net.key.DeviceKeyStoreDelegate;
32import org.slf4j.Logger;
33
34import java.util.Collection;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37import static org.onosproject.security.AppGuard.checkPermission;
38import static org.onosproject.security.AppPermission.Type.DEVICE_KEY_READ;
39import static org.onosproject.security.AppPermission.Type.DEVICE_KEY_WRITE;
40import static org.slf4j.LoggerFactory.getLogger;
41
42/**
43 * Implementation of device key services.
44 */
45public 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