Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.vtnrsc.portpair.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.util.Collections; |
Mahesh Poojary Huawei | 3fe5eff | 2015-11-29 15:13:18 +0530 | [diff] [blame] | 22 | import java.util.Set; |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 23 | |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.apache.felix.scr.annotations.Service; |
| 30 | import org.onlab.util.KryoNamespace; |
| 31 | import org.onosproject.store.serializers.KryoNamespaces; |
| 32 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 33 | import org.onosproject.store.service.MultiValuedTimestamp; |
| 34 | import org.onosproject.store.service.StorageService; |
| 35 | import org.onosproject.store.service.WallClockTimestamp; |
| 36 | import org.onosproject.vtnrsc.PortPair; |
| 37 | import org.onosproject.vtnrsc.PortPairId; |
Mahesh Poojary Huawei | 3fe5eff | 2015-11-29 15:13:18 +0530 | [diff] [blame] | 38 | import org.onosproject.vtnrsc.portpair.PortPairListener; |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 39 | import org.onosproject.vtnrsc.portpair.PortPairService; |
| 40 | import org.slf4j.Logger; |
| 41 | |
Mahesh Poojary Huawei | 3fe5eff | 2015-11-29 15:13:18 +0530 | [diff] [blame] | 42 | import com.google.common.collect.Sets; |
| 43 | |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 44 | /** |
| 45 | * Provides implementation of the portPairService. |
| 46 | */ |
| 47 | @Component(immediate = true) |
| 48 | @Service |
| 49 | public class PortPairManager implements PortPairService { |
| 50 | |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 51 | private static final String PORT_PAIR_ID_NULL = "PortPair ID cannot be null"; |
| 52 | private static final String PORT_PAIR_NULL = "PortPair cannot be null"; |
Mahesh Poojary Huawei | 3fe5eff | 2015-11-29 15:13:18 +0530 | [diff] [blame] | 53 | private static final String LISTENER_NOT_NULL = "Listener cannot be null"; |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 54 | |
Mahesh Poojary Huawei | 3fe5eff | 2015-11-29 15:13:18 +0530 | [diff] [blame] | 55 | private final Logger log = getLogger(getClass()); |
| 56 | private final Set<PortPairListener> listeners = Sets.newCopyOnWriteArraySet(); |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 57 | private EventuallyConsistentMap<PortPairId, PortPair> portPairStore; |
| 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(PortPair.class); |
| 69 | |
| 70 | portPairStore = storageService |
| 71 | .<PortPairId, PortPair>eventuallyConsistentMapBuilder() |
| 72 | .withName("portpairstore").withSerializer(serializer) |
| 73 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); |
| 74 | |
| 75 | log.info("Started"); |
| 76 | } |
| 77 | |
| 78 | @Deactivate |
| 79 | public void deactivate() { |
| 80 | portPairStore.destroy(); |
Mahesh Poojary Huawei | 3fe5eff | 2015-11-29 15:13:18 +0530 | [diff] [blame] | 81 | listeners.clear(); |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 82 | log.info("Stopped"); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean exists(PortPairId portPairId) { |
| 87 | checkNotNull(portPairId, PORT_PAIR_ID_NULL); |
| 88 | return portPairStore.containsKey(portPairId); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public int getPortPairCount() { |
| 93 | return portPairStore.size(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Iterable<PortPair> getPortPairs() { |
| 98 | return Collections.unmodifiableCollection(portPairStore.values()); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public PortPair getPortPair(PortPairId portPairId) { |
| 103 | checkNotNull(portPairId, PORT_PAIR_ID_NULL); |
| 104 | return portPairStore.get(portPairId); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean createPortPair(PortPair portPair) { |
| 109 | checkNotNull(portPair, PORT_PAIR_NULL); |
| 110 | |
| 111 | portPairStore.put(portPair.portPairId(), portPair); |
| 112 | if (!portPairStore.containsKey(portPair.portPairId())) { |
| 113 | log.debug("The portPair is created failed which identifier was {}", portPair.portPairId() |
| 114 | .toString()); |
| 115 | return false; |
| 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean updatePortPair(PortPair portPair) { |
| 122 | checkNotNull(portPair, PORT_PAIR_NULL); |
| 123 | |
| 124 | if (!portPairStore.containsKey(portPair.portPairId())) { |
| 125 | log.debug("The portPair is not exist whose identifier was {} ", |
| 126 | portPair.portPairId().toString()); |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | portPairStore.put(portPair.portPairId(), portPair); |
| 131 | |
| 132 | if (!portPair.equals(portPairStore.get(portPair.portPairId()))) { |
| 133 | log.debug("The portPair is updated failed whose identifier was {} ", |
| 134 | portPair.portPairId().toString()); |
| 135 | return false; |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public boolean removePortPair(PortPairId portPairId) { |
| 142 | checkNotNull(portPairId, PORT_PAIR_NULL); |
| 143 | |
| 144 | portPairStore.remove(portPairId); |
| 145 | if (portPairStore.containsKey(portPairId)) { |
| 146 | log.debug("The portPair is removed failed whose identifier was {}", |
| 147 | portPairId.toString()); |
| 148 | return false; |
| 149 | } |
| 150 | return true; |
| 151 | } |
Mahesh Poojary Huawei | 3fe5eff | 2015-11-29 15:13:18 +0530 | [diff] [blame] | 152 | |
| 153 | @Override |
| 154 | public void addListener(PortPairListener listener) { |
| 155 | checkNotNull(listener, LISTENER_NOT_NULL); |
| 156 | listeners.add(listener); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void removeListener(PortPairListener listener) { |
| 161 | checkNotNull(listener, LISTENER_NOT_NULL); |
| 162 | listeners.remove(listener); |
| 163 | } |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 164 | } |