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 | |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame^] | 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Service; |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame^] | 24 | import org.onlab.util.KryoNamespace; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent; |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 28 | import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type; |
| 29 | import org.onosproject.openstacknetworking.api.PreCommitPortService; |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame^] | 30 | import org.onosproject.store.serializers.KryoNamespaces; |
| 31 | import org.onosproject.store.service.ConsistentMap; |
| 32 | import org.onosproject.store.service.Serializer; |
| 33 | import org.onosproject.store.service.StorageService; |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame^] | 36 | import java.util.HashMap; |
| 37 | import java.util.HashSet; |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 38 | import java.util.Map; |
| 39 | import java.util.Objects; |
| 40 | import java.util.Set; |
| 41 | |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame^] | 42 | import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID; |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 43 | import static org.slf4j.LoggerFactory.getLogger; |
| 44 | |
| 45 | /** |
| 46 | * Implementation of pre-commit service. |
| 47 | */ |
| 48 | @Service |
| 49 | @Component(immediate = true) |
| 50 | public class PreCommitPortManager implements PreCommitPortService { |
| 51 | |
| 52 | protected final Logger log = getLogger(getClass()); |
| 53 | |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame^] | 54 | private static final KryoNamespace SERIALIZER_PRE_COMMIT = KryoNamespace.newBuilder() |
| 55 | .register(KryoNamespaces.API) |
| 56 | .register(OpenstackNetworkEvent.Type.class) |
| 57 | .build(); |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected CoreService coreService; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected StorageService storageService; |
| 64 | |
| 65 | private ConsistentMap<String, Map<Type, Set<String>>> store; |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 66 | |
| 67 | @Activate |
| 68 | protected void activate() { |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame^] | 69 | |
| 70 | ApplicationId appId = coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID); |
| 71 | |
| 72 | store = storageService.<String, Map<Type, Set<String>>>consistentMapBuilder() |
| 73 | .withSerializer(Serializer.using(SERIALIZER_PRE_COMMIT)) |
| 74 | .withName("openstack-pre-commit-store") |
| 75 | .withApplicationId(appId) |
| 76 | .build(); |
| 77 | |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 78 | log.info("Started"); |
| 79 | } |
| 80 | |
| 81 | @Deactivate |
| 82 | protected void deactivate() { |
| 83 | log.info("Stopped"); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void subscribePreCommit(String portId, Type eventType, String className) { |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame^] | 88 | store.computeIfAbsent(portId, s -> new HashMap<>()); |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 89 | |
| 90 | store.compute(portId, (k, v) -> { |
| 91 | |
| 92 | if (className == null || className.isEmpty()) { |
| 93 | return null; |
| 94 | } |
| 95 | |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame^] | 96 | Objects.requireNonNull(v).computeIfAbsent(eventType, s -> new HashSet<>()); |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 97 | |
| 98 | |
| 99 | Objects.requireNonNull(v).compute(eventType, (i, j) -> { |
| 100 | Objects.requireNonNull(j).add(className); |
| 101 | return j; |
| 102 | }); |
| 103 | |
| 104 | return v; |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void unsubscribePreCommit(String portId, Type eventType, String className) { |
| 110 | |
| 111 | store.computeIfPresent(portId, (k, v) -> { |
| 112 | |
| 113 | if (className == null || className.isEmpty()) { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | Objects.requireNonNull(v).computeIfPresent(eventType, (i, j) -> { |
| 118 | Objects.requireNonNull(j).remove(className); |
| 119 | return j; |
| 120 | }); |
| 121 | |
| 122 | return v; |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public int subscriberCountByEventType(String portId, Type eventType) { |
| 128 | |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame^] | 129 | Map<Type, Set<String>> typeMap = store.asJavaMap().get(portId); |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 130 | |
| 131 | if (typeMap == null || typeMap.isEmpty()) { |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | if (typeMap.get(eventType) == null || typeMap.get(eventType).isEmpty()) { |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | return typeMap.get(eventType).size(); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public int subscriberCount(String portId) { |
| 144 | |
Jian Li | 0ce2c41 | 2018-07-24 21:09:04 +0900 | [diff] [blame^] | 145 | Map<Type, Set<String>> typeMap = store.asJavaMap().get(portId); |
Jian Li | 8f64feb | 2018-07-24 13:20:16 +0900 | [diff] [blame] | 146 | |
| 147 | if (typeMap == null || typeMap.isEmpty()) { |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | return typeMap.values().stream() |
| 152 | .filter(Objects::nonNull) |
| 153 | .map(Set::size) |
| 154 | .reduce(0, Integer::sum); |
| 155 | } |
| 156 | } |