Added code for cluster aware behaviour to LispMappingService.
Change-Id: I9e6cd6e806dd1e2c3656a3b4ed3b973a85868f68
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.opendaylight.controller.md.sal.binding.api.NotificationService;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
+import org.opendaylight.lispflowmapping.clustering.ClusterNodeModulSwitcherImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LispMappingService implements IFlowMapping, IMapRequestResultHandler,
IMapNotifyHandler, OdlLispProtoListener, AutoCloseable {
protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
+ private final ClusterNodeModulSwitcherImpl clusterNodeModulSwitcher;
private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
public LispMappingService(final NotificationService notificationService,
final IMappingService mappingService,
- final OdlLispSbService odlLispService) {
+ final OdlLispSbService odlLispService, final EntityOwnershipService entityOnwershipService) {
this.notificationService = notificationService;
this.mapService = mappingService;
this.lispSB = odlLispService;
+ clusterNodeModulSwitcher = new ClusterNodeModulSwitcherImpl(entityOnwershipService);
LOG.debug("LispMappingService Module constructed!");
}
public void initialize() {
mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
- mapServer = new MapServer(mapService, smr, this, notificationService);
+ mapServer = new MapServer(mapService, smr, this, notificationService, clusterNodeModulSwitcher);
LOG.info("LISP (RFC6830) Mapping Service init finished");
}
public void basicInit() {
mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
- mapServer = new MapServer(mapService, smr, this, notificationService);
+ mapServer = new MapServer(mapService, smr, this, notificationService, clusterNodeModulSwitcher);
}
public MapReply handleMapRequest(MapRequest request) {
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.opendaylight.controller.md.sal.binding.api.NotificationService;
+import org.opendaylight.lispflowmapping.clustering.ClusterNodeModulSwitcherImpl;
import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChanged;
import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceListener;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MapServer implements IMapServerAsync, OdlMappingserviceListener {
protected static final Logger LOG = LoggerFactory.getLogger(MapServer.class);
+ private final ClusterNodeModulSwitcherImpl clusterNodeModulSwitcher;
private IMappingService mapService;
private boolean subscriptionService;
private IMapNotifyHandler notifyHandler;
private NotificationService notificationService;
+ private ListenerRegistration<MapServer> mapServerListenerRegistration;
public MapServer(IMappingService mapService, boolean subscriptionService,
- IMapNotifyHandler notifyHandler, NotificationService notificationService) {
+ IMapNotifyHandler notifyHandler, NotificationService notificationService,
+ final ClusterNodeModulSwitcherImpl clusterNodeModulSwitcher) {
Preconditions.checkNotNull(mapService);
this.mapService = mapService;
this.subscriptionService = subscriptionService;
this.notifyHandler = notifyHandler;
this.notificationService = notificationService;
if (notificationService != null) {
- notificationService.registerNotificationListener(this);
+ mapServerListenerRegistration = notificationService.registerNotificationListener(this);
}
+ this.clusterNodeModulSwitcher = clusterNodeModulSwitcher;
}
@Override
@Override
public void onMappingChanged(MappingChanged notification) {
if (subscriptionService) {
- sendSmrs(notification.getMappingRecord(), getSubscribers(notification.getMappingRecord().getEid()));
+ if (clusterNodeModulSwitcher.isMaster()) {
+ sendSmrs(notification.getMappingRecord(), getSubscribers(notification.getMappingRecord().getEid()));
+ }
if (notification.getChangeType().equals(MappingChange.Removed)) {
removeSubscribers(notification.getMappingRecord().getEid());
}
interface="org.opendaylight.controller.md.sal.binding.api.NotificationService" />
<reference id="lispDAO"
interface="org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO" />
+ <reference id="entityOwnershipService"
+ interface="org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService" />
<odl:rpc-service id="odlLispSbService"
interface="org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService" />
<argument ref="notificationService" />
<argument ref="mappingService" />
<argument ref="odlLispSbService" />
+ <argument ref="entityOwnershipService" />
</bean>
<service ref="lispMappingService"
interface="org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping"
interface="org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler"
odl:type="default" />
<odl:notification-listener ref="lispMappingService" />
-</blueprint>
\ No newline at end of file
+</blueprint>
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
+import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import java.lang.reflect.Field;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.opendaylight.controller.md.sal.binding.api.NotificationService;
+import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
private final NotificationService notificationService = Mockito.mock(NotificationService.class);
private final IMappingService mappingService = Mockito.mock(IMappingService.class);
private final OdlLispSbService odlLispSbService = Mockito.mock(OdlLispSbService.class);
+ private final EntityOwnershipService entityOwnershipSrvice = Mockito.mock(EntityOwnershipService.class);
@InjectMocks
private LispMappingService lispMappingService = new LispMappingService(
notificationService,
mappingService,
- odlLispSbService);
+ odlLispSbService, entityOwnershipSrvice);
private static final byte[] IPV4_BYTES_1 = new byte[] {1, 2, 3, 0};
private static final byte[] IPV4_BYTES_2 = new byte[] {1, 2, 4, 0};
Mockito.when(mapRegister.getMappingRecordItem())
.thenReturn(Lists.newArrayList(MAPPING_RECORD_ITEM_BUILDER.build()));
Mockito.when(tlsMapNotifyMock.get()).thenReturn(getDefaultMapNotifyPair());
+ Mockito.when(entityOwnershipSrvice.getOwnershipState(Mockito.any(Entity.class))).thenReturn(Optional.of
+ (new EntityOwnershipState(true, true)));
lispMappingService.onAddMapping(addMapping);
Mockito.verify(odlLispSbService, Mockito.times(2)).sendMapNotify(Mockito.argThat(new TransportAddressMatch()));
.setMapNotify(new org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105
.mapnotifymessage.MapNotifyBuilder().setKeyId((short) 1).build())
.setTransportAddress(TRANSPORT_ADDRESS);
+ Mockito.when(entityOwnershipSrvice.getOwnershipState(Mockito.any(Entity.class))).thenReturn(Optional.of
+ (new EntityOwnershipState(true, true)));
lispMappingService.onAddMapping(addMapping);
Mockito.verify(odlLispSbService).sendMapNotify(smnib.build());
Mockito.when(requestMapping.getTransportAddress()).thenReturn(TRANSPORT_ADDRESS_1);
Mockito.when(mapRequest.getEidItem()).thenReturn(Lists.newArrayList(EID_ITEM_BUILDER.build()));
Mockito.when(tlsMapReplyMock.get()).thenReturn(mapReply);
+ Mockito.when(entityOwnershipSrvice.getOwnershipState(Mockito.any(Entity.class))).thenReturn(Optional.of
+ (new EntityOwnershipState(true, true)));
// result
final SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder()
Mockito.when(mapRequest.getEidItem()).thenReturn(Lists.newArrayList(EID_ITEM_BUILDER.build()));
Mockito.when(tlsMapReplyMock.get()).thenReturn(null);
+ Mockito.when(entityOwnershipSrvice.getOwnershipState(Mockito.any(Entity.class))).thenReturn(Optional.of
+ (new EntityOwnershipState(true, true)));
lispMappingService.onRequestMapping(requestMapping);
Mockito.verifyZeroInteractions(odlLispSbService);
}
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.opendaylight.controller.md.sal.binding.api.NotificationService;
+import org.opendaylight.lispflowmapping.clustering.ClusterNodeModulSwitcherImpl;
import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
@Mock private static IMappingService mapService;
@Mock private static IMapNotifyHandler notifyHandler;
@Mock private static NotificationService notificationService;
+ @Mock private static ClusterNodeModulSwitcherImpl clusterNodeModulSwitcher;
+
@Spy private static Set<SubscriberRLOC> subscriberSetMock_1 = new HashSet<>();
@Spy private static Set<SubscriberRLOC> subscriberSetMock_2 = new HashSet<>();
@Spy private static Set<SubscriberRLOC> subscriberSetMock_3 = new HashSet<>();
@Before
public void init() throws NoSuchFieldException, IllegalAccessException {
- mapServer = new MapServer(mapService, true, notifyHandler, notificationService);
+ mapServer = new MapServer(mapService, true, notifyHandler, notificationService, clusterNodeModulSwitcher);
subscriberSetMock_1.add(SUBSCRIBER_RLOC_1);
subscriberSetMock_1.add(SUBSCRIBER_RLOC_2);
subscriberSetMock_2.add(SUBSCRIBER_RLOC_3);