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