Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.pce.pcestore; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
| 21 | |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 22 | import java.util.List; |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 23 | import java.util.Map; |
| 24 | import java.util.stream.Collectors; |
| 25 | |
| 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.apache.felix.scr.annotations.Service; |
| 32 | |
| 33 | import org.onlab.util.KryoNamespace; |
| 34 | import org.onosproject.incubator.net.tunnel.TunnelId; |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 35 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 36 | import org.onosproject.net.resource.ResourceConsumer; |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 37 | import org.onosproject.pce.pceservice.ExplicitPathInfo; |
Priyanka B | cdf9b10 | 2016-06-07 20:01:38 +0530 | [diff] [blame] | 38 | import org.onosproject.pce.pceservice.constraint.CapabilityConstraint; |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 39 | import org.onosproject.pce.pceservice.constraint.CostConstraint; |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 40 | import org.onosproject.pce.pceservice.TunnelConsumerId; |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 41 | import org.onosproject.pce.pceservice.LspType; |
Priyanka B | 4c3b451 | 2016-07-22 11:41:49 +0530 | [diff] [blame] | 42 | import org.onosproject.pce.pceservice.constraint.SharedBandwidthConstraint; |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 43 | import org.onosproject.pce.pcestore.api.PceStore; |
| 44 | import org.onosproject.store.serializers.KryoNamespaces; |
| 45 | import org.onosproject.store.service.ConsistentMap; |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 46 | import org.onosproject.store.service.DistributedSet; |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 47 | import org.onosproject.store.service.Serializer; |
| 48 | import org.onosproject.store.service.StorageService; |
| 49 | |
| 50 | import org.slf4j.Logger; |
| 51 | import org.slf4j.LoggerFactory; |
| 52 | |
| 53 | /** |
| 54 | * Manages the pool of available labels to devices, links and tunnels. |
| 55 | */ |
| 56 | @Component(immediate = true) |
| 57 | @Service |
| 58 | public class DistributedPceStore implements PceStore { |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 59 | private static final String PATH_INFO_NULL = "Path Info cannot be null"; |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 60 | private static final String PCECC_TUNNEL_INFO_NULL = "PCECC Tunnel Info cannot be null"; |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 61 | private static final String TUNNEL_ID_NULL = "Tunnel Id cannot be null"; |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 62 | |
| 63 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected StorageService storageService; |
| 67 | |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 68 | // Mapping tunnel with device local info with tunnel consumer id |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 69 | private ConsistentMap<TunnelId, ResourceConsumer> tunnelInfoMap; |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 70 | |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 71 | // List of Failed path info |
| 72 | private DistributedSet<PcePathInfo> failedPathSet; |
| 73 | |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 74 | // Maintains tunnel name mapped to explicit path info |
| 75 | private ConsistentMap<String, List<ExplicitPathInfo>> tunnelNameExplicitPathInfoMap; |
| 76 | |
Priyanka B | 3fdb9dd | 2016-08-08 10:47:24 +0530 | [diff] [blame] | 77 | private static final Serializer SERIALIZER = Serializer |
| 78 | .using(new KryoNamespace.Builder().register(KryoNamespaces.API) |
| 79 | .register(PcePathInfo.class) |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 80 | .register(ExplicitPathInfo.class) |
| 81 | .register(ExplicitPathInfo.Type.class) |
Priyanka B | 3fdb9dd | 2016-08-08 10:47:24 +0530 | [diff] [blame] | 82 | .register(CostConstraint.class) |
| 83 | .register(CostConstraint.Type.class) |
| 84 | .register(BandwidthConstraint.class) |
| 85 | .register(SharedBandwidthConstraint.class) |
| 86 | .register(CapabilityConstraint.class) |
| 87 | .register(CapabilityConstraint.CapabilityType.class) |
| 88 | .register(LspType.class) |
| 89 | .build()); |
Avantika-Huawei | 28b5375 | 2016-06-23 17:04:49 +0530 | [diff] [blame] | 90 | |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 91 | @Activate |
| 92 | protected void activate() { |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 93 | tunnelInfoMap = storageService.<TunnelId, ResourceConsumer>consistentMapBuilder() |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 94 | .withName("onos-pce-tunnelinfomap") |
| 95 | .withSerializer(Serializer.using( |
| 96 | new KryoNamespace.Builder() |
| 97 | .register(KryoNamespaces.API) |
| 98 | .register(TunnelId.class, |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 99 | TunnelConsumerId.class) |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 100 | .build())) |
| 101 | .build(); |
| 102 | |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 103 | failedPathSet = storageService.<PcePathInfo>setBuilder() |
| 104 | .withName("failed-path-info") |
Priyanka B | 3fdb9dd | 2016-08-08 10:47:24 +0530 | [diff] [blame] | 105 | .withSerializer(SERIALIZER) |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 106 | .build() |
| 107 | .asDistributedSet(); |
| 108 | |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 109 | tunnelNameExplicitPathInfoMap = storageService.<String, List<ExplicitPathInfo>>consistentMapBuilder() |
| 110 | .withName("onos-pce-explicitpathinfo") |
| 111 | .withSerializer(Serializer.using( |
| 112 | new KryoNamespace.Builder() |
| 113 | .register(KryoNamespaces.API) |
| 114 | .register(ExplicitPathInfo.class) |
| 115 | .register(ExplicitPathInfo.Type.class) |
| 116 | .build())) |
| 117 | .build(); |
| 118 | |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 119 | log.info("Started"); |
| 120 | } |
| 121 | |
| 122 | @Deactivate |
| 123 | protected void deactivate() { |
| 124 | log.info("Stopped"); |
| 125 | } |
| 126 | |
| 127 | @Override |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 128 | public boolean existsTunnelInfo(TunnelId tunnelId) { |
| 129 | checkNotNull(tunnelId, TUNNEL_ID_NULL); |
| 130 | return tunnelInfoMap.containsKey(tunnelId); |
| 131 | } |
| 132 | |
| 133 | @Override |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 134 | public boolean existsFailedPathInfo(PcePathInfo failedPathInfo) { |
| 135 | checkNotNull(failedPathInfo, PATH_INFO_NULL); |
| 136 | return failedPathSet.contains(failedPathInfo); |
| 137 | } |
| 138 | |
| 139 | @Override |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 140 | public int getTunnelInfoCount() { |
| 141 | return tunnelInfoMap.size(); |
| 142 | } |
| 143 | |
| 144 | @Override |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 145 | public int getFailedPathInfoCount() { |
| 146 | return failedPathSet.size(); |
| 147 | } |
| 148 | |
| 149 | @Override |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 150 | public Map<TunnelId, ResourceConsumer> getTunnelInfos() { |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 151 | return tunnelInfoMap.entrySet().stream() |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 152 | .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().value())); |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 156 | public Iterable<PcePathInfo> getFailedPathInfos() { |
| 157 | return ImmutableSet.copyOf(failedPathSet); |
| 158 | } |
| 159 | |
| 160 | @Override |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 161 | public ResourceConsumer getTunnelInfo(TunnelId tunnelId) { |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 162 | checkNotNull(tunnelId, TUNNEL_ID_NULL); |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 163 | return tunnelInfoMap.get(tunnelId) == null ? null : tunnelInfoMap.get(tunnelId).value(); |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 167 | public void addTunnelInfo(TunnelId tunnelId, ResourceConsumer tunnelConsumerId) { |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 168 | checkNotNull(tunnelId, TUNNEL_ID_NULL); |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 169 | checkNotNull(tunnelConsumerId, PCECC_TUNNEL_INFO_NULL); |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 170 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 171 | tunnelInfoMap.put(tunnelId, tunnelConsumerId); |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 175 | public void addFailedPathInfo(PcePathInfo failedPathInfo) { |
| 176 | checkNotNull(failedPathInfo, PATH_INFO_NULL); |
| 177 | failedPathSet.add(failedPathInfo); |
| 178 | } |
| 179 | |
| 180 | @Override |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 181 | public boolean removeTunnelInfo(TunnelId tunnelId) { |
| 182 | checkNotNull(tunnelId, TUNNEL_ID_NULL); |
| 183 | |
| 184 | if (tunnelInfoMap.remove(tunnelId) == null) { |
| 185 | log.error("Tunnel info deletion for tunnel id {} has failed.", tunnelId.toString()); |
| 186 | return false; |
| 187 | } |
| 188 | return true; |
| 189 | } |
Mahesh Poojary S | f920ec0 | 2016-05-17 23:16:09 +0530 | [diff] [blame] | 190 | |
| 191 | @Override |
| 192 | public boolean removeFailedPathInfo(PcePathInfo failedPathInfo) { |
| 193 | checkNotNull(failedPathInfo, PATH_INFO_NULL); |
| 194 | |
| 195 | if (!failedPathSet.remove(failedPathInfo)) { |
| 196 | log.error("Failed path info {} deletion has failed.", failedPathInfo.toString()); |
| 197 | return false; |
| 198 | } |
| 199 | return true; |
| 200 | } |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 201 | |
| 202 | @Override |
| 203 | public boolean tunnelNameExplicitPathInfoMap(String tunnelName, List<ExplicitPathInfo> explicitPathInfo) { |
| 204 | checkNotNull(tunnelName); |
| 205 | checkNotNull(explicitPathInfo); |
| 206 | return tunnelNameExplicitPathInfoMap.put(tunnelName, explicitPathInfo) != null ? true : false; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public List<ExplicitPathInfo> getTunnelNameExplicitPathInfoMap(String tunnelName) { |
| 211 | checkNotNull(tunnelName); |
| 212 | if (tunnelNameExplicitPathInfoMap.get(tunnelName) != null) { |
| 213 | return tunnelNameExplicitPathInfoMap.get(tunnelName).value(); |
| 214 | } |
| 215 | return null; |
| 216 | } |
| 217 | |
Mahesh Poojary S | ba82729 | 2016-05-09 11:31:12 +0530 | [diff] [blame] | 218 | } |