7ef74057cfbed59c3e4391d76afd9486f37af073
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / manager / VppNodeManager.java
1 /*
2  * Copyright (c) 2016 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.groupbasedpolicy.renderer.vpp.manager;
10
11 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.Connected;
12 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.Connecting;
13 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Objects;
21 import java.util.concurrent.Callable;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.Future;
26 import java.util.stream.Collectors;
27
28 import javax.annotation.Nonnull;
29 import javax.annotation.Nullable;
30
31 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
32 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
33 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
34 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
35 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
36 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
37 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
38 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
39 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
40 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
41 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.mappers.HostIdToInterfaceInfoMapper;
42 import org.opendaylight.groupbasedpolicy.renderer.vpp.nat.NatUtil;
43 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
44 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppRendererProcessingException;
45 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.EthernetCsmacd;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.RendererNodes;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNode;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.VppInterfaceAugmentation;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.VppInterfaceAugmentationBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.renderers.renderer.renderer.nodes.renderer.node.PhysicalInterface;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.renderers.renderer.renderer.nodes.renderer.node.PhysicalInterfaceBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.renderers.renderer.renderer.nodes.renderer.node.PhysicalInterfaceKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
70 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73
74 import com.google.common.base.Optional;
75 import com.google.common.base.Preconditions;
76 import com.google.common.base.Splitter;
77 import com.google.common.base.Strings;
78 import com.google.common.collect.Lists;
79 import com.google.common.collect.Sets;
80 import com.google.common.util.concurrent.CheckedFuture;
81 import com.google.common.util.concurrent.FutureCallback;
82 import com.google.common.util.concurrent.Futures;
83 import com.google.common.util.concurrent.ListenableFuture;
84
85 public class VppNodeManager {
86
87     private static final short DURATION = 3000;
88     private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
89     private static final Logger LOG = LoggerFactory.getLogger(VppNodeManager.class);
90     private static final String V3PO_CAPABILITY = "(urn:opendaylight:params:xml:ns:yang:v3po?revision=2017-06-07)v3po";
91     private static final String INTERFACES_CAPABILITY = "(urn:ietf:params:xml:ns:yang:ietf-interfaces?revision=2014-05-08)ietf-interfaces";
92     private static final NodeId CONTROLLER_CONFIG_NODE = new NodeId("controller-config");
93     private static final String NO_PUBLIC_INT_SPECIFIED = "unspecified";
94     private static final String PUBLIC_INTERFACE = "public-interface";
95     private final Map<NodeId, PhysicalInterfaceKey> extInterfaces = new HashMap<>();
96     private final DataBroker dataBroker;
97     private final List<String> requiredCapabilities;
98     private final MountPointService mountService;
99
100     public VppNodeManager(@Nonnull final DataBroker dataBroker,
101             @Nonnull final BindingAwareBroker.ProviderContext session, @Nullable String physicalInterfaces) {
102         this.dataBroker = Preconditions.checkNotNull(dataBroker);
103         this.mountService = Preconditions.checkNotNull(session.getSALService(MountPointService.class));
104         requiredCapabilities = initializeRequiredCapabilities();
105         if (!Strings.isNullOrEmpty(physicalInterfaces) && !Objects.equals(physicalInterfaces, NO_PUBLIC_INT_SPECIFIED)) {
106             loadPhysicalInterfaces(physicalInterfaces);
107         }
108     }
109
110     /**
111      * Caches list of physical interfaces.
112      */
113     private void loadPhysicalInterfaces(@Nonnull String physicalInterfaces) {
114         for (String intfOnNode : Sets.newConcurrentHashSet(Splitter.on(",").split(physicalInterfaces))) {
115             List<String> entries = Lists.newArrayList(Splitter.on(":").split(intfOnNode));
116             if (entries.size() != 2) {
117                 LOG.warn("Cannot resolve {} initial configuration for physical interfaces.", intfOnNode);
118                 continue;
119             }
120             NodeId nodeId = new NodeId(entries.get(0));
121             PhysicalInterfaceKey infaceKey = new PhysicalInterfaceKey(entries.get(1));
122             LOG.info("Interface %s on node %swill be considered as external", infaceKey, nodeId);
123             extInterfaces.put(nodeId, infaceKey);
124         }
125     }
126
127     /**
128      * Synchronizes nodes to DataStore based on their modification state which results in
129      * create/update/remove of Node.
130      */
131     public void syncNodes(final Node dataAfter, final Node dataBefore) {
132         if (isControllerConfigNode(dataAfter, dataBefore)) {
133             LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
134             return;
135         }
136         ListenableFuture<String> syncFuture = Futures.immediateFuture(null);
137         // New node
138         if (dataBefore == null && dataAfter != null) {
139             syncFuture = createNode(dataAfter);
140         }
141         // Connected/disconnected node
142         else if (dataBefore != null && dataAfter != null) {
143             syncFuture = updateNode(dataAfter);
144         }
145         // Removed node
146         else if (dataBefore != null) {
147             syncFuture = removeNode(dataBefore);
148         }
149         Futures.addCallback(syncFuture, new FutureCallback<String>() {
150             @Override
151             public void onSuccess(@Nullable String message) {
152                 LOG.info("Node synchronization completed. {} ", message);
153             }
154
155             @Override
156             public void onFailure(@Nonnull Throwable t) {
157                 LOG.warn("Node synchronization failed. Data before: {} after {}", dataBefore, dataAfter);
158             }
159         });
160     }
161
162     private boolean isControllerConfigNode(final Node dataAfter, final Node dataBefore) {
163         if (dataAfter != null) {
164             return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
165         }
166         return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
167     }
168
169     private ListenableFuture<String> createNode(final Node node) {
170         final String nodeId = node.getNodeId().getValue();
171         LOG.info("Registering new node {}", nodeId);
172         final NetconfNode netconfNode = getNodeAugmentation(node);
173         if (netconfNode == null) {
174             final String message = String.format("Node %s is not an netconf node", nodeId);
175             return Futures.immediateFuture(message);
176         }
177         final NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
178         switch (connectionStatus) {
179             case Connecting: {
180                 final String message = String.format("Connecting device %s ...", nodeId);
181                 return Futures.immediateFuture(message);
182             }
183             case Connected: {
184                 return resolveConnectedNode(node, netconfNode);
185             }
186             case UnableToConnect: {
187                 final String message = String.format("Connection status is unable to connect for node %s", nodeId);
188                 return Futures.immediateFuture(message);
189             }
190             default: {
191                 final String message = String.format("Unknown connection status for node %s", nodeId);
192                 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
193             }
194         }
195     }
196
197     private ListenableFuture<String> updateNode(final Node node) {
198         final String nodeId = node.getNodeId().getValue();
199         LOG.info("Updating node {}", nodeId);
200         final NetconfNode netconfNode = getNodeAugmentation(node);
201         if (netconfNode == null) {
202             final String message = String.format("Node %s is not an netconf node", nodeId);
203             return Futures.immediateFuture(message);
204         }
205         final NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
206         if (Connected.equals(afterNodeStatus)) {
207             return resolveConnectedNode(node, netconfNode);
208         } else if (Connecting.equals(afterNodeStatus)) {
209             final String cause = String.format("Node %s is disconnected, removing from available nodes", nodeId);
210             return resolveDisconnectedNode(node, cause);
211         } else if (UnableToConnect.equals(afterNodeStatus)) {
212             final String cause = String.format("New node %s status is unable to connect, removing from available nodes",
213                     nodeId);
214             return resolveDisconnectedNode(node, cause);
215         } else {
216             final String cause = String.format("New node status is unknown. Node %s will be removed from available nodes",
217                     nodeId);
218             return resolveDisconnectedNode(node, cause);
219         }
220     }
221
222     private ListenableFuture<String> removeNode(final Node node) {
223         final String cause = String.format("Node %s is removed", node.getNodeId().getValue());
224         return resolveDisconnectedNode(node, cause);
225     }
226
227     private ListenableFuture<String> resolveConnectedNode(final Node node, final NetconfNode netconfNode) {
228         final String nodeId = node.getNodeId().getValue();
229         final InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
230         final RendererNode rendererNode = remapNode(mountPointIid);
231         if (!isCapableNetconfDevice(node, netconfNode)) {
232             final String message = String.format("Node %s is not connected", nodeId);
233             return Futures.immediateFuture(message);
234         }
235         final DataBroker mountpoint = getNodeMountPoint(mountPointIid);
236         if (mountpoint == null) {
237             final String message = String.format("Mountpoint not available for node %s", nodeId);
238             return Futures.immediateFuture(message);
239         }
240         final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
241         wTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode), rendererNode, true);
242         final boolean submit = DataStoreHelper.submitToDs(wTx);
243         if (submit) {
244             final String message = String.format("Node %s is capable and ready", nodeId);
245             syncPhysicalInterfacesInLocalDs(mountpoint, mountPointIid);
246             NatUtil.resolveOutboundNatInterface(mountpoint, mountPointIid, node.getNodeId(), extInterfaces);
247             return Futures.immediateFuture(message);
248         } else {
249             final String message = String.format("Failed to resolve connected node %s", nodeId);
250             return Futures.immediateFuture(message);
251         }
252     }
253
254     private ListenableFuture<String> resolveDisconnectedNode(final Node node, final String cause) {
255         final InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
256         final RendererNode rendererNode = remapNode(mountPointIid);
257         final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
258         wTx.delete(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode));
259         extInterfaces.remove(node.getNodeId());
260         final CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = wTx.submit();
261         try {
262             checkedFuture.checkedGet();
263             return Futures.immediateFuture(cause);
264         } catch (TransactionCommitFailedException e) {
265             final String message = String.format("Failed to resolve disconnected node %s", node.getNodeId().getValue());
266             return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
267         }
268     }
269
270     @Nullable
271     private DataBroker getNodeMountPoint(final InstanceIdentifier<Node> mountPointIid) {
272         final Future<Optional<MountPoint>> futureOptionalObject = getMountpointFromSal(mountPointIid);
273         try {
274             final Optional<MountPoint> optionalObject = futureOptionalObject.get();
275             LOG.debug("Optional mountpoint object: {}", optionalObject);
276             MountPoint mountPoint;
277             if (optionalObject.isPresent()) {
278                 mountPoint = optionalObject.get();
279                 Optional<DataBroker> optionalDataBroker = mountPoint.getService(DataBroker.class);
280                 if (optionalDataBroker.isPresent()) {
281                     return optionalDataBroker.get();
282                 } else {
283                     LOG.warn("Cannot obtain data broker from mountpoint {}", mountPoint);
284                 }
285             } else {
286                 LOG.warn("Cannot obtain mountpoint with IID {}", mountPointIid);
287             }
288             return null;
289         } catch (ExecutionException | InterruptedException e) {
290             LOG.warn("Unable to obtain mountpoint ... {}", e);
291             return null;
292         }
293     }
294
295     private RendererNode remapNode(final InstanceIdentifier<Node> path) {
296         final RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
297         rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
298         return rendererNodeBuilder.build();
299     }
300
301     private InstanceIdentifier<Node> getMountpointIid(final Node node) {
302         return InstanceIdentifier.builder(NetworkTopology.class)
303                 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
304                 .child(Node.class, new NodeKey(node.getNodeId()))
305                 .build();
306     }
307
308     private boolean isCapableNetconfDevice(final Node node, final NetconfNode netconfAugmentation) {
309         if (netconfAugmentation.getAvailableCapabilities() == null
310                 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
311                 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
312             LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
313             return false;
314         }
315         if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
316             LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
317             return false;
318         }
319         return true;
320     }
321
322     private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
323         final List<String> availableCapabilities = capabilities.stream()
324                 .map(AvailableCapability::getCapability)
325                 .collect(Collectors.toList());
326         return requiredCapabilities.stream()
327                 .allMatch(availableCapabilities::contains);
328     }
329
330     private NetconfNode getNodeAugmentation(final Node node) {
331         final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
332         if (netconfNode == null) {
333             LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
334             return null;
335         }
336         return netconfNode;
337     }
338
339     /**
340      * Initialize all common capabilities required by VPP renderer. Any connected node is examined
341      * whether it's
342      * an appropriate device to handle configuration created by this renderer. A device must support
343      * all capabilities
344      * in list below.
345      *
346      * @return list of string representations of required capabilities
347      */
348     private List<String> initializeRequiredCapabilities() {
349         // Required device capabilities
350         String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
351         return Arrays.asList(capabilityEntries);
352     }
353
354     // TODO bug 7699
355     // This works as a workaround for mountpoint registration in cluster. If application is registered on different
356     // node as netconf service, it obtains mountpoint registered by SlaveSalFacade (instead of MasterSalFacade). However
357     // this service registers mountpoint a moment later then connectionStatus is set to "Connected". If NodeManager hits
358     // state where device is connected but mountpoint is not yet available, try to get it again in a while
359     private Future<Optional<MountPoint>> getMountpointFromSal(final InstanceIdentifier<Node> iid) {
360         final ExecutorService executorService = Executors.newSingleThreadExecutor();
361         final Callable<Optional<MountPoint>> task = () -> {
362             byte attempt = 0;
363             do {
364                 try {
365                     final Optional<MountPoint> optionalMountpoint = mountService.getMountPoint(iid);
366                     if (optionalMountpoint.isPresent()) {
367                         return optionalMountpoint;
368                     }
369                     LOG.warn("Mountpoint {} is not registered yet", iid);
370                     Thread.sleep(DURATION);
371                 } catch (InterruptedException e) {
372                     LOG.warn("Thread interrupted to ", e);
373                 }
374                 attempt++;
375             } while (attempt <= 3);
376             return Optional.absent();
377         };
378         return executorService.submit(task);
379     }
380
381     private void syncPhysicalInterfacesInLocalDs(DataBroker mountPointDataBroker, InstanceIdentifier<Node> nodeIid) {
382         ReadWriteTransaction rwTx = dataBroker.newReadWriteTransaction();
383         ReadOnlyTransaction rTx = mountPointDataBroker.newReadOnlyTransaction();
384         Optional<Interfaces> readIfaces = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
385                 InstanceIdentifier.create(Interfaces.class), rTx);
386         if (readIfaces.isPresent()) {
387             InstanceIdentifier<RendererNode> rendererNodeIid = VppIidFactory.getRendererNodesIid()
388                 .builder()
389                 .child(RendererNode.class, new RendererNodeKey(nodeIid))
390                 .build();
391             Optional<RendererNode> optRendNode = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
392                     rendererNodeIid, rwTx);
393             NodeId nodeId = nodeIid.firstKeyOf(Node.class).getNodeId();
394             RendererNode rendNode = new RendererNodeBuilder(optRendNode.get())
395                 .addAugmentation(VppInterfaceAugmentation.class, resolveTerminationPoints(nodeId, readIfaces.get()))
396                 .build();
397             rwTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(optRendNode.get()), rendNode,
398                     true);
399         }
400         rTx.close();
401         DataStoreHelper.submitToDs(rwTx);
402     }
403
404     private VppInterfaceAugmentation resolveTerminationPoints(NodeId nodeId, Interfaces interfaces) {
405         List<PhysicalInterface> phIfaces = new ArrayList<>();
406         if (interfaces != null && interfaces.getInterface() != null) {
407             interfaces.getInterface()
408                 .stream()
409                 .filter(iface -> iface.getType().equals(EthernetCsmacd.class))
410                 .filter(iface -> iface.getAugmentation(Interface1.class) != null)
411                 .forEach(iface -> {
412                     PhysicalInterfaceBuilder phIface = new PhysicalInterfaceBuilder();
413                     phIface.setInterfaceName(iface.getName());
414                     phIface.setType(iface.getType());
415                     phIface.setAddress(resolveIpAddress(iface.getAugmentation(Interface1.class)));
416
417                     if (extInterfaces.get(nodeId) != null
418                             && extInterfaces.get(nodeId).getInterfaceName().equals(phIface.getInterfaceName())) {
419                         phIface.setExternal(true);
420                         extInterfaces.put(nodeId, new PhysicalInterfaceKey(iface.getName()));
421                         HostIdToInterfaceInfoMapper.getInstance()
422                                 .addInterfaceInfo(nodeId.getValue(), HostIdToInterfaceInfoMapper.InterfaceType.PUBLIC,
423                                         phIface.getInterfaceName(), phIface.getAddress().get(0));
424                         LOG.info("Interface {} is marked as public interface based on bundle configuration.",
425                                 iface.getName());
426                     }
427                     if (PUBLIC_INTERFACE.equals(iface.getDescription())) {
428                         phIface.setExternal(true);
429                         extInterfaces.put(nodeId, new PhysicalInterfaceKey(iface.getName()));
430                         HostIdToInterfaceInfoMapper.getInstance()
431                                 .addInterfaceInfo(nodeId.getValue(), HostIdToInterfaceInfoMapper.InterfaceType.PUBLIC,
432                                         phIface.getInterfaceName(), phIface.getAddress().get(0));
433                         LOG.info("Interface {} is marked as public interface based on HC configuration.",
434                                 iface.getName());
435                     }
436                     phIfaces.add(phIface.build());
437                 });
438         }
439         return new VppInterfaceAugmentationBuilder().setPhysicalInterface(phIfaces).build();
440     }
441
442     private List<IpAddress> resolveIpAddress(Interface1 iface) {
443         if (iface.getIpv4() != null && iface.getIpv4().getAddress() != null) {
444             return iface.getIpv4().getAddress().stream().map(ipv4 ->
445                     new IpAddress(new Ipv4Address(ipv4.getIp().getValue()))).collect(Collectors.toList());
446         } else if (iface.getIpv6() != null && iface.getIpv6().getAddress() != null) {
447             return iface.getIpv6().getAddress().stream().map(ipv6 ->
448                     new IpAddress(new Ipv4Address(ipv6.getIp().getValue()))).collect(Collectors.toList());
449         }
450         return Lists.newArrayList();
451     }
452
453     public static Map<NodeId, String> resolvePublicInterfaces(ReadTransaction rTx) {
454         Map<NodeId, String> nodes = new HashMap<>();
455         Optional<RendererNodes> rendNodes =
456                 DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodesIid(), rTx);
457         if (!rendNodes.isPresent()) {
458             return nodes;
459         }
460         rendNodes.get()
461             .getRendererNode()
462             .stream()
463             .filter(rn -> rn.getAugmentation(VppInterfaceAugmentation.class) != null)
464             .filter(rn -> rn.getAugmentation(VppInterfaceAugmentation.class).getPhysicalInterface() != null)
465             .forEach(rn -> {
466                 java.util.Optional<PhysicalInterface> pubInt = rn.getAugmentation(VppInterfaceAugmentation.class)
467                     .getPhysicalInterface()
468                     .stream()
469                     .filter(phInt -> phInt.isExternal())
470                     .findFirst();
471                 if (pubInt.isPresent()) {
472                     nodes.put(rn.getNodePath().firstKeyOf(Node.class).getNodeId(), pubInt.get().getInterfaceName());
473                 }
474             });
475         return nodes;
476     }
477 }