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