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