blob: e02ff357d368c0284627478438e0cb787181740e [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;
27import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent;
Jian Li8f64feb2018-07-24 13:20:16 +090028import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type;
29import org.onosproject.openstacknetworking.api.PreCommitPortService;
Jian Li0ce2c412018-07-24 21:09:04 +090030import org.onosproject.store.serializers.KryoNamespaces;
31import org.onosproject.store.service.ConsistentMap;
32import org.onosproject.store.service.Serializer;
33import org.onosproject.store.service.StorageService;
Jian Li8f64feb2018-07-24 13:20:16 +090034import org.slf4j.Logger;
35
Jian Li0ce2c412018-07-24 21:09:04 +090036import java.util.HashMap;
37import java.util.HashSet;
Jian Li8f64feb2018-07-24 13:20:16 +090038import java.util.Map;
39import java.util.Objects;
40import java.util.Set;
41
Jian Li0ce2c412018-07-24 21:09:04 +090042import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID;
Jian Li8f64feb2018-07-24 13:20:16 +090043import static org.slf4j.LoggerFactory.getLogger;
44
45/**
46 * Implementation of pre-commit service.
47 */
48@Service
49@Component(immediate = true)
50public class PreCommitPortManager implements PreCommitPortService {
51
52 protected final Logger log = getLogger(getClass());
53
Jian Li0ce2c412018-07-24 21:09:04 +090054 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 Li8f64feb2018-07-24 13:20:16 +090066
67 @Activate
68 protected void activate() {
Jian Li0ce2c412018-07-24 21:09:04 +090069
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 Li8f64feb2018-07-24 13:20:16 +090078 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 Li0ce2c412018-07-24 21:09:04 +090088 store.computeIfAbsent(portId, s -> new HashMap<>());
Jian Li8f64feb2018-07-24 13:20:16 +090089
90 store.compute(portId, (k, v) -> {
91
92 if (className == null || className.isEmpty()) {
93 return null;
94 }
95
Jian Li0ce2c412018-07-24 21:09:04 +090096 Objects.requireNonNull(v).computeIfAbsent(eventType, s -> new HashSet<>());
Jian Li8f64feb2018-07-24 13:20:16 +090097
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 Li0ce2c412018-07-24 21:09:04 +0900129 Map<Type, Set<String>> typeMap = store.asJavaMap().get(portId);
Jian Li8f64feb2018-07-24 13:20:16 +0900130
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 Li0ce2c412018-07-24 21:09:04 +0900145 Map<Type, Set<String>> typeMap = store.asJavaMap().get(portId);
Jian Li8f64feb2018-07-24 13:20:16 +0900146
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}