blob: 1e030920543cc3a7df6f40ee6aed0168b008de58 [file] [log] [blame]
Phaneendra Manda8beaee72015-10-28 17:58:48 +05301/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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;
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 Huaweic106f452015-12-05 14:21:10 +053030import org.onosproject.event.AbstractListenerManager;
Phaneendra Manda8beaee72015-10-28 17:58:48 +053031import org.onosproject.store.serializers.KryoNamespaces;
32import org.onosproject.store.service.EventuallyConsistentMap;
Bharat saraswal513e8172015-12-11 01:04:50 +053033import org.onosproject.store.service.EventuallyConsistentMapEvent;
34import org.onosproject.store.service.EventuallyConsistentMapListener;
Phaneendra Manda8beaee72015-10-28 17:58:48 +053035import org.onosproject.store.service.MultiValuedTimestamp;
36import org.onosproject.store.service.StorageService;
37import org.onosproject.store.service.WallClockTimestamp;
38import org.onosproject.vtnrsc.PortPairGroup;
39import org.onosproject.vtnrsc.PortPairGroupId;
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +053040import org.onosproject.vtnrsc.portpairgroup.PortPairGroupEvent;
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053041import org.onosproject.vtnrsc.portpairgroup.PortPairGroupListener;
Phaneendra Manda8beaee72015-10-28 17:58:48 +053042import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
43import org.slf4j.Logger;
44
45/**
46 * Provides implementation of the portPairGroupService.
47 */
48@Component(immediate = true)
49@Service
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +053050public class PortPairGroupManager extends AbstractListenerManager<PortPairGroupEvent, PortPairGroupListener> implements
51 PortPairGroupService {
Phaneendra Manda8beaee72015-10-28 17:58:48 +053052
Phaneendra Manda8beaee72015-10-28 17:58:48 +053053 private static final String PORT_PAIR_GROUP_ID_NULL = "PortPairGroup ID cannot be null";
54 private static final String PORT_PAIR_GROUP_NULL = "PortPairGroup cannot be null";
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053055 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
Bharat saraswal513e8172015-12-11 01:04:50 +053056 private static final String EVENT_NOT_NULL = "event cannot be null";
Phaneendra Manda8beaee72015-10-28 17:58:48 +053057
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053058 private final Logger log = getLogger(getClass());
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +053059
Phaneendra Manda8beaee72015-10-28 17:58:48 +053060 private EventuallyConsistentMap<PortPairGroupId, PortPairGroup> portPairGroupStore;
61
Bharat saraswal513e8172015-12-11 01:04:50 +053062 private EventuallyConsistentMapListener<PortPairGroupId, PortPairGroup> portPairGroupListener =
63 new InnerPortPairGroupStoreListener();
64
Phaneendra Manda8beaee72015-10-28 17:58:48 +053065 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected StorageService storageService;
67
68 @Activate
69 public void activate() {
Bharat saraswal513e8172015-12-11 01:04:50 +053070 eventDispatcher.addSink(PortPairGroupEvent.class, listenerRegistry);
Phaneendra Manda8beaee72015-10-28 17:58:48 +053071 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
72 .register(KryoNamespaces.API)
73 .register(MultiValuedTimestamp.class)
74 .register(PortPairGroup.class);
75
76 portPairGroupStore = storageService
77 .<PortPairGroupId, PortPairGroup>eventuallyConsistentMapBuilder()
78 .withName("portpairgroupstore").withSerializer(serializer)
79 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
80
Bharat saraswal513e8172015-12-11 01:04:50 +053081 portPairGroupStore.addListener(portPairGroupListener);
82
Phaneendra Manda8beaee72015-10-28 17:58:48 +053083 log.info("Started");
84 }
85
86 @Deactivate
87 public void deactivate() {
Bharat saraswal513e8172015-12-11 01:04:50 +053088 eventDispatcher.removeSink(PortPairGroupEvent.class);
Phaneendra Manda8beaee72015-10-28 17:58:48 +053089 portPairGroupStore.destroy();
90 log.info("Stopped");
91 }
92
93 @Override
94 public boolean exists(PortPairGroupId portPairGroupId) {
95 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_ID_NULL);
96 return portPairGroupStore.containsKey(portPairGroupId);
97 }
98
99 @Override
100 public int getPortPairGroupCount() {
101 return portPairGroupStore.size();
102 }
103
104 @Override
105 public Iterable<PortPairGroup> getPortPairGroups() {
106 return Collections.unmodifiableCollection(portPairGroupStore.values());
107 }
108
109 @Override
110 public PortPairGroup getPortPairGroup(PortPairGroupId portPairGroupId) {
111 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_ID_NULL);
112 return portPairGroupStore.get(portPairGroupId);
113 }
114
115 @Override
116 public boolean createPortPairGroup(PortPairGroup portPairGroup) {
117 checkNotNull(portPairGroup, PORT_PAIR_GROUP_NULL);
118
119 portPairGroupStore.put(portPairGroup.portPairGroupId(), portPairGroup);
120 if (!portPairGroupStore.containsKey(portPairGroup.portPairGroupId())) {
121 log.debug("The portPairGroup is created failed which identifier was {}", portPairGroup.portPairGroupId()
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +0530122 .toString());
Phaneendra Manda8beaee72015-10-28 17:58:48 +0530123 return false;
124 }
125 return true;
126 }
127
128 @Override
129 public boolean updatePortPairGroup(PortPairGroup portPairGroup) {
130 checkNotNull(portPairGroup, PORT_PAIR_GROUP_NULL);
131
132 if (!portPairGroupStore.containsKey(portPairGroup.portPairGroupId())) {
133 log.debug("The portPairGroup is not exist whose identifier was {} ",
134 portPairGroup.portPairGroupId().toString());
135 return false;
136 }
137
138 portPairGroupStore.put(portPairGroup.portPairGroupId(), portPairGroup);
139
140 if (!portPairGroup.equals(portPairGroupStore.get(portPairGroup.portPairGroupId()))) {
141 log.debug("The portPairGroup is updated failed whose identifier was {} ",
142 portPairGroup.portPairGroupId().toString());
143 return false;
144 }
145 return true;
146 }
147
148 @Override
149 public boolean removePortPairGroup(PortPairGroupId portPairGroupId) {
150 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_NULL);
151
152 portPairGroupStore.remove(portPairGroupId);
153 if (portPairGroupStore.containsKey(portPairGroupId)) {
154 log.debug("The portPairGroup is removed failed whose identifier was {}",
155 portPairGroupId.toString());
156 return false;
157 }
158 return true;
159 }
Bharat saraswal513e8172015-12-11 01:04:50 +0530160
161
162 private class InnerPortPairGroupStoreListener
163 implements
164 EventuallyConsistentMapListener<PortPairGroupId, PortPairGroup> {
165
166 @Override
167 public void event(EventuallyConsistentMapEvent<PortPairGroupId, PortPairGroup> event) {
168 checkNotNull(event, EVENT_NOT_NULL);
169 PortPairGroup portPairGroup = event.value();
170 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
171 notifyListeners(new PortPairGroupEvent(
172 PortPairGroupEvent.Type.PORT_PAIR_GROUP_PUT,
173 portPairGroup));
174 }
175 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
176 notifyListeners(new PortPairGroupEvent(
177 PortPairGroupEvent.Type.PORT_PAIR_GROUP_DELETE,
178 portPairGroup));
179 }
180 }
181 }
182
183 /**
184 * Notifies specify event to all listeners.
185 *
186 * @param event PortPairGroup event
187 */
188 private void notifyListeners(PortPairGroupEvent event) {
189 checkNotNull(event, EVENT_NOT_NULL);
190 post(event);
191 }
Phaneendra Manda8beaee72015-10-28 17:58:48 +0530192}