2 * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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
8 package org.opendaylight.netvirt.coe.listeners;
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Objects;
16 import java.util.Optional;
17 import javax.annotation.Nullable;
18 import javax.inject.Inject;
19 import javax.inject.Singleton;
20 import org.apache.aries.blueprint.annotation.service.Reference;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.genius.datastoreutils.listeners.DataTreeEventCallbackRegistrar;
24 import org.opendaylight.genius.infra.Datastore;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
26 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
27 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
28 import org.opendaylight.genius.mdsalutil.MDSALUtil;
29 import org.opendaylight.genius.mdsalutil.NwConstants;
30 import org.opendaylight.genius.utils.ServiceIndex;
31 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
32 import org.opendaylight.netvirt.coe.api.SouthboundInterfaceInfo;
33 import org.opendaylight.netvirt.coe.caches.PodsCache;
34 import org.opendaylight.netvirt.coe.utils.CoeUtils;
35 import org.opendaylight.serviceutils.tools.mdsal.listener.AbstractSyncDataTreeChangeListener;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.coe.northbound.pod.rev170611.coe.Pods;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceTypeFlowBased;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflow;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflowBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 public class TerminationPointStateListener extends
56 AbstractSyncDataTreeChangeListener<OvsdbTerminationPointAugmentation> {
57 private static final Logger LOG = LoggerFactory.getLogger(TerminationPointStateListener.class);
58 private final PodsCache podsCache;
59 private final DataTreeEventCallbackRegistrar eventCallbacks;
60 private final ManagedNewTransactionRunner txRunner;
61 private final CoeUtils coeUtils;
64 public TerminationPointStateListener(@Reference DataBroker dataBroker, PodsCache podsCache,
65 @Reference DataTreeEventCallbackRegistrar eventCallbacks, CoeUtils coeUtils) {
66 super(dataBroker, LogicalDatastoreType.OPERATIONAL,
67 InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class).child(Node.class)
68 .child(TerminationPoint.class).augmentation(OvsdbTerminationPointAugmentation.class).build());
69 this.podsCache = podsCache;
70 this.eventCallbacks = eventCallbacks;
71 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
72 this.coeUtils = coeUtils;
76 public void remove(OvsdbTerminationPointAugmentation tpOld) {
81 public void update(@Nullable OvsdbTerminationPointAugmentation tpOld, OvsdbTerminationPointAugmentation tpNew) {
82 LOG.debug("Received Update DataChange Notification for ovsdb termination point {}", tpNew.getName());
84 SouthboundInterfaceInfo tpNewDetails = coeUtils.getSouthboundInterfaceDetails(tpNew);
85 SouthboundInterfaceInfo tpOldDetails = coeUtils.getSouthboundInterfaceDetails(tpOld);
87 if (!Objects.equals(tpNewDetails, tpOldDetails)) {
88 Optional<String> interfaceNameOptional = tpNewDetails.getInterfaceName();
89 Optional<String> macAddressOptional = tpNewDetails.getMacAddress();
90 if (interfaceNameOptional.isPresent() && macAddressOptional.isPresent()) {
91 String interfaceName = interfaceNameOptional.get();
92 String macAddress = macAddressOptional.get();
93 boolean isServiceGateway = tpNewDetails.isServiceGateway().orElse(false);
94 LOG.debug("Detected external interface-id {} and attached mac address {} for {}", interfaceName,
95 macAddress, tpNew.getName());
96 eventCallbacks.onAddOrUpdate(LogicalDatastoreType.OPERATIONAL,
97 coeUtils.getPodMetaInstanceId(interfaceName), (unused, alsoUnused) -> {
98 LOG.info("Pod configuration {} detected for termination-point {},"
99 + "proceeding with l2 and l3 configurations", interfaceName, tpNew.getName());
100 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(
101 CONFIGURATION, tx -> {
102 InstanceIdentifier<Pods> instanceIdentifier = coeUtils.getPodUUIDforPodName(
103 interfaceName, getDataBroker());
104 Pods pods = podsCache.get(instanceIdentifier).get();
106 IpAddress podIpAddress = pods.getInterface().get(0).getIpAddress();
107 String clusterId = pods.getClusterId().getValue();
108 String elanInstanceName = CoeUtils.buildElanInstanceName(
109 pods.getHostIpAddress().stringValue(), clusterId);
110 coeUtils.updateElanInterfaceWithStaticMac(macAddress, podIpAddress,
111 interfaceName, elanInstanceName, tx);
112 if (!isServiceGateway) {
113 coeUtils.createVpnInterface(clusterId, pods, interfaceName,
114 macAddress,false, tx);
115 LOG.debug("Bind Kube Proxy Service for {}", interfaceName);
116 bindKubeProxyService(tx, interfaceName);
119 }), LOG, "Error handling pod configuration for termination-point");
120 return DataTreeEventCallbackRegistrar.NextAction.UNREGISTER;
127 public void add(OvsdbTerminationPointAugmentation tpNew) {
131 private void bindKubeProxyService(TypedReadWriteTransaction<Datastore.Configuration> tx,
132 String interfaceName) {
133 int priority = ServiceIndex.getIndex(NwConstants.COE_KUBE_PROXY_SERVICE_NAME,
134 NwConstants.COE_KUBE_PROXY_SERVICE_INDEX);
135 int instructionKey = 0;
136 List<Instruction> instructions = new ArrayList<>();
137 instructions.add(MDSALUtil.buildAndGetGotoTableInstruction(
138 NwConstants.COE_KUBE_PROXY_TABLE, ++instructionKey));
139 BoundServices serviceInfo =
140 getBoundServices(String.format("%s.%s", NwConstants.COE_KUBE_PROXY_SERVICE_NAME, interfaceName),
141 ServiceIndex.getIndex(NwConstants.COE_KUBE_PROXY_SERVICE_NAME,
142 NwConstants.COE_KUBE_PROXY_SERVICE_INDEX),
143 priority, NwConstants.COOKIE_COE_KUBE_PROXY_TABLE, instructions);
144 InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier =
145 coeUtils.buildKubeProxyServicesIId(interfaceName);
146 tx.put(boundServicesInstanceIdentifier, serviceInfo,true);
149 private static BoundServices getBoundServices(String serviceName, short servicePriority, int flowPriority,
150 BigInteger cookie, List<Instruction> instructions) {
151 StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority)
152 .setInstruction(instructions);
153 return new BoundServicesBuilder().withKey(new BoundServicesKey(servicePriority)).setServiceName(serviceName)
154 .setServicePriority(servicePriority).setServiceType(ServiceTypeFlowBased.class)
155 .addAugmentation(StypeOpenflow.class, augBuilder.build()).build();