chengfan | 8ea6a56 | 2016-11-26 16:37:02 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016 Open Networking Foundation |
chengfan | 8ea6a56 | 2016-11-26 16:37:02 +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.tenbi.tunnel; |
| 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.osgi.service.component.annotations.Activate; |
| 20 | import org.osgi.service.component.annotations.Component; |
| 21 | import org.osgi.service.component.annotations.Deactivate; |
| 22 | import org.osgi.service.component.annotations.Reference; |
| 23 | import org.osgi.service.component.annotations.ReferenceCardinality; |
chengfan | 8ea6a56 | 2016-11-26 16:37:02 +0800 | [diff] [blame] | 24 | import org.onosproject.event.AbstractListenerManager; |
| 25 | import org.onosproject.tetopology.management.api.TeTopology; |
| 26 | import org.onosproject.tetopology.management.api.TeTopologyKey; |
| 27 | import org.onosproject.tetopology.management.api.TeTopologyService; |
| 28 | import org.onosproject.tetunnel.api.TeTunnelAdminService; |
| 29 | import org.onosproject.tetunnel.api.TeTunnelService; |
| 30 | import org.onosproject.tetunnel.api.tunnel.DefaultTeTunnel; |
| 31 | import org.onosproject.tetunnel.api.tunnel.TeTunnel; |
| 32 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.IetfTe; |
| 33 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.IetfTeOpParam; |
| 34 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.IetfTeService; |
| 35 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.IetfTeEvent; |
| 36 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.IetfTeEventListener; |
| 37 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.tunnelsgrouping.tunnels.Tunnel; |
| 38 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.IetfTeTypes; |
| 39 | import org.onosproject.yms.ymsm.YmsService; |
| 40 | import org.slf4j.Logger; |
| 41 | |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.Collection; |
| 44 | import java.util.List; |
| 45 | import java.util.Optional; |
| 46 | |
| 47 | import static com.google.common.base.Preconditions.checkNotNull; |
| 48 | import static org.onosproject.tetopology.management.api.TeTopology.BIT_MERGED; |
| 49 | import static org.onosproject.teyang.utils.tunnel.TunnelConverter.buildIetfTeWithTunnels; |
| 50 | import static org.onosproject.teyang.utils.tunnel.TunnelConverter.te2YangTunnelConverter; |
| 51 | import static org.onosproject.teyang.utils.tunnel.TunnelConverter.yang2TeTunnel; |
| 52 | import static org.slf4j.LoggerFactory.getLogger; |
| 53 | |
| 54 | /** |
| 55 | * The IETF TE Tunnel NBI Manager implementation. |
| 56 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 57 | @Component(immediate = true, service = IetfTeService.class) |
chengfan | 8ea6a56 | 2016-11-26 16:37:02 +0800 | [diff] [blame] | 58 | public class TeTunnelNbiManager |
| 59 | extends AbstractListenerManager<IetfTeEvent, IetfTeEventListener> |
| 60 | implements IetfTeService { |
| 61 | private final Logger log = getLogger(getClass()); |
| 62 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
chengfan | 8ea6a56 | 2016-11-26 16:37:02 +0800 | [diff] [blame] | 64 | protected YmsService ymsService; |
| 65 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
chengfan | 8ea6a56 | 2016-11-26 16:37:02 +0800 | [diff] [blame] | 67 | protected TeTunnelService tunnelService; |
| 68 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
chengfan | 8ea6a56 | 2016-11-26 16:37:02 +0800 | [diff] [blame] | 70 | protected TeTopologyService toplogyService; |
| 71 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
chengfan | 8ea6a56 | 2016-11-26 16:37:02 +0800 | [diff] [blame] | 73 | protected TeTunnelAdminService tunnelAdminService; |
| 74 | |
| 75 | @Activate |
| 76 | protected void activate() { |
| 77 | ymsService.registerService(this, IetfTeService.class, null); |
chengfan | 7c7ff58 | 2016-12-01 19:12:13 +0800 | [diff] [blame] | 78 | ymsService.registerService(null, IetfTeTypes.class, null); |
chengfan | 8ea6a56 | 2016-11-26 16:37:02 +0800 | [diff] [blame] | 79 | log.info("started"); |
| 80 | } |
| 81 | |
| 82 | @Deactivate |
| 83 | protected void deactivate() { |
| 84 | ymsService.unRegisterService(this, IetfTeService.class); |
chengfan | 7c7ff58 | 2016-12-01 19:12:13 +0800 | [diff] [blame] | 85 | ymsService.unRegisterService(null, IetfTeTypes.class); |
chengfan | 8ea6a56 | 2016-11-26 16:37:02 +0800 | [diff] [blame] | 86 | log.info("stopped"); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public IetfTe getIetfTe(IetfTeOpParam ietfTe) { |
| 91 | List<Tunnel> tunnels = new ArrayList<>(); |
| 92 | Collection<TeTunnel> teTunnels = tunnelService.getTeTunnels(); |
| 93 | teTunnels.forEach(teTunnel -> { |
chaodeyu | 2a0e59d | 2017-02-11 21:29:34 +0800 | [diff] [blame] | 94 | Tunnel tunnel = te2YangTunnelConverter(teTunnel, false); |
chengfan | 8ea6a56 | 2016-11-26 16:37:02 +0800 | [diff] [blame] | 95 | tunnels.add(tunnel); |
| 96 | }); |
| 97 | IetfTe newIetfTe = buildIetfTeWithTunnels(tunnels); |
| 98 | return ietfTe.processSubtreeFiltering(newIetfTe, false); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void setIetfTe(IetfTeOpParam ietfTe) { |
| 103 | checkNotNull(ietfTe, "Ietf te params should not be null"); |
| 104 | //FIXME use topology id configured by user |
| 105 | // for there is no topology id param in the definition of te tunnel |
| 106 | // we use the merged topology id as the default topology where we create |
| 107 | // the tunnel, need to talk with the ietf-te draft writer. |
| 108 | TeTopologyKey topologyKey = getTopologyKey(); |
| 109 | if (topologyKey == null) { |
| 110 | log.error("No usable topology now!"); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | ietfTe.te().tunnels().tunnel().forEach(tunnel -> { |
| 115 | DefaultTeTunnel teTunnel = yang2TeTunnel(tunnel, topologyKey); |
| 116 | tunnelAdminService.createTeTunnel(teTunnel); |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void globalsRpc() { |
| 122 | |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void interfacesRpc() { |
| 127 | |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void tunnelsRpc() { |
| 132 | //TODO add implement for the te tunnel rpc |
| 133 | } |
| 134 | |
| 135 | private TeTopologyKey getTopologyKey() { |
| 136 | TeTopologyKey key = null; |
| 137 | Optional<TeTopology> teTopology = toplogyService |
| 138 | .teTopologies() |
| 139 | .teTopologies() |
| 140 | .values() |
| 141 | .stream() |
| 142 | .filter(topology -> topology.flags().get(BIT_MERGED)) |
| 143 | .findFirst(); |
| 144 | if (teTopology.isPresent()) { |
| 145 | TeTopology topology = teTopology.get(); |
| 146 | key = topology.teTopologyId(); |
| 147 | } |
| 148 | return key; |
| 149 | } |
| 150 | } |