blob: 29260728bcf7986c5723df54877ac57d3a6f928d [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 Li8a5fb642018-09-14 15:50:04 +090027import org.onosproject.openstacknetworking.api.InstancePort;
Jian Li628a7cb2018-08-11 23:04:24 +090028import org.onosproject.openstacknetworking.api.InstancePortAdminService;
Jian Li0ce2c412018-07-24 21:09:04 +090029import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent;
Jian Li8f64feb2018-07-24 13:20:16 +090030import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type;
31import org.onosproject.openstacknetworking.api.PreCommitPortService;
Jian Li0ce2c412018-07-24 21:09:04 +090032import org.onosproject.store.serializers.KryoNamespaces;
33import org.onosproject.store.service.ConsistentMap;
34import org.onosproject.store.service.Serializer;
35import org.onosproject.store.service.StorageService;
Jian Li8f64feb2018-07-24 13:20:16 +090036import org.slf4j.Logger;
37
Jian Li0ce2c412018-07-24 21:09:04 +090038import java.util.HashMap;
39import java.util.HashSet;
Jian Li8f64feb2018-07-24 13:20:16 +090040import java.util.Map;
41import java.util.Objects;
42import java.util.Set;
43
Jian Li0ce2c412018-07-24 21:09:04 +090044import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID;
Jian Li8a5fb642018-09-14 15:50:04 +090045import static org.onosproject.openstacknetworking.api.InstancePort.State.REMOVE_PENDING;
Jian Li8f64feb2018-07-24 13:20:16 +090046import static org.slf4j.LoggerFactory.getLogger;
47
48/**
49 * Implementation of pre-commit service.
50 */
51@Service
52@Component(immediate = true)
53public class PreCommitPortManager implements PreCommitPortService {
54
55 protected final Logger log = getLogger(getClass());
56
Jian Li0ce2c412018-07-24 21:09:04 +090057 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 Li8f64feb2018-07-24 13:20:16 +090069
70 @Activate
71 protected void activate() {
Jian Li0ce2c412018-07-24 21:09:04 +090072
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 Li8f64feb2018-07-24 13:20:16 +090081 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 Li0ce2c412018-07-24 21:09:04 +090091 store.computeIfAbsent(portId, s -> new HashMap<>());
Jian Li8f64feb2018-07-24 13:20:16 +090092
93 store.compute(portId, (k, v) -> {
94
95 if (className == null || className.isEmpty()) {
96 return null;
97 }
98
Jian Li0ce2c412018-07-24 21:09:04 +090099 Objects.requireNonNull(v).computeIfAbsent(eventType, s -> new HashSet<>());
Jian Li8f64feb2018-07-24 13:20:16 +0900100
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 Li628a7cb2018-08-11 23:04:24 +0900112 public void unsubscribePreCommit(String portId, Type eventType,
113 InstancePortAdminService service, String className) {
Jian Li8f64feb2018-07-24 13:20:16 +0900114
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 Li628a7cb2018-08-11 23:04:24 +0900128
129 if (subscriberCountByEventType(portId, eventType) == 0) {
Jian Li8a5fb642018-09-14 15:50:04 +0900130
131 InstancePort instPort = service.instancePort(portId);
132
133 if (instPort != null && instPort.state() == REMOVE_PENDING) {
134 service.removeInstancePort(portId);
135 }
Jian Li628a7cb2018-08-11 23:04:24 +0900136 }
Jian Li8f64feb2018-07-24 13:20:16 +0900137 }
138
139 @Override
140 public int subscriberCountByEventType(String portId, Type eventType) {
141
Jian Li0ce2c412018-07-24 21:09:04 +0900142 Map<Type, Set<String>> typeMap = store.asJavaMap().get(portId);
Jian Li8f64feb2018-07-24 13:20:16 +0900143
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 Li0ce2c412018-07-24 21:09:04 +0900158 Map<Type, Set<String>> typeMap = store.asJavaMap().get(portId);
Jian Li8f64feb2018-07-24 13:20:16 +0900159
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}