2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.bgpcep.pcep.tunnel.provider;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.concurrent.ExecutionException;
20 import org.opendaylight.bgpcep.pcep.topology.spi.AbstractInstructionExecutor;
21 import org.opendaylight.bgpcep.programming.topology.TopologyProgrammingUtil;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.ReadTransaction;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.AdministrativeStatus;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Arguments2Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.PcepCreateP2pTunnelInput1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp.object.LspBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.bandwidth.object.BandwidthBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.classtype.object.ClassTypeBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.AddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.Ipv4CaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.Ipv6CaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.ipv4._case.Ipv4Builder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.address.family.ipv6._case.Ipv6Builder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.endpoints.object.EndpointsObjBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lspa.object.LspaBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.AddLspInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.AddLspInputBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.NetworkTopologyPcepService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.OperationResult;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.add.lsp.args.Arguments;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev200120.add.lsp.args.ArgumentsBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev181109.PcepCreateP2pTunnelInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.programming.rev130930.TpReference;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.igp.termination.point.attributes.TerminationPointType;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.igp.termination.point.attributes.termination.point.type.Ip;
58 import org.opendaylight.yangtools.yang.binding.DataObject;
59 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
60 import org.opendaylight.yangtools.yang.common.RpcResult;
62 final class CreateTunnelInstructionExecutor extends AbstractInstructionExecutor {
63 private final DataBroker dataProvider;
64 private final NetworkTopologyPcepService topologyService;
65 private final PcepCreateP2pTunnelInput p2pTunnelInput;
67 CreateTunnelInstructionExecutor(final PcepCreateP2pTunnelInput p2pTunnelInput, final DataBroker dataProvider,
68 final NetworkTopologyPcepService topologyService) {
69 super(p2pTunnelInput);
70 this.p2pTunnelInput = p2pTunnelInput;
71 this.dataProvider = dataProvider;
72 this.topologyService = topologyService;
75 private static void checkLinkIsnotExistent(final InstanceIdentifier<Topology> tii,
76 final AddLspInputBuilder addLspInput, final ReadTransaction rt) {
77 final InstanceIdentifier<Link> lii = NodeChangedListener.linkIdentifier(tii, addLspInput.getNode(),
78 addLspInput.getName());
80 Preconditions.checkState(!rt.read(LogicalDatastoreType.OPERATIONAL, lii).get().isPresent());
81 } catch (final InterruptedException | ExecutionException e) {
82 throw new IllegalStateException("Failed to ensure link existence.", e);
86 private static AddressFamily buildAddressFamily(final TerminationPoint sp, final TerminationPoint dp) {
87 // We need the IGP augmentation -- it has IP addresses
88 final TerminationPoint1 sp1 = requireNonNull(sp.augmentation(TerminationPoint1.class));
89 final TerminationPoint1 dp1 = requireNonNull(dp.augmentation(TerminationPoint1.class));
92 final TerminationPointType spt = sp1.getIgpTerminationPointAttributes().getTerminationPointType();
93 final TerminationPointType dpt = dp1.getIgpTerminationPointAttributes().getTerminationPointType();
95 // The types have to match
96 Preconditions.checkArgument(spt.implementedInterface().equals(dpt.implementedInterface()));
98 // And they have to actually be Ip
99 final Ip sips = (Ip) spt;
100 final Ip dips = (Ip) dpt;
103 * Now a bit of magic. We need to find 'like' addresses, e.g. both
104 * IPv4 or both IPv6. We are in IPv6-enabled world now, so let's
107 Optional<AddressFamily> ret = findIpv6(sips.getIpAddress(), dips.getIpAddress());
108 if (!ret.isPresent()) {
109 ret = findIpv4(sips.getIpAddress(), dips.getIpAddress());
112 // We need to have a ret now
113 Preconditions.checkArgument(ret != null, "Failed to find like Endpoint addresses");
118 private static Optional<AddressFamily> findIpv4(final List<IpAddress> srcs, final List<IpAddress> dsts) {
119 for (final IpAddress sc : srcs) {
120 if (sc.getIpv4Address() != null) {
121 for (final IpAddress dc : dsts) {
122 if (dc.getIpv4Address() != null) {
123 return Optional.of(new Ipv4CaseBuilder().setIpv4(new Ipv4Builder()
124 .setSourceIpv4Address(new Ipv4AddressNoZone(sc.getIpv4Address()))
125 .setDestinationIpv4Address(new Ipv4AddressNoZone(dc.getIpv4Address()))
132 return Optional.empty();
135 private static Optional<AddressFamily> findIpv6(final List<IpAddress> srcs, final List<IpAddress> dsts) {
136 for (final IpAddress sc : srcs) {
137 if (sc.getIpv6Address() != null) {
138 for (final IpAddress dc : dsts) {
139 if (dc.getIpv6Address() != null) {
140 return Optional.of(new Ipv6CaseBuilder().setIpv6(new Ipv6Builder()
141 .setSourceIpv6Address(new Ipv6AddressNoZone(sc.getIpv6Address()))
142 .setDestinationIpv6Address(new Ipv6AddressNoZone(dc.getIpv6Address()))
149 return Optional.empty();
153 protected ListenableFuture<OperationResult> invokeOperation() {
154 try (ReadTransaction transaction = this.dataProvider.newReadOnlyTransaction()) {
155 AddLspInput addLspInput = createAddLspInput(transaction);
157 return Futures.transform(
158 this.topologyService.addLsp(addLspInput),
159 RpcResult::getResult, MoreExecutors.directExecutor());
163 private AddLspInput createAddLspInput(final ReadTransaction transaction) {
164 final InstanceIdentifier<Topology> tii = TopologyProgrammingUtil.topologyForInput(this.p2pTunnelInput);
165 final TpReader dr = new TpReader(transaction, tii, this.p2pTunnelInput.getDestination());
166 final TerminationPoint dp = requireNonNull(dr.getTp());
168 final TpReader sr = new TpReader(transaction, tii, this.p2pTunnelInput.getSource());
169 final TerminationPoint sp = requireNonNull(sr.getTp());
171 final Node sn = requireNonNull(sr.getNode());
172 final AddLspInputBuilder ab = new AddLspInputBuilder();
173 ab.setNode(requireNonNull(TunelProgrammingUtil.supportingNode(sn)));
174 ab.setName(this.p2pTunnelInput.getSymbolicPathName());
176 checkLinkIsnotExistent(tii, ab, transaction);
178 ab.setArguments(buildArguments(sp, dp));
182 private Arguments buildArguments(final TerminationPoint sp, final TerminationPoint dp) {
183 final ArgumentsBuilder args = new ArgumentsBuilder();
184 if (this.p2pTunnelInput.getBandwidth() != null) {
185 args.setBandwidth(new BandwidthBuilder().setBandwidth(this.p2pTunnelInput.getBandwidth()).build());
187 if (this.p2pTunnelInput.getClassType() != null) {
188 args.setClassType(new ClassTypeBuilder().setClassType(this.p2pTunnelInput.getClassType()).build());
190 args.setEndpointsObj(new EndpointsObjBuilder().setAddressFamily(buildAddressFamily(sp, dp)).build());
191 args.setEro(TunelProgrammingUtil.buildEro(this.p2pTunnelInput.getExplicitHops()));
192 args.setLspa(new LspaBuilder(this.p2pTunnelInput).build());
194 final AdministrativeStatus adminStatus = this.p2pTunnelInput.augmentation(PcepCreateP2pTunnelInput1.class)
195 .getAdministrativeStatus();
196 if (adminStatus != null) {
197 args.addAugmentation(new Arguments2Builder().setLsp(new LspBuilder()
198 .setAdministrative(adminStatus == AdministrativeStatus.Active).build()).build());
203 private static final class TpReader {
204 private final ReadTransaction rt;
205 private final InstanceIdentifier<Node> nii;
206 private final InstanceIdentifier<TerminationPoint> tii;
208 TpReader(final ReadTransaction rt, final InstanceIdentifier<Topology> topo, final TpReference ref) {
209 this.rt = requireNonNull(rt);
211 this.nii = topo.child(Node.class, new NodeKey(ref.getNode()));
212 this.tii = this.nii.child(TerminationPoint.class, new TerminationPointKey(ref.getTp()));
215 private DataObject read(final InstanceIdentifier<?> id) {
217 return this.rt.read(LogicalDatastoreType.OPERATIONAL, id).get().get();
218 } catch (final InterruptedException | ExecutionException e) {
219 throw new IllegalStateException("Failed to read data.", e);
224 return (Node) read(this.nii);
227 TerminationPoint getTp() {
228 return (TerminationPoint) read(this.tii);