SfcGeniusDataUtils { public static Optional<MacAddress> getServiceFunctionMacAddress(String ifName) { String theLogicalInterfaceName = SfcGeniusUtilsDataGetter.getServiceFunctionAttachedInterfaceName(ifName) .orElseThrow(() -> new RuntimeException("Interface is not present in the CONFIG DS")); Interface theIf = SfcGeniusUtilsDataGetter.getServiceFunctionAttachedInterfaceState(ifName) .orElseThrow(() -> new RuntimeException("Interface is not present in the OPERATIONAL DS")); BigInteger theDataplaneId = SfcGeniusUtils.getDpnIdFromLowerLayerIfList(theIf.getLowerLayerIf()); return SfcGeniusUtilsDataGetter.getBridgeFromDpnId(theDataplaneId) .map(BridgeRefEntry::getBridgeReference) .map(OvsdbBridgeRef::getValue) .map(iid -> iid.firstKeyOf(Node.class)) .map(NodeKey::getNodeId) .map(NodeId::getValue) .map(theBridgeName -> SfcGeniusUtilsDataGetter.readOvsNodeInterfaces(theBridgeName, theLogicalInterfaceName)) .orElseThrow(() -> new RuntimeException( String.format("Could not get ovsDB information for IF: %s", theLogicalInterfaceName))) .stream() .filter(ifExternalId -> ifExternalId.getExternalIdKey() != null) .filter(theExternalId -> theExternalId.getExternalIdKey().equals(SfcGeniusConstants.MAC_KEY)) .map(theMac -> new MacAddress(theMac.getExternalIdValue())) .findFirst(); } private  SfcGeniusDataUtils(); static Optional<MacAddress> getServiceFunctionMacAddress(String ifName); static Optional<MacAddress> getServiceFunctionForwarderPortMacAddress(String ifName); static boolean isSfUsingALogicalInterface(ServiceFunction sf); static String getSfLogicalInterface(ServiceFunction sf); static List<String> getInterfaceLowerLayerIf(String theIfName);  }
@Test(expected = RuntimeException.class) public void readMacAddressInterfaceDoesNotExist() { PowerMockito.when(SfcGeniusUtilsDataGetter.getServiceFunctionAttachedInterfaceName(ifName)).thenReturn(null); SfcGeniusDataUtils.getServiceFunctionMacAddress(ifName); }
@Test(expected = RuntimeException.class) public void readMacAddressInterfaceNotProperlyConfigured() { PowerMockito.when(SfcGeniusUtilsDataGetter.getServiceFunctionAttachedInterfaceState(ifName)).thenReturn(null); SfcGeniusDataUtils.getServiceFunctionMacAddress(ifName); }
@Test(expected = RuntimeException.class) public void readMacAddressInterfaceWrongBridge() { PowerMockito.when(SfcGeniusUtilsDataGetter.getBridgeFromDpnId(theDpnId)).thenReturn(null); SfcGeniusDataUtils.getServiceFunctionMacAddress(ifName); }
@Test(expected = RuntimeException.class) public void readMacAddressNotOvsBridge() { PowerMockito.when(SfcGeniusUtilsDataGetter.getBridgeFromDpnId(theDpnId)) .thenReturn(Optional.of(new BridgeRefEntryBuilder() .setKey(new BridgeRefEntryKey(theDpnId)) .build())); SfcGeniusDataUtils.getServiceFunctionMacAddress(ifName); }
@Test(expected = org.opendaylight.sfc.genius.impl.utils.SfcGeniusRuntimeException.class) public void readMacAddressInvalidDpnId() { PowerMockito.when(SfcGeniusUtilsDataGetter.getServiceFunctionAttachedInterfaceState(ifName)) .thenReturn(Optional.of(new InterfaceBuilder() .setKey(new InterfaceKey(logicalIfName)) .setPhysAddress(new PhysAddress("52:c1:91:54:fc:7a")) .setLowerLayerIf(new ArrayList<String>() {{ add("qua-qua");}}) .setType(L2vlan.class) .build())); SfcGeniusDataUtils.getServiceFunctionMacAddress(ifName); }
@Test public void readMacAddressInvalidTerminationPoint() { List<InterfaceExternalIds> theInterfaces = new ArrayList<InterfaceExternalIds>() {{ add(new InterfaceExternalIdsBuilder() .build()); }}; PowerMockito.when(SfcGeniusUtilsDataGetter.readOvsNodeInterfaces(anyString(), anyString())).thenReturn(theInterfaces); Optional<MacAddress> theMac = SfcGeniusDataUtils.getServiceFunctionMacAddress(ifName); Assert.assertFalse(theMac.isPresent()); }
@Test public void readMacAddress() { Optional<MacAddress> theMac = SfcGeniusDataUtils.getServiceFunctionMacAddress(ifName); Assert.assertTrue(theMac.isPresent()); Assert.assertEquals(new MacAddress(macAddress), theMac.get()); }