blob: 47dbe1cf7d510b9e1ee28fe497a0ac29a201bebf [file] [log] [blame]
Phaneendra Manda05db8b72015-10-28 17:15:38 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Phaneendra Manda05db8b72015-10-28 17:15:38 +05303 *
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.vtnrsc.portpair.impl;
17
Phaneendra Manda05db8b72015-10-28 17:15:38 +053018import org.onlab.util.KryoNamespace;
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053019import org.onosproject.event.AbstractListenerManager;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053020import org.onosproject.store.serializers.KryoNamespaces;
21import org.onosproject.store.service.EventuallyConsistentMap;
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053022import org.onosproject.store.service.EventuallyConsistentMapEvent;
23import org.onosproject.store.service.EventuallyConsistentMapListener;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053024import org.onosproject.store.service.MultiValuedTimestamp;
25import org.onosproject.store.service.StorageService;
26import org.onosproject.store.service.WallClockTimestamp;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053027import org.onosproject.vtnrsc.DefaultPortPair;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053028import org.onosproject.vtnrsc.PortPair;
29import org.onosproject.vtnrsc.PortPairId;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053030import org.onosproject.vtnrsc.TenantId;
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053031import org.onosproject.vtnrsc.portpair.PortPairEvent;
Mahesh Poojary Huawei3fe5eff2015-11-29 15:13:18 +053032import org.onosproject.vtnrsc.portpair.PortPairListener;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053033import org.onosproject.vtnrsc.portpair.PortPairService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070034import org.osgi.service.component.annotations.Activate;
35import org.osgi.service.component.annotations.Component;
36import org.osgi.service.component.annotations.Deactivate;
37import org.osgi.service.component.annotations.Reference;
38import org.osgi.service.component.annotations.ReferenceCardinality;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053039import org.slf4j.Logger;
40
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041import java.util.Collections;
42import java.util.UUID;
43
44import static com.google.common.base.Preconditions.checkNotNull;
45import static org.slf4j.LoggerFactory.getLogger;
46
Phaneendra Manda05db8b72015-10-28 17:15:38 +053047/**
48 * Provides implementation of the portPairService.
49 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070050@Component(immediate = true, service = PortPairService.class)
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053051public class PortPairManager extends AbstractListenerManager<PortPairEvent, PortPairListener> implements
52 PortPairService {
Phaneendra Manda05db8b72015-10-28 17:15:38 +053053
Phaneendra Manda05db8b72015-10-28 17:15:38 +053054 private static final String PORT_PAIR_ID_NULL = "PortPair ID cannot be null";
55 private static final String PORT_PAIR_NULL = "PortPair cannot be null";
Mahesh Poojary Huawei3fe5eff2015-11-29 15:13:18 +053056 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053057 private static final String EVENT_NOT_NULL = "event cannot be null";
Phaneendra Manda05db8b72015-10-28 17:15:38 +053058
Mahesh Poojary Huawei3fe5eff2015-11-29 15:13:18 +053059 private final Logger log = getLogger(getClass());
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053060
Phaneendra Manda05db8b72015-10-28 17:15:38 +053061 private EventuallyConsistentMap<PortPairId, PortPair> portPairStore;
62
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053063 private EventuallyConsistentMapListener<PortPairId, PortPair> portPairListener =
64 new InnerPortPairStoreListener();
65
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Phaneendra Manda05db8b72015-10-28 17:15:38 +053067 protected StorageService storageService;
68
69 @Activate
70 public void activate() {
71
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053072 eventDispatcher.addSink(PortPairEvent.class, listenerRegistry);
Phaneendra Manda05db8b72015-10-28 17:15:38 +053073 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
74 .register(KryoNamespaces.API)
75 .register(MultiValuedTimestamp.class)
Phaneendra Manda8db7d092016-06-04 00:17:24 +053076 .register(PortPair.class, PortPairId.class, UUID.class, DefaultPortPair.class, TenantId.class);
Phaneendra Manda05db8b72015-10-28 17:15:38 +053077
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053078 portPairStore = storageService.<PortPairId, PortPair>eventuallyConsistentMapBuilder()
79 .withName("portpairstore")
80 .withSerializer(serializer)
81 .withTimestampProvider((k, v) -> new WallClockTimestamp())
82 .build();
Phaneendra Manda05db8b72015-10-28 17:15:38 +053083
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053084 portPairStore.addListener(portPairListener);
85
Phaneendra Manda05db8b72015-10-28 17:15:38 +053086 log.info("Started");
87 }
88
89 @Deactivate
90 public void deactivate() {
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053091 eventDispatcher.removeSink(PortPairEvent.class);
Phaneendra Manda05db8b72015-10-28 17:15:38 +053092 portPairStore.destroy();
93 log.info("Stopped");
94 }
95
96 @Override
97 public boolean exists(PortPairId portPairId) {
98 checkNotNull(portPairId, PORT_PAIR_ID_NULL);
99 return portPairStore.containsKey(portPairId);
100 }
101
102 @Override
103 public int getPortPairCount() {
104 return portPairStore.size();
105 }
106
107 @Override
108 public Iterable<PortPair> getPortPairs() {
109 return Collections.unmodifiableCollection(portPairStore.values());
110 }
111
112 @Override
113 public PortPair getPortPair(PortPairId portPairId) {
114 checkNotNull(portPairId, PORT_PAIR_ID_NULL);
115 return portPairStore.get(portPairId);
116 }
117
118 @Override
119 public boolean createPortPair(PortPair portPair) {
120 checkNotNull(portPair, PORT_PAIR_NULL);
121
122 portPairStore.put(portPair.portPairId(), portPair);
123 if (!portPairStore.containsKey(portPair.portPairId())) {
Mahesh Poojary S28bfba72015-12-05 14:14:37 +0530124 log.debug("The portPair is created failed which identifier was {}", portPair.portPairId().toString());
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530125 return false;
126 }
127 return true;
128 }
129
130 @Override
131 public boolean updatePortPair(PortPair portPair) {
132 checkNotNull(portPair, PORT_PAIR_NULL);
133
134 if (!portPairStore.containsKey(portPair.portPairId())) {
Mahesh Poojary S28bfba72015-12-05 14:14:37 +0530135 log.debug("The portPair is not exist whose identifier was {} ", portPair.portPairId().toString());
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530136 return false;
137 }
138
139 portPairStore.put(portPair.portPairId(), portPair);
140
141 if (!portPair.equals(portPairStore.get(portPair.portPairId()))) {
Mahesh Poojary S28bfba72015-12-05 14:14:37 +0530142 log.debug("The portPair is updated failed whose identifier was {} ", portPair.portPairId().toString());
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530143 return false;
144 }
145 return true;
146 }
147
148 @Override
149 public boolean removePortPair(PortPairId portPairId) {
150 checkNotNull(portPairId, PORT_PAIR_NULL);
151
152 portPairStore.remove(portPairId);
153 if (portPairStore.containsKey(portPairId)) {
Mahesh Poojary S28bfba72015-12-05 14:14:37 +0530154 log.debug("The portPair is removed failed whose identifier was {}", portPairId.toString());
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530155 return false;
156 }
157 return true;
158 }
Bharat saraswal89f5f0d2015-12-11 01:01:37 +0530159
160 private class InnerPortPairStoreListener
161 implements
162 EventuallyConsistentMapListener<PortPairId, PortPair> {
163
164 @Override
165 public void event(EventuallyConsistentMapEvent<PortPairId, PortPair> event) {
166 checkNotNull(event, EVENT_NOT_NULL);
167 PortPair portPair = event.value();
168 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
169 notifyListeners(new PortPairEvent(
170 PortPairEvent.Type.PORT_PAIR_PUT,
171 portPair));
172 }
173 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
174 notifyListeners(new PortPairEvent(
175 PortPairEvent.Type.PORT_PAIR_DELETE,
176 portPair));
177 }
178 }
179 }
180
181 /**
182 * Notifies specify event to all listeners.
183 *
184 * @param event port pair event
185 */
186 private void notifyListeners(PortPairEvent event) {
187 checkNotNull(event, EVENT_NOT_NULL);
188 post(event);
189 }
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530190}