*/
package org.opendaylight.netvirt.elan.l2gw.ha;
-import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
+import static org.opendaylight.controller.md.sal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
import com.google.common.base.Optional;
+import com.google.common.base.Strings;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.genius.utils.hwvtep.HwvtepHACache;
+import org.opendaylight.genius.infra.Datastore;
+import org.opendaylight.genius.infra.Datastore.Configuration;
+import org.opendaylight.genius.infra.Datastore.Operational;
+import org.opendaylight.genius.infra.TypedReadWriteTransaction;
+import org.opendaylight.genius.infra.TypedWriteTransaction;
+import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
import org.opendaylight.netvirt.elan.l2gw.ha.commands.SwitchesCmd;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalRef;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
public final class HwvtepHAUtil {
- static Logger LOG = LoggerFactory.getLogger(HwvtepHAUtil.class);
+ private static final Logger LOG = LoggerFactory.getLogger(HwvtepHAUtil.class);
//TODO reuse HWvtepSouthboundConstants
public static final String HA_ENABLED = "ha_enabled";
public static final String MANAGER_KEY = "managerKey";
public static final String L2GW_JOB_KEY = ":l2gw";
- static HwvtepHACache hwvtepHACache = HwvtepHACache.getInstance();
-
private HwvtepHAUtil() { }
public static HwvtepPhysicalLocatorRef buildLocatorRef(InstanceIdentifier<Node> nodeIid, String tepIp) {
public static ManagerOtherConfigsBuilder getOtherConfigBuilder(String key, String val) {
ManagerOtherConfigsBuilder otherConfigsBuilder = new ManagerOtherConfigsBuilder();
ManagerOtherConfigsKey otherConfigsKey = new ManagerOtherConfigsKey(key);
- otherConfigsBuilder.setKey(otherConfigsKey);
+ otherConfigsBuilder.withKey(otherConfigsKey);
otherConfigsBuilder.setOtherConfigKey(key);
otherConfigsBuilder.setOtherConfigValue(val);
return otherConfigsBuilder;
}
- public static Node readNode(ReadWriteTransaction tx, LogicalDatastoreType storeType,
- InstanceIdentifier<Node> nodeId)
- throws ReadFailedException {
- Optional<Node> optional = tx.read(storeType, nodeId).checkedGet();
- if (optional.isPresent()) {
- return optional.get();
- }
- return null;
- }
-
public static String convertToGlobalNodeId(String psNodeId) {
int idx = psNodeId.indexOf(PHYSICALSWITCH);
if (idx > 0) {
return physicalLocatorRef;
}
- public static boolean isEmptyList(List list) {
+ public static boolean isEmptyList(@Nullable List list) {
return list == null || list.isEmpty();
}
public static boolean isEmpty(Collection collection) {
- if (collection == null || collection.isEmpty()) {
- return true;
- }
- return false;
- }
-
- public static void mergeManagedByNode(Node psNode,
- PhysicalSwitchAugmentationBuilder builder,
- InstanceIdentifier<Node> haNodePath,
- InstanceIdentifier<Node> haPsPath, NodeId haPSNodeId) {
- PhysicalSwitchAugmentation psAugmentation = psNode.getAugmentation(PhysicalSwitchAugmentation.class);
- builder.setManagedBy(new HwvtepGlobalRef(haNodePath));
- builder.setHwvtepNodeName(psAugmentation.getHwvtepNodeName());
- builder.setHwvtepNodeDescription(psAugmentation.getHwvtepNodeDescription());
- builder.setTunnelIps(psAugmentation.getTunnelIps());
- builder.setPhysicalSwitchUuid(getUUid(psAugmentation.getHwvtepNodeName().getValue()));
+ return collection == null || collection.isEmpty();
}
+ @Nullable
public static Node getOriginal(DataObjectModification<Node> mod) {
Node node = null;
switch (mod.getModificationType()) {
return node;
}
+ @Nullable
public static Node getUpdated(DataObjectModification<Node> mod) {
- Node node = null;
switch (mod.getModificationType()) {
case SUBTREE_MODIFIED:
- node = mod.getDataAfter();
- break;
case WRITE:
- if (mod.getDataAfter() != null) {
- node = mod.getDataAfter();
- }
- break;
+ return mod.getDataAfter();
default:
- break;
+ return null;
}
- return node;
}
+ @Nullable
public static Node getCreated(DataObjectModification<Node> mod) {
if (mod.getModificationType() == DataObjectModification.ModificationType.WRITE
&& mod.getDataBefore() == null) {
return null;
}
+ @Nullable
public static Node getRemoved(DataObjectModification<Node> mod) {
if (mod.getModificationType() == DataObjectModification.ModificationType.DELETE) {
return mod.getDataBefore();
return null;
}
+ @Nullable
+ public static String getPsName(Node psNode) {
+ String psNodeId = psNode.getNodeId().getValue();
+ if (psNodeId.contains(PHYSICALSWITCH)) {
+ return psNodeId.substring(psNodeId.indexOf(PHYSICALSWITCH) + PHYSICALSWITCH.length());
+ }
+ return null;
+ }
+
+ @Nullable
+ public static String getPsName(InstanceIdentifier<Node> psNodeIid) {
+ String psNodeId = psNodeIid.firstKeyOf(Node.class).getNodeId().getValue();
+ if (psNodeId.contains(PHYSICALSWITCH)) {
+ return psNodeId.substring(psNodeId.indexOf(PHYSICALSWITCH) + PHYSICALSWITCH.length());
+ }
+ return null;
+ }
+
public static InstanceIdentifier<Node> getGlobalNodePathFromPSNode(Node psNode) {
- if (psNode == null
- || psNode.getAugmentation(PhysicalSwitchAugmentation.class) == null
- || psNode.getAugmentation(PhysicalSwitchAugmentation.class).getManagedBy() == null) {
- return null;
+ String psNodeId = psNode.getNodeId().getValue();
+ if (psNodeId.contains(PHYSICALSWITCH)) {
+ return convertToInstanceIdentifier(psNodeId.substring(0, psNodeId.indexOf(PHYSICALSWITCH)));
}
- return (InstanceIdentifier<Node>)psNode
- .getAugmentation(PhysicalSwitchAugmentation.class).getManagedBy().getValue();
+ return convertToInstanceIdentifier(psNodeId);
}
+ @Nullable
public static InstanceIdentifier<Node> convertPsPath(Node psNode, InstanceIdentifier<Node> nodePath) {
String psNodeId = psNode.getNodeId().getValue();
- String psName = psNodeId.substring(psNodeId.indexOf(PHYSICALSWITCH) + PHYSICALSWITCH.length());
- String haPsNodeIdVal = nodePath.firstKeyOf(Node.class).getNodeId().getValue() + PHYSICALSWITCH + psName;
- InstanceIdentifier<Node> haPsPath = convertToInstanceIdentifier(haPsNodeIdVal);
- return haPsPath;
+ if (psNodeId.contains(PHYSICALSWITCH)) {
+ String psName = psNodeId.substring(psNodeId.indexOf(PHYSICALSWITCH) + PHYSICALSWITCH.length());
+ String haPsNodeIdVal = nodePath.firstKeyOf(Node.class).getNodeId().getValue() + PHYSICALSWITCH + psName;
+ InstanceIdentifier<Node> haPsPath = convertToInstanceIdentifier(haPsNodeIdVal);
+ return haPsPath;
+ } else {
+ LOG.error("Failed to find ps path from node {}", psNode);
+ return null;
+ }
}
public static NodeBuilder getNodeBuilderForPath(InstanceIdentifier<Node> haPath) {
return nodeBuilder;
}
+ @Nullable
public static String getHAIdFromManagerOtherConfig(Node node) {
- if (node.getAugmentation(HwvtepGlobalAugmentation.class) == null) {
+ if (node.augmentation(HwvtepGlobalAugmentation.class) == null) {
return null;
}
- HwvtepGlobalAugmentation globalAugmentation = node.getAugmentation(HwvtepGlobalAugmentation.class);
+ HwvtepGlobalAugmentation globalAugmentation = node.augmentation(HwvtepGlobalAugmentation.class);
if (globalAugmentation != null) {
List<Managers> managers = globalAugmentation.getManagers();
- if (managers != null && managers.size() > 0 && managers.get(0).getManagerOtherConfigs() != null) {
+ if (managers != null && !managers.isEmpty() && managers.get(0).getManagerOtherConfigs() != null) {
for (ManagerOtherConfigs configs : managers.get(0).getManagerOtherConfigs()) {
- if (configs.getOtherConfigKey().equals(HA_ID)) {
+ if (HA_ID.equals(configs.getOtherConfigKey())) {
return configs.getOtherConfigValue();
}
}
return childNodeIds;
}
HwvtepGlobalAugmentation augmentation =
- haGlobalConfigNodeOptional.get().getAugmentation(HwvtepGlobalAugmentation.class);
+ haGlobalConfigNodeOptional.get().augmentation(HwvtepGlobalAugmentation.class);
if (augmentation != null && augmentation.getManagers() != null
&& augmentation.getManagers().size() > 0) {
Managers managers = augmentation.getManagers().get(0);
return childNodeIds;
}
for (ManagerOtherConfigs otherConfigs : managers.getManagerOtherConfigs()) {
- if (otherConfigs.getOtherConfigKey().equals(HA_CHILDREN)) {
+ if (HA_CHILDREN.equals(otherConfigs.getOtherConfigKey())) {
String nodeIdsVal = otherConfigs.getOtherConfigValue();
if (nodeIdsVal != null) {
String[] parts = nodeIdsVal.split(",");
return childNodeIds;
}
- /**
- * Return PS children for passed PS node .
- *
- * @param psNodId PS node path
- * @return child Switches
- */
- public static Set<InstanceIdentifier<Node>> getPSChildrenIdsForHAPSNode(String psNodId) {
- if (!psNodId.contains(PHYSICALSWITCH)) {
- return Collections.emptySet();
- }
- String nodeId = convertToGlobalNodeId(psNodId);
- InstanceIdentifier<Node> iid = convertToInstanceIdentifier(nodeId);
- if (hwvtepHACache.isHAParentNode(iid)) {
- Set<InstanceIdentifier<Node>> childSwitchIds = new HashSet<>();
- Set<InstanceIdentifier<Node>> childGlobalIds = hwvtepHACache.getChildrenForHANode(iid);
- final String append = psNodId.substring(psNodId.indexOf(PHYSICALSWITCH));
- for (InstanceIdentifier<Node> childId : childGlobalIds) {
- String childIdVal = childId.firstKeyOf(Node.class).getNodeId().getValue();
- childSwitchIds.add(convertToInstanceIdentifier(childIdVal + append));
- }
- return childSwitchIds;
- }
- return Collections.EMPTY_SET;
- }
-
public static HwvtepGlobalAugmentation getGlobalAugmentationOfNode(Node node) {
HwvtepGlobalAugmentation result = null;
if (node != null) {
- result = node.getAugmentation(HwvtepGlobalAugmentation.class);
+ result = node.augmentation(HwvtepGlobalAugmentation.class);
}
if (result == null) {
result = new HwvtepGlobalAugmentationBuilder().build();
public static PhysicalSwitchAugmentation getPhysicalSwitchAugmentationOfNode(Node psNode) {
PhysicalSwitchAugmentation result = null;
if (psNode != null) {
- result = psNode.getAugmentation(PhysicalSwitchAugmentation.class);
+ result = psNode.augmentation(PhysicalSwitchAugmentation.class);
}
if (result == null) {
result = new PhysicalSwitchAugmentationBuilder().build();
ManagersBuilder builder1 = new ManagersBuilder();
- builder1.setKey(new ManagersKey(new Uri(MANAGER_KEY)));
+ builder1.withKey(new ManagersKey(new Uri(MANAGER_KEY)));
List<ManagerOtherConfigs> otherConfigses = new ArrayList<>();
- StringBuffer stringBuffer = new StringBuffer();
- for (NodeId nodeId : nodeIds) {
- stringBuffer.append(nodeId.getValue());
- stringBuffer.append(",");
- }
-
- String children = stringBuffer.substring(0, stringBuffer.toString().length() - 1);
-
+ String children = nodeIds.stream().map(NodeId::getValue).collect(Collectors.joining(","));
otherConfigses.add(getOtherConfigBuilder(HA_CHILDREN, children).build());
builder1.setManagerOtherConfigs(otherConfigses);
List<Managers> managers = new ArrayList<>();
boolean switchesAlreadyPresent = false;
if (haNode.isPresent()) {
Node node = haNode.get();
- HwvtepGlobalAugmentation augmentation = node.getAugmentation(HwvtepGlobalAugmentation.class);
+ HwvtepGlobalAugmentation augmentation = node.augmentation(HwvtepGlobalAugmentation.class);
if (augmentation != null) {
if (augmentation.getSwitches() != null) {
if (augmentation.getSwitches().size() > 0) {
}
}
if (!switchesAlreadyPresent) {
- HwvtepGlobalAugmentation augmentation = childNode.getAugmentation(HwvtepGlobalAugmentation.class);
+ HwvtepGlobalAugmentation augmentation = childNode.augmentation(HwvtepGlobalAugmentation.class);
if (augmentation != null && augmentation.getSwitches() != null) {
List<Switches> src = augmentation.getSwitches();
if (src != null && src.size() > 0) {
* @param haNodePath Ha node path
* @param haGlobalCfg HA global node object
*/
- public static void buildGlobalConfigForHANode(ReadWriteTransaction tx,
+ public static void buildGlobalConfigForHANode(TypedWriteTransaction<Configuration> tx,
Node childNode,
InstanceIdentifier<Node> haNodePath,
Optional<Node> haGlobalCfg) {
nodeBuilder.setNodeId(haNodePath.firstKeyOf(Node.class).getNodeId());
nodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, hwvtepGlobalBuilder.build());
Node configHANode = nodeBuilder.build();
- tx.merge(CONFIGURATION, haNodePath, configHANode,Boolean.TRUE);
+ tx.merge(haNodePath, configHANode, CREATE_MISSING_PARENTS);
}
- public static void deleteNodeIfPresent(ReadWriteTransaction tx,
- LogicalDatastoreType logicalDatastoreType,
- InstanceIdentifier<?> iid) throws ReadFailedException {
- if (tx.read(logicalDatastoreType, iid).checkedGet().isPresent()) {
+ public static <D extends Datastore> void deleteNodeIfPresent(TypedReadWriteTransaction<D> tx,
+ InstanceIdentifier<?> iid) throws ExecutionException, InterruptedException {
+ if (tx.read(iid).get().isPresent()) {
LOG.info("Deleting child node {}", getNodeIdVal(iid));
- tx.delete(logicalDatastoreType, iid);
+ tx.delete(iid);
}
}
* @param key Node object
* @param haNode Ha Node from which to be deleted
* @param tx Transaction
- * @throws ReadFailedException Exception thrown if read fails
- * @throws ExecutionException Exception thrown if Execution fail
- * @throws InterruptedException Thread interrupted Exception
*/
- public static void deletePSNodesOfNode(InstanceIdentifier<Node> key,
- Node haNode,
- ReadWriteTransaction tx)
- throws InterruptedException, ExecutionException, ReadFailedException {
+ public static void deletePSNodesOfNode(InstanceIdentifier<Node> key, Node haNode,
+ TypedReadWriteTransaction<Configuration> tx) throws ExecutionException, InterruptedException {
//read from switches attribute and clean up them
- HwvtepGlobalAugmentation globalAugmentation = haNode.getAugmentation(HwvtepGlobalAugmentation.class);
+ HwvtepGlobalAugmentation globalAugmentation = haNode.augmentation(HwvtepGlobalAugmentation.class);
if (globalAugmentation == null) {
return;
}
if (switches != null) {
for (Switches switche : switches) {
InstanceIdentifier<Node> psId = (InstanceIdentifier<Node>)switche.getSwitchRef().getValue();
- deleteNodeIfPresent(tx, CONFIGURATION, psId);
+ deleteNodeIfPresent(tx, psId);
deleted.put(psId, Boolean.TRUE);
}
}
//also read from managed by attribute of switches and cleanup them as a back up if the above cleanup fails
- Optional<Topology> topologyOptional = tx
- .read(CONFIGURATION, key.firstIdentifierOf(Topology.class)).checkedGet();
+ Optional<Topology> topologyOptional = tx .read(key.firstIdentifierOf(Topology.class)).get();
String deletedNodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
if (topologyOptional.isPresent()) {
Topology topology = topologyOptional.get();
if (topology.getNode() != null) {
for (Node psNode : topology.getNode()) {
- PhysicalSwitchAugmentation ps = psNode.getAugmentation(PhysicalSwitchAugmentation.class);
+ PhysicalSwitchAugmentation ps = psNode.augmentation(PhysicalSwitchAugmentation.class);
if (ps != null) {
InstanceIdentifier<Node> iid = (InstanceIdentifier<Node>)ps.getManagedBy().getValue();
String nodeIdVal = iid.firstKeyOf(Node.class).getNodeId().getValue();
InstanceIdentifier<Node> psNodeId =
convertToInstanceIdentifier(psNode.getNodeId().getValue());
if (deleted.containsKey(psNodeId)) {
- deleteNodeIfPresent(tx, CONFIGURATION, psNodeId);
+ deleteNodeIfPresent(tx, psNodeId);
}
}
}
*
* @param haPath HA node path from whih switches will be deleted
* @param tx Transaction object
- * @throws ReadFailedException Exception thrown if read fails
- * @throws ExecutionException Exception thrown if Execution fail
- * @throws InterruptedException Thread interrupted Exception
*/
public static void deleteSwitchesManagedByNode(InstanceIdentifier<Node> haPath,
- ReadWriteTransaction tx)
- throws InterruptedException, ExecutionException, ReadFailedException {
+ TypedReadWriteTransaction<Operational> tx) throws ExecutionException, InterruptedException {
- Optional<Node> nodeOptional = tx.read(OPERATIONAL, haPath).checkedGet();
+ Optional<Node> nodeOptional = tx.read(haPath).get();
if (!nodeOptional.isPresent()) {
return;
}
Node node = nodeOptional.get();
- HwvtepGlobalAugmentation globalAugmentation = node.getAugmentation(HwvtepGlobalAugmentation.class);
+ HwvtepGlobalAugmentation globalAugmentation = node.augmentation(HwvtepGlobalAugmentation.class);
if (globalAugmentation == null) {
return;
}
if (switches != null) {
for (Switches switche : switches) {
InstanceIdentifier<Node> id = (InstanceIdentifier<Node>)switche.getSwitchRef().getValue();
- deleteNodeIfPresent(tx, OPERATIONAL, id);
+ deleteNodeIfPresent(tx, id);
}
}
}
return true;
}
- public static String getPsName(InstanceIdentifier<Node> psNodeIid) {
- String psNodeId = psNodeIid.firstKeyOf(Node.class).getNodeId().getValue();
- if (psNodeId.contains(PHYSICALSWITCH)) {
- return psNodeId.substring(psNodeId.indexOf(PHYSICALSWITCH) + PHYSICALSWITCH.length());
+ public static void addToCacheIfHAChildNode(InstanceIdentifier<Node> childPath, Node childNode,
+ HwvtepNodeHACache hwvtepNodeHACache) {
+ String haId = HwvtepHAUtil.getHAIdFromManagerOtherConfig(childNode);
+ if (!Strings.isNullOrEmpty(haId)) {
+ InstanceIdentifier<Node> parentId = HwvtepHAUtil.createInstanceIdentifierFromHAId(haId);
+ hwvtepNodeHACache.addChild(parentId, childPath/*child*/);
}
- return null;
}
}