2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.vtnrsc.portpairgroup.impl;
18 import static com.google.common.base.Preconditions.checkNotNull;
19 import static org.slf4j.LoggerFactory.getLogger;
21 import java.util.Collections;
23 import org.apache.felix.scr.annotations.Activate;
24 import org.apache.felix.scr.annotations.Component;
25 import org.apache.felix.scr.annotations.Deactivate;
26 import org.apache.felix.scr.annotations.Reference;
27 import org.apache.felix.scr.annotations.ReferenceCardinality;
28 import org.apache.felix.scr.annotations.Service;
29 import org.onlab.util.KryoNamespace;
30 import org.onosproject.store.serializers.KryoNamespaces;
31 import org.onosproject.store.service.EventuallyConsistentMap;
32 import org.onosproject.store.service.MultiValuedTimestamp;
33 import org.onosproject.store.service.StorageService;
34 import org.onosproject.store.service.WallClockTimestamp;
35 import org.onosproject.vtnrsc.PortPairGroup;
36 import org.onosproject.vtnrsc.PortPairGroupId;
37 import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
38 import org.slf4j.Logger;
41 * Provides implementation of the portPairGroupService.
43 @Component(immediate = true)
45 public class PortPairGroupManager implements PortPairGroupService {
47 private final Logger log = getLogger(getClass());
49 private static final String PORT_PAIR_GROUP_ID_NULL = "PortPairGroup ID cannot be null";
50 private static final String PORT_PAIR_GROUP_NULL = "PortPairGroup cannot be null";
52 private EventuallyConsistentMap<PortPairGroupId, PortPairGroup> portPairGroupStore;
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected StorageService storageService;
58 public void activate() {
60 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
61 .register(KryoNamespaces.API)
62 .register(MultiValuedTimestamp.class)
63 .register(PortPairGroup.class);
65 portPairGroupStore = storageService
66 .<PortPairGroupId, PortPairGroup>eventuallyConsistentMapBuilder()
67 .withName("portpairgroupstore").withSerializer(serializer)
68 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
74 public void deactivate() {
75 portPairGroupStore.destroy();
80 public boolean exists(PortPairGroupId portPairGroupId) {
81 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_ID_NULL);
82 return portPairGroupStore.containsKey(portPairGroupId);
86 public int getPortPairGroupCount() {
87 return portPairGroupStore.size();
91 public Iterable<PortPairGroup> getPortPairGroups() {
92 return Collections.unmodifiableCollection(portPairGroupStore.values());
96 public PortPairGroup getPortPairGroup(PortPairGroupId portPairGroupId) {
97 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_ID_NULL);
98 return portPairGroupStore.get(portPairGroupId);
102 public boolean createPortPairGroup(PortPairGroup portPairGroup) {
103 checkNotNull(portPairGroup, PORT_PAIR_GROUP_NULL);
105 portPairGroupStore.put(portPairGroup.portPairGroupId(), portPairGroup);
106 if (!portPairGroupStore.containsKey(portPairGroup.portPairGroupId())) {
107 log.debug("The portPairGroup is created failed which identifier was {}", portPairGroup.portPairGroupId()
115 public boolean updatePortPairGroup(PortPairGroup portPairGroup) {
116 checkNotNull(portPairGroup, PORT_PAIR_GROUP_NULL);
118 if (!portPairGroupStore.containsKey(portPairGroup.portPairGroupId())) {
119 log.debug("The portPairGroup is not exist whose identifier was {} ",
120 portPairGroup.portPairGroupId().toString());
124 portPairGroupStore.put(portPairGroup.portPairGroupId(), portPairGroup);
126 if (!portPairGroup.equals(portPairGroupStore.get(portPairGroup.portPairGroupId()))) {
127 log.debug("The portPairGroup is updated failed whose identifier was {} ",
128 portPairGroup.portPairGroupId().toString());
135 public boolean removePortPairGroup(PortPairGroupId portPairGroupId) {
136 checkNotNull(portPairGroupId, PORT_PAIR_GROUP_NULL);
138 portPairGroupStore.remove(portPairGroupId);
139 if (portPairGroupStore.containsKey(portPairGroupId)) {
140 log.debug("The portPairGroup is removed failed whose identifier was {}",
141 portPairGroupId.toString());