Skip to content

Commit

Permalink
#187 Add Java docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nkorange committed Dec 7, 2018
1 parent 7d2f652 commit 0409aae
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Set;

/**
* Service to visit CMDB store
*
* @author <a href="mailto:[email protected]">nkorange</a>
*/
public interface CmdbService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*
* @author <a href="mailto:[email protected]">nkorange</a>
* @see SelectorType
* @see SelectorJsonAdapter
*/
public abstract class AbstractSelector {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class LabelSelector extends AbstractSelector {
private CmdbReader cmdbReader;

/**
* The labels relevant to this the select strategy.
* The labels relevant to this the selector.
*
* @see com.alibaba.nacos.api.cmdb.pojo.Label
*/
Expand Down Expand Up @@ -108,138 +108,152 @@ public LabelSelector() {
cmdbReader = context.getBean(CmdbReader.class);
}

/**
* Parse the label expression.
* <p>
* Currently we support the very single type of expression:
* <pre>
* consumer.labelA = provider.labelA & consumer.labelB = provider.labelB
* </pre>
* Later we will implement a interpreter to parse this expression in a standard LL parser way.
*
* @param expression the label expression to parse
* @return collection of labels
*/

public static Set<String> parseExpression(String expression) {
return ExpressionInterpreter.parseExpression(expression);
}

String[] elements = expression.split(" ");
Set<String> gotLabels = new HashSet<>();
int index = 0;

index = checkInnerSyntax(elements, index);
@Override
public List<IpAddress> select(String consumer, List<IpAddress> providers) {
List<IpAddress> ipAddressList = new ArrayList<>();
for (IpAddress ipAddress : providers) {

if (index == -1) {
return new HashSet<>();
boolean matched = true;
for (String labelName : getLabels()) {
if (!StringUtils.equals(cmdbReader.queryLabel(consumer, PreservedEntityTypes.ip.name(), labelName),
cmdbReader.queryLabel(ipAddress.getIp(), PreservedEntityTypes.ip.name(), labelName))) {
matched = false;
break;
}
}
if (matched) {
ipAddressList.add(ipAddress);
}
}

gotLabels.add(elements[index++].split(PROVIDER_PREFIX)[1]);

while (index < elements.length) {
if (ipAddressList.isEmpty()) {
return providers;
}

index = checkOuterSyntax(elements, index);
return ipAddressList;
}

if (index >= elements.length) {
return gotLabels;
}
/**
* Expression interpreter for label selector.
* <p>
* For now it supports very limited set of syntax rules.
*/
public static class ExpressionInterpreter {

/**
* Parse the label expression.
* <p>
* Currently we support the very single type of expression:
* <pre>
* consumer.labelA = provider.labelA & consumer.labelB = provider.labelB
* </pre>
* Later we will implement a interpreter to parse this expression in a standard LL parser way.
*
* @param expression the label expression to parse
* @return collection of labels
*/
public static Set<String> parseExpression(String expression) {

String[] elements = expression.split(" ");
Set<String> gotLabels = new HashSet<>();
int index = 0;

index = checkInnerSyntax(elements, index);

if (index == -1) {
return new HashSet<>();
}

gotLabels.add(elements[index++].split(PROVIDER_PREFIX)[1]);
}

return gotLabels;
}
while (index < elements.length) {

private static int skipEmpty(String[] elements, int start) {
while (start < elements.length && StringUtils.isBlank(elements[start])) {
start++;
}
return start;
}
index = checkOuterSyntax(elements, index);

private static int checkOuterSyntax(String[] elements, int start) {
if (index >= elements.length) {
return gotLabels;
}

int index = start;
if (index == -1) {
return new HashSet<>();
}

index = skipEmpty(elements, index);
if (index >= elements.length) {
return index;
gotLabels.add(elements[index++].split(PROVIDER_PREFIX)[1]);
}

return gotLabels;
}

if (!SUPPORTED_OUTER_CONNCETORS.contains(elements[index++])) {
return -1;
private static int skipEmpty(String[] elements, int start) {
while (start < elements.length && StringUtils.isBlank(elements[start])) {
start++;
}
return start;
}

return checkInnerSyntax(elements, index);
}
private static int checkOuterSyntax(String[] elements, int start) {

private static int checkInnerSyntax(String[] elements, int start) {
int index = start;

int index = start;
index = skipEmpty(elements, index);
if (index >= elements.length) {
return index;
}

index = skipEmpty(elements, index);
if (index >= elements.length) {
return -1;
}
if (!SUPPORTED_OUTER_CONNCETORS.contains(elements[index++])) {
return -1;
}

if (!elements[index].startsWith(CONSUMER_PREFIX)) {
return -1;
return checkInnerSyntax(elements, index);
}

String labelConsumer = elements[index++].split(CONSUMER_PREFIX)[1];
private static int checkInnerSyntax(String[] elements, int start) {

index = skipEmpty(elements, index);
if (index >= elements.length) {
return -1;
}
int index = start;

if (!SUPPORTED_INNER_CONNCETORS.contains(elements[index++])) {
return -1;
}

index = skipEmpty(elements, index);
if (index >= elements.length) {
return -1;
}
index = skipEmpty(elements, index);
if (index >= elements.length) {
return -1;
}

if (!elements[index].startsWith(PROVIDER_PREFIX)) {
return -1;
}
if (!elements[index].startsWith(CONSUMER_PREFIX)) {
return -1;
}

String labelProvider = elements[index].split(PROVIDER_PREFIX)[1];
String labelConsumer = elements[index++].split(CONSUMER_PREFIX)[1];

if (!labelConsumer.equals(labelProvider)) {
return -1;
}
index = skipEmpty(elements, index);
if (index >= elements.length) {
return -1;
}

return index;
}
if (!SUPPORTED_INNER_CONNCETORS.contains(elements[index++])) {
return -1;
}

@Override
public List<IpAddress> select(String consumer, List<IpAddress> providers) {
List<IpAddress> ipAddressList = new ArrayList<>();
for (IpAddress ipAddress : providers) {
index = skipEmpty(elements, index);
if (index >= elements.length) {
return -1;
}

boolean matched = true;
for (String labelName : getLabels()) {
if (!StringUtils.equals(cmdbReader.queryLabel(consumer, PreservedEntityTypes.ip.name(), labelName),
cmdbReader.queryLabel(ipAddress.getIp(), PreservedEntityTypes.ip.name(), labelName))) {
matched = false;
break;
}
if (!elements[index].startsWith(PROVIDER_PREFIX)) {
return -1;
}
if (matched) {
ipAddressList.add(ipAddress);

String labelProvider = elements[index].split(PROVIDER_PREFIX)[1];

if (!labelConsumer.equals(labelProvider)) {
return -1;
}
}

if (ipAddressList.isEmpty()) {
return providers;
return index;
}

return ipAddressList;
}

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
import java.lang.reflect.Type;

/**
* Json adapter for class Selector.
* <p>
* When deserializing object for class Selector, we should consider to convert the selector to
* its runtime child class object. And this adapter helps us to finish it.
*
* @author <a href="mailto:[email protected]">nkorange</a>
*/
public class SelectorJsonAdapter implements ObjectDeserializer, ObjectSerializer {
Expand Down

0 comments on commit 0409aae

Please sign in to comment.