blob: fd38cd9da58fa34c99ffcc0972fc1a4645d35370 [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;
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 Huaweic106f452015-12-05 14:21:10 +053038import org.onosproject.vtnrsc.portpairgroup.PortPairGroupEvent;
Mahesh Poojary Huawei2e4cd652015-11-29 15:28:20 +053039import org.onosproject.vtnrsc.portpairgroup.PortPairGroupListener;
Phaneendra Manda8beaee72015-10-28 17:58:48 +053040import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
41import org.slf4j.Logger;
42
43/**
44 * Provides implementation of the portPairGroupService.
45 */
46@Component(immediate = true)
47@Service
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +053048public class PortPairGroupManager extends AbstractListenerManager<PortPairGroupEvent, PortPairGroupListener> implements
49 PortPairGroupService {
Phaneendra Manda8beaee72015-10-28 17:58:48 +053050
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());
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +053056
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();
81 log.info("Stopped");
82 }
83
84 @Override
85 public boolean exists(PortPairGroupId portPairGroupId) {
86 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_ID_NULL);
87 return portPairGroupStore.containsKey(portPairGroupId);
88 }
89
90 @Override
91 public int getPortPairGroupCount() {
92 return portPairGroupStore.size();
93 }
94
95 @Override
96 public Iterable<PortPairGroup> getPortPairGroups() {
97 return Collections.unmodifiableCollection(portPairGroupStore.values());
98 }
99
100 @Override
101 public PortPairGroup getPortPairGroup(PortPairGroupId portPairGroupId) {
102 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_ID_NULL);
103 return portPairGroupStore.get(portPairGroupId);
104 }
105
106 @Override
107 public boolean createPortPairGroup(PortPairGroup portPairGroup) {
108 checkNotNull(portPairGroup, PORT_PAIR_GROUP_NULL);
109
110 portPairGroupStore.put(portPairGroup.portPairGroupId(), portPairGroup);
111 if (!portPairGroupStore.containsKey(portPairGroup.portPairGroupId())) {
112 log.debug("The portPairGroup is created failed which identifier was {}", portPairGroup.portPairGroupId()
Mahesh Poojary Huaweic106f452015-12-05 14:21:10 +0530113 .toString());
Phaneendra Manda8beaee72015-10-28 17:58:48 +0530114 return false;
115 }
116 return true;
117 }
118
119 @Override
120 public boolean updatePortPairGroup(PortPairGroup portPairGroup) {
121 checkNotNull(portPairGroup, PORT_PAIR_GROUP_NULL);
122
123 if (!portPairGroupStore.containsKey(portPairGroup.portPairGroupId())) {
124 log.debug("The portPairGroup is not exist whose identifier was {} ",
125 portPairGroup.portPairGroupId().toString());
126 return false;
127 }
128
129 portPairGroupStore.put(portPairGroup.portPairGroupId(), portPairGroup);
130
131 if (!portPairGroup.equals(portPairGroupStore.get(portPairGroup.portPairGroupId()))) {
132 log.debug("The portPairGroup is updated failed whose identifier was {} ",
133 portPairGroup.portPairGroupId().toString());
134 return false;
135 }
136 return true;
137 }
138
139 @Override
140 public boolean removePortPairGroup(PortPairGroupId portPairGroupId) {
141 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_NULL);
142
143 portPairGroupStore.remove(portPairGroupId);
144 if (portPairGroupStore.containsKey(portPairGroupId)) {
145 log.debug("The portPairGroup is removed failed whose identifier was {}",
146 portPairGroupId.toString());
147 return false;
148 }
149 return true;
150 }
Phaneendra Manda8beaee72015-10-28 17:58:48 +0530151}