blob: beaf8ae6d8fe18131dbcef6153402ca000de5d75 [file] [log] [blame]
Mahesh Poojary Sf920ec02016-05-17 23:16:09 +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.util;
17
18import com.google.common.collect.ImmutableSet;
19
20import java.util.concurrent.ConcurrentHashMap;
21import java.util.concurrent.ConcurrentMap;
22import java.util.HashSet;
23import java.util.List;
24import java.util.Map;
25import java.util.Set;
26import java.util.stream.Collectors;
27
28import org.onosproject.incubator.net.resource.label.LabelResourceId;
29import org.onosproject.incubator.net.tunnel.TunnelId;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.Link;
32import org.onosproject.net.resource.ResourceConsumer;
33import org.onosproject.pce.pcestore.PceccTunnelInfo;
34import org.onosproject.pce.pcestore.PcePathInfo;
35import org.onosproject.pce.pcestore.api.LspLocalLabelInfo;
36import org.onosproject.pce.pcestore.api.PceStore;
37
38/**
39 * Provides test implementation of PceStore.
40 */
41public class PceStoreAdapter implements PceStore {
42
43 // Mapping device with global node label
44 private ConcurrentMap<DeviceId, LabelResourceId> globalNodeLabelMap = new ConcurrentHashMap<>();
45
46 // Mapping link with adjacency label
47 private ConcurrentMap<Link, LabelResourceId> adjLabelMap = new ConcurrentHashMap<>();
48
49 // Mapping tunnel with device local info with tunnel consumer id
50 private ConcurrentMap<TunnelId, PceccTunnelInfo> tunnelInfoMap = new ConcurrentHashMap<>();
51
52 // Set of Path info
53 private Set<PcePathInfo> failedPathInfoSet = new HashSet<>();
54
55 @Override
56 public boolean existsGlobalNodeLabel(DeviceId id) {
57 return globalNodeLabelMap.containsKey(id);
58 }
59
60 @Override
61 public boolean existsAdjLabel(Link link) {
62 return adjLabelMap.containsKey(link);
63 }
64
65 @Override
66 public boolean existsTunnelInfo(TunnelId tunnelId) {
67 return tunnelInfoMap.containsKey(tunnelId);
68 }
69
70 @Override
71 public boolean existsFailedPathInfo(PcePathInfo pathInfo) {
72 return failedPathInfoSet.contains(pathInfo);
73 }
74
75 @Override
76 public int getGlobalNodeLabelCount() {
77 return globalNodeLabelMap.size();
78 }
79
80 @Override
81 public int getAdjLabelCount() {
82 return adjLabelMap.size();
83 }
84
85 @Override
86 public int getTunnelInfoCount() {
87 return tunnelInfoMap.size();
88 }
89
90 @Override
91 public int getFailedPathInfoCount() {
92 return failedPathInfoSet.size();
93 }
94
95 @Override
96 public Map<DeviceId, LabelResourceId> getGlobalNodeLabels() {
97 return globalNodeLabelMap.entrySet().stream()
98 .collect(Collectors.toMap(Map.Entry::getKey, e -> (LabelResourceId) e.getValue()));
99 }
100
101 @Override
102 public Map<Link, LabelResourceId> getAdjLabels() {
103 return adjLabelMap.entrySet().stream()
104 .collect(Collectors.toMap(Map.Entry::getKey, e -> (LabelResourceId) e.getValue()));
105 }
106
107 @Override
108 public Map<TunnelId, PceccTunnelInfo> getTunnelInfos() {
109 return tunnelInfoMap.entrySet().stream()
110 .collect(Collectors.toMap(Map.Entry::getKey, e -> (PceccTunnelInfo) e.getValue()));
111 }
112
113 @Override
114 public Iterable<PcePathInfo> getFailedPathInfos() {
115 return ImmutableSet.copyOf(failedPathInfoSet);
116 }
117
118 @Override
119 public LabelResourceId getGlobalNodeLabel(DeviceId id) {
120 return globalNodeLabelMap.get(id);
121 }
122
123 @Override
124 public LabelResourceId getAdjLabel(Link link) {
125 return adjLabelMap.get(link);
126 }
127
128 @Override
129 public PceccTunnelInfo getTunnelInfo(TunnelId tunnelId) {
130 return tunnelInfoMap.get(tunnelId);
131 }
132
133 @Override
134 public void addGlobalNodeLabel(DeviceId deviceId, LabelResourceId labelId) {
135 globalNodeLabelMap.put(deviceId, labelId);
136 }
137
138 @Override
139 public void addAdjLabel(Link link, LabelResourceId labelId) {
140 adjLabelMap.put(link, labelId);
141 }
142
143 @Override
144 public void addTunnelInfo(TunnelId tunnelId, PceccTunnelInfo pceccTunnelInfo) {
145 tunnelInfoMap.put(tunnelId, pceccTunnelInfo);
146 }
147
148 @Override
149 public void addFailedPathInfo(PcePathInfo pathInfo) {
150 failedPathInfoSet.add(pathInfo);
151 }
152
153 @Override
154 public boolean updateTunnelInfo(TunnelId tunnelId, List<LspLocalLabelInfo> lspLocalLabelInfoList) {
155 if (!tunnelInfoMap.containsKey((tunnelId))) {
156 return false;
157 }
158
159 PceccTunnelInfo labelStoreInfo = tunnelInfoMap.get(tunnelId);
160 labelStoreInfo.lspLocalLabelInfoList(lspLocalLabelInfoList);
161 tunnelInfoMap.put(tunnelId, labelStoreInfo);
162 return true;
163 }
164
165 @Override
166 public boolean updateTunnelInfo(TunnelId tunnelId, ResourceConsumer tunnelConsumerId) {
167 if (!tunnelInfoMap.containsKey((tunnelId))) {
168 return false;
169 }
170
171 PceccTunnelInfo tunnelInfo = tunnelInfoMap.get(tunnelId);
172 tunnelInfo.tunnelConsumerId(tunnelConsumerId);
173 tunnelInfoMap.put(tunnelId, tunnelInfo);
174 return true;
175 }
176
177 @Override
178 public boolean removeGlobalNodeLabel(DeviceId id) {
179 globalNodeLabelMap.remove(id);
180 if (globalNodeLabelMap.containsKey(id)) {
181 return false;
182 }
183 return true;
184 }
185
186 @Override
187 public boolean removeAdjLabel(Link link) {
188 adjLabelMap.remove(link);
189 if (adjLabelMap.containsKey(link)) {
190 return false;
191 }
192 return true;
193 }
194
195 @Override
196 public boolean removeTunnelInfo(TunnelId tunnelId) {
197 tunnelInfoMap.remove(tunnelId);
198 if (tunnelInfoMap.containsKey(tunnelId)) {
199 return false;
200 }
201 return true;
202 }
203
204 @Override
205 public boolean removeFailedPathInfo(PcePathInfo pathInfo) {
206 if (failedPathInfoSet.remove(pathInfo)) {
207 return false;
208 }
209 return true;
210 }
211}