summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/utils/GlusterCoreUtil.java
blob: 47121da1866d6963d901afe5c07d5bb5956ce85d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
 * GlusterCoreUtil.java
 *
 * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Console.
 *
 * Gluster Management Console is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gluster Management Console is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.gluster.storage.management.core.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.gluster.storage.management.core.model.Brick;
import org.gluster.storage.management.core.model.Disk;
import org.gluster.storage.management.core.model.Entity;
import org.gluster.storage.management.core.model.Partition;
import org.gluster.storage.management.core.model.Server;



public class GlusterCoreUtil {
	// Convert from Disk list to Qualified disk name list 
	public static final List<String> getQualifiedDiskNames(List<Disk> diskList) {
		List<String> qualifiedDiskNames = new ArrayList<String>();
		for (Disk disk : diskList) {
			qualifiedDiskNames.add(disk.getQualifiedName());
		}
		return qualifiedDiskNames;
	}
	
	public static final List<String> getQualifiedBrickList(Set<Brick> bricks) {
		List<String> qualifiedBricks = new ArrayList<String>();
		for (Brick brick : bricks) {
			qualifiedBricks.add(brick.getQualifiedName());
		}
		return qualifiedBricks;
	}
	
	/**
	 * Compares the two entity lists and returns the list of entities from first list that have been modified in the second
	 * 
	 * @param oldEntities
	 * @param newEntities
	 * @return List of entities that have been modified
	 */
	public static <T extends Entity> Map<T, T> getModifiedEntities(List<T> oldEntities, List<T> newEntities) {
		Map<T, T> modifiedEntities = new HashMap<T, T>();
		for (T oldEntity : oldEntities) {
			T newEntity = getEntity(newEntities, oldEntity.getName(), false);
			if(newEntity != null && !oldEntity.equals(newEntity)) {
				// old and new entities differ. mark it as modified.
				modifiedEntities.put(oldEntity, newEntity);
			}
		}
		return modifiedEntities;
	}

	/**
	 * Compares the two entity lists and returns the list of entities present only in the second argument
	 * <code>newEntities</code>
	 * 
	 * @param oldEntities
	 * @param newEntities
	 * @param caseInsensitive If true, the entity name comparison will be done in case insensitive manner
	 * @return List of entities that are present only in the second argument <code>newEntities</code>
	 */
	public static <T extends Entity> Set<T> getAddedEntities(List<T> oldEntities, List<T> newEntities,
			boolean caseInsensitive) {
		Set<T> addedEntities = new HashSet<T>();
		for (T newEntity : newEntities) {
			if (!containsEntity(oldEntities, newEntity, caseInsensitive)) {
				// old entity list doesn't contain this entity. mark it as new.
				addedEntities.add(newEntity);
			}
		}
		return addedEntities;
	}

	public static <T extends Entity> boolean containsEntity(List<T> entityList, Entity searchEntity,
			boolean caseInsensitive) {
		return getEntity(entityList, searchEntity.getName(), caseInsensitive) != null;
	}
	
	public static <T extends Entity> boolean containsEntityWithName(List<T> entityList, String searchName,
			boolean caseInsensitive) {
		return getEntity(entityList, searchName, caseInsensitive) != null;
	}

	public static <T extends Entity> T getEntity(Collection<T> entityList, String searchEntityName, boolean caseInsensitive) {
		for (T entity : entityList) {
			String nextEntityName = entity.getName();
			if (nextEntityName.equals(searchEntityName)
					|| (caseInsensitive && nextEntityName.equalsIgnoreCase(searchEntityName))) {
				return entity;
			}
		}

		return null;
	}
	
	public static void updateServerNameOnDevices(Server server) {
		String serverName = server.getName();
		for(Disk disk : server.getDisks()) {
			disk.setServerName(serverName);
			
			if (disk.getRaidDisks() != null) {
				for (Disk raidDisk : disk.getRaidDisks()) {
					raidDisk.setServerName(serverName);
				}
			}
			
			if (disk.getPartitions() != null) {
				for (Partition partition : disk.getPartitions()) {
					partition.setServerName(serverName);
				}
			}
		}
	}

	/**
	 * Skips (removes) the entities from given list till (including) the given {@code tillServer}, upto a maximum of
	 * {@code maxCount} entities<br>
	 * 
	 * @param entities
	 *            List of entities to be pruned
	 * @param maxCount
	 *            Maximum number of entities to be returned
	 * @param tillEntity
	 *            A list of entities after skipping the ones appearing before, and including {@code tillEntity}. If the
	 *            resulting list is bigger than {@code maxCount}, then the first {@code maxCount} number of entities
	 *            will be returned.
	 */
	public static <T extends Entity> List<T> skipEntities(List<T> entities, Integer maxCount, String tillEntity) {
		List<T> servers = skipEntitiesTill(entities, tillEntity);
		return skipEntitiesByMaxCount(servers, maxCount);
	}

	/**
	 * Removes extra entities from given list to return the first (or maximum of) {@code maxCount} entities<br>
	 * 
	 * @param entities
	 *            List of entities to be pruned
	 * @param maxCount
	 *            Maximum number of entities to be returned
	 * @param tillEntity
	 *            the first (or a maximum of) {@code maxCount} entities from the given list
	 */
	public static <T extends Entity> List<T> skipEntitiesByMaxCount(List<T> entities, Integer maxCount) {
		if(maxCount == null || maxCount <= 0 || maxCount > entities.size()) {
			return entities;
		}
		
		return entities.subList(0, maxCount);
	}

	/**
	 * Skips (removes) the entities from given list till (including) the given entity. <br>
	 * 
	 * @param entities
	 *            List of entities to be pruned
	 * @param tillEntity
	 *            A list of entities after skipping the ones appearing before, and including {@code tillEntity}
	 */
	public static <T extends Entity> List<T> skipEntitiesTill(List<T> entities, String tillEntity) {
		if(tillEntity == null) {
			return entities;
		}
		
		int index = indexOfEntity(entities, tillEntity);
		if(index == -1) {
			// given entity not found. return an empty list.
			return new ArrayList<T>();
		} else {
			return entities.subList(index + 1, entities.size());
		}
	}

	public static <T extends Entity> int indexOfEntity(List<T> entities, String entityName) {
		int index = -1;
		for(int i = 0; i < entities.size(); i++) {
			if(entities.get(i).getName().equalsIgnoreCase(entityName)) {
				index = i;
				break;
			}
		}
		return index;
	}
}