2 * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.interfacemgr.commons;
10 import com.google.common.base.Optional;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
14 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.*;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.monitor.params.SourceBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.monitor.profile.create.input.Profile;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.monitor.profile.create.input.ProfileBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.monitor.start.input.ConfigBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.*;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.monitor.id.map.InterfaceMonitorId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.monitor.id.map.InterfaceMonitorIdBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.monitor.id.map.InterfaceMonitorIdKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.monitor.id._interface.map.MonitorIdInterface;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.monitor.id._interface.map.MonitorIdInterfaceBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.monitor.id._interface.map.MonitorIdInterfaceKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeVxlan;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.concurrent.ExecutionException;
39 import java.util.concurrent.Future;
41 public class AlivenessMonitorUtils {
43 private static final Logger LOG = LoggerFactory.getLogger(AlivenessMonitorUtils.class);
44 private static final long FAILURE_THRESHOLD = 4;
45 private static final long MONITORING_INTERVAL = 10000;
46 private static final long MONITORING_WINDOW = 4;
48 public static void startLLDPMonitoring(AlivenessMonitorService alivenessMonitorService, DataBroker dataBroker,
49 Interface trunkInterface) {
50 //LLDP monitoring for the trunk interface
51 String trunkInterfaceName = trunkInterface.getName();
52 IfTunnel ifTunnel = trunkInterface.getAugmentation(IfTunnel.class);
53 if(ifTunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeVxlan.class) && ifTunnel.isInternal()) {
54 MonitorStartInput lldpMonitorInput = new MonitorStartInputBuilder().setConfig(new ConfigBuilder()
55 .setSource(new SourceBuilder().setEndpointType(getInterfaceForMonitoring(trunkInterfaceName,
56 ifTunnel.getTunnelSource())).build())
57 .setMode(MonitoringMode.OneOne)
58 .setProfileId(allocateProfile(alivenessMonitorService, FAILURE_THRESHOLD, ifTunnel.getMonitorInterval(), MONITORING_WINDOW,
59 EtherTypes.Lldp)).build()).build();
61 Future<RpcResult<MonitorStartOutput>> result = alivenessMonitorService.monitorStart(lldpMonitorInput);
62 RpcResult<MonitorStartOutput> rpcResult = result.get();
64 if (rpcResult.isSuccessful()) {
65 monitorId = rpcResult.getResult().getMonitorId();
66 createOrUpdateInterfaceMonitorIdMap(dataBroker, trunkInterfaceName, monitorId);
67 createOrUpdateMonitorIdInterfaceMap(dataBroker, trunkInterfaceName, monitorId);
68 LOG.trace("Started LLDP monitoring with id {}", monitorId);
70 LOG.warn("RPC Call to start monitoring returned with Errors {}", rpcResult.getErrors());
72 } catch (InterruptedException | ExecutionException e) {
73 LOG.warn("Exception when starting monitoring", e);
78 public static void stopLLDPMonitoring(AlivenessMonitorService alivenessMonitorService, DataBroker dataBroker,
79 Interface trunkInterface) {
80 IfTunnel ifTunnel = trunkInterface.getAugmentation(IfTunnel.class);
81 if(!(ifTunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeVxlan.class)&& ifTunnel.isInternal())){
84 LOG.debug("stop LLDP monitoring for {}", trunkInterface.getName());
85 List<Long> monitorIds = getMonitorIdForInterface(dataBroker, trunkInterface.getName());
86 if (monitorIds == null) {
87 LOG.error("Monitor Id doesn't exist for Interface {}", trunkInterface);
90 for (Long monitorId : monitorIds) {
91 String interfaceName = getInterfaceFromMonitorId(dataBroker, monitorId);
92 if (interfaceName != null) {
93 MonitorStopInput input = new MonitorStopInputBuilder().setMonitorId(monitorId).build();
94 alivenessMonitorService.monitorStop(input);
95 removeMonitorIdInterfaceMap(dataBroker, monitorId);
96 removeMonitorIdFromInterfaceMonitorIdMap(dataBroker, interfaceName, monitorId);
102 public static String getInterfaceFromMonitorId(DataBroker broker, Long monitorId) {
103 InstanceIdentifier<MonitorIdInterface> id = InstanceIdentifier.builder(MonitorIdInterfaceMap.class).child(MonitorIdInterface.class, new MonitorIdInterfaceKey(monitorId)).build();
104 Optional<MonitorIdInterface> interfaceMonitorIdMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
105 if(interfaceMonitorIdMap.isPresent()) {
106 return interfaceMonitorIdMap.get().getInterfaceName();
111 private static void removeMonitorIdInterfaceMap(DataBroker broker, long monitorId) {
112 InstanceIdentifier<MonitorIdInterface> id = InstanceIdentifier.builder(MonitorIdInterfaceMap.class).child(MonitorIdInterface.class, new MonitorIdInterfaceKey(monitorId)).build();
113 Optional<MonitorIdInterface> monitorIdInterfaceMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
114 if(monitorIdInterfaceMap.isPresent()) {
115 MDSALUtil.syncDelete(broker, LogicalDatastoreType.OPERATIONAL, id);
119 private static void removeMonitorIdFromInterfaceMonitorIdMap(DataBroker broker, String infName, long monitorId) {
120 InstanceIdentifier<InterfaceMonitorId> id = InstanceIdentifier.builder(InterfaceMonitorIdMap.class).child(InterfaceMonitorId.class, new InterfaceMonitorIdKey(infName)).build();
121 Optional<InterfaceMonitorId> interfaceMonitorIdMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
122 if(interfaceMonitorIdMap.isPresent()) {
123 InterfaceMonitorId interfaceMonitorIdInstance = interfaceMonitorIdMap.get();
124 List<Long> existingMonitorIds = interfaceMonitorIdInstance.getMonitorId();
125 if (existingMonitorIds != null && existingMonitorIds.contains(monitorId)) {
126 existingMonitorIds.remove(monitorId);
127 InterfaceMonitorIdBuilder interfaceMonitorIdBuilder = new InterfaceMonitorIdBuilder();
128 interfaceMonitorIdInstance = interfaceMonitorIdBuilder.setKey(new InterfaceMonitorIdKey(infName)).setMonitorId(existingMonitorIds).build();
129 MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, id, interfaceMonitorIdInstance);
134 private static org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.endpoint.endpoint.type.Interface getInterfaceForMonitoring(String interfaceName, org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress ipAddress) {
135 return new org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.endpoint.
136 endpoint.type.InterfaceBuilder().setInterfaceIp(ipAddress).setInterfaceName(interfaceName).build();
139 public static void handleTunnelMonitorUpdates(AlivenessMonitorService alivenessMonitorService, DataBroker dataBroker,
140 Interface interfaceOld, Interface interfaceNew) {
141 String interfaceName = interfaceNew.getName();
142 IfTunnel ifTunnelNew = interfaceNew.getAugmentation(IfTunnel.class);
143 if(!(ifTunnelNew.getTunnelInterfaceType().isAssignableFrom(TunnelTypeVxlan.class)&&
144 ifTunnelNew.isInternal())){
147 LOG.debug("handling tunnel monitoring updates for interface {}", interfaceName);
149 stopLLDPMonitoring(alivenessMonitorService, dataBroker, interfaceOld);
150 if(ifTunnelNew.isMonitorEnabled()) {
151 startLLDPMonitoring(alivenessMonitorService, dataBroker, interfaceNew);
153 // Delete old profile from Aliveness Manager
154 IfTunnel ifTunnelOld = interfaceOld.getAugmentation(IfTunnel.class);
155 if(ifTunnelNew.getMonitorInterval() != ifTunnelOld.getMonitorInterval()) {
156 LOG.debug("deleting older monitor profile for interface {}", interfaceName);
157 long profileId = allocateProfile(alivenessMonitorService, FAILURE_THRESHOLD, ifTunnelOld.getMonitorInterval(), MONITORING_WINDOW, EtherTypes.Lldp);
158 MonitorProfileDeleteInput profileDeleteInput = new MonitorProfileDeleteInputBuilder().setProfileId(profileId).build();
159 alivenessMonitorService.monitorProfileDelete(profileDeleteInput);
165 public static void createOrUpdateInterfaceMonitorIdMap(DataBroker broker, String infName, long monitorId) {
166 InterfaceMonitorId interfaceMonitorIdInstance;
167 List<Long> existingMonitorIds;
168 InterfaceMonitorIdBuilder interfaceMonitorIdBuilder = new InterfaceMonitorIdBuilder();
169 InstanceIdentifier<InterfaceMonitorId> id = InstanceIdentifier.builder(InterfaceMonitorIdMap.class).child(InterfaceMonitorId.class, new InterfaceMonitorIdKey(infName)).build();
170 Optional<InterfaceMonitorId> interfaceMonitorIdMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
171 if (interfaceMonitorIdMap.isPresent()) {
172 interfaceMonitorIdInstance = interfaceMonitorIdMap.get();
173 existingMonitorIds = interfaceMonitorIdInstance.getMonitorId();
174 if (existingMonitorIds == null) {
175 existingMonitorIds = new ArrayList<>();
177 if (!existingMonitorIds.contains(monitorId)) {
178 existingMonitorIds.add(monitorId);
179 interfaceMonitorIdInstance = interfaceMonitorIdBuilder.setKey(new InterfaceMonitorIdKey(infName)).setMonitorId(existingMonitorIds).build();
180 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, id, interfaceMonitorIdInstance);
183 existingMonitorIds = new ArrayList<>();
184 existingMonitorIds.add(monitorId);
185 interfaceMonitorIdInstance = interfaceMonitorIdBuilder.setMonitorId(existingMonitorIds).setKey(new InterfaceMonitorIdKey(infName)).setInterfaceName(infName).build();
186 MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, id, interfaceMonitorIdInstance);
190 public static void createOrUpdateMonitorIdInterfaceMap(DataBroker broker,String infName, long monitorId) {
191 MonitorIdInterface monitorIdInterfaceInstance;
192 String existinginterfaceName;
193 MonitorIdInterfaceBuilder monitorIdInterfaceBuilder = new MonitorIdInterfaceBuilder();
194 InstanceIdentifier<MonitorIdInterface> id = InstanceIdentifier.builder(MonitorIdInterfaceMap.class).child(MonitorIdInterface.class, new MonitorIdInterfaceKey(monitorId)).build();
195 Optional<MonitorIdInterface> monitorIdInterfaceMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
196 if(monitorIdInterfaceMap.isPresent()) {
197 monitorIdInterfaceInstance = monitorIdInterfaceMap.get();
198 existinginterfaceName = monitorIdInterfaceInstance.getInterfaceName();
199 if(!existinginterfaceName.equals(infName)) {
200 monitorIdInterfaceInstance = monitorIdInterfaceBuilder.setKey(new MonitorIdInterfaceKey(monitorId)).setInterfaceName(infName).build();
201 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, id, monitorIdInterfaceInstance);
204 monitorIdInterfaceInstance = monitorIdInterfaceBuilder.setMonitorId(monitorId).setKey(new MonitorIdInterfaceKey(monitorId)).setInterfaceName(infName).build();
205 MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, id, monitorIdInterfaceInstance);
209 public static List<Long> getMonitorIdForInterface(DataBroker broker, String infName) {
210 InstanceIdentifier<InterfaceMonitorId> id = InstanceIdentifier.builder(InterfaceMonitorIdMap.class).child(InterfaceMonitorId.class, new InterfaceMonitorIdKey(infName)).build();
211 Optional<InterfaceMonitorId> interfaceMonitorIdMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
212 if(interfaceMonitorIdMap.isPresent()) {
213 return interfaceMonitorIdMap.get().getMonitorId();
218 public static long createMonitorProfile(AlivenessMonitorService alivenessMonitor, MonitorProfileCreateInput monitorProfileCreateInput) {
220 Future<RpcResult<MonitorProfileCreateOutput>> result = alivenessMonitor.monitorProfileCreate(monitorProfileCreateInput);
221 RpcResult<MonitorProfileCreateOutput> rpcResult = result.get();
222 if(rpcResult.isSuccessful()) {
223 return rpcResult.getResult().getProfileId();
225 LOG.warn("RPC Call to Get Profile Id Id returned with Errors {}.. Trying to fetch existing profile ID", rpcResult.getErrors());
227 Profile createProfile = monitorProfileCreateInput.getProfile();
228 Future<RpcResult<MonitorProfileGetOutput>> existingProfile = alivenessMonitor.monitorProfileGet(buildMonitorGetProfile(createProfile.getMonitorInterval(), createProfile.getMonitorWindow(), createProfile.getFailureThreshold(), createProfile.getProtocolType()));
229 RpcResult<MonitorProfileGetOutput> rpcGetResult = existingProfile.get();
230 if(rpcGetResult.isSuccessful()){
231 return rpcGetResult.getResult().getProfileId();
233 LOG.warn("RPC Call to Get Existing Profile Id returned with Errors {}", rpcGetResult.getErrors());
236 LOG.warn("Exception when getting existing profile",e);
239 } catch (InterruptedException | ExecutionException e) {
240 LOG.warn("Exception when allocating profile Id",e);
245 private static MonitorProfileGetInput buildMonitorGetProfile(long monitorInterval, long monitorWindow, long failureThreshold, EtherTypes protocolType){
246 MonitorProfileGetInputBuilder buildGetProfile = new MonitorProfileGetInputBuilder();
247 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();
248 profileBuilder.setFailureThreshold(failureThreshold);
249 profileBuilder.setMonitorInterval(monitorInterval);
250 profileBuilder.setMonitorWindow(monitorWindow);
251 profileBuilder.setProtocolType(protocolType);
252 buildGetProfile.setProfile(profileBuilder.build());
253 return (buildGetProfile.build());
256 public static long allocateProfile(AlivenessMonitorService alivenessMonitor, long FAILURE_THRESHOLD, long MONITORING_INTERVAL,
257 long MONITORING_WINDOW, EtherTypes etherTypes) {
258 MonitorProfileCreateInput input = new MonitorProfileCreateInputBuilder().
259 setProfile(new ProfileBuilder().setFailureThreshold(FAILURE_THRESHOLD)
260 .setMonitorInterval(MONITORING_INTERVAL).setMonitorWindow(MONITORING_WINDOW).
261 setProtocolType(etherTypes).build()).build();
262 return createMonitorProfile(alivenessMonitor, input);
265 public static long allocateDefaultProfile(AlivenessMonitorService alivenessMonitor, EtherTypes etherType ) {
266 MonitorProfileCreateInput input = new MonitorProfileCreateInputBuilder().
267 setProfile(getDefaultMonitorProfile(etherType)).build();
268 return createMonitorProfile(alivenessMonitor, input);
271 public static Profile getDefaultMonitorProfile(EtherTypes etherType) {
272 ProfileBuilder profileBuilder = new ProfileBuilder();
273 profileBuilder.setProtocolType(etherType);
274 profileBuilder.setFailureThreshold(FAILURE_THRESHOLD)
275 .setMonitorInterval(MONITORING_INTERVAL).setMonitorWindow(MONITORING_WINDOW).setProtocolType(etherType);
276 return profileBuilder.build();