blob: 4547764e2f2ea762322e51611bc6fd0b6324dddc [file] [log] [blame]
Phaneendra Manda05db8b72015-10-28 17:15:38 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.util.Collections;
22
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
28import org.apache.felix.scr.annotations.Service;
29import org.onlab.util.KryoNamespace;
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053030import org.onosproject.event.AbstractListenerManager;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053031import org.onosproject.store.serializers.KryoNamespaces;
32import org.onosproject.store.service.EventuallyConsistentMap;
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053033import org.onosproject.store.service.EventuallyConsistentMapEvent;
34import org.onosproject.store.service.EventuallyConsistentMapListener;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053035import org.onosproject.store.service.MultiValuedTimestamp;
36import org.onosproject.store.service.StorageService;
37import org.onosproject.store.service.WallClockTimestamp;
38import org.onosproject.vtnrsc.PortPair;
39import org.onosproject.vtnrsc.PortPairId;
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053040import org.onosproject.vtnrsc.portpair.PortPairEvent;
Mahesh Poojary Huawei3fe5eff2015-11-29 15:13:18 +053041import org.onosproject.vtnrsc.portpair.PortPairListener;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053042import org.onosproject.vtnrsc.portpair.PortPairService;
43import org.slf4j.Logger;
44
45/**
46 * Provides implementation of the portPairService.
47 */
48@Component(immediate = true)
49@Service
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053050public class PortPairManager extends AbstractListenerManager<PortPairEvent, PortPairListener> implements
51 PortPairService {
Phaneendra Manda05db8b72015-10-28 17:15:38 +053052
Phaneendra Manda05db8b72015-10-28 17:15:38 +053053 private static final String PORT_PAIR_ID_NULL = "PortPair ID cannot be null";
54 private static final String PORT_PAIR_NULL = "PortPair cannot be null";
Mahesh Poojary Huawei3fe5eff2015-11-29 15:13:18 +053055 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053056 private static final String EVENT_NOT_NULL = "event cannot be null";
Phaneendra Manda05db8b72015-10-28 17:15:38 +053057
Mahesh Poojary Huawei3fe5eff2015-11-29 15:13:18 +053058 private final Logger log = getLogger(getClass());
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053059
Phaneendra Manda05db8b72015-10-28 17:15:38 +053060 private EventuallyConsistentMap<PortPairId, PortPair> portPairStore;
61
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053062 private EventuallyConsistentMapListener<PortPairId, PortPair> portPairListener =
63 new InnerPortPairStoreListener();
64
Phaneendra Manda05db8b72015-10-28 17:15:38 +053065 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected StorageService storageService;
67
68 @Activate
69 public void activate() {
70
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053071 eventDispatcher.addSink(PortPairEvent.class, listenerRegistry);
Phaneendra Manda05db8b72015-10-28 17:15:38 +053072 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
73 .register(KryoNamespaces.API)
74 .register(MultiValuedTimestamp.class)
75 .register(PortPair.class);
76
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053077 portPairStore = storageService.<PortPairId, PortPair>eventuallyConsistentMapBuilder()
78 .withName("portpairstore")
79 .withSerializer(serializer)
80 .withTimestampProvider((k, v) -> new WallClockTimestamp())
81 .build();
Phaneendra Manda05db8b72015-10-28 17:15:38 +053082
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053083 portPairStore.addListener(portPairListener);
84
Phaneendra Manda05db8b72015-10-28 17:15:38 +053085 log.info("Started");
86 }
87
88 @Deactivate
89 public void deactivate() {
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053090 eventDispatcher.removeSink(PortPairEvent.class);
Phaneendra Manda05db8b72015-10-28 17:15:38 +053091 portPairStore.destroy();
92 log.info("Stopped");
93 }
94
95 @Override
96 public boolean exists(PortPairId portPairId) {
97 checkNotNull(portPairId, PORT_PAIR_ID_NULL);
98 return portPairStore.containsKey(portPairId);
99 }
100
101 @Override
102 public int getPortPairCount() {
103 return portPairStore.size();
104 }
105
106 @Override
107 public Iterable<PortPair> getPortPairs() {
108 return Collections.unmodifiableCollection(portPairStore.values());
109 }
110
111 @Override
112 public PortPair getPortPair(PortPairId portPairId) {
113 checkNotNull(portPairId, PORT_PAIR_ID_NULL);
114 return portPairStore.get(portPairId);
115 }
116
117 @Override
118 public boolean createPortPair(PortPair portPair) {
119 checkNotNull(portPair, PORT_PAIR_NULL);
120
121 portPairStore.put(portPair.portPairId(), portPair);
122 if (!portPairStore.containsKey(portPair.portPairId())) {
Mahesh Poojary S28bfba72015-12-05 14:14:37 +0530123 log.debug("The portPair is created failed which identifier was {}", portPair.portPairId().toString());
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530124 return false;
125 }
126 return true;
127 }
128
129 @Override
130 public boolean updatePortPair(PortPair portPair) {
131 checkNotNull(portPair, PORT_PAIR_NULL);
132
133 if (!portPairStore.containsKey(portPair.portPairId())) {
Mahesh Poojary S28bfba72015-12-05 14:14:37 +0530134 log.debug("The portPair is not exist whose identifier was {} ", portPair.portPairId().toString());
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530135 return false;
136 }
137
138 portPairStore.put(portPair.portPairId(), portPair);
139
140 if (!portPair.equals(portPairStore.get(portPair.portPairId()))) {
Mahesh Poojary S28bfba72015-12-05 14:14:37 +0530141 log.debug("The portPair is updated failed whose identifier was {} ", portPair.portPairId().toString());
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530142 return false;
143 }
144 return true;
145 }
146
147 @Override
148 public boolean removePortPair(PortPairId portPairId) {
149 checkNotNull(portPairId, PORT_PAIR_NULL);
150
151 portPairStore.remove(portPairId);
152 if (portPairStore.containsKey(portPairId)) {
Mahesh Poojary S28bfba72015-12-05 14:14:37 +0530153 log.debug("The portPair is removed failed whose identifier was {}", portPairId.toString());
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530154 return false;
155 }
156 return true;
157 }
Bharat saraswal89f5f0d2015-12-11 01:01:37 +0530158
159 private class InnerPortPairStoreListener
160 implements
161 EventuallyConsistentMapListener<PortPairId, PortPair> {
162
163 @Override
164 public void event(EventuallyConsistentMapEvent<PortPairId, PortPair> event) {
165 checkNotNull(event, EVENT_NOT_NULL);
166 PortPair portPair = event.value();
167 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
168 notifyListeners(new PortPairEvent(
169 PortPairEvent.Type.PORT_PAIR_PUT,
170 portPair));
171 }
172 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
173 notifyListeners(new PortPairEvent(
174 PortPairEvent.Type.PORT_PAIR_DELETE,
175 portPair));
176 }
177 }
178 }
179
180 /**
181 * Notifies specify event to all listeners.
182 *
183 * @param event port pair event
184 */
185 private void notifyListeners(PortPairEvent event) {
186 checkNotNull(event, EVENT_NOT_NULL);
187 post(event);
188 }
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530189}