Jian Li | 4545cf1 | 2016-02-10 11:06:06 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 4545cf1 | 2016-02-10 11:06:06 -0800 | [diff] [blame] | 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 | package org.onosproject.cpman.impl.message; |
| 17 | |
Jian Li | 4545cf1 | 2016-02-10 11:06:06 -0800 | [diff] [blame] | 18 | import org.onosproject.cpman.ControlMessage; |
| 19 | import org.onosproject.cpman.message.ControlMessageAdminService; |
| 20 | import org.onosproject.cpman.message.ControlMessageEvent; |
| 21 | import org.onosproject.cpman.message.ControlMessageListener; |
| 22 | import org.onosproject.cpman.message.ControlMessageProvider; |
| 23 | import org.onosproject.cpman.message.ControlMessageProviderRegistry; |
| 24 | import org.onosproject.cpman.message.ControlMessageProviderService; |
| 25 | import org.onosproject.cpman.message.ControlMessageService; |
| 26 | import org.onosproject.cpman.message.ControlMessageStore; |
| 27 | import org.onosproject.cpman.message.ControlMessageStoreDelegate; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
| 30 | import org.onosproject.net.provider.AbstractProviderService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 31 | import org.osgi.service.component.annotations.Activate; |
| 32 | import org.osgi.service.component.annotations.Component; |
| 33 | import org.osgi.service.component.annotations.Deactivate; |
| 34 | import org.osgi.service.component.annotations.Reference; |
| 35 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jian Li | 4545cf1 | 2016-02-10 11:06:06 -0800 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | |
Jian Li | 72b9b12 | 2016-02-11 15:58:51 -0800 | [diff] [blame] | 38 | import java.util.Set; |
Jian Li | 4545cf1 | 2016-02-10 11:06:06 -0800 | [diff] [blame] | 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Provides implementation of the control message SB & NB APIs. |
| 45 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 46 | @Component(immediate = true, service = { ControlMessageService.class, ControlMessageAdminService.class, |
| 47 | ControlMessageProviderRegistry.class }) |
Jian Li | 4545cf1 | 2016-02-10 11:06:06 -0800 | [diff] [blame] | 48 | public class ControlMessageManager |
| 49 | extends AbstractListenerProviderRegistry<ControlMessageEvent, ControlMessageListener, |
| 50 | ControlMessageProvider, ControlMessageProviderService> |
| 51 | implements ControlMessageService, ControlMessageAdminService, |
| 52 | ControlMessageProviderRegistry { |
| 53 | |
| 54 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 55 | |
| 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
| 58 | private final ControlMessageStoreDelegate delegate = new InternalStoreDelegate(); |
| 59 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 4545cf1 | 2016-02-10 11:06:06 -0800 | [diff] [blame] | 61 | protected ControlMessageStore store; |
| 62 | |
| 63 | @Activate |
| 64 | public void activate() { |
| 65 | store.setDelegate(delegate); |
| 66 | eventDispatcher.addSink(ControlMessageEvent.class, listenerRegistry); |
| 67 | |
| 68 | log.info("Started"); |
| 69 | } |
| 70 | |
| 71 | @Deactivate |
| 72 | public void deactivate() { |
| 73 | store.unsetDelegate(delegate); |
| 74 | eventDispatcher.removeSink(ControlMessageEvent.class); |
| 75 | |
| 76 | log.info("Stopped"); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected ControlMessageProviderService createProviderService(ControlMessageProvider provider) { |
| 81 | return new InternalControlMessageProviderService(provider); |
| 82 | } |
| 83 | |
| 84 | private class InternalControlMessageProviderService |
| 85 | extends AbstractProviderService<ControlMessageProvider> |
| 86 | implements ControlMessageProviderService { |
| 87 | InternalControlMessageProviderService(ControlMessageProvider provider) { |
| 88 | super(provider); |
| 89 | } |
| 90 | |
| 91 | @Override |
Jian Li | 72b9b12 | 2016-02-11 15:58:51 -0800 | [diff] [blame] | 92 | public void updateStatsInfo(DeviceId deviceId, Set<ControlMessage> controlMessages) { |
Jian Li | 4545cf1 | 2016-02-10 11:06:06 -0800 | [diff] [blame] | 93 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 94 | checkValidity(); |
| 95 | |
| 96 | ControlMessageEvent event = |
| 97 | store.updateStatsInfo(this.provider().id(), deviceId, controlMessages); |
| 98 | |
| 99 | post(event); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private class InternalStoreDelegate implements ControlMessageStoreDelegate { |
| 104 | @Override |
| 105 | public void notify(ControlMessageEvent event) { |
| 106 | post(event); |
| 107 | } |
| 108 | } |
| 109 | } |