Thomas Vachuska
Committed by Gerrit Code Review

Corrected output of network configuration subjectKeys in JSON output; no longer …

…relies on subject.toString.

Change-Id: If4e07bc27cfaf8feb59397a5f104e5f663504f6e
......@@ -35,13 +35,13 @@ import static com.google.common.base.Strings.isNullOrEmpty;
description = "Manages network configuration")
public class NetworkConfigCommand extends AbstractShellCommand {
@Argument(index = 0, name = "subjectKey", description = "Subject key",
@Argument(index = 0, name = "subjectClassKey", description = "Subject class key",
required = false, multiValued = false)
String subjectKey = null;
String subjectClassKey = null;
@Argument(index = 1, name = "subject", description = "Subject",
@Argument(index = 1, name = "subjectKey", description = "Subject key",
required = false, multiValued = false)
String subject = null;
String subjectKey = null;
@Argument(index = 2, name = "configKey", description = "Config key",
required = false, multiValued = false)
......@@ -54,18 +54,18 @@ public class NetworkConfigCommand extends AbstractShellCommand {
protected void execute() {
service = get(NetworkConfigService.class);
JsonNode root = mapper.createObjectNode();
if (isNullOrEmpty(subjectKey)) {
if (isNullOrEmpty(subjectClassKey)) {
addAll((ObjectNode) root);
} else {
SubjectFactory subjectFactory = service.getSubjectFactory(subjectKey);
if (isNullOrEmpty(subject)) {
SubjectFactory subjectFactory = service.getSubjectFactory(subjectClassKey);
if (isNullOrEmpty(subjectKey)) {
addSubjectClass((ObjectNode) root, subjectFactory);
} else {
Object s = subjectFactory.createSubject(subject);
Object s = subjectFactory.createSubject(subjectKey);
if (isNullOrEmpty(configKey)) {
addSubject((ObjectNode) root, s);
} else {
root = getSubjectConfig(getConfig(s, subjectKey, configKey));
root = getSubjectConfig(getConfig(s, subjectClassKey, configKey));
}
}
}
......@@ -82,14 +82,14 @@ public class NetworkConfigCommand extends AbstractShellCommand {
service.getSubjectClasses()
.forEach(sc -> {
SubjectFactory sf = service.getSubjectFactory(sc);
addSubjectClass(newObject(root, sf.subjectKey()), sf);
addSubjectClass(newObject(root, sf.subjectClassKey()), sf);
});
}
@SuppressWarnings("unchecked")
private void addSubjectClass(ObjectNode root, SubjectFactory sf) {
service.getSubjects(sf.subjectClass())
.forEach(s -> addSubject(newObject(root, s.toString()), s));
.forEach(s -> addSubject(newObject(root, sf.subjectKey(s)), s));
}
private void addSubject(ObjectNode root, Object s) {
......
......@@ -42,7 +42,7 @@ public class NetworkConfigRegistryCommand extends AbstractShellCommand {
private void print(ConfigFactory configFactory) {
print(shortOnly ? SHORT_FMT : FMT,
configFactory.subjectFactory().subjectKey(),
configFactory.subjectFactory().subjectClassKey(),
configFactory.configKey(),
configFactory.subjectFactory().subjectClass().getName(),
configFactory.configClass().getName());
......
......@@ -41,27 +41,27 @@ public interface NetworkConfigService
/**
* Returns the subject factory with the specified key.
*
* @param subjectKey subject class key
* @param subjectClassKey subject class key
* @return subject class
*/
SubjectFactory getSubjectFactory(String subjectKey);
SubjectFactory getSubjectFactory(String subjectClassKey);
/**
* Returns the subject factory for the specified class.
*
* @param subjectClass subject class
* @return subject class key
* @return subject class factory
*/
SubjectFactory getSubjectFactory(Class subjectClass);
/**
* Returns the configuration class with the specified key.
*
* @param subjectKey subject class key
* @param subjectClassKey subject class key
* @param configKey subject class name
* @return subject class
*/
Class<? extends Config> getConfigClass(String subjectKey, String configKey);
Class<? extends Config> getConfigClass(String subjectClassKey, String configKey);
/**
* Returns the set of subjects for which some configuration is available.
......
......@@ -28,7 +28,7 @@ import com.google.common.annotations.Beta;
public abstract class SubjectFactory<S> {
private final Class<S> subjectClass;
private final String subjectKey;
private final String subjectClassKey;
/**
* Creates a new configuration factory for the specified class of subjects
......@@ -37,11 +37,11 @@ public abstract class SubjectFactory<S> {
* composite JSON trees.
*
* @param subjectClass subject class
* @param subjectKey subject class key
* @param subjectClassKey subject class key
*/
protected SubjectFactory(Class<S> subjectClass, String subjectKey) {
protected SubjectFactory(Class<S> subjectClass, String subjectClassKey) {
this.subjectClass = subjectClass;
this.subjectKey = subjectKey;
this.subjectClassKey = subjectClassKey;
}
/**
......@@ -60,8 +60,20 @@ public abstract class SubjectFactory<S> {
*
* @return configuration key
*/
public String subjectKey() {
return subjectKey;
public String subjectClassKey() {
return subjectClassKey;
}
/**
* Returns the unique key of the specified configuration subject.
* This is primarily aimed for use in composite JSON trees in external
* representations and has no bearing on the internal behaviours.
*
* @param subject specific subject
* @return subject key
*/
public String subjectKey(S subject) {
return subject.toString();
}
/**
......
......@@ -43,6 +43,10 @@ public final class SubjectFactories {
public ApplicationId createSubject(String key) {
return coreService.registerApplication(key);
}
@Override
public String subjectKey(ApplicationId subject) {
return subject.name();
}
};
public static final SubjectFactory<DeviceId> DEVICE_SUBJECT_FACTORY =
......@@ -59,6 +63,10 @@ public final class SubjectFactories {
public ConnectPoint createSubject(String key) {
return ConnectPoint.deviceConnectPoint(key);
}
@Override
public String subjectKey(ConnectPoint subject) {
return key(subject);
}
};
public static final SubjectFactory<HostId> HOST_SUBJECT_FACTORY =
......@@ -78,6 +86,10 @@ public final class SubjectFactories {
return LinkKey.linkKey(ConnectPoint.deviceConnectPoint(cps[0]),
ConnectPoint.deviceConnectPoint(cps[1]));
}
@Override
public String subjectKey(LinkKey subject) {
return key(subject.src()) + "-" + key(subject.dst());
}
};
/**
......@@ -90,4 +102,8 @@ public final class SubjectFactories {
coreService = service;
}
private static String key(ConnectPoint subject) {
return subject.deviceId() + "/" + subject.port();
}
}
......
......@@ -29,7 +29,7 @@ public class NetworkConfigServiceAdapter implements NetworkConfigService {
}
@Override
public SubjectFactory getSubjectFactory(String subjectKey) {
public SubjectFactory getSubjectFactory(String subjectClassKey) {
return null;
}
......@@ -39,7 +39,7 @@ public class NetworkConfigServiceAdapter implements NetworkConfigService {
}
@Override
public Class<? extends Config> getConfigClass(String subjectKey, String configKey) {
public Class<? extends Config> getConfigClass(String subjectClassKey, String configKey) {
return null;
}
......
......@@ -96,7 +96,7 @@ public class NetworkConfigManager
configClasses.put(identifier(configFactory), configFactory.configClass());
SubjectFactory subjectFactory = configFactory.subjectFactory();
subjectClasses.putIfAbsent(subjectFactory.subjectKey(), subjectFactory);
subjectClasses.putIfAbsent(subjectFactory.subjectClassKey(), subjectFactory);
subjectClassKeys.putIfAbsent(subjectFactory.subjectClass(), subjectFactory);
store.addConfigFactory(configFactory);
......@@ -145,8 +145,8 @@ public class NetworkConfigManager
}
@Override
public SubjectFactory getSubjectFactory(String subjectKey) {
return subjectClasses.get(subjectKey);
public SubjectFactory getSubjectFactory(String subjectClassKey) {
return subjectClasses.get(subjectClassKey);
}
@Override
......@@ -155,8 +155,8 @@ public class NetworkConfigManager
}
@Override
public Class<? extends Config> getConfigClass(String subjectKey, String configKey) {
return configClasses.get(new ConfigIdentifier(subjectKey, configKey));
public Class<? extends Config> getConfigClass(String subjectClassKey, String configKey) {
return configClasses.get(new ConfigIdentifier(subjectClassKey, configKey));
}
@Override
......@@ -255,7 +255,7 @@ public class NetworkConfigManager
}
private static ConfigIdentifier identifier(ConfigFactory factory) {
return new ConfigIdentifier(factory.subjectFactory().subjectKey(), factory.configKey());
return new ConfigIdentifier(factory.subjectFactory().subjectClassKey(), factory.configKey());
}
static final class ConfigIdentifier {
......