blob: 5f80ef64b1ad9f49a3e48d0d43dc95c10892939a [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;
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053022import java.util.Set;
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;
31import org.onosproject.store.serializers.KryoNamespaces;
32import org.onosproject.store.service.EventuallyConsistentMap;
33import org.onosproject.store.service.MultiValuedTimestamp;
34import org.onosproject.store.service.StorageService;
35import org.onosproject.store.service.WallClockTimestamp;
36import org.onosproject.vtnrsc.PortPairGroup;
37import org.onosproject.vtnrsc.PortPairGroupId;
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053038import org.onosproject.vtnrsc.portpairgroup.PortPairGroupListener;
Phaneendra Manda8beaee72015-10-28 17:58:48 +053039import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
40import org.slf4j.Logger;
41
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053042import com.google.common.collect.Sets;
43
Phaneendra Manda8beaee72015-10-28 17:58:48 +053044/**
45 * Provides implementation of the portPairGroupService.
46 */
47@Component(immediate = true)
48@Service
49public class PortPairGroupManager implements PortPairGroupService {
50
Phaneendra Manda8beaee72015-10-28 17:58:48 +053051 private static final String PORT_PAIR_GROUP_ID_NULL = "PortPairGroup ID cannot be null";
52 private static final String PORT_PAIR_GROUP_NULL = "PortPairGroup cannot be null";
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053053 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
Phaneendra Manda8beaee72015-10-28 17:58:48 +053054
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053055 private final Logger log = getLogger(getClass());
56 private final Set<PortPairGroupListener> listeners = Sets.newCopyOnWriteArraySet();
Phaneendra Manda8beaee72015-10-28 17:58:48 +053057 private EventuallyConsistentMap<PortPairGroupId, PortPairGroup> portPairGroupStore;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected StorageService storageService;
61
62 @Activate
63 public void activate() {
64
65 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
66 .register(KryoNamespaces.API)
67 .register(MultiValuedTimestamp.class)
68 .register(PortPairGroup.class);
69
70 portPairGroupStore = storageService
71 .<PortPairGroupId, PortPairGroup>eventuallyConsistentMapBuilder()
72 .withName("portpairgroupstore").withSerializer(serializer)
73 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
74
75 log.info("Started");
76 }
77
78 @Deactivate
79 public void deactivate() {
80 portPairGroupStore.destroy();
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053081 listeners.clear();
Phaneendra Manda8beaee72015-10-28 17:58:48 +053082 log.info("Stopped");
83 }
84
85 @Override
86 public boolean exists(PortPairGroupId portPairGroupId) {
87 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_ID_NULL);
88 return portPairGroupStore.containsKey(portPairGroupId);
89 }
90
91 @Override
92 public int getPortPairGroupCount() {
93 return portPairGroupStore.size();
94 }
95
96 @Override
97 public Iterable<PortPairGroup> getPortPairGroups() {
98 return Collections.unmodifiableCollection(portPairGroupStore.values());
99 }
100
101 @Override
102 public PortPairGroup getPortPairGroup(PortPairGroupId portPairGroupId) {
103 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_ID_NULL);
104 return portPairGroupStore.get(portPairGroupId);
105 }
106
107 @Override
108 public boolean createPortPairGroup(PortPairGroup portPairGroup) {
109 checkNotNull(portPairGroup, PORT_PAIR_GROUP_NULL);
110
111 portPairGroupStore.put(portPairGroup.portPairGroupId(), portPairGroup);
112 if (!portPairGroupStore.containsKey(portPairGroup.portPairGroupId())) {
113 log.debug("The portPairGroup is created failed which identifier was {}", portPairGroup.portPairGroupId()
114 .toString());
115 return false;
116 }
117 return true;
118 }
119
120 @Override
121 public boolean updatePortPairGroup(PortPairGroup portPairGroup) {
122 checkNotNull(portPairGroup, PORT_PAIR_GROUP_NULL);
123
124 if (!portPairGroupStore.containsKey(portPairGroup.portPairGroupId())) {
125 log.debug("The portPairGroup is not exist whose identifier was {} ",
126 portPairGroup.portPairGroupId().toString());
127 return false;
128 }
129
130 portPairGroupStore.put(portPairGroup.portPairGroupId(), portPairGroup);
131
132 if (!portPairGroup.equals(portPairGroupStore.get(portPairGroup.portPairGroupId()))) {
133 log.debug("The portPairGroup is updated failed whose identifier was {} ",
134 portPairGroup.portPairGroupId().toString());
135 return false;
136 }
137 return true;
138 }
139
140 @Override
141 public boolean removePortPairGroup(PortPairGroupId portPairGroupId) {
142 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_NULL);
143
144 portPairGroupStore.remove(portPairGroupId);
145 if (portPairGroupStore.containsKey(portPairGroupId)) {
146 log.debug("The portPairGroup is removed failed whose identifier was {}",
147 portPairGroupId.toString());
148 return false;
149 }
150 return true;
151 }
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +0530152
153 @Override
154 public void addListener(PortPairGroupListener listener) {
155 checkNotNull(listener, LISTENER_NOT_NULL);
156 listeners.add(listener);
157 }
158
159 @Override
160 public void removeListener(PortPairGroupListener listener) {
161 checkNotNull(listener, LISTENER_NOT_NULL);
162 listeners.remove(listener);
163 }
Phaneendra Manda8beaee72015-10-28 17:58:48 +0530164}