blob: 4c76c1edcd8eb630aba80d23f242b1abf553a559 [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
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 Manda8beaee72015-10-28 17:58:48 +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 Huaweic106f452015-12-05 14:21:10 +053031import org.onosproject.event.AbstractListenerManager;
Phaneendra Manda8beaee72015-10-28 17:58:48 +053032import org.onosproject.store.serializers.KryoNamespaces;
33import org.onosproject.store.service.EventuallyConsistentMap;
Bharat saraswal513e8172015-12-11 01:04:50 +053034import org.onosproject.store.service.EventuallyConsistentMapEvent;
35import org.onosproject.store.service.EventuallyConsistentMapListener;
Phaneendra Manda8beaee72015-10-28 17:58:48 +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.DefaultPortPairGroup;
Phaneendra Manda8beaee72015-10-28 17:58:48 +053040import org.onosproject.vtnrsc.PortPairGroup;
41import org.onosproject.vtnrsc.PortPairGroupId;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053042import org.onosproject.vtnrsc.PortPairId;
43import org.onosproject.vtnrsc.TenantId;
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +053044import org.onosproject.vtnrsc.portpairgroup.PortPairGroupEvent;
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053045import org.onosproject.vtnrsc.portpairgroup.PortPairGroupListener;
Phaneendra Manda8beaee72015-10-28 17:58:48 +053046import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
47import org.slf4j.Logger;
48
49/**
50 * Provides implementation of the portPairGroupService.
51 */
52@Component(immediate = true)
53@Service
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +053054public class PortPairGroupManager extends AbstractListenerManager<PortPairGroupEvent, PortPairGroupListener> implements
55 PortPairGroupService {
Phaneendra Manda8beaee72015-10-28 17:58:48 +053056
Phaneendra Manda8beaee72015-10-28 17:58:48 +053057 private static final String PORT_PAIR_GROUP_ID_NULL = "PortPairGroup ID cannot be null";
58 private static final String PORT_PAIR_GROUP_NULL = "PortPairGroup cannot be null";
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053059 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
Bharat saraswal513e8172015-12-11 01:04:50 +053060 private static final String EVENT_NOT_NULL = "event cannot be null";
Phaneendra Manda8beaee72015-10-28 17:58:48 +053061
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053062 private final Logger log = getLogger(getClass());
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +053063
Phaneendra Manda8beaee72015-10-28 17:58:48 +053064 private EventuallyConsistentMap<PortPairGroupId, PortPairGroup> portPairGroupStore;
65
Bharat saraswal513e8172015-12-11 01:04:50 +053066 private EventuallyConsistentMapListener<PortPairGroupId, PortPairGroup> portPairGroupListener =
67 new InnerPortPairGroupStoreListener();
68
Phaneendra Manda8beaee72015-10-28 17:58:48 +053069 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected StorageService storageService;
71
72 @Activate
73 public void activate() {
Bharat saraswal513e8172015-12-11 01:04:50 +053074 eventDispatcher.addSink(PortPairGroupEvent.class, listenerRegistry);
Phaneendra Manda8beaee72015-10-28 17:58:48 +053075 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
76 .register(KryoNamespaces.API)
77 .register(MultiValuedTimestamp.class)
Phaneendra Manda8db7d092016-06-04 00:17:24 +053078 .register(PortPairGroup.class, PortPairGroupId.class, UUID.class, DefaultPortPairGroup.class,
79 TenantId.class, PortPairId.class);
Phaneendra Manda8beaee72015-10-28 17:58:48 +053080
81 portPairGroupStore = storageService
82 .<PortPairGroupId, PortPairGroup>eventuallyConsistentMapBuilder()
83 .withName("portpairgroupstore").withSerializer(serializer)
84 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
85
Bharat saraswal513e8172015-12-11 01:04:50 +053086 portPairGroupStore.addListener(portPairGroupListener);
87
Phaneendra Manda8beaee72015-10-28 17:58:48 +053088 log.info("Started");
89 }
90
91 @Deactivate
92 public void deactivate() {
Bharat saraswal513e8172015-12-11 01:04:50 +053093 eventDispatcher.removeSink(PortPairGroupEvent.class);
Phaneendra Manda8beaee72015-10-28 17:58:48 +053094 portPairGroupStore.destroy();
95 log.info("Stopped");
96 }
97
98 @Override
99 public boolean exists(PortPairGroupId portPairGroupId) {
100 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_ID_NULL);
101 return portPairGroupStore.containsKey(portPairGroupId);
102 }
103
104 @Override
105 public int getPortPairGroupCount() {
106 return portPairGroupStore.size();
107 }
108
109 @Override
110 public Iterable<PortPairGroup> getPortPairGroups() {
111 return Collections.unmodifiableCollection(portPairGroupStore.values());
112 }
113
114 @Override
115 public PortPairGroup getPortPairGroup(PortPairGroupId portPairGroupId) {
116 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_ID_NULL);
117 return portPairGroupStore.get(portPairGroupId);
118 }
119
120 @Override
121 public boolean createPortPairGroup(PortPairGroup portPairGroup) {
122 checkNotNull(portPairGroup, PORT_PAIR_GROUP_NULL);
123
124 portPairGroupStore.put(portPairGroup.portPairGroupId(), portPairGroup);
125 if (!portPairGroupStore.containsKey(portPairGroup.portPairGroupId())) {
126 log.debug("The portPairGroup is created failed which identifier was {}", portPairGroup.portPairGroupId()
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +0530127 .toString());
Phaneendra Manda8beaee72015-10-28 17:58:48 +0530128 return false;
129 }
130 return true;
131 }
132
133 @Override
134 public boolean updatePortPairGroup(PortPairGroup portPairGroup) {
135 checkNotNull(portPairGroup, PORT_PAIR_GROUP_NULL);
136
137 if (!portPairGroupStore.containsKey(portPairGroup.portPairGroupId())) {
138 log.debug("The portPairGroup is not exist whose identifier was {} ",
139 portPairGroup.portPairGroupId().toString());
140 return false;
141 }
142
143 portPairGroupStore.put(portPairGroup.portPairGroupId(), portPairGroup);
144
145 if (!portPairGroup.equals(portPairGroupStore.get(portPairGroup.portPairGroupId()))) {
146 log.debug("The portPairGroup is updated failed whose identifier was {} ",
147 portPairGroup.portPairGroupId().toString());
148 return false;
149 }
150 return true;
151 }
152
153 @Override
154 public boolean removePortPairGroup(PortPairGroupId portPairGroupId) {
155 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_NULL);
156
157 portPairGroupStore.remove(portPairGroupId);
158 if (portPairGroupStore.containsKey(portPairGroupId)) {
159 log.debug("The portPairGroup is removed failed whose identifier was {}",
160 portPairGroupId.toString());
161 return false;
162 }
163 return true;
164 }
Bharat saraswal513e8172015-12-11 01:04:50 +0530165
166
167 private class InnerPortPairGroupStoreListener
168 implements
169 EventuallyConsistentMapListener<PortPairGroupId, PortPairGroup> {
170
171 @Override
172 public void event(EventuallyConsistentMapEvent<PortPairGroupId, PortPairGroup> event) {
173 checkNotNull(event, EVENT_NOT_NULL);
174 PortPairGroup portPairGroup = event.value();
175 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
176 notifyListeners(new PortPairGroupEvent(
177 PortPairGroupEvent.Type.PORT_PAIR_GROUP_PUT,
178 portPairGroup));
179 }
180 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
181 notifyListeners(new PortPairGroupEvent(
182 PortPairGroupEvent.Type.PORT_PAIR_GROUP_DELETE,
183 portPairGroup));
184 }
185 }
186 }
187
188 /**
189 * Notifies specify event to all listeners.
190 *
191 * @param event PortPairGroup event
192 */
193 private void notifyListeners(PortPairGroupEvent event) {
194 checkNotNull(event, EVENT_NOT_NULL);
195 post(event);
196 }
Phaneendra Manda8beaee72015-10-28 17:58:48 +0530197}