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