Himal Kumar | b43724d | 2016-04-29 14:15:57 +1000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Himal Kumar | b43724d | 2016-04-29 14:15:57 +1000 | [diff] [blame] | 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.castor; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Himal Kumar | b43724d | 2016-04-29 14:15:57 +1000 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.util.KryoNamespace; |
| 22 | import org.onosproject.net.intent.Key; |
| 23 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 24 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 25 | import org.onosproject.store.serializers.KryoNamespaces; |
Himal Kumar | b43724d | 2016-04-29 14:15:57 +1000 | [diff] [blame] | 26 | import org.onosproject.store.service.ConsistentMap; |
| 27 | import org.onosproject.store.service.DistributedSet; |
| 28 | import org.onosproject.store.service.Serializer; |
| 29 | import org.onosproject.store.service.StorageService; |
| 30 | import org.onosproject.store.service.Versioned; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | import org.osgi.service.component.annotations.Activate; |
| 32 | import org.osgi.service.component.annotations.Component; |
| 33 | import org.osgi.service.component.annotations.Deactivate; |
| 34 | import org.osgi.service.component.annotations.Reference; |
| 35 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Himal Kumar | b43724d | 2016-04-29 14:15:57 +1000 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import java.util.HashMap; |
| 40 | import java.util.Map; |
| 41 | import java.util.Set; |
| 42 | |
| 43 | /** |
| 44 | * Distributed Store for Castor. |
| 45 | */ |
| 46 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 47 | @Component(immediate = true, service = CastorStore.class) |
Himal Kumar | b43724d | 2016-04-29 14:15:57 +1000 | [diff] [blame] | 48 | public class DistributedCastorStore implements CastorStore { |
| 49 | |
| 50 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 51 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Himal Kumar | b43724d | 2016-04-29 14:15:57 +1000 | [diff] [blame] | 53 | protected StorageService storageService; |
| 54 | |
| 55 | private ConsistentMap<IpAddress, MacAddress> addressMap; |
| 56 | private ConsistentMap<Key, PointToPointIntent> peerIntents; |
| 57 | private ConsistentMap<String, MultiPointToSinglePointIntent> layer2Intents; |
| 58 | private DistributedSet<Peer> allPeers; |
| 59 | private DistributedSet<Peer> customers; |
| 60 | private DistributedSet<Peer> routeServers; |
| 61 | |
| 62 | |
| 63 | @Activate |
| 64 | protected void activate() { |
| 65 | |
| 66 | addressMap = storageService.<IpAddress, MacAddress>consistentMapBuilder() |
| 67 | .withName("castor-addressMap") |
| 68 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 69 | .build(); |
| 70 | |
| 71 | peerIntents = storageService.<Key, PointToPointIntent>consistentMapBuilder() |
| 72 | .withName("castor-peerIntents") |
| 73 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 74 | .build(); |
| 75 | |
| 76 | layer2Intents = storageService.<String, MultiPointToSinglePointIntent>consistentMapBuilder() |
| 77 | .withName("castor-layer2Intents") |
| 78 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 79 | .build(); |
| 80 | |
| 81 | allPeers = storageService.<Peer>setBuilder() |
| 82 | .withName("castor-allPeers") |
| 83 | .withSerializer(Serializer.using( |
| 84 | new KryoNamespace.Builder() |
| 85 | .register(KryoNamespaces.API) |
| 86 | .register(Peer.class) |
| 87 | .build())) |
| 88 | .build() |
| 89 | .asDistributedSet(); |
| 90 | |
| 91 | customers = storageService.<Peer>setBuilder() |
| 92 | .withName("castor-customers") |
| 93 | .withSerializer(Serializer.using( |
| 94 | new KryoNamespace.Builder() |
| 95 | .register(KryoNamespaces.API) |
| 96 | .register(Peer.class) |
| 97 | .build())) |
| 98 | .build() |
| 99 | .asDistributedSet(); |
| 100 | |
| 101 | routeServers = storageService.<Peer>setBuilder() |
| 102 | .withName("castor-routeServers") |
| 103 | .withSerializer(Serializer.using( |
| 104 | new KryoNamespace.Builder() |
| 105 | .register(KryoNamespaces.API) |
| 106 | .register(Peer.class) |
| 107 | .build())) |
| 108 | .build() |
| 109 | .asDistributedSet(); |
| 110 | |
| 111 | log.info("Started"); |
| 112 | |
| 113 | } |
| 114 | |
| 115 | @Deactivate |
| 116 | protected void deactivate() { |
| 117 | |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Set<Peer> getAllPeers() { |
| 122 | return ImmutableSet.copyOf(allPeers); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void storePeer(Peer peer) { |
| 127 | allPeers.add(peer); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public Set<Peer> getServers() { |
| 132 | return ImmutableSet.copyOf(routeServers); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void storeServer(Peer server) { |
| 137 | routeServers.add(server); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public Set<Peer> getCustomers() { |
| 142 | return ImmutableSet.copyOf(customers); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void storeCustomer(Peer customer) { |
| 147 | customers.add(customer); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Map<IpAddress, MacAddress> getAddressMap() { |
| 152 | Map<IpAddress, MacAddress> validMapping = new HashMap<>(); |
| 153 | for (Map.Entry<IpAddress, Versioned<MacAddress>> entry: addressMap.entrySet()) { |
| 154 | validMapping.put(entry.getKey(), entry.getValue().value()); |
| 155 | } |
| 156 | return validMapping; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void setAddressMap(IpAddress ip, MacAddress mac) { |
| 161 | addressMap.put(ip, mac); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Map<Key, PointToPointIntent> getPeerIntents() { |
| 166 | Map<Key, PointToPointIntent> validMapping = new HashMap<>(); |
| 167 | for (Map.Entry<Key, Versioned<PointToPointIntent>> entry: peerIntents.entrySet()) { |
| 168 | validMapping.put(entry.getKey(), entry.getValue().value()); |
| 169 | } |
| 170 | return validMapping; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void storePeerIntent(Key key, PointToPointIntent intent) { |
| 175 | peerIntents.put(key, intent); |
| 176 | |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public Map<String, MultiPointToSinglePointIntent> getLayer2Intents() { |
| 181 | Map<String, MultiPointToSinglePointIntent> validMapping = new HashMap<>(); |
| 182 | for (Map.Entry<String, Versioned<MultiPointToSinglePointIntent>> entry: layer2Intents.entrySet()) { |
| 183 | validMapping.put(entry.getKey(), entry.getValue().value()); |
| 184 | } |
| 185 | return validMapping; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void storeLayer2Intent(String key, MultiPointToSinglePointIntent intent) { |
| 190 | layer2Intents.put(key, intent); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public Map<String, String> getCustomersMap() { |
| 195 | Map<String, String> peerMap = new HashMap<>(); |
| 196 | for (Peer cust : customers) { |
| 197 | peerMap.put(cust.getName(), cust.getIpAddress()); |
| 198 | } |
| 199 | return peerMap; |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void removeCustomer(Peer peer) { |
| 204 | customers.remove(peer); |
| 205 | allPeers.remove(peer); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void removePeerIntent(Key key) { |
| 210 | peerIntents.remove(key); |
| 211 | |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public void removeLayer2Intent(String key) { |
| 216 | layer2Intents.remove(key); |
| 217 | |
| 218 | } |
| 219 | } |
| 220 | |