blob: e0788fbeca99387b1ba7399419394b7869faa4a3 [file] [log] [blame]
Jian Li8f64feb2018-07-24 13:20:16 +09001/*
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 */
16package org.onosproject.openstacknetworking.impl;
17
Jian Li8f64feb2018-07-24 13:20:16 +090018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
Jian Li0ce2c412018-07-24 21:09:04 +090021import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
Jian Li8f64feb2018-07-24 13:20:16 +090023import org.apache.felix.scr.annotations.Service;
Jian Li0ce2c412018-07-24 21:09:04 +090024import org.onlab.util.KryoNamespace;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
Jian Li628a7cb2018-08-11 23:04:24 +090027import org.onosproject.openstacknetworking.api.InstancePortAdminService;
Jian Li0ce2c412018-07-24 21:09:04 +090028import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent;
Jian Li8f64feb2018-07-24 13:20:16 +090029import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type;
30import org.onosproject.openstacknetworking.api.PreCommitPortService;
Jian Li0ce2c412018-07-24 21:09:04 +090031import org.onosproject.store.serializers.KryoNamespaces;
32import org.onosproject.store.service.ConsistentMap;
33import org.onosproject.store.service.Serializer;
34import org.onosproject.store.service.StorageService;
Jian Li8f64feb2018-07-24 13:20:16 +090035import org.slf4j.Logger;
36
Jian Li0ce2c412018-07-24 21:09:04 +090037import java.util.HashMap;
38import java.util.HashSet;
Jian Li8f64feb2018-07-24 13:20:16 +090039import java.util.Map;
40import java.util.Objects;
41import java.util.Set;
42
Jian Li0ce2c412018-07-24 21:09:04 +090043import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID;
Jian Li8f64feb2018-07-24 13:20:16 +090044import static org.slf4j.LoggerFactory.getLogger;
45
46/**
47 * Implementation of pre-commit service.
48 */
49@Service
50@Component(immediate = true)
51public class PreCommitPortManager implements PreCommitPortService {
52
53 protected final Logger log = getLogger(getClass());
54
Jian Li0ce2c412018-07-24 21:09:04 +090055 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 Li8f64feb2018-07-24 13:20:16 +090067
68 @Activate
69 protected void activate() {
Jian Li0ce2c412018-07-24 21:09:04 +090070
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 Li8f64feb2018-07-24 13:20:16 +090079 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 Li0ce2c412018-07-24 21:09:04 +090089 store.computeIfAbsent(portId, s -> new HashMap<>());
Jian Li8f64feb2018-07-24 13:20:16 +090090
91 store.compute(portId, (k, v) -> {
92
93 if (className == null || className.isEmpty()) {
94 return null;
95 }
96
Jian Li0ce2c412018-07-24 21:09:04 +090097 Objects.requireNonNull(v).computeIfAbsent(eventType, s -> new HashSet<>());
Jian Li8f64feb2018-07-24 13:20:16 +090098
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 Li628a7cb2018-08-11 23:04:24 +0900110 public void unsubscribePreCommit(String portId, Type eventType,
111 InstancePortAdminService service, String className) {
Jian Li8f64feb2018-07-24 13:20:16 +0900112
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 Li628a7cb2018-08-11 23:04:24 +0900126
127 if (subscriberCountByEventType(portId, eventType) == 0) {
128 service.removeInstancePort(portId);
129 }
Jian Li8f64feb2018-07-24 13:20:16 +0900130 }
131
132 @Override
133 public int subscriberCountByEventType(String portId, Type eventType) {
134
Jian Li0ce2c412018-07-24 21:09:04 +0900135 Map<Type, Set<String>> typeMap = store.asJavaMap().get(portId);
Jian Li8f64feb2018-07-24 13:20:16 +0900136
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 Li0ce2c412018-07-24 21:09:04 +0900151 Map<Type, Set<String>> typeMap = store.asJavaMap().get(portId);
Jian Li8f64feb2018-07-24 13:20:16 +0900152
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}