Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.openstacknetworking.impl; |
| 17 | |
| 18 | import com.google.common.collect.Maps; |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type; |
| 25 | import org.onosproject.openstacknetworking.api.PreCommitPortService; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import java.util.Map; |
| 29 | import java.util.Objects; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
| 33 | |
| 34 | /** |
| 35 | * Implementation of pre-commit service. |
| 36 | */ |
| 37 | @Service |
| 38 | @Component(immediate = true) |
| 39 | public class PreCommitPortManager implements PreCommitPortService { |
| 40 | |
| 41 | protected final Logger log = getLogger(getClass()); |
| 42 | |
| 43 | private Map<String, Map<Type, Set<String>>> store = Maps.newConcurrentMap(); |
| 44 | |
| 45 | @Activate |
| 46 | protected void activate() { |
| 47 | log.info("Started"); |
| 48 | } |
| 49 | |
| 50 | @Deactivate |
| 51 | protected void deactivate() { |
| 52 | log.info("Stopped"); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void subscribePreCommit(String portId, Type eventType, String className) { |
| 57 | store.computeIfAbsent(portId, s -> Maps.newConcurrentMap()); |
| 58 | |
| 59 | store.compute(portId, (k, v) -> { |
| 60 | |
| 61 | if (className == null || className.isEmpty()) { |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | Objects.requireNonNull(v).computeIfAbsent(eventType, |
| 66 | s -> Sets.newConcurrentHashSet()); |
| 67 | |
| 68 | |
| 69 | Objects.requireNonNull(v).compute(eventType, (i, j) -> { |
| 70 | Objects.requireNonNull(j).add(className); |
| 71 | return j; |
| 72 | }); |
| 73 | |
| 74 | return v; |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void unsubscribePreCommit(String portId, Type eventType, String className) { |
| 80 | |
| 81 | store.computeIfPresent(portId, (k, v) -> { |
| 82 | |
| 83 | if (className == null || className.isEmpty()) { |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | Objects.requireNonNull(v).computeIfPresent(eventType, (i, j) -> { |
| 88 | Objects.requireNonNull(j).remove(className); |
| 89 | return j; |
| 90 | }); |
| 91 | |
| 92 | return v; |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public int subscriberCountByEventType(String portId, Type eventType) { |
| 98 | |
| 99 | Map<Type, Set<String>> typeMap = store.get(portId); |
| 100 | |
| 101 | if (typeMap == null || typeMap.isEmpty()) { |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | if (typeMap.get(eventType) == null || typeMap.get(eventType).isEmpty()) { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | return typeMap.get(eventType).size(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public int subscriberCount(String portId) { |
| 114 | |
| 115 | Map<Type, Set<String>> typeMap = store.get(portId); |
| 116 | |
| 117 | if (typeMap == null || typeMap.isEmpty()) { |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | return typeMap.values().stream() |
| 122 | .filter(Objects::nonNull) |
| 123 | .map(Set::size) |
| 124 | .reduce(0, Integer::sum); |
| 125 | } |
| 126 | } |