blob: 9b2941f2c511f04fa1d2f385bb4c1a9ae42572bd [file] [log] [blame]
Mahesh Poojary Sba827292016-05-09 11:31:12 +05301/*
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 */
16package org.onosproject.pce.pcestore;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053020import com.google.common.collect.ImmutableSet;
21
Mahesh Poojary Sba827292016-05-09 11:31:12 +053022import java.util.Map;
23import java.util.stream.Collectors;
24
25import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.apache.felix.scr.annotations.Service;
31
32import org.onlab.util.KryoNamespace;
33import org.onosproject.incubator.net.tunnel.TunnelId;
Mahesh Poojary S33536202016-05-30 07:22:36 +053034import org.onosproject.net.intent.constraint.BandwidthConstraint;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053035import org.onosproject.net.resource.ResourceConsumer;
Priyanka Bcdf9b102016-06-07 20:01:38 +053036import org.onosproject.pce.pceservice.constraint.CapabilityConstraint;
Mahesh Poojary S33536202016-05-30 07:22:36 +053037import org.onosproject.pce.pceservice.constraint.CostConstraint;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053038import org.onosproject.pce.pceservice.TunnelConsumerId;
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053039import org.onosproject.pce.pceservice.LspType;
Priyanka B4c3b4512016-07-22 11:41:49 +053040import org.onosproject.pce.pceservice.constraint.SharedBandwidthConstraint;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053041import org.onosproject.pce.pcestore.api.PceStore;
42import org.onosproject.store.serializers.KryoNamespaces;
43import org.onosproject.store.service.ConsistentMap;
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053044import org.onosproject.store.service.DistributedSet;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053045import org.onosproject.store.service.Serializer;
46import org.onosproject.store.service.StorageService;
47
48import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
51/**
52 * Manages the pool of available labels to devices, links and tunnels.
53 */
54@Component(immediate = true)
55@Service
56public class DistributedPceStore implements PceStore {
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053057 private static final String PATH_INFO_NULL = "Path Info cannot be null";
Mahesh Poojary Sba827292016-05-09 11:31:12 +053058 private static final String PCECC_TUNNEL_INFO_NULL = "PCECC Tunnel Info cannot be null";
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053059 private static final String TUNNEL_ID_NULL = "Tunnel Id cannot be null";
Mahesh Poojary Sba827292016-05-09 11:31:12 +053060
61 private final Logger log = LoggerFactory.getLogger(getClass());
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected StorageService storageService;
65
Mahesh Poojary Sba827292016-05-09 11:31:12 +053066 // Mapping tunnel with device local info with tunnel consumer id
Avantika-Huawei9e848e82016-09-01 12:12:42 +053067 private ConsistentMap<TunnelId, ResourceConsumer> tunnelInfoMap;
Mahesh Poojary Sba827292016-05-09 11:31:12 +053068
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053069 // List of Failed path info
70 private DistributedSet<PcePathInfo> failedPathSet;
71
Priyanka B3fdb9dd2016-08-08 10:47:24 +053072 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-Huawei28b53752016-06-23 17:04:49 +053083
Mahesh Poojary Sba827292016-05-09 11:31:12 +053084 @Activate
85 protected void activate() {
Avantika-Huawei9e848e82016-09-01 12:12:42 +053086 tunnelInfoMap = storageService.<TunnelId, ResourceConsumer>consistentMapBuilder()
Mahesh Poojary Sba827292016-05-09 11:31:12 +053087 .withName("onos-pce-tunnelinfomap")
88 .withSerializer(Serializer.using(
89 new KryoNamespace.Builder()
90 .register(KryoNamespaces.API)
91 .register(TunnelId.class,
Avantika-Huawei9e848e82016-09-01 12:12:42 +053092 TunnelConsumerId.class)
Mahesh Poojary Sba827292016-05-09 11:31:12 +053093 .build()))
94 .build();
95
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053096 failedPathSet = storageService.<PcePathInfo>setBuilder()
97 .withName("failed-path-info")
Priyanka B3fdb9dd2016-08-08 10:47:24 +053098 .withSerializer(SERIALIZER)
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +053099 .build()
100 .asDistributedSet();
101
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530102 log.info("Started");
103 }
104
105 @Deactivate
106 protected void deactivate() {
107 log.info("Stopped");
108 }
109
110 @Override
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530111 public boolean existsTunnelInfo(TunnelId tunnelId) {
112 checkNotNull(tunnelId, TUNNEL_ID_NULL);
113 return tunnelInfoMap.containsKey(tunnelId);
114 }
115
116 @Override
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530117 public boolean existsFailedPathInfo(PcePathInfo failedPathInfo) {
118 checkNotNull(failedPathInfo, PATH_INFO_NULL);
119 return failedPathSet.contains(failedPathInfo);
120 }
121
122 @Override
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530123 public int getTunnelInfoCount() {
124 return tunnelInfoMap.size();
125 }
126
127 @Override
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530128 public int getFailedPathInfoCount() {
129 return failedPathSet.size();
130 }
131
132 @Override
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530133 public Map<TunnelId, ResourceConsumer> getTunnelInfos() {
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530134 return tunnelInfoMap.entrySet().stream()
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530135 .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().value()));
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530136 }
137
138 @Override
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530139 public Iterable<PcePathInfo> getFailedPathInfos() {
140 return ImmutableSet.copyOf(failedPathSet);
141 }
142
143 @Override
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530144 public ResourceConsumer getTunnelInfo(TunnelId tunnelId) {
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530145 checkNotNull(tunnelId, TUNNEL_ID_NULL);
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530146 return tunnelInfoMap.get(tunnelId) == null ? null : tunnelInfoMap.get(tunnelId).value();
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530147 }
148
149 @Override
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530150 public void addTunnelInfo(TunnelId tunnelId, ResourceConsumer tunnelConsumerId) {
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530151 checkNotNull(tunnelId, TUNNEL_ID_NULL);
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530152 checkNotNull(tunnelConsumerId, PCECC_TUNNEL_INFO_NULL);
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530153
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530154 tunnelInfoMap.put(tunnelId, tunnelConsumerId);
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530155 }
156
157 @Override
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +0530158 public void addFailedPathInfo(PcePathInfo failedPathInfo) {
159 checkNotNull(failedPathInfo, PATH_INFO_NULL);
160 failedPathSet.add(failedPathInfo);
161 }
162
163 @Override
Mahesh Poojary Sba827292016-05-09 11:31:12 +0530164 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 Sf920ec02016-05-17 23:16:09 +0530173
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 Sba827292016-05-09 11:31:12 +0530184}