blob: 8110f5dcb98615aaf11f3fca9fff0bc89fee764c [file] [log] [blame]
Brian Stankeca93d9a2016-02-10 09:17:35 -05001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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
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;
Brian Stankeca93d9a2016-02-10 09:17:35 -050041import static org.slf4j.LoggerFactory.getLogger;
42
43/**
44 * Implementation of device key services.
45 */
Brian Stankee312fc72016-02-16 15:07:13 -050046@Component(immediate = true, enabled = true)
47@Service
Brian Stankeca93d9a2016-02-10 09:17:35 -050048public class DeviceKeyManager extends AbstractListenerManager<DeviceKeyEvent, DeviceKeyListener>
49 implements DeviceKeyService, DeviceKeyAdminService {
50
51 private final Logger log = getLogger(getClass());
52
Brian Stankeca93d9a2016-02-10 09:17:35 -050053 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 Stankeca93d9a2016-02-10 09:17:35 -050074 checkNotNull(deviceKey, "Device key cannot be null");
75 store.createOrUpdateDeviceKey(deviceKey);
76 }
77
78 @Override
79 public void removeKey(DeviceKeyId deviceKeyId) {
Brian Stankeca93d9a2016-02-10 09:17:35 -050080 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