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