[ONOS-5607] Add AbstractLispRouter base class with impl classes
Change-Id: I7b79301db684b7f15307dc26be0c6e665399b01d
diff --git a/protocols/lisp/api/src/main/java/org/onosproject/lisp/ctl/AbstractLispRouter.java b/protocols/lisp/api/src/main/java/org/onosproject/lisp/ctl/AbstractLispRouter.java
new file mode 100644
index 0000000..d8e24c4
--- /dev/null
+++ b/protocols/lisp/api/src/main/java/org/onosproject/lisp/ctl/AbstractLispRouter.java
@@ -0,0 +1,165 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.lisp.ctl;
+
+import io.netty.channel.Channel;
+import org.onlab.packet.IpAddress;
+import org.onosproject.lisp.msg.protocols.LispMessage;
+import org.onosproject.net.Device;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+
+/**
+ * An abstract representation of a LISP router.
+ * This class can be extended by others to serve as a base for their vendor
+ * specific representation of a router.
+ */
+public abstract class AbstractLispRouter implements LispRouter {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ private static final String DROP_MESSAGE_WARN =
+ "Drop message {} destined to router {} as channel is closed.";
+
+ private Channel channel;
+ private String channelId;
+
+ private boolean connected;
+ private boolean subscribed;
+ private LispRouterId routerId;
+ private LispRouterAgent agent;
+
+ /**
+ * A default constructor.
+ *
+ * @param routerId router identifier
+ */
+ AbstractLispRouter(LispRouterId routerId) {
+ this.routerId = routerId;
+ }
+
+ @Override
+ public final String channelId() {
+ return channelId;
+ }
+
+ @Override
+ public final IpAddress routerId() {
+ return routerId.id();
+ }
+
+ @Override
+ public final String stringId() {
+ return routerId.toString();
+ }
+
+ @Override
+ public final void setChannel(Channel channel) {
+ this.channel = channel;
+ final SocketAddress address = channel.remoteAddress();
+ if (address instanceof InetSocketAddress) {
+ final InetSocketAddress inetAddress = (InetSocketAddress) address;
+ final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress());
+ if (ipAddress.isIp4()) {
+ channelId = ipAddress.toString() + ':' + inetAddress.getPort();
+ } else {
+ channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort();
+ }
+ }
+ }
+
+ @Override
+ public final void setConnected(boolean connected) {
+ this.connected = connected;
+ }
+
+ @Override
+ public final boolean isConnected() {
+ return connected;
+ }
+
+
+ @Override
+ public final boolean isSubscribed() {
+ return subscribed;
+ }
+
+ @Override
+ public final void setSubscribed(boolean subscribed) {
+ this.subscribed = subscribed;
+ }
+
+ @Override
+ public final void setAgent(LispRouterAgent agent) {
+ // we never assign the agent more than one time
+ if (this.agent == null) {
+ this.agent = agent;
+ }
+ }
+
+ @Override
+ public final Device.Type deviceType() {
+ return Device.Type.ROUTER;
+ }
+
+ @Override
+ public void sendMessage(LispMessage message) {
+ if (channel.isOpen()) {
+ // TODO: need to consider to use writeAndFlush if possible
+ channel.write(message);
+ agent.processDownstreamMessage(routerId, message);
+ } else {
+ log.warn(DROP_MESSAGE_WARN, message, routerId);
+ }
+ }
+
+ @Override
+ public void handleMessage(LispMessage message) {
+ this.agent.processUpstreamMessage(routerId, message);
+ }
+
+ @Override
+ public final boolean connectRouter() {
+ return this.agent.addConnectedRouter(routerId, this);
+ }
+
+ @Override
+ public final void disconnectRouter() {
+ setConnected(false);
+ channel.close();
+ }
+
+ @Override
+ public String toString() {
+
+ StringBuilder sb = new StringBuilder();
+ sb.append(this.getClass().getName());
+
+ String address = (channel != null) ? channel.remoteAddress().toString() : "?";
+ String routerId = (stringId() != null) ? stringId() : "?";
+
+ sb.append(" [");
+ sb.append(address);
+ sb.append(" routerId[");
+ sb.append(routerId);
+ sb.append("]]");
+
+ return sb.toString();
+ }
+}
diff --git a/protocols/lisp/api/src/main/java/org/onosproject/lisp/ctl/DefaultLispRouter.java b/protocols/lisp/api/src/main/java/org/onosproject/lisp/ctl/DefaultLispRouter.java
new file mode 100644
index 0000000..05b3662
--- /dev/null
+++ b/protocols/lisp/api/src/main/java/org/onosproject/lisp/ctl/DefaultLispRouter.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.lisp.ctl;
+
+/**
+ * A default implementation LispRouter class.
+ */
+public class DefaultLispRouter extends AbstractLispRouter implements LispRouter {
+
+ /**
+ * A default constructor.
+ *
+ * @param routerId router identifier
+ */
+ DefaultLispRouter(LispRouterId routerId) {
+ super(routerId);
+ }
+}
diff --git a/protocols/lisp/api/src/test/java/org/onosproject/lisp/ctl/LispRouterAdapter.java b/protocols/lisp/api/src/test/java/org/onosproject/lisp/ctl/LispRouterAdapter.java
index d6570e8..c693447 100644
--- a/protocols/lisp/api/src/test/java/org/onosproject/lisp/ctl/LispRouterAdapter.java
+++ b/protocols/lisp/api/src/test/java/org/onosproject/lisp/ctl/LispRouterAdapter.java
@@ -24,6 +24,9 @@
* Test adapter for the LISP router interface.
*/
public class LispRouterAdapter implements LispRouter {
+
+ private boolean subscribed;
+
@Override
public void sendMessage(LispMessage msg) {
@@ -71,12 +74,12 @@
@Override
public boolean isSubscribed() {
- return false;
+ return subscribed;
}
@Override
public void setSubscribed(boolean subscribed) {
-
+ this.subscribed = subscribed;
}
@Override
diff --git a/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispControllerImpl.java b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispControllerImpl.java
index 5d82527..560cf8e 100644
--- a/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispControllerImpl.java
+++ b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispControllerImpl.java
@@ -93,12 +93,12 @@
ConcurrentMap<LispRouterId, LispRouter> connectedRouters = Maps.newConcurrentMap();
+ final LispAuthenticationConfig authConfig = LispAuthenticationConfig.getInstance();
+ LispControllerBootstrap bootstrap = new LispControllerBootstrap();
+
private Set<LispRouterListener> lispRouterListeners = new CopyOnWriteArraySet<>();
private Set<LispMessageListener> lispMessageListeners = new CopyOnWriteArraySet<>();
- private final LispControllerBootstrap bootstrap = new LispControllerBootstrap();
- private final LispAuthenticationConfig authConfig = LispAuthenticationConfig.getInstance();
-
@Activate
public void activate(ComponentContext context) {
coreService.registerApplication(APP_ID, this::cleanup);
diff --git a/protocols/lisp/ctl/src/test/java/org/onosproject/lisp/ctl/impl/LispControllerImplTest.java b/protocols/lisp/ctl/src/test/java/org/onosproject/lisp/ctl/impl/LispControllerImplTest.java
index 8335d64..106770a 100644
--- a/protocols/lisp/ctl/src/test/java/org/onosproject/lisp/ctl/impl/LispControllerImplTest.java
+++ b/protocols/lisp/ctl/src/test/java/org/onosproject/lisp/ctl/impl/LispControllerImplTest.java
@@ -18,7 +18,6 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
-import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -58,6 +57,7 @@
import static junit.framework.TestCase.fail;
import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
@@ -136,7 +136,7 @@
}
}
- public void waitUntilUpdateIsCalled() throws InterruptedException {
+ void waitUntilUpdateIsCalled() throws InterruptedException {
incomingLatch.await();
outgoingLatch.await();
}
@@ -169,10 +169,10 @@
messageListener = new TestMessageListener();
controller.addMessageListener(messageListener);
- controller.coreService = EasyMock.createMock(CoreService.class);
+ controller.coreService = createMock(CoreService.class);
ComponentConfigService mockConfigService =
- EasyMock.createMock(ComponentConfigService.class);
+ createMock(ComponentConfigService.class);
expect(mockConfigService.getProperties(anyObject())).andReturn(ImmutableSet.of());
mockConfigService.registerProperties(controller.getClass());
expectLastCall();
@@ -182,20 +182,34 @@
controller.cfgService = mockConfigService;
replay(mockConfigService);
- ComponentContext mockContext = EasyMock.createMock(ComponentContext.class);
+ ComponentContext mockContext = createMock(ComponentContext.class);
Dictionary<String, Object> properties = new Hashtable<>();
properties.put("lispAuthKey", "onos");
properties.put("lispAuthKeyId", 1);
expect(mockContext.getProperties()).andReturn(properties);
replay(mockContext);
+
+ LispControllerBootstrap bootstrap = createMock(LispControllerBootstrap.class);
+ bootstrap.start();
+ expectLastCall();
+ controller.bootstrap = bootstrap;
+ replay(bootstrap);
+
controller.activate(mockContext);
}
@After
public void tearDown() {
+ LispControllerBootstrap bootstrap = createMock(LispControllerBootstrap.class);
+ bootstrap.stop();
+ expectLastCall();
+ controller.bootstrap = bootstrap;
+ replay(bootstrap);
+
controller.removeRouterListener(routerListener);
controller.removeMessageListener(messageListener);
+
controller.deactivate();
}
@@ -252,6 +266,26 @@
}
/**
+ * Tests adding and removing subscribed routers.
+ */
+ @Test
+ public void testAddRemoveSubscribedRouter() {
+ router1.setSubscribed(true);
+ router2.setSubscribed(true);
+
+ // Test adding connected routers into agent
+ boolean addRouter1 = agent.addConnectedRouter(routerId1, router1);
+ assertThat(addRouter1, is(true));
+ boolean addRouter2 = agent.addConnectedRouter(routerId2, router2);
+ assertThat(addRouter2, is(true));
+
+ assertThat(Lists.newArrayList(
+ controller.getSubscribedRouters()), hasSize(2));
+ assertThat(Lists.newArrayList(
+ controller.getSubscribedRouters()), hasItems(router1, router2));
+ }
+
+ /**
* Tests adding and removing LISP messages.
*/
@Test