blob: 18105b25dacf3faf2c8f2788f33c61aeb769c14e [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;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053022import java.util.UUID;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053023
24import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.apache.felix.scr.annotations.Service;
30import org.onlab.util.KryoNamespace;
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053031import org.onosproject.event.AbstractListenerManager;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053032import org.onosproject.store.serializers.KryoNamespaces;
33import org.onosproject.store.service.EventuallyConsistentMap;
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053034import org.onosproject.store.service.EventuallyConsistentMapEvent;
35import org.onosproject.store.service.EventuallyConsistentMapListener;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053036import org.onosproject.store.service.MultiValuedTimestamp;
37import org.onosproject.store.service.StorageService;
38import org.onosproject.store.service.WallClockTimestamp;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053039import org.onosproject.vtnrsc.DefaultPortPair;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053040import org.onosproject.vtnrsc.PortPair;
41import org.onosproject.vtnrsc.PortPairId;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053042import org.onosproject.vtnrsc.TenantId;
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053043import org.onosproject.vtnrsc.portpair.PortPairEvent;
Mahesh Poojary Huawei3fe5eff2015-11-29 15:13:18 +053044import org.onosproject.vtnrsc.portpair.PortPairListener;
Phaneendra Manda05db8b72015-10-28 17:15:38 +053045import org.onosproject.vtnrsc.portpair.PortPairService;
46import org.slf4j.Logger;
47
48/**
49 * Provides implementation of the portPairService.
50 */
51@Component(immediate = true)
52@Service
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053053public class PortPairManager extends AbstractListenerManager<PortPairEvent, PortPairListener> implements
54 PortPairService {
Phaneendra Manda05db8b72015-10-28 17:15:38 +053055
Phaneendra Manda05db8b72015-10-28 17:15:38 +053056 private static final String PORT_PAIR_ID_NULL = "PortPair ID cannot be null";
57 private static final String PORT_PAIR_NULL = "PortPair cannot be null";
Mahesh Poojary Huawei3fe5eff2015-11-29 15:13:18 +053058 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053059 private static final String EVENT_NOT_NULL = "event cannot be null";
Phaneendra Manda05db8b72015-10-28 17:15:38 +053060
Mahesh Poojary Huawei3fe5eff2015-11-29 15:13:18 +053061 private final Logger log = getLogger(getClass());
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053062
Phaneendra Manda05db8b72015-10-28 17:15:38 +053063 private EventuallyConsistentMap<PortPairId, PortPair> portPairStore;
64
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053065 private EventuallyConsistentMapListener<PortPairId, PortPair> portPairListener =
66 new InnerPortPairStoreListener();
67
Phaneendra Manda05db8b72015-10-28 17:15:38 +053068 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected StorageService storageService;
70
71 @Activate
72 public void activate() {
73
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053074 eventDispatcher.addSink(PortPairEvent.class, listenerRegistry);
Phaneendra Manda05db8b72015-10-28 17:15:38 +053075 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
76 .register(KryoNamespaces.API)
77 .register(MultiValuedTimestamp.class)
Phaneendra Manda8db7d092016-06-04 00:17:24 +053078 .register(PortPair.class, PortPairId.class, UUID.class, DefaultPortPair.class, TenantId.class);
Phaneendra Manda05db8b72015-10-28 17:15:38 +053079
Mahesh Poojary S28bfba72015-12-05 14:14:37 +053080 portPairStore = storageService.<PortPairId, PortPair>eventuallyConsistentMapBuilder()
81 .withName("portpairstore")
82 .withSerializer(serializer)
83 .withTimestampProvider((k, v) -> new WallClockTimestamp())
84 .build();
Phaneendra Manda05db8b72015-10-28 17:15:38 +053085
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053086 portPairStore.addListener(portPairListener);
87
Phaneendra Manda05db8b72015-10-28 17:15:38 +053088 log.info("Started");
89 }
90
91 @Deactivate
92 public void deactivate() {
Bharat saraswal89f5f0d2015-12-11 01:01:37 +053093 eventDispatcher.removeSink(PortPairEvent.class);
Phaneendra Manda05db8b72015-10-28 17:15:38 +053094 portPairStore.destroy();
95 log.info("Stopped");
96 }
97
98 @Override
99 public boolean exists(PortPairId portPairId) {
100 checkNotNull(portPairId, PORT_PAIR_ID_NULL);
101 return portPairStore.containsKey(portPairId);
102 }
103
104 @Override
105 public int getPortPairCount() {
106 return portPairStore.size();
107 }
108
109 @Override
110 public Iterable<PortPair> getPortPairs() {
111 return Collections.unmodifiableCollection(portPairStore.values());
112 }
113
114 @Override
115 public PortPair getPortPair(PortPairId portPairId) {
116 checkNotNull(portPairId, PORT_PAIR_ID_NULL);
117 return portPairStore.get(portPairId);
118 }
119
120 @Override
121 public boolean createPortPair(PortPair portPair) {
122 checkNotNull(portPair, PORT_PAIR_NULL);
123
124 portPairStore.put(portPair.portPairId(), portPair);
125 if (!portPairStore.containsKey(portPair.portPairId())) {
Mahesh Poojary S28bfba72015-12-05 14:14:37 +0530126 log.debug("The portPair is created failed which identifier was {}", portPair.portPairId().toString());
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530127 return false;
128 }
129 return true;
130 }
131
132 @Override
133 public boolean updatePortPair(PortPair portPair) {
134 checkNotNull(portPair, PORT_PAIR_NULL);
135
136 if (!portPairStore.containsKey(portPair.portPairId())) {
Mahesh Poojary S28bfba72015-12-05 14:14:37 +0530137 log.debug("The portPair is not exist whose identifier was {} ", portPair.portPairId().toString());
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530138 return false;
139 }
140
141 portPairStore.put(portPair.portPairId(), portPair);
142
143 if (!portPair.equals(portPairStore.get(portPair.portPairId()))) {
Mahesh Poojary S28bfba72015-12-05 14:14:37 +0530144 log.debug("The portPair is updated failed whose identifier was {} ", portPair.portPairId().toString());
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530145 return false;
146 }
147 return true;
148 }
149
150 @Override
151 public boolean removePortPair(PortPairId portPairId) {
152 checkNotNull(portPairId, PORT_PAIR_NULL);
153
154 portPairStore.remove(portPairId);
155 if (portPairStore.containsKey(portPairId)) {
Mahesh Poojary S28bfba72015-12-05 14:14:37 +0530156 log.debug("The portPair is removed failed whose identifier was {}", portPairId.toString());
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530157 return false;
158 }
159 return true;
160 }
Bharat saraswal89f5f0d2015-12-11 01:01:37 +0530161
162 private class InnerPortPairStoreListener
163 implements
164 EventuallyConsistentMapListener<PortPairId, PortPair> {
165
166 @Override
167 public void event(EventuallyConsistentMapEvent<PortPairId, PortPair> event) {
168 checkNotNull(event, EVENT_NOT_NULL);
169 PortPair portPair = event.value();
170 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
171 notifyListeners(new PortPairEvent(
172 PortPairEvent.Type.PORT_PAIR_PUT,
173 portPair));
174 }
175 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
176 notifyListeners(new PortPairEvent(
177 PortPairEvent.Type.PORT_PAIR_DELETE,
178 portPair));
179 }
180 }
181 }
182
183 /**
184 * Notifies specify event to all listeners.
185 *
186 * @param event port pair event
187 */
188 private void notifyListeners(PortPairEvent event) {
189 checkNotNull(event, EVENT_NOT_NULL);
190 post(event);
191 }
Phaneendra Manda05db8b72015-10-28 17:15:38 +0530192}