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.portpair.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.PortPair;
36 import org.onosproject.vtnrsc.PortPairId;
37 import org.onosproject.vtnrsc.portpair.PortPairService;
38 import org.slf4j.Logger;
41 * Provides implementation of the portPairService.
43 @Component(immediate = true)
45 public class PortPairManager implements PortPairService {
47 private final Logger log = getLogger(getClass());
49 private static final String PORT_PAIR_ID_NULL = "PortPair ID cannot be null";
50 private static final String PORT_PAIR_NULL = "PortPair cannot be null";
52 private EventuallyConsistentMap<PortPairId, PortPair> portPairStore;
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(PortPair.class);
65 portPairStore = storageService
66 .<PortPairId, PortPair>eventuallyConsistentMapBuilder()
67 .withName("portpairstore").withSerializer(serializer)
68 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
74 public void deactivate() {
75 portPairStore.destroy();
80 public boolean exists(PortPairId portPairId) {
81 checkNotNull(portPairId, PORT_PAIR_ID_NULL);
82 return portPairStore.containsKey(portPairId);
86 public int getPortPairCount() {
87 return portPairStore.size();
91 public Iterable<PortPair> getPortPairs() {
92 return Collections.unmodifiableCollection(portPairStore.values());
96 public PortPair getPortPair(PortPairId portPairId) {
97 checkNotNull(portPairId, PORT_PAIR_ID_NULL);
98 return portPairStore.get(portPairId);
102 public boolean createPortPair(PortPair portPair) {
103 checkNotNull(portPair, PORT_PAIR_NULL);
105 portPairStore.put(portPair.portPairId(), portPair);
106 if (!portPairStore.containsKey(portPair.portPairId())) {
107 log.debug("The portPair is created failed which identifier was {}", portPair.portPairId()
115 public boolean updatePortPair(PortPair portPair) {
116 checkNotNull(portPair, PORT_PAIR_NULL);
118 if (!portPairStore.containsKey(portPair.portPairId())) {
119 log.debug("The portPair is not exist whose identifier was {} ",
120 portPair.portPairId().toString());
124 portPairStore.put(portPair.portPairId(), portPair);
126 if (!portPair.equals(portPairStore.get(portPair.portPairId()))) {
127 log.debug("The portPair is updated failed whose identifier was {} ",
128 portPair.portPairId().toString());
135 public boolean removePortPair(PortPairId portPairId) {
136 checkNotNull(portPairId, PORT_PAIR_NULL);
138 portPairStore.remove(portPairId);
139 if (portPairStore.containsKey(portPairId)) {
140 log.debug("The portPair is removed failed whose identifier was {}",
141 portPairId.toString());