c29aff14d887a4de52e8955be7a629c3e918d955
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / renderer / hwvtep / statehelpers / HwVTEPInterfaceStateUpdateHelper.java
1 /*
2  * Copyright (c) 2016, 2017 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.genius.interfacemanager.renderer.hwvtep.statehelpers;
9
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
12 import static org.opendaylight.mdsal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
13
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
19 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
20 import org.opendaylight.genius.interfacemanager.commons.InterfaceMetaUtils;
21 import org.opendaylight.genius.interfacemanager.renderer.hwvtep.utilities.SouthboundUtils;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelsKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdParams;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdStatus;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public final class HwVTEPInterfaceStateUpdateHelper {
33     private static final Logger LOG = LoggerFactory.getLogger(HwVTEPInterfaceStateUpdateHelper.class);
34
35     private HwVTEPInterfaceStateUpdateHelper() {
36     }
37
38     public static List<ListenableFuture<Void>> updatePhysicalSwitch(ManagedNewTransactionRunner txRunner,
39             InstanceIdentifier<Tunnels> tunnelsInstanceIdentifier, Tunnels tunnelsNew) {
40         LOG.debug("updating physical switch for tunnels");
41         return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
42             String interfaceName = InterfaceMetaUtils
43                     .getInterfaceForTunnelInstanceIdentifier(tunnelsInstanceIdentifier.toString(), tx);
44             if (interfaceName != null) {
45                 // update opstate of interface if TEP has gone down/up as a result of
46                 // BFD monitoring
47                 InterfaceManagerCommonUtils.updateOpState(tx, interfaceName,
48                         getTunnelOpState(tunnelsNew.getBfdStatus()));
49             }
50         }));
51     }
52
53     private static OperStatus getTunnelOpState(List<BfdStatus> tunnelBfdStatus) {
54         OperStatus livenessState = OperStatus.Down;
55         if (tunnelBfdStatus != null && !tunnelBfdStatus.isEmpty()) {
56             for (BfdStatus bfdState : tunnelBfdStatus) {
57                 if (SouthboundUtils.BFD_OP_STATE.equalsIgnoreCase(bfdState.getBfdStatusKey())) {
58                     String bfdOpState = bfdState.getBfdStatusValue();
59                     if (SouthboundUtils.BFD_STATE_UP.equalsIgnoreCase(bfdOpState)) {
60                         livenessState = OperStatus.Up;
61                     } else {
62                         livenessState = OperStatus.Down;
63                     }
64                     break;
65                 }
66             }
67         }
68         return livenessState;
69     }
70
71     public static List<ListenableFuture<Void>> startBfdMonitoring(ManagedNewTransactionRunner txRunner,
72             InstanceIdentifier<Tunnels> tunnelsInstanceIdentifier, Tunnels tunnelsNew) {
73         LOG.debug("starting bfd monitoring for the hwvtep {}", tunnelsInstanceIdentifier);
74
75         TunnelsBuilder tunnelsBuilder = new TunnelsBuilder();
76         tunnelsBuilder.withKey(new TunnelsKey(tunnelsNew.getLocalLocatorRef(), tunnelsNew.getRemoteLocatorRef()));
77         tunnelsBuilder.setLocalLocatorRef(tunnelsNew.getLocalLocatorRef());
78         tunnelsBuilder.setRemoteLocatorRef(tunnelsNew.getLocalLocatorRef());
79         List<BfdParams> bfdParams = new ArrayList<>();
80         SouthboundUtils.fillBfdParameters(bfdParams, null);
81         tunnelsBuilder.setBfdParams(bfdParams);
82         return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
83             tx -> tx.put(tunnelsInstanceIdentifier, tunnelsBuilder.build(), CREATE_MISSING_PARENTS)));
84     }
85 }