package org.opendaylight.bgpcep.pcep.tunnel.provider;
+import java.util.ArrayList;
+import java.util.List;
+
import org.opendaylight.bgpcep.pcep.topology.spi.AbstractTopologyProgrammingExecutor;
import org.opendaylight.bgpcep.programming.spi.InstructionExecutor;
import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
import org.opendaylight.bgpcep.programming.spi.SuccessfulRpcResult;
+import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
+import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.BandwidthBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.classtype.object.ClassTypeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobjects;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.LspaBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.submit.instruction.output.result.FailureBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.submit.instruction.output.result.failure.Failure;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspInputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspInputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.add.lsp.args.ArgumentsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.programming.rev131102.TopologyInstructionInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.p2p.rev130819.tunnel.p2p.path.cfg.attributes.ExplicitHops;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.PcepCreateP2pTunnelInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.PcepCreateP2pTunnelOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.PcepCreateP2pTunnelOutputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.PcepUpdateTunnelOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.PcepUpdateTunnelOutputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.TopologyTunnelPcepProgrammingService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.AdministrativeStatus;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.ExplicitHops1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.Link1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.SupportingNode1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.programming.rev130930.BaseTunnelInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.programming.rev130930.TpReference;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNode;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
+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;
+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;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.ListenableFuture;
final class TunnelProgramming implements TopologyTunnelPcepProgrammingService {
- private final NetworkTopologyPcepService pcepTopology;
+ private static final Logger LOG = LoggerFactory.getLogger(TunnelProgramming.class);
+ private final NetworkTopologyPcepService topologyService;
+ private final DataProviderService dataProvider;
private final InstructionScheduler scheduler;
- TunnelProgramming(final InstructionScheduler scheduler, final NetworkTopologyPcepService pcepTopology) {
+ TunnelProgramming(final InstructionScheduler scheduler, final DataProviderService dataProvider,
+ final NetworkTopologyPcepService topologyService) {
this.scheduler = Preconditions.checkNotNull(scheduler);
- this.pcepTopology = Preconditions.checkNotNull(pcepTopology);
+ this.dataProvider = Preconditions.checkNotNull(dataProvider);
+ this.topologyService = Preconditions.checkNotNull(topologyService);
}
- @Override
- public ListenableFuture<RpcResult<PcepCreateP2pTunnelOutput>> pcepCreateP2pTunnel(final PcepCreateP2pTunnelInput input) {
- Preconditions.checkNotNull(input.getLinkId());
- Preconditions.checkNotNull(input.getSourceTp());
- Preconditions.checkNotNull(input.getDestinationTp());
+ private static final class TpReader {
+ private final DataModificationTransaction t;
+ private final InstanceIdentifier<Node> nii;
+ private final InstanceIdentifier<TerminationPoint> tii;
+
+ TpReader(final DataModificationTransaction t, final InstanceIdentifier<Topology> topo, final TpReference ref) {
+ this.t = Preconditions.checkNotNull(t);
+
+ nii = InstanceIdentifier.builder(topo).node(Node.class, new NodeKey(ref.getNode())).toInstance();
+ tii = InstanceIdentifier.builder(nii).node(TerminationPoint.class, new TerminationPointKey(ref.getTp())).toInstance();
+ }
+
+ private Node getNode() {
+ return (Node) t.readOperationalData(nii);
+ }
+
+ private TerminationPoint getTp() {
+ return (TerminationPoint) t.readOperationalData(tii);
+ }
+ }
+
+ private AddressFamily buildAddressFamily(final TerminationPoint sp, final TerminationPoint dp) {
+ // We need the IGP augmentation -- it has IP addresses
+ final TerminationPoint1 sp1 = Preconditions.checkNotNull(sp.getAugmentation(TerminationPoint1.class));
+ final TerminationPoint1 dp1 = Preconditions.checkNotNull(dp.getAugmentation(TerminationPoint1.class));
+
+ // Get the types
+ final TerminationPointType spt = sp1.getIgpTerminationPointAttributes().getTerminationPointType();
+ final TerminationPointType dpt = dp1.getIgpTerminationPointAttributes().getTerminationPointType();
+
+ // The types have to match
+ Preconditions.checkArgument(spt.getImplementedInterface().equals(dpt.getImplementedInterface()));
+
+ // And they have to actually be Ip
+ final Ip sips = (Ip) spt;
+ final Ip dips = (Ip) dpt;
+
+ /*
+ * Now a bit of magic. We need to find 'like' addresses, e.g. both
+ * IPv4 or both IPv6. We are in IPv6-enabled world now, so let's
+ * prefer that.
+ */
+ AddressFamily ret = findIpv6(sips.getIpAddress(), dips.getIpAddress());
+ if (ret == null) {
+ ret = findIpv4(sips.getIpAddress(), dips.getIpAddress());
+ }
+
+ // We need to have a ret now
+ Preconditions.checkArgument(ret != null, "Failed to find like Endpoint addresses");
+
+ return ret;
+ }
+
+ private AddressFamily findIpv4(final List<IpAddress> srcs, final List<IpAddress> dsts) {
+ for (final IpAddress sc : srcs) {
+ if (sc.getIpv4Address() != null) {
+ for (final IpAddress dc : dsts) {
+ if (dc.getIpv4Address() != null) {
+ return new Ipv4Builder().
+ setSourceIpv4Address(sc.getIpv4Address()).
+ setDestinationIpv4Address(dc.getIpv4Address()).build();
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+
+ private AddressFamily findIpv6(final List<IpAddress> srcs, final List<IpAddress> dsts) {
+ for (final IpAddress sc : srcs) {
+ if (sc.getIpv6Address() != null) {
+ for (final IpAddress dc : dsts) {
+ if (dc.getIpv6Address() != null) {
+ return new Ipv6Builder().
+ setSourceIpv6Address(sc.getIpv6Address()).
+ setDestinationIpv6Address(dc.getIpv6Address()).build();
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+
+ private NodeId supportingNode(final DataModificationTransaction t, final Node node) {
+ for (SupportingNode n : node.getSupportingNode()) {
+ final SupportingNode1 n1 = n.getAugmentation(SupportingNode1.class);
+ if (n1 != null && n1.getPathComputationClient().isControlling()) {
+ return n.getKey().getNodeRef();
+ }
+ }
+
+ return null;
+ }
+
+ private Ero buildEro(final List<ExplicitHops> explicitHops) {
+ final EroBuilder b = new EroBuilder();
- final AddLspInputBuilder ab = new AddLspInputBuilder();
- ab.fieldsFrom(input);
+ if (!explicitHops.isEmpty()) {
+ final List<Subobjects> subobjs = new ArrayList<>(explicitHops.size());
+ for (ExplicitHops h : explicitHops) {
+ final ExplicitHops1 h1 = h.getAugmentation(ExplicitHops1.class);
+ if (h1 != null) {
+ final SubobjectsBuilder sb = new SubobjectsBuilder();
+ sb.fieldsFrom(h1);
+ sb.setLoose(h.isLoose());
+ subobjs.add(sb.build());
+ } else {
+ LOG.debug("Ignoring unhandled explicit hop {}", h);
+ }
+ }
+ b.setSubobjects(subobjs);
+ }
+ return b.build();
+ }
+
+ @Override
+ public ListenableFuture<RpcResult<PcepCreateP2pTunnelOutput>> pcepCreateP2pTunnel(final PcepCreateP2pTunnelInput input) {
final InstructionExecutor e = new AbstractTopologyProgrammingExecutor() {
@Override
protected ListenableFuture<OperationResult> executeImpl() {
- final ListenableFuture<RpcResult<AddLspOutput>> s =
- (ListenableFuture<RpcResult<AddLspOutput>>) pcepTopology.addLsp(ab.build());
+ final InstanceIdentifier<Topology> tii = topologyIdentifier(input);
- return Futures.transform(s, new Function<RpcResult<AddLspOutput>, OperationResult>() {
- @Override
- public OperationResult apply(final RpcResult<AddLspOutput> input) {
- return input.getResult();
- }
- });
+ final DataModificationTransaction t = dataProvider.beginTransaction();
+
+
+ final TpReader dr = new TpReader(t, tii, input.getDestination());
+ final TpReader sr = new TpReader(t, tii, input.getSource());
+
+ final Node sn = Preconditions.checkNotNull(sr.getNode());
+ final TerminationPoint sp = Preconditions.checkNotNull(sr.getTp());
+ final TerminationPoint dp = Preconditions.checkNotNull(dr.getTp());
+
+ final AddLspInputBuilder ab = new AddLspInputBuilder();
+ ab.setNode(Preconditions.checkNotNull(supportingNode(t, sn)));
+ ab.setName(Preconditions.checkNotNull(input.getSymbolicPathName()));
+
+ // The link has to be non-existent
+ final InstanceIdentifier<Link> lii = NodeChangedListener.linkIdentifier(tii, ab.getNode(), ab.getName());
+ Preconditions.checkState(t.readOperationalData(lii) == null);
+
+ final ArgumentsBuilder args = new ArgumentsBuilder();
+ args.setAdministrative(input.getAdministrativeStatus() == AdministrativeStatus.Active);
+ args.setBandwidth(new BandwidthBuilder().setBandwidth(input.getBandwidth()).build());
+ args.setClassType(new ClassTypeBuilder().setClassType(input.getClassType()).build());
+ args.setEndpointsObj(new EndpointsObjBuilder().setAddressFamily(buildAddressFamily(sp, dp)).build());
+ args.setEro(buildEro(input.getExplicitHops()));
+ args.setLspa(new LspaBuilder(input).build());
+
+ ab.setArguments(args.build());
+
+ return Futures.transform(
+ (ListenableFuture<RpcResult<AddLspOutput>>) topologyService.addLsp(ab.build()),
+ new Function<RpcResult<AddLspOutput>, OperationResult>() {
+ @Override
+ public OperationResult apply(final RpcResult<AddLspOutput> input) {
+ return input.getResult();
+ }
+ });
}
};
return Futures.immediateFuture(res);
}
- @Override
- public ListenableFuture<RpcResult<PcepDestroyTunnelOutput>> pcepDestroyTunnel(final PcepDestroyTunnelInput input) {
- Preconditions.checkNotNull(input.getLinkId());
+ // FIXME: topology programming utility class
+ private InstanceIdentifier<Topology> topologyIdentifier(final TopologyInstructionInput input) {
+ return InstanceIdentifier.builder().
+ node(NetworkTopology.class).node(Topology.class, new TopologyKey(input.getTopologyId())).toInstance();
+ }
+
+ // FIXME: tunnel programming utility class
+ private InstanceIdentifier<Link> linkIdentifier(final InstanceIdentifier<Topology> topology, final BaseTunnelInput input) {
+ return InstanceIdentifier.builder(topology).
+ node(Link.class, new LinkKey(Preconditions.checkNotNull(input.getLinkId()))).toInstance();
+ }
- final RemoveLspInputBuilder ab = new RemoveLspInputBuilder();
- ab.fieldsFrom(input);
+ private Node sourceNode(final DataModificationTransaction t, final InstanceIdentifier<Topology> topology, final Link link) {
+ final InstanceIdentifier<Node> nii = InstanceIdentifier.builder(topology).
+ node(Node.class, new NodeKey(link.getSource().getSourceNode())).toInstance();
+ return (Node) t.readOperationalData(nii);
+ }
+ @Override
+ public ListenableFuture<RpcResult<PcepDestroyTunnelOutput>> pcepDestroyTunnel(final PcepDestroyTunnelInput input) {
final InstructionExecutor e = new AbstractTopologyProgrammingExecutor() {
@Override
protected ListenableFuture<OperationResult> executeImpl() {
- final ListenableFuture<RpcResult<RemoveLspOutput>> s =
- (ListenableFuture<RpcResult<RemoveLspOutput>>) pcepTopology.removeLsp(ab.build());
+ final InstanceIdentifier<Topology> tii = topologyIdentifier(input);
+ final InstanceIdentifier<Link> lii = linkIdentifier(tii, input);
- return Futures.transform(s, new Function<RpcResult<RemoveLspOutput>, OperationResult>() {
- @Override
- public OperationResult apply(final RpcResult<RemoveLspOutput> input) {
- return input.getResult();
- }
- });
+ final DataModificationTransaction t = dataProvider.beginTransaction();
+
+ // The link has to exist
+ final Link link = (Link) t.readOperationalData(lii);
+ Preconditions.checkState(link != null);
+
+ // The source node has to exist
+ final Node node = sourceNode(t, tii, link);
+ Preconditions.checkState(node != null);
+
+ final RemoveLspInputBuilder ab = new RemoveLspInputBuilder();
+ ab.setName(link.getAugmentation(Link1.class).getSymbolicPathName());
+ ab.setNode(node.getSupportingNode().get(0).getKey().getNodeRef());
+
+ return Futures.transform(
+ (ListenableFuture<RpcResult<RemoveLspOutput>>) topologyService.removeLsp(ab.build()),
+ new Function<RpcResult<RemoveLspOutput>, OperationResult>() {
+ @Override
+ public OperationResult apply(final RpcResult<RemoveLspOutput> input) {
+ return input.getResult();
+ }
+ });
}
};
@Override
public ListenableFuture<RpcResult<PcepUpdateTunnelOutput>> pcepUpdateTunnel(final PcepUpdateTunnelInput input) {
- Preconditions.checkNotNull(input.getLinkId());
-
- final UpdateLspInputBuilder ab = new UpdateLspInputBuilder();
- ab.fieldsFrom(input);
-
final InstructionExecutor e = new AbstractTopologyProgrammingExecutor() {
@Override
protected ListenableFuture<OperationResult> executeImpl() {
- final ListenableFuture<RpcResult<UpdateLspOutput>> s =
- (ListenableFuture<RpcResult<UpdateLspOutput>>) pcepTopology.updateLsp(ab.build());
+ final InstanceIdentifier<Topology> tii = topologyIdentifier(input);
+ final InstanceIdentifier<Link> lii = linkIdentifier(tii, input);
- return Futures.transform(s, new Function<RpcResult<UpdateLspOutput>, OperationResult>() {
- @Override
- public OperationResult apply(final RpcResult<UpdateLspOutput> input) {
- return input.getResult();
- }
- });
+ final DataModificationTransaction t = dataProvider.beginTransaction();
+
+ // The link has to exist
+ final Link link = (Link) t.readOperationalData(lii);
+ Preconditions.checkState(link != null);
+
+ // The source node has to exist
+ final Node node = sourceNode(t, tii, link);
+ Preconditions.checkState(node != null);
+
+ final UpdateLspInputBuilder ab = new UpdateLspInputBuilder();
+ ab.setName(link.getAugmentation(Link1.class).getSymbolicPathName());
+ ab.setNode(Preconditions.checkNotNull(supportingNode(t, node)));
+
+ final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.update.lsp.args.ArgumentsBuilder args =
+ new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.update.lsp.args.ArgumentsBuilder();
+
+ args.setAdministrative(input.getAdministrativeStatus() == AdministrativeStatus.Active);
+ args.setBandwidth(new BandwidthBuilder().setBandwidth(input.getBandwidth()).build());
+ args.setClassType(new ClassTypeBuilder().setClassType(input.getClassType()).build());
+ args.setEro(buildEro(input.getExplicitHops()));
+ args.setLspa(new LspaBuilder(input).build());
+
+ ab.setArguments(args.build());
+
+ return Futures.transform(
+ (ListenableFuture<RpcResult<UpdateLspOutput>>) topologyService.updateLsp(ab.build()),
+ new Function<RpcResult<UpdateLspOutput>, OperationResult>() {
+ @Override
+ public OperationResult apply(final RpcResult<UpdateLspOutput> input) {
+ return input.getResult();
+ }
+ });
}
};