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