Tunnel monitoring changes 50/37650/5
authorKiran N Upadhyaya <kiran.n.upadhyaya@ericsson.com>
Mon, 4 Apr 2016 13:22:12 +0000 (18:52 +0530)
committerVishal Thapar <vishal.thapar@ericsson.com>
Tue, 26 Apr 2016 17:08:53 +0000 (17:08 +0000)
Code revision in Aliveness Monitor files related to fetching mac address
from data store.
Monitor profile creation for both the tunnel endpoints was fixed.
Changes in JUNIT test cases related to tunnel monitoring.

Change-Id: I0843ee855f854a3fd98d212516292d8b34a8409c
Signed-off-by: Kiran N Upadhyaya <kiran.n.upadhyaya@ericsson.com>
alivenessmonitor/alivenessmonitor-api/src/main/yang/aliveness-monitor.yang
alivenessmonitor/alivenessmonitor-impl/src/main/java/org/opendaylight/vpnservice/alivenessmonitor/internal/AbstractAlivenessProtocolHandler.java
alivenessmonitor/alivenessmonitor-impl/src/main/java/org/opendaylight/vpnservice/alivenessmonitor/internal/AlivenessMonitor.java
alivenessmonitor/alivenessmonitor-impl/src/main/java/org/opendaylight/vpnservice/alivenessmonitor/internal/AlivenessProtocolHandlerLLDP.java
interfacemgr/interfacemgr-impl/src/main/java/org/opendaylight/vpnservice/interfacemgr/commons/AlivenessMonitorUtils.java
interfacemgr/interfacemgr-impl/src/test/java/org/opendaylight/vpnservice/interfacemgr/test/InterfaceManagerTestUtil.java

index 134288f6542e598977e164edf30d248c4c28f71e..3d521bfbb07ff452abfbddf1a430fe98e95fdd4a 100644 (file)
@@ -65,6 +65,17 @@ module aliveness-monitor {
         }
     }
 
+    rpc monitor-profile-get {
+            input {
+                container profile {
+                    uses monitor-profile-params;
+                }
+            }
+            output {
+                leaf profile-id { type uint32; }
+            }
+        }
+
     rpc monitor-start {
         input {
             container config {
index 9537a9f66147df651f4658b4e698ef3c8f4002fa..bfa2565462b1cd9721450401d7c96563784e4967 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -13,6 +13,7 @@ import java.util.concurrent.Future;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetNodeconnectorIdFromInterfaceInputBuilder;
@@ -81,13 +82,22 @@ abstract class AbstractAlivenessProtocolHandler implements AlivenessProtocolHand
         return new NodeId(ncId.getValue().substring(0,ncId.getValue().lastIndexOf(":")));
     }
 
-    protected byte[] getMacAddress(String interfaceName) {
-        InstanceIdentifier<NodeConnector> ncId = getNodeConnectorId(interfaceName);
-        if(ncId != null) {
-            String macAddress = inventoryReader.getMacAddress(ncId);
-            if(!Strings.isNullOrEmpty(macAddress)) {
-                return AlivenessMonitorUtil.parseMacAddress(macAddress);
-            }
+    protected org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceFromOperDS(String interfaceName){
+        InstanceIdentifier.InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> idBuilder =
+                InstanceIdentifier.builder(InterfacesState.class).child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.class,
+                        new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey(interfaceName));
+        InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> id = idBuilder.build();
+        Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> interfaceStateOptional = read(LogicalDatastoreType.OPERATIONAL, id);
+        if(interfaceStateOptional.isPresent()) {
+            return interfaceStateOptional.get();
+        }
+        return null;
+    }
+
+    protected byte[] getMacAddress(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState, String interfaceName) {
+        String macAddress = interfaceState.getPhysAddress().getValue();
+        if(!Strings.isNullOrEmpty(macAddress)) {
+            return AlivenessMonitorUtil.parseMacAddress(macAddress);
         }
         return null;
     }
index a00c7a64387fd4533b57f01978b6061e19e69a75..fb24cdaf64b3ced33c6c2695899819365ada8bc4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -52,6 +52,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.MonitorStopInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.MonitorUnpauseInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.MonitoringMode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.MonitorProfileGetInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.MonitorProfileGetOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.MonitorProfileGetOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629._interface.monitor.map.InterfaceMonitorEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629._interface.monitor.map.InterfaceMonitorEntryBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629._interface.monitor.map.InterfaceMonitorEntryKey;
@@ -555,10 +558,13 @@ public class AlivenessMonitor implements AlivenessMonitorService, PacketProcessi
                 WriteTransaction tx = broker.newWriteOnlyTransaction();
 
                 tx.put(LogicalDatastoreType.OPERATIONAL, getMonitoringInfoId(monitorId), monitoringInfo, CREATE_MISSING_PARENT);
+                LOG.debug("adding oper monitoring info {}", monitoringInfo);
 
                 tx.put(LogicalDatastoreType.OPERATIONAL, getMonitorStateId(monitoringKey), monitoringState, CREATE_MISSING_PARENT);
+                LOG.debug("adding oper monitoring state {}", monitoringState);
 
                 tx.put(LogicalDatastoreType.OPERATIONAL, getMonitorMapId(monitorId), mapEntry, CREATE_MISSING_PARENT);
+                LOG.debug("adding oper map entry {}", mapEntry);
 
                 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
                     @Override
@@ -970,6 +976,36 @@ public class AlivenessMonitor implements AlivenessMonitorService, PacketProcessi
         return result;
     }
 
+
+    @Override
+    public Future<RpcResult<MonitorProfileGetOutput>> monitorProfileGet(MonitorProfileGetInput input){
+        LOG.debug("Monitor Profile Get operation for input profile- {}", input.getProfile());
+        RpcResultBuilder<MonitorProfileGetOutput> rpcResultBuilder;
+        try{
+            final Long profileId = getExistingProfileId(input);
+
+            MonitorProfileGetOutputBuilder output = new MonitorProfileGetOutputBuilder().setProfileId(profileId);
+            rpcResultBuilder = RpcResultBuilder.success();
+            rpcResultBuilder.withResult(output.build());
+        }catch(Exception e){
+            LOG.error("Retrieval of monitor profile ID for input {} failed due to {}" , input, e);
+            rpcResultBuilder = RpcResultBuilder.failed();
+        }
+        return Futures.immediateFuture(rpcResultBuilder.build());
+    }
+
+    private Long getExistingProfileId(MonitorProfileGetInput input){
+        org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.monitor.profile.get.input.Profile profile = input.getProfile();
+        final Long failureThreshold = profile.getFailureThreshold();
+        final Long monitorInterval = profile.getMonitorInterval();
+        final Long monitorWindow = profile.getMonitorWindow();
+        final EtherTypes ethType = profile.getProtocolType();
+        LOG.debug("getExistingProfileId for profile : {}", input.getProfile());
+        String idKey = getUniqueProfileKey(failureThreshold, monitorInterval, monitorWindow, ethType);
+        LOG.debug("Obtained existing profile ID for profile : {}", input.getProfile());
+        return (Long.valueOf(getUniqueId(idKey)));
+    }
+
     private String getUniqueProfileKey(Long failureThreshold,Long monitorInterval,Long monitorWindow,EtherTypes ethType) {
         return new StringBuilder().append(failureThreshold).append(AlivenessMonitorConstants.SEPERATOR)
                                   .append(monitorInterval).append(AlivenessMonitorConstants.SEPERATOR)
index c1c045a474e65c94728dc7d48e57c6e7452316b7..1a4a42b77919d501a6384c20253ea8ec16c89772 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -23,6 +23,7 @@ import org.opendaylight.controller.liblldp.LLDPTLV;
 import org.opendaylight.controller.liblldp.LLDPTLV.TLVType;
 import org.opendaylight.controller.liblldp.Packet;
 import org.opendaylight.controller.liblldp.PacketException;
+import org.opendaylight.vpnservice.interfacemgr.globals.IfmConstants;
 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
 import org.opendaylight.vpnservice.mdsalutil.ActionType;
 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
@@ -139,7 +140,8 @@ public class AlivenessProtocolHandlerLLDP extends AbstractAlivenessProtocolHandl
         }
 
         //Get Mac Address for the source interface
-        byte[] sourceMac = getMacAddress(sourceInterface);
+        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState = getInterfaceFromOperDS(sourceInterface);
+        byte[] sourceMac = getMacAddress(interfaceState, sourceInterface);
         if(sourceMac == null) {
             LOG.error("Could not read mac address for the source interface {} from the Inventory. "
                     + "LLDP packet cannot be send.", sourceInterface);
@@ -150,26 +152,19 @@ public class AlivenessProtocolHandlerLLDP extends AbstractAlivenessProtocolHandl
 
         long nodeId = -1, portNum = -1;
         try {
-            GetPortFromInterfaceInput input = new GetPortFromInterfaceInputBuilder().setIntfName(sourceInterface).build();
-            Future<RpcResult<GetPortFromInterfaceOutput>> portOutput = interfaceService.getPortFromInterface(input);
-            RpcResult<GetPortFromInterfaceOutput> result = portOutput.get();
-            if(result.isSuccessful()) {
-                GetPortFromInterfaceOutput output = result.getResult();
-                nodeId = output.getDpid().longValue();
-                portNum = output.getPortno();
-            } else {
-                LOG.error("Could not retrieve port details for interface {}", sourceInterface);
-                return;
-            }
-        }catch(InterruptedException | ExecutionException e) {
-            LOG.error("Failed to retrieve interface service RPC Result ", e);
+            String lowerLayerIf = interfaceState.getLowerLayerIf().get(0);
+            NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
+            nodeId = Long.valueOf(getDpnFromNodeConnectorId(nodeConnectorId));
+            portNum = Long.valueOf(getPortNoFromNodeConnectorId(nodeConnectorId));
+        }catch(Exception e) {
+            LOG.error("Failed to retrieve node id and port number ", e);
             return;
         }
 
         Ethernet ethenetLLDPPacket = makeLLDPPacket(Long.toString(nodeId), portNum, 0, sourceMac, sourceInterface);
 
         try {
-            List<ActionInfo> actions = getInterfaceActions(sourceInterface, portNum);
+            List<ActionInfo> actions = getInterfaceActions(interfaceState, portNum);
             if(actions.isEmpty()) {
                 LOG.error("No interface actions to send packet out over interface {}", sourceInterface);
                 return;
@@ -182,14 +177,27 @@ public class AlivenessProtocolHandlerLLDP extends AbstractAlivenessProtocolHandl
         }
     }
 
-    private List<ActionInfo> getInterfaceActions(String interfaceName, long portNum) throws InterruptedException, ExecutionException {
+    public static String getDpnFromNodeConnectorId(NodeConnectorId portId) {
+        /*
+         * NodeConnectorId is of form 'openflow:dpnid:portnum'
+         */
+        String[] split = portId.getValue().split(IfmConstants.OF_URI_SEPARATOR);
+        return split[1];
+    }
+
+    public static String getPortNoFromNodeConnectorId(NodeConnectorId portId) {
+        /*
+         * NodeConnectorId is of form 'openflow:dpnid:portnum'
+         */
+        String[] split = portId.getValue().split(IfmConstants.OF_URI_SEPARATOR);
+        return split[2];
+    }
+    private List<ActionInfo> getInterfaceActions(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState,long portNum) throws InterruptedException, ExecutionException {
         Class<? extends InterfaceType> intfType;
-        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface interfaceInfo =
-                                                                                                     getInterfaceFromConfigDS(interfaceName);
-        if(interfaceInfo != null) {
-            intfType = interfaceInfo.getType();
+        if(interfaceState != null) {
+            intfType = interfaceState.getType();
         } else {
-            LOG.error("Could not retrieve port type for interface {} to construct actions", interfaceName);
+            LOG.error("Could not retrieve port type for interface {} to construct actions", interfaceState.getName());
             return Collections.emptyList();
         }
 
index 12992dc9717a74078e44760d47c47d0ed496c507..36d26ff99ffa2e78af87f6654fe64d3d800195e2 100644 (file)
@@ -224,13 +224,37 @@ public class AlivenessMonitorUtils {
             if(rpcResult.isSuccessful()) {
                 return rpcResult.getResult().getProfileId();
             } else {
-                LOG.warn("RPC Call to Get Profile Id Id returned with Errors {}", rpcResult.getErrors());
+                LOG.warn("RPC Call to Get Profile Id Id returned with Errors {}.. Trying to fetch existing profile ID", rpcResult.getErrors());
+                try{
+                    Profile createProfile = monitorProfileCreateInput.getProfile();
+                    Future<RpcResult<MonitorProfileGetOutput>> existingProfile = alivenessMonitor.monitorProfileGet(buildMonitorGetProfile(createProfile.getMonitorInterval(), createProfile.getMonitorWindow(), createProfile.getFailureThreshold(), createProfile.getProtocolType()));
+                    RpcResult<MonitorProfileGetOutput> rpcGetResult = existingProfile.get();
+                    if(rpcGetResult.isSuccessful()){
+                        return rpcGetResult.getResult().getProfileId();
+                    }else{
+                        LOG.warn("RPC Call to Get Existing Profile Id returned with Errors {}", rpcGetResult.getErrors());
+                    }
+                }catch(Exception e){
+                    LOG.warn("Exception when getting existing profile",e);
+                }
             }
         } catch (InterruptedException | ExecutionException e) {
             LOG.warn("Exception when allocating profile Id",e);
         }
         return 0;
     }
+
+    private static MonitorProfileGetInput buildMonitorGetProfile(long monitorInterval, long monitorWindow, long failureThreshold, EtherTypes protocolType){
+        MonitorProfileGetInputBuilder buildGetProfile = new MonitorProfileGetInputBuilder();
+        org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.monitor.profile.get.input.ProfileBuilder profileBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.monitor.profile.get.input.ProfileBuilder();
+        profileBuilder.setFailureThreshold(failureThreshold);
+        profileBuilder.setMonitorInterval(monitorInterval);
+        profileBuilder.setMonitorWindow(monitorWindow);
+        profileBuilder.setProtocolType(protocolType);
+        buildGetProfile.setProfile(profileBuilder.build());
+        return (buildGetProfile.build());
+    };
+
     public static long allocateProfile(AlivenessMonitorService alivenessMonitor, long FAILURE_THRESHOLD, long MONITORING_INTERVAL,
                                               long MONITORING_WINDOW, EtherTypes etherTypes) {
         MonitorProfileCreateInput input = new MonitorProfileCreateInputBuilder().
index 28bb764393cf92e733afc7e81017934671575988..c3ca61c957f319f12626c359c04a98ad7f86e924 100644 (file)
@@ -170,7 +170,7 @@ public class InterfaceManagerTestUtil {
         IpAddress remoteIp = new IpAddress(Ipv4Address.getDefaultInstance(remoteIpStr));
         IpAddress localIp =  new IpAddress(Ipv4Address.getDefaultInstance(localIpStr));
         IfTunnel tunnel = new IfTunnelBuilder().setTunnelDestination(remoteIp).setTunnelGateway(localIp).setTunnelSource(localIp)
-                    .setTunnelInterfaceType( tunType).setInternal(true).build();
+                    .setTunnelInterfaceType( tunType).setInternal(true).setMonitorEnabled(false).build();
         builder.addAugmentation(IfTunnel.class, tunnel);
         return builder.build();
     }