Adjust to odlparent 3 Checkstyle settings
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / confighelpers / ItmTunnelStateAddHelper.java
1 /*
2  * Copyright (c) 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.itm.confighelpers;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.Collections;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
16 import org.opendaylight.genius.itm.impl.ITMBatchingUtils;
17 import org.opendaylight.genius.itm.impl.ItmUtils;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.TunnelOperStatus;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnels_state.StateTunnelList;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnels_state.StateTunnelListKey;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public final class ItmTunnelStateAddHelper {
27     private static final Logger LOG = LoggerFactory.getLogger(ItmTunnelStateAddHelper.class);
28
29     private ItmTunnelStateAddHelper() { }
30
31     @SuppressWarnings("checkstyle:IllegalCatch")
32     public static List<ListenableFuture<Void>> addTunnel(Interface iface, IInterfaceManager ifaceManager,
33                                                          DataBroker broker) throws Exception {
34         LOG.debug("Invoking ItmTunnelStateAddHelper for Interface {} ", iface);
35         final WriteTransaction writeTransaction = broker.newWriteOnlyTransaction();
36         StateTunnelListKey tlKey = ItmUtils.getTunnelStateKey(iface);
37         LOG.trace("TunnelStateKey: {} for interface: {}", tlKey, iface.getName());
38         InstanceIdentifier<StateTunnelList> stListId = ItmUtils.buildStateTunnelListId(tlKey);
39         StateTunnelList tunnelStateList;
40         TunnelOperStatus tunnelOperStatus;
41         boolean tunnelState = iface.getOperStatus().equals(Interface.OperStatus.Up);
42         switch (iface.getOperStatus()) {
43             case Up:
44                 tunnelOperStatus = TunnelOperStatus.Up;
45                 break;
46             case Down:
47                 tunnelOperStatus = TunnelOperStatus.Down;
48                 break;
49             case Unknown:
50                 tunnelOperStatus = TunnelOperStatus.Unknown;
51                 break;
52             default:
53                 tunnelOperStatus = TunnelOperStatus.Ignore;
54         }
55
56         // Create new Tunnel State
57         try {
58             /*
59              * FIXME: A defensive try-catch to find issues without
60              * disrupting existing behavior.
61              */
62             tunnelStateList = ItmUtils.buildStateTunnelList(tlKey, iface.getName(), tunnelState, tunnelOperStatus,
63                     ifaceManager, broker);
64             LOG.trace("Batching the Creation of tunnel_state: {} for Id: {}", tunnelStateList, stListId);
65             ITMBatchingUtils.write(stListId, tunnelStateList, ITMBatchingUtils.EntityType.DEFAULT_OPERATIONAL);
66         } catch (Exception e) {
67             LOG.warn("Exception trying to create tunnel state for {}", iface.getName(), e);
68         }
69
70         return Collections.singletonList(writeTransaction.submit());
71     }
72
73 }