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