+
+ public static boolean getSwitchStatus(DataBroker broker, BigInteger switchId) {
+ NodeId nodeId = new NodeId("openflow:" + switchId);
+ LOG.debug("getSwitchStatus : Querying switch with dpnId {} is up/down", nodeId);
+ InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node> nodeInstanceId
+ = InstanceIdentifier.builder(Nodes.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight
+ .inventory.rev130819.nodes.Node.class, new NodeKey(nodeId)).build();
+ Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node> nodeOptional =
+ SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(broker,
+ LogicalDatastoreType.OPERATIONAL, nodeInstanceId);
+ if (nodeOptional.isPresent()) {
+ LOG.debug("getSwitchStatus : Switch {} is up", nodeId);
+ return true;
+ }
+ LOG.debug("getSwitchStatus : Switch {} is down", nodeId);
+ return false;
+ }
+
+ public static boolean isExternalNetwork(DataBroker broker, Uuid networkId) {
+ InstanceIdentifier<Networks> id = buildNetworkIdentifier(networkId);
+ Optional<Networks> networkData =
+ SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(
+ broker, LogicalDatastoreType.CONFIGURATION, id);
+ return networkData.isPresent();
+ }
+
+ @Nullable
+ public static String getElanInstancePhysicalNetwok(String elanInstanceName, DataBroker broker) {
+ ElanInstance elanInstance = getElanInstanceByName(elanInstanceName, broker);
+ if (null != elanInstance) {
+ return elanInstance.getPhysicalNetworkName();
+ }
+ return null;
+
+ }
+
+ public static Map<String, String> getOpenvswitchOtherConfigMap(BigInteger dpnId, DataBroker dataBroker) {
+ String otherConfigVal = getProviderMappings(dpnId, dataBroker);
+ return getMultiValueMap(otherConfigVal);
+ }
+
+ public static Map<String, String> getMultiValueMap(String multiKeyValueStr) {
+ if (Strings.isNullOrEmpty(multiKeyValueStr)) {
+ return Collections.emptyMap();
+ }
+
+ Map<String, String> valueMap = new HashMap<>();
+ Splitter splitter = Splitter.on(OTHER_CONFIG_PARAMETERS_DELIMITER);
+ for (String keyValue : splitter.split(multiKeyValueStr)) {
+ String[] split = keyValue.split(OTHER_CONFIG_KEY_VALUE_DELIMITER, 2);
+ if (split.length == 2) {
+ valueMap.put(split[0], split[1]);
+ }
+ }
+
+ return valueMap;
+ }
+
+ public static Optional<Node> getBridgeRefInfo(BigInteger dpnId, DataBroker dataBroker) {
+ InstanceIdentifier<BridgeRefEntry> bridgeRefInfoPath = InstanceIdentifier.create(BridgeRefInfo.class)
+ .child(BridgeRefEntry.class, new BridgeRefEntryKey(dpnId));
+
+ Optional<BridgeRefEntry> bridgeRefEntry =
+ SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
+ LogicalDatastoreType.OPERATIONAL, bridgeRefInfoPath);
+ if (!bridgeRefEntry.isPresent()) {
+ LOG.info("getBridgeRefInfo : bridgeRefEntry is not present for {}", dpnId);
+ return Optional.absent();
+ }
+
+ InstanceIdentifier<Node> nodeId =
+ bridgeRefEntry.get().getBridgeReference().getValue().firstIdentifierOf(Node.class);
+
+ return SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
+ LogicalDatastoreType.OPERATIONAL, nodeId);
+ }
+
+ @Nullable
+ public static String getProviderMappings(BigInteger dpId, DataBroker dataBroker) {
+ return getBridgeRefInfo(dpId, dataBroker).toJavaUtil().map(node -> getOpenvswitchOtherConfigs(node,
+ PROVIDER_MAPPINGS, dataBroker)).orElse(null);
+ }
+
+ @Nullable
+ public static String getOpenvswitchOtherConfigs(Node node, String key, DataBroker dataBroker) {
+ OvsdbNodeAugmentation ovsdbNode = node.augmentation(OvsdbNodeAugmentation.class);
+ if (ovsdbNode == null) {
+ Optional<Node> nodeFromReadOvsdbNode = readOvsdbNode(node, dataBroker);
+ if (nodeFromReadOvsdbNode.isPresent()) {
+ ovsdbNode = nodeFromReadOvsdbNode.get().augmentation(OvsdbNodeAugmentation.class);
+ }
+ }
+
+ if (ovsdbNode != null && ovsdbNode.getOpenvswitchOtherConfigs() != null) {
+ for (OpenvswitchOtherConfigs openvswitchOtherConfigs : ovsdbNode.getOpenvswitchOtherConfigs()) {
+ if (Objects.equals(openvswitchOtherConfigs.getOtherConfigKey(), key)) {
+ return openvswitchOtherConfigs.getOtherConfigValue();
+ }
+ }
+ }
+ LOG.info("getOpenvswitchOtherConfigs : OtherConfigs is not present for ovsdbNode {}", node.getNodeId());
+ return null;
+ }
+
+ @Nonnull
+ public static Optional<Node> readOvsdbNode(Node bridgeNode, DataBroker dataBroker) {
+ OvsdbBridgeAugmentation bridgeAugmentation = extractBridgeAugmentation(bridgeNode);
+ if (bridgeAugmentation != null) {
+ InstanceIdentifier<Node> ovsdbNodeIid =
+ (InstanceIdentifier<Node>) bridgeAugmentation.getManagedBy().getValue();
+ return SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
+ LogicalDatastoreType.OPERATIONAL, ovsdbNodeIid);
+ }
+ return Optional.absent();
+
+ }
+
+ @Nullable
+ public static OvsdbBridgeAugmentation extractBridgeAugmentation(Node node) {
+ if (node == null) {
+ return null;
+ }
+ return node.augmentation(OvsdbBridgeAugmentation.class);
+ }
+
+ // Use Objects.requireNonNullElse instead with JDK9+
+ @Nonnull
+ public static <T> T requireNonNullElse(@Nullable T obj, @Nonnull T defaultObj) {
+ return obj != null ? obj : requireNonNull(defaultObj);
+ }
+
+ public static String getDefaultFibRouteToSNATForSubnetJobKey(String subnetName, BigInteger dpnId) {
+ return NatConstants.NAT_DJC_PREFIX + subnetName + dpnId;
+ }
+
+ public static ExternalSubnets getExternalSubnets(DataBroker dataBroker) {
+ InstanceIdentifier<ExternalSubnets> subnetsIdentifier =
+ InstanceIdentifier.builder(ExternalSubnets.class)
+ .build();
+ try {
+ Optional<ExternalSubnets> optionalExternalSubnets = SingleTransactionDataBroker
+ .syncReadOptional(dataBroker, LogicalDatastoreType.CONFIGURATION, subnetsIdentifier);
+ if (optionalExternalSubnets.isPresent()) {
+ return optionalExternalSubnets.get();
+ }
+ } catch (ReadFailedException e) {
+ LOG.error("Failed to read the subnets from the datastore.");
+ }
+ return null;
+
+ }
+
+ public static void addFlow(TypedWriteTransaction<Configuration> confTx, IMdsalApiManager mdsalManager,
+ BigInteger dpId, short tableId, String flowId, int priority, String flowName, BigInteger cookie,
+ List<? extends MatchInfoBase> matches, List<InstructionInfo> instructions) {
+ FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId, flowId, priority, flowName,
+ NatConstants.DEFAULT_IDLE_TIMEOUT, NatConstants.DEFAULT_IDLE_TIMEOUT, cookie, matches,
+ instructions);
+ LOG.trace("syncFlow : Installing DpnId {}, flowId {}", dpId, flowId);
+ mdsalManager.addFlow(confTx, flowEntity);
+ }
+
+ public static void removeFlow(TypedReadWriteTransaction<Configuration> confTx, IMdsalApiManager mdsalManager,
+ BigInteger dpId, short tableId, String flowId) throws ExecutionException, InterruptedException {
+ LOG.trace("syncFlow : Removing Acl Flow DpnId {}, flowId {}", dpId, flowId);
+ mdsalManager.removeFlow(confTx, dpId, flowId, tableId);
+ }
+
+ public static String getIpv6FlowRef(BigInteger dpnId, short tableId, long routerID) {
+ return new StringBuilder().append(NatConstants.IPV6_FLOWID_PREFIX).append(dpnId).append(NatConstants
+ .FLOWID_SEPARATOR).append(tableId).append(NatConstants.FLOWID_SEPARATOR).append(routerID).toString();
+ }
+
+ public static String getTunnelInterfaceName(BigInteger srcDpId, BigInteger dstDpId,
+ ItmRpcService itmManager) {
+ Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
+ RpcResult<GetTunnelInterfaceNameOutput> rpcResult;
+ try {
+ Future<RpcResult<GetTunnelInterfaceNameOutput>> result = itmManager
+ .getTunnelInterfaceName(new GetTunnelInterfaceNameInputBuilder().setSourceDpid(srcDpId)
+ .setDestinationDpid(dstDpId).setTunnelType(tunType).build());
+ rpcResult = result.get();
+ if (!rpcResult.isSuccessful()) {
+ tunType = TunnelTypeGre.class ;
+ result = itmManager.getTunnelInterfaceName(new GetTunnelInterfaceNameInputBuilder()
+ .setSourceDpid(srcDpId)
+ .setDestinationDpid(dstDpId)
+ .setTunnelType(tunType)
+ .build());
+ rpcResult = result.get();
+ if (!rpcResult.isSuccessful()) {
+ LOG.warn("getTunnelInterfaceName : RPC Call to getTunnelInterfaceId returned with Errors {}",
+ rpcResult.getErrors());
+ } else {
+ return rpcResult.getResult().getInterfaceName();
+ }
+ LOG.warn("getTunnelInterfaceName : RPC Call to getTunnelInterfaceId returned with Errors {}",
+ rpcResult.getErrors());
+ } else {
+ return rpcResult.getResult().getInterfaceName();
+ }
+ } catch (InterruptedException | ExecutionException | NullPointerException e) {
+ LOG.error("getTunnelInterfaceName : Exception when getting tunnel interface Id for tunnel "
+ + "between {} and {}", srcDpId, dstDpId);
+ }
+ return null;
+ }
+
+ public static String getIpv6JobKey(String routerName) {
+ return "Ipv6." + routerName;
+ }