Initial Alivenessmonitor code
[vpnservice.git] / alivenessmonitor / alivenessmonitor-impl / src / main / java / org / opendaylight / vpnservice / alivenessmonitor / internal / AbstractAlivenessProtocolHandler.java
1 /*
2  * Copyright (c) 2015 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.vpnservice.alivenessmonitor.internal;
9
10 import java.util.concurrent.ExecutionException;
11 import java.util.concurrent.Future;
12
13 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetNodeconnectorIdFromInterfaceInputBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetNodeconnectorIdFromInterfaceInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetNodeconnectorIdFromInterfaceOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31
32 import com.google.common.base.Optional;
33 import com.google.common.base.Strings;
34
35 abstract class AbstractAlivenessProtocolHandler implements AlivenessProtocolHandler {
36
37     protected ServiceProvider serviceProvider;
38     private InventoryReader inventoryReader;
39
40     public AbstractAlivenessProtocolHandler(ServiceProvider serviceProvider) {
41         this.serviceProvider = serviceProvider;
42         inventoryReader = new InventoryReader(serviceProvider.getDataBroker());
43     }
44
45     private InstanceIdentifier<NodeConnector> getNodeConnectorId(String interfaceName) {
46         InstanceIdentifier<Interface> id =  InstanceIdentifier.builder(Interfaces.class)
47                                       .child(Interface.class, new InterfaceKey(interfaceName)).build();
48
49         Optional<Interface> port = read(LogicalDatastoreType.CONFIGURATION, id);
50         if(port.isPresent()) {
51             NodeConnectorId ncId = getNodeConnectorIdFromInterface(interfaceName);
52             NodeId nodeId = getNodeIdFromNodeConnectorId(ncId);
53
54             InstanceIdentifier<NodeConnector> ncIdentifier =
55                                           InstanceIdentifier.builder(Nodes.class)
56                                                       .child(Node.class, new NodeKey(nodeId))
57                                                       .child(NodeConnector.class, new NodeConnectorKey(ncId)).build();
58             return ncIdentifier;
59         }
60         return null;
61     }
62
63     private NodeConnectorId getNodeConnectorIdFromInterface(String interfaceName) {
64         GetNodeconnectorIdFromInterfaceInput input = new GetNodeconnectorIdFromInterfaceInputBuilder().setIntfName(interfaceName).build();
65         Future<RpcResult<GetNodeconnectorIdFromInterfaceOutput>> output =  serviceProvider.getInterfaceManager().getNodeconnectorIdFromInterface(input);
66         RpcResult<GetNodeconnectorIdFromInterfaceOutput> result = null;
67         try {
68              result = output.get();
69              if(result.isSuccessful()) {
70                  GetNodeconnectorIdFromInterfaceOutput ncIdOutput = result.getResult();
71                  return ncIdOutput.getNodeconnectorId();
72              }
73         } catch(ExecutionException | InterruptedException e) {
74             //TODO: Handle exception
75         }
76
77         return null;
78     }
79
80     private NodeId getNodeIdFromNodeConnectorId(NodeConnectorId ncId) {
81         return new NodeId(ncId.getValue().substring(0,ncId.getValue().lastIndexOf(":")));
82     }
83
84     protected byte[] getMacAddress(String interfaceName) {
85         InstanceIdentifier<NodeConnector> ncId = getNodeConnectorId(interfaceName);
86         if(ncId != null) {
87             String macAddress = inventoryReader.getMacAddress(ncId);
88             if(!Strings.isNullOrEmpty(macAddress)) {
89                 return AlivenessMonitorUtil.parseMacAddress(macAddress);
90             }
91         }
92         return null;
93     }
94
95     private InstanceIdentifier<Interface> getInterfaceIdentifier(InterfaceKey interfaceKey) {
96         InstanceIdentifier.InstanceIdentifierBuilder<Interface> interfaceInstanceIdentifierBuilder =
97                 InstanceIdentifier.builder(Interfaces.class).child(Interface.class, interfaceKey);
98         return interfaceInstanceIdentifierBuilder.build();
99     }
100
101     protected Interface getInterfaceFromConfigDS(String interfaceName) {
102         InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
103         InstanceIdentifier<Interface> interfaceId = getInterfaceIdentifier(interfaceKey);
104         Optional<Interface> interfaceOptional = read(LogicalDatastoreType.CONFIGURATION, interfaceId);
105         if (!interfaceOptional.isPresent()) {
106             return null;
107         }
108
109         return interfaceOptional.get();
110     }
111
112
113     private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
114             InstanceIdentifier<T> path) {
115
116         ReadOnlyTransaction tx = serviceProvider.getDataBroker().newReadOnlyTransaction();
117
118         Optional<T> result = Optional.absent();
119         try {
120             result = tx.read(datastoreType, path).get();
121         } catch (Exception e) {
122             throw new RuntimeException(e);
123         } finally {
124             tx.close();
125         }
126
127         return result;
128     }
129
130 }