f48629d95b9dc606830362031a507940d0471f7d
[netvirt.git] / coe / impl / src / main / java / org / opendaylight / netvirt / coe / listeners / TerminationPointStateListener.java
1 /*
2  * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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 package org.opendaylight.netvirt.coe.listeners;
9
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11
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;
53
54 @Singleton
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;
62
63     @Inject
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;
73     }
74
75     @Override
76     public void remove(OvsdbTerminationPointAugmentation tpOld) {
77        // DO nothing
78     }
79
80     @Override
81     public void update(@Nullable OvsdbTerminationPointAugmentation tpOld, OvsdbTerminationPointAugmentation tpNew) {
82         LOG.debug("Received Update DataChange Notification for ovsdb termination point {}", tpNew.getName());
83
84         SouthboundInterfaceInfo tpNewDetails = coeUtils.getSouthboundInterfaceDetails(tpNew);
85         SouthboundInterfaceInfo tpOldDetails = coeUtils.getSouthboundInterfaceDetails(tpOld);
86
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();
105                                 if (pods != null) {
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                                     coeUtils.createVpnInterface(pods.getClusterId().getValue(), pods, interfaceName,
113                                             macAddress,false, tx);
114                                     LOG.debug("Bind Kube Proxy Service for {}", interfaceName);
115                                     bindKubeProxyService(tx, interfaceName);
116                                     if (isServiceGateway) {
117                                         String ipValue = podIpAddress.getIpv4Address() != null
118                                                 ? podIpAddress.getIpv4Address().getValue() :
119                                                 podIpAddress.getIpv6Address().getValue();
120                                         coeUtils.updateServiceGatewayList(tx, interfaceName, ipValue, macAddress);
121                                     }
122                                 }
123                             }), LOG, "Error handling pod configuration for termination-point");
124                         return DataTreeEventCallbackRegistrar.NextAction.UNREGISTER;
125                     });
126             }
127         }
128     }
129
130     @Override
131     public void add(OvsdbTerminationPointAugmentation tpNew) {
132         update(null, tpNew);
133     }
134
135     private void bindKubeProxyService(TypedReadWriteTransaction<Datastore.Configuration> tx,
136                                              String interfaceName) {
137         int priority = ServiceIndex.getIndex(NwConstants.COE_KUBE_PROXY_SERVICE_NAME,
138                 NwConstants.COE_KUBE_PROXY_SERVICE_INDEX);
139         int instructionKey = 0;
140         List<Instruction> instructions = new ArrayList<>();
141         instructions.add(MDSALUtil.buildAndGetGotoTableInstruction(
142                 NwConstants.COE_KUBE_PROXY_TABLE, ++instructionKey));
143         BoundServices serviceInfo =
144                 getBoundServices(String.format("%s.%s", NwConstants.COE_KUBE_PROXY_SERVICE_NAME, interfaceName),
145                         ServiceIndex.getIndex(NwConstants.COE_KUBE_PROXY_SERVICE_NAME,
146                                 NwConstants.COE_KUBE_PROXY_SERVICE_INDEX),
147                         priority, NwConstants.COOKIE_COE_KUBE_PROXY_TABLE, instructions);
148         InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier =
149                 coeUtils.buildKubeProxyServicesIId(interfaceName);
150         tx.put(boundServicesInstanceIdentifier, serviceInfo,true);
151     }
152
153     private static BoundServices getBoundServices(String serviceName, short servicePriority, int flowPriority,
154                                                  BigInteger cookie, List<Instruction> instructions) {
155         StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority)
156                 .setInstruction(instructions);
157         return new BoundServicesBuilder().withKey(new BoundServicesKey(servicePriority)).setServiceName(serviceName)
158                 .setServicePriority(servicePriority).setServiceType(ServiceTypeFlowBased.class)
159                 .addAugmentation(StypeOpenflow.class, augBuilder.build()).build();
160     }
161 }