tony-liu | f7d2a26 | 2016-10-17 16:32:24 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
tony-liu | f7d2a26 | 2016-10-17 16:32:24 +0800 | [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 | |
| 17 | package org.onosproject.actn.mdsc.pce.impl; |
| 18 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
tony-liu | f7d2a26 | 2016-10-17 16:32:24 +0800 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.osgi.service.component.annotations.Activate; |
| 22 | import org.osgi.service.component.annotations.Component; |
| 23 | import org.osgi.service.component.annotations.Deactivate; |
tony-liu | f7d2a26 | 2016-10-17 16:32:24 +0800 | [diff] [blame] | 24 | import org.onosproject.actn.mdsc.pce.TeTunnelPce; |
| 25 | import org.onosproject.actn.mdsc.pce.TeTunnelPceService; |
| 26 | import org.onosproject.tetunnel.api.tunnel.TeTunnel; |
| 27 | import org.onosproject.tetunnel.api.tunnel.path.TeRouteSubobject; |
| 28 | import org.slf4j.Logger; |
| 29 | import static org.slf4j.LoggerFactory.getLogger; |
| 30 | |
| 31 | import java.util.Collection; |
| 32 | import java.util.List; |
| 33 | |
| 34 | /** |
| 35 | * Implementation of Te Tunnel PCE service. |
| 36 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | @Component(immediate = true, service = TeTunnelPceService.class) |
tony-liu | f7d2a26 | 2016-10-17 16:32:24 +0800 | [diff] [blame] | 38 | public class TeTunnelPceManager implements TeTunnelPceService { |
| 39 | |
| 40 | private static final Logger log = getLogger(TeTunnelPceManager.class); |
| 41 | |
| 42 | private List<TeTunnelPce> pces = Lists.newLinkedList(); |
| 43 | |
| 44 | @Activate |
| 45 | protected void activate() { |
| 46 | pces.add(0, new DefaultTeTunnelPce()); |
| 47 | log.info("Started"); |
| 48 | } |
| 49 | |
| 50 | @Deactivate |
| 51 | protected void deactivate() { |
| 52 | log.info("Stopped"); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public Collection<List<TeRouteSubobject>> computePaths(TeTunnel teTunnel) { |
| 57 | TeTunnelPce pce = null; |
| 58 | synchronized (pces) { |
| 59 | for (TeTunnelPce p : pces) { |
| 60 | if (p.isSuitable(teTunnel)) { |
| 61 | pce = p; |
| 62 | } |
| 63 | } |
| 64 | } |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 65 | if (pce != null) { |
| 66 | return pce.computePaths(teTunnel); |
| 67 | } else { |
| 68 | return ImmutableList.of(); |
| 69 | } |
tony-liu | f7d2a26 | 2016-10-17 16:32:24 +0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public Collection<List<TeRouteSubobject>> computePaths(TeTunnel teTunnel, |
| 74 | TeTunnelPce pce) { |
| 75 | return pce == null ? null : pce.computePaths(teTunnel); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void registerPce(TeTunnelPce pce) { |
| 80 | synchronized (pces) { |
| 81 | int index = 0; |
| 82 | while (pces.get(index).getPriority() > pce.getPriority()) { |
| 83 | index++; |
| 84 | } |
| 85 | |
| 86 | pces.add(index, pce); |
| 87 | } |
| 88 | } |
| 89 | } |