blob: cc06f4764579ef0febffac880da3ac71cdbb7495 [file] [log] [blame]
Brian Stankeca93d9a2016-02-10 09:17:35 -05001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian Stankeca93d9a2016-02-10 09:17:35 -05003 *
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
Brian Stankeca93d9a2016-02-10 09:17:35 -050019import org.onosproject.event.AbstractListenerManager;
Claudine Chiu31ad5272016-02-17 20:56:24 +000020import org.onosproject.net.key.DeviceKey;
21import org.onosproject.net.key.DeviceKeyAdminService;
22import org.onosproject.net.key.DeviceKeyEvent;
23import org.onosproject.net.key.DeviceKeyId;
24import org.onosproject.net.key.DeviceKeyListener;
25import org.onosproject.net.key.DeviceKeyService;
26import org.onosproject.net.key.DeviceKeyStore;
27import org.onosproject.net.key.DeviceKeyStoreDelegate;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070028import org.osgi.service.component.annotations.Activate;
29import org.osgi.service.component.annotations.Component;
30import org.osgi.service.component.annotations.Deactivate;
31import org.osgi.service.component.annotations.Reference;
32import org.osgi.service.component.annotations.ReferenceCardinality;
Brian Stankeca93d9a2016-02-10 09:17:35 -050033import org.slf4j.Logger;
34
35import java.util.Collection;
36
37import static com.google.common.base.Preconditions.checkNotNull;
38import static org.onosproject.security.AppGuard.checkPermission;
39import static org.onosproject.security.AppPermission.Type.DEVICE_KEY_READ;
Brian Stankeca93d9a2016-02-10 09:17:35 -050040import static org.slf4j.LoggerFactory.getLogger;
41
42/**
43 * Implementation of device key services.
44 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070045@Component(immediate = true, service = { DeviceKeyService.class, DeviceKeyAdminService.class })
Brian Stankeca93d9a2016-02-10 09:17:35 -050046public class DeviceKeyManager extends AbstractListenerManager<DeviceKeyEvent, DeviceKeyListener>
47 implements DeviceKeyService, DeviceKeyAdminService {
48
49 private final Logger log = getLogger(getClass());
50
Brian Stankeca93d9a2016-02-10 09:17:35 -050051 private DeviceKeyStoreDelegate delegate = this::post;
52
Ray Milkeyd84f89b2018-08-17 14:54:17 -070053 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian Stankeca93d9a2016-02-10 09:17:35 -050054 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 Stankeca93d9a2016-02-10 09:17:35 -050072 checkNotNull(deviceKey, "Device key cannot be null");
73 store.createOrUpdateDeviceKey(deviceKey);
74 }
75
76 @Override
77 public void removeKey(DeviceKeyId deviceKeyId) {
Brian Stankeca93d9a2016-02-10 09:17:35 -050078 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