blob: f5ff8e895d7bce9dcc1f3a128f900547bf5c9ff4 [file] [log] [blame]
Phaneendra Manda8beaee72015-10-28 17:58:48 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Phaneendra Manda8beaee72015-10-28 17:58:48 +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.portpairgroup.impl;
17
Phaneendra Manda8beaee72015-10-28 17:58:48 +053018import org.onlab.util.KryoNamespace;
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +053019import org.onosproject.event.AbstractListenerManager;
Phaneendra Manda8beaee72015-10-28 17:58:48 +053020import org.onosproject.store.serializers.KryoNamespaces;
21import org.onosproject.store.service.EventuallyConsistentMap;
Bharat saraswal513e8172015-12-11 01:04:50 +053022import org.onosproject.store.service.EventuallyConsistentMapEvent;
23import org.onosproject.store.service.EventuallyConsistentMapListener;
Phaneendra Manda8beaee72015-10-28 17:58:48 +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.DefaultPortPairGroup;
Phaneendra Manda8beaee72015-10-28 17:58:48 +053028import org.onosproject.vtnrsc.PortPairGroup;
29import org.onosproject.vtnrsc.PortPairGroupId;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053030import org.onosproject.vtnrsc.PortPairId;
31import org.onosproject.vtnrsc.TenantId;
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +053032import org.onosproject.vtnrsc.portpairgroup.PortPairGroupEvent;
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053033import org.onosproject.vtnrsc.portpairgroup.PortPairGroupListener;
Phaneendra Manda8beaee72015-10-28 17:58:48 +053034import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035import org.osgi.service.component.annotations.Activate;
36import org.osgi.service.component.annotations.Component;
37import org.osgi.service.component.annotations.Deactivate;
38import org.osgi.service.component.annotations.Reference;
39import org.osgi.service.component.annotations.ReferenceCardinality;
Phaneendra Manda8beaee72015-10-28 17:58:48 +053040import org.slf4j.Logger;
41
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042import java.util.Collections;
43import java.util.UUID;
44
45import static com.google.common.base.Preconditions.checkNotNull;
46import static org.slf4j.LoggerFactory.getLogger;
47
Phaneendra Manda8beaee72015-10-28 17:58:48 +053048/**
49 * Provides implementation of the portPairGroupService.
50 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070051@Component(immediate = true, service = PortPairGroupService.class)
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +053052public class PortPairGroupManager extends AbstractListenerManager<PortPairGroupEvent, PortPairGroupListener> implements
53 PortPairGroupService {
Phaneendra Manda8beaee72015-10-28 17:58:48 +053054
Phaneendra Manda8beaee72015-10-28 17:58:48 +053055 private static final String PORT_PAIR_GROUP_ID_NULL = "PortPairGroup ID cannot be null";
56 private static final String PORT_PAIR_GROUP_NULL = "PortPairGroup cannot be null";
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053057 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
Bharat saraswal513e8172015-12-11 01:04:50 +053058 private static final String EVENT_NOT_NULL = "event cannot be null";
Phaneendra Manda8beaee72015-10-28 17:58:48 +053059
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053060 private final Logger log = getLogger(getClass());
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +053061
Phaneendra Manda8beaee72015-10-28 17:58:48 +053062 private EventuallyConsistentMap<PortPairGroupId, PortPairGroup> portPairGroupStore;
63
Bharat saraswal513e8172015-12-11 01:04:50 +053064 private EventuallyConsistentMapListener<PortPairGroupId, PortPairGroup> portPairGroupListener =
65 new InnerPortPairGroupStoreListener();
66
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Phaneendra Manda8beaee72015-10-28 17:58:48 +053068 protected StorageService storageService;
69
70 @Activate
71 public void activate() {
Bharat saraswal513e8172015-12-11 01:04:50 +053072 eventDispatcher.addSink(PortPairGroupEvent.class, listenerRegistry);
Phaneendra Manda8beaee72015-10-28 17:58:48 +053073 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
74 .register(KryoNamespaces.API)
75 .register(MultiValuedTimestamp.class)
Phaneendra Manda8db7d092016-06-04 00:17:24 +053076 .register(PortPairGroup.class, PortPairGroupId.class, UUID.class, DefaultPortPairGroup.class,
77 TenantId.class, PortPairId.class);
Phaneendra Manda8beaee72015-10-28 17:58:48 +053078
79 portPairGroupStore = storageService
80 .<PortPairGroupId, PortPairGroup>eventuallyConsistentMapBuilder()
81 .withName("portpairgroupstore").withSerializer(serializer)
82 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
83
Bharat saraswal513e8172015-12-11 01:04:50 +053084 portPairGroupStore.addListener(portPairGroupListener);
85
Phaneendra Manda8beaee72015-10-28 17:58:48 +053086 log.info("Started");
87 }
88
89 @Deactivate
90 public void deactivate() {
Bharat saraswal513e8172015-12-11 01:04:50 +053091 eventDispatcher.removeSink(PortPairGroupEvent.class);
Phaneendra Manda8beaee72015-10-28 17:58:48 +053092 portPairGroupStore.destroy();
93 log.info("Stopped");
94 }
95
96 @Override
97 public boolean exists(PortPairGroupId portPairGroupId) {
98 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_ID_NULL);
99 return portPairGroupStore.containsKey(portPairGroupId);
100 }
101
102 @Override
103 public int getPortPairGroupCount() {
104 return portPairGroupStore.size();
105 }
106
107 @Override
108 public Iterable<PortPairGroup> getPortPairGroups() {
109 return Collections.unmodifiableCollection(portPairGroupStore.values());
110 }
111
112 @Override
113 public PortPairGroup getPortPairGroup(PortPairGroupId portPairGroupId) {
114 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_ID_NULL);
115 return portPairGroupStore.get(portPairGroupId);
116 }
117
118 @Override
119 public boolean createPortPairGroup(PortPairGroup portPairGroup) {
120 checkNotNull(portPairGroup, PORT_PAIR_GROUP_NULL);
121
122 portPairGroupStore.put(portPairGroup.portPairGroupId(), portPairGroup);
123 if (!portPairGroupStore.containsKey(portPairGroup.portPairGroupId())) {
124 log.debug("The portPairGroup is created failed which identifier was {}", portPairGroup.portPairGroupId()
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +0530125 .toString());
Phaneendra Manda8beaee72015-10-28 17:58:48 +0530126 return false;
127 }
128 return true;
129 }
130
131 @Override
132 public boolean updatePortPairGroup(PortPairGroup portPairGroup) {
133 checkNotNull(portPairGroup, PORT_PAIR_GROUP_NULL);
134
135 if (!portPairGroupStore.containsKey(portPairGroup.portPairGroupId())) {
136 log.debug("The portPairGroup is not exist whose identifier was {} ",
137 portPairGroup.portPairGroupId().toString());
138 return false;
139 }
140
141 portPairGroupStore.put(portPairGroup.portPairGroupId(), portPairGroup);
142
143 if (!portPairGroup.equals(portPairGroupStore.get(portPairGroup.portPairGroupId()))) {
144 log.debug("The portPairGroup is updated failed whose identifier was {} ",
145 portPairGroup.portPairGroupId().toString());
146 return false;
147 }
148 return true;
149 }
150
151 @Override
152 public boolean removePortPairGroup(PortPairGroupId portPairGroupId) {
153 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_NULL);
154
155 portPairGroupStore.remove(portPairGroupId);
156 if (portPairGroupStore.containsKey(portPairGroupId)) {
157 log.debug("The portPairGroup is removed failed whose identifier was {}",
158 portPairGroupId.toString());
159 return false;
160 }
161 return true;
162 }
Bharat saraswal513e8172015-12-11 01:04:50 +0530163
164
165 private class InnerPortPairGroupStoreListener
166 implements
167 EventuallyConsistentMapListener<PortPairGroupId, PortPairGroup> {
168
169 @Override
170 public void event(EventuallyConsistentMapEvent<PortPairGroupId, PortPairGroup> event) {
171 checkNotNull(event, EVENT_NOT_NULL);
172 PortPairGroup portPairGroup = event.value();
173 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
174 notifyListeners(new PortPairGroupEvent(
175 PortPairGroupEvent.Type.PORT_PAIR_GROUP_PUT,
176 portPairGroup));
177 }
178 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
179 notifyListeners(new PortPairGroupEvent(
180 PortPairGroupEvent.Type.PORT_PAIR_GROUP_DELETE,
181 portPairGroup));
182 }
183 }
184 }
185
186 /**
187 * Notifies specify event to all listeners.
188 *
189 * @param event PortPairGroup event
190 */
191 private void notifyListeners(PortPairGroupEvent event) {
192 checkNotNull(event, EVENT_NOT_NULL);
193 post(event);
194 }
Phaneendra Manda8beaee72015-10-28 17:58:48 +0530195}