Bump MDSAL to 4.0.0
[bgpcep.git] / pcep / tunnel / tunnel-provider / src / main / java / org / opendaylight / bgpcep / pcep / tunnel / provider / TunelProgrammingUtil.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.bgpcep.pcep.tunnel.provider;
10
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.mdsal.binding.api.ReadTransaction;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.Ero;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.EroBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.FailureType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.OperationResult;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.p2p.rev130819.tunnel.p2p.path.cfg.attributes.ExplicitHops;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev181109.ExplicitHops1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev181109.SupportingNode1;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNode;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 final class TunelProgrammingUtil {
40     public static final ListenableFuture<OperationResult> RESULT = Futures.immediateFuture(new OperationResult() {
41         @Override
42         public Class<OperationResult> implementedInterface() {
43             return OperationResult.class;
44         }
45
46         @Override
47         public FailureType getFailure() {
48             return FailureType.Unsent;
49         }
50
51         @Override
52         public List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109
53                 .operation.result.Error> getError() {
54             return Collections.emptyList();
55         }
56     });
57     private static final Logger LOG = LoggerFactory.getLogger(TunelProgrammingUtil.class);
58
59     private TunelProgrammingUtil() {
60         throw new UnsupportedOperationException();
61     }
62
63     public static Ero buildEro(final List<ExplicitHops> explicitHops) {
64         final EroBuilder b = new EroBuilder();
65
66         if (!explicitHops.isEmpty()) {
67             final List<Subobject> subobjs = new ArrayList<>(explicitHops.size());
68             for (final ExplicitHops h : explicitHops) {
69
70                 final ExplicitHops1 h1 = h.augmentation(ExplicitHops1.class);
71                 if (h1 != null) {
72                     final SubobjectBuilder sb = new SubobjectBuilder();
73                     sb.fieldsFrom(h1);
74                     sb.setLoose(h.isLoose());
75                     subobjs.add(sb.build());
76                 } else {
77                     LOG.debug("Ignoring unhandled explicit hop {}", h);
78                 }
79             }
80             b.setSubobject(subobjs);
81         }
82         return b.build();
83     }
84
85     public static NodeId supportingNode(final Node node) {
86         for (final SupportingNode n : node.getSupportingNode()) {
87             final SupportingNode1 n1 = n.augmentation(SupportingNode1.class);
88             if (n1 != null && n1.getPathComputationClient().isControlling()) {
89                 return n.key().getNodeRef();
90             }
91         }
92
93         return null;
94     }
95
96     public static Optional<Node> sourceNode(final ReadTransaction rt, final InstanceIdentifier<Topology> topology,
97             final Link link) throws InterruptedException, ExecutionException {
98         return rt.read(LogicalDatastoreType.OPERATIONAL,
99                 topology.child(Node.class, new NodeKey(link.getSource().getSourceNode()))).get();
100     }
101 }