Bug:4343-Ipaddress not set in FlowCapableNode 05/27805/3
authorKamal Rameshan <kramesha@cisco.com>
Sun, 27 Sep 2015 17:30:50 +0000 (10:30 -0700)
committermichal rehak <mirehak@cisco.com>
Thu, 15 Oct 2015 16:26:14 +0000 (16:26 +0000)
Change-Id: Ic48b2ba2494f1765a50b52309662d2bee228d741
Signed-off-by: Kamal Rameshan <kramesha@cisco.com>
Signed-off-by: Michal Rehak <mirehak@cisco.com>
(cherry picked from commit c48b2ba2494f1765a50b52309662d2bee228d741)

openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/common/NodeStaticReplyTranslatorUtil.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImpl.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImplTest.java

index 78e70be11c52a2577ccf3cfb222229b64cc23088..229b05d9e61672a264628ad5fa7b80eed03edcbc 100644 (file)
@@ -16,6 +16,7 @@ import javax.annotation.CheckForNull;
 import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.TableFeaturesReplyConvertor;
 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Counter32;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
@@ -76,7 +77,7 @@ public class NodeStaticReplyTranslatorUtil {
      * @param reply
      * @return
      */
-    public static FlowCapableNode nodeDescTranslator(@CheckForNull final MultipartReplyDesc reply) {
+    public static FlowCapableNode nodeDescTranslator(@CheckForNull final MultipartReplyDesc reply, final IpAddress ipAddress) {
         Preconditions.checkArgument(reply != null);
         final FlowCapableNodeBuilder flowCapAugBuilder = new FlowCapableNodeBuilder();
         flowCapAugBuilder.setDescription(reply.getDpDesc());
@@ -87,6 +88,9 @@ public class NodeStaticReplyTranslatorUtil {
         flowCapAugBuilder.setTable(Collections.<Table>emptyList());
         flowCapAugBuilder.setMeter(Collections.<Meter>emptyList());
         flowCapAugBuilder.setGroup(Collections.<Group>emptyList());
+        if (ipAddress != null) {
+            flowCapAugBuilder.setIpAddress(ipAddress);
+        }
         return flowCapAugBuilder.build();
     }
 
index d39d0a5bd36003d38799822697a7e0d657aaba52..b0b8ef61105587f817612d2e7013b3d93a211392 100644 (file)
@@ -15,6 +15,10 @@ import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import io.netty.util.HashedWheelTimer;
 import java.math.BigInteger;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
@@ -55,6 +59,9 @@ import org.opendaylight.openflowplugin.impl.connection.OutboundQueueProviderImpl
 import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolListenerFullImpl;
 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
@@ -522,6 +529,29 @@ public class DeviceManagerImpl implements DeviceManager, AutoCloseable {
         }
     }
 
+    private static IpAddress getIpAddressOf(final DeviceContext deviceContext) {
+
+        InetSocketAddress remoteAddress = deviceContext.getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress();
+
+        if (remoteAddress == null) {
+            LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", deviceContext.getDeviceState().getNodeId());
+            return null;
+        }
+        LOG.info("IP address of switch is :"+remoteAddress);
+
+        final InetAddress address = remoteAddress.getAddress();
+        String hostAddress = address.getHostAddress();
+        if (address instanceof Inet4Address) {
+            return new IpAddress(new Ipv4Address(hostAddress));
+        }
+        if (address instanceof Inet6Address) {
+            return new IpAddress(new Ipv6Address(hostAddress));
+        }
+        LOG.info("Illegal IP address {} of switch:{} ", address, deviceContext.getDeviceState().getNodeId());
+        return null;
+
+    }
+
     static void translateAndWriteReply(final MultipartType type, final DeviceContext dContext,
                                                final InstanceIdentifier<Node> nodeII, final Collection<MultipartReply> result) {
         try {
@@ -531,7 +561,7 @@ public class DeviceManagerImpl implements DeviceManager, AutoCloseable {
                     case OFPMPDESC:
                         Preconditions.checkArgument(body instanceof MultipartReplyDescCase);
                         final MultipartReplyDesc replyDesc = ((MultipartReplyDescCase) body).getMultipartReplyDesc();
-                        final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc);
+                        final FlowCapableNode fcNode = NodeStaticReplyTranslatorUtil.nodeDescTranslator(replyDesc, getIpAddressOf(dContext));
                         final InstanceIdentifier<FlowCapableNode> fNodeII = nodeII.augmentation(FlowCapableNode.class);
                         dContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, fNodeII, fcNode);
                         break;
index 11a09cf3ca2be7810b26c3a7ce24425417c82529..94393a37b2957dd21dc1eea8ef1de9696efafb9a 100644 (file)
@@ -134,10 +134,27 @@ public class DeviceManagerImplTest {
     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
     @Mock
     private TranslatorLibrary translatorLibrary;
+    @Mock
+    private ConnectionContext mockConnectionContext;
+    @Mock
+    private ConnectionAdapter mockedConnectionAdapter;
+    @Mock
+    private DeviceContextImpl mockedDeviceContext;
 
     @Before
     public void setUp() throws Exception {
         OpenflowPortsUtil.init();
+
+        when(mockConnectionContext.getNodeId()).thenReturn(new NodeId("dummyNodeId"));
+        when(mockConnectionContext.getFeatures()).thenReturn(mockFeatures);
+        when(mockConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
+        when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
+
+        final Capabilities capabilitiesV13 = Mockito.mock(Capabilities.class);
+        final CapabilitiesV10 capabilitiesV10 = Mockito.mock(CapabilitiesV10.class);
+        when(mockFeatures.getCapabilities()).thenReturn(capabilitiesV13);
+        when(mockFeatures.getCapabilitiesV10()).thenReturn(capabilitiesV10);
+        when(mockFeatures.getDatapathId()).thenReturn(BigInteger.valueOf(21L));
     }
 
     @Test
@@ -178,7 +195,6 @@ public class DeviceManagerImplTest {
     public void onDeviceContextLevelUp(boolean withException) {
         DeviceManagerImpl deviceManager = prepareDeviceManager(withException);
 
-        DeviceContextImpl mockedDeviceContext = mock(DeviceContextImpl.class);
         if (withException) {
             doThrow(new IllegalStateException("dummy")).when(mockedDeviceContext).initialSubmitTransaction();
             DeviceState mockedDeviceState = mock(DeviceState.class);
@@ -241,20 +257,7 @@ public class DeviceManagerImplTest {
     }
 
     protected ConnectionContext buildMockConnectionContext(short ofpVersion) {
-        ConnectionContext mockConnectionContext = mock(ConnectionContext.class);
-        when(mockConnectionContext.getNodeId()).thenReturn(new NodeId("dummyNodeId"));
-
-        when(mockConnectionContext.getFeatures()).thenReturn(mockFeatures);
-
-        ConnectionAdapter mockedConnectionAdapter = mock(ConnectionAdapter.class);
-        when(mockConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
         when(mockFeatures.getVersion()).thenReturn(ofpVersion);
-        final Capabilities capabilitiesV13 = Mockito.mock(Capabilities.class);
-        final CapabilitiesV10 capabilitiesV10 = Mockito.mock(CapabilitiesV10.class);
-        when(mockFeatures.getCapabilities()).thenReturn(capabilitiesV13);
-        when(mockFeatures.getCapabilitiesV10()).thenReturn(capabilitiesV10);
-        when(mockFeatures.getDatapathId()).thenReturn(BigInteger.valueOf(21L));
-
         when(outboundQueueProvider.reserveEntry()).thenReturn(43L);
         Mockito.doAnswer(new Answer<Void>() {
             @Override
@@ -287,14 +290,12 @@ public class DeviceManagerImplTest {
 
     @Test
     public void chainTableTrunkWriteOF10Test() {
-        DeviceContext mockedDeviceContext = mock(DeviceContext.class);
         DeviceState mockedDeviceState = mock(DeviceState.class);
 
         GetFeaturesOutput mockedFeatures = mock(GetFeaturesOutput.class);
         when(mockedFeatures.getTables()).thenReturn((short) 2);
         when(mockedDeviceState.getFeatures()).thenReturn(mockedFeatures);
 
-
         when(mockedDeviceState.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_II);
         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
 
@@ -311,7 +312,11 @@ public class DeviceManagerImplTest {
 
     @Test
     public void testTranslateAndWriteReplyTypeDesc() {
-        DeviceContext mockedDeviceContext = mock(DeviceContext.class);
+        final ConnectionContext connectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_3);
+        Mockito.when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
+        DeviceState deviceState = Mockito.mock(DeviceState.class);
+        Mockito.when(mockedDeviceContext.getDeviceState()).thenReturn(deviceState);
+
         Collection<MultipartReply> multipartReplyMessages = prepareDataforTypeDesc(mockedDeviceContext);
 
         DeviceManagerImpl.translateAndWriteReply(MultipartType.OFPMPDESC, mockedDeviceContext, DUMMY_NODE_II, multipartReplyMessages);
@@ -332,8 +337,6 @@ public class DeviceManagerImplTest {
 
     @Test
     public void translateAndWriteReplyTypeTableFeatures() {
-        DeviceContext mockedDeviceContext = mock(DeviceContext.class);
-
         TableFeaturesBuilder tableFeature = new TableFeaturesBuilder();
         tableFeature.setTableId(DUMMY_TABLE_ID);
         List<TableFeatures> tableFeatures = new ArrayList<>();
@@ -354,7 +357,6 @@ public class DeviceManagerImplTest {
 
     @Test
     public void translateAndWriteReplyTypeMeterFeatures() {
-        DeviceContext mockedDeviceContext = mock(DeviceContext.class);
         DeviceState mockedDeviceState = mock(DeviceState.class);
         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
 
@@ -376,8 +378,6 @@ public class DeviceManagerImplTest {
 
     @Test
     public void translateAndWriteReplyTypeGroupFeatures() {
-        DeviceContext mockedDeviceContext = mock(DeviceContext.class);
-
         MultipartReplyGroupFeaturesBuilder multipartReplyGroupFeaturesBuilder = new MultipartReplyGroupFeaturesBuilder();
         multipartReplyGroupFeaturesBuilder.setTypes(new GroupTypes(true, true, true, true));
         multipartReplyGroupFeaturesBuilder.setCapabilities(new GroupCapabilities(true, true, true, true));
@@ -400,8 +400,6 @@ public class DeviceManagerImplTest {
 
     @Test
     public void translateAndWriteReplyTypePortDesc() {
-        DeviceContext mockedDeviceContext = mock(DeviceContext.class);
-
         ConnectionContext mockedPrimaryConnectionContext = mock(ConnectionContext.class);
         FeaturesReply mockedFeatures = mock(FeaturesReply.class);
         when(mockedFeatures.getDatapathId()).thenReturn(new BigInteger(DUMMY_DATAPATH_ID));
@@ -435,10 +433,11 @@ public class DeviceManagerImplTest {
 
     @Test
     public void createSuccessProcessingCallbackTest() {
-        DeviceContext mockedDeviceContext = mock(DeviceContext.class);
         DeviceState mockedDeviceState = mock(DeviceState.class);
         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
 
+        final ConnectionContext connectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_3);
+
         List<MultipartReply> multipartReplies = new ArrayList<>(prepareDataforTypeDesc(mockedDeviceContext));
         RpcResult<List<MultipartReply>> result = RpcResultBuilder.<List<MultipartReply>>success(multipartReplies).build();
         ListenableFuture<RpcResult<List<MultipartReply>>> mockedRequestContextFuture = Futures.immediateFuture(result);