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