use of org.onosproject.core.CoreService in project onos by opennetworkinglab.
the class MeterRequestCodec method decode.
@Override
public MeterRequest decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
final JsonCodec<Band> meterBandCodec = context.codec(Band.class);
// parse device id
DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID), DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
// application id
if (applicationId == null) {
CoreService coreService = context.getService(CoreService.class);
applicationId = coreService.registerApplication(REST_APP_ID);
}
// parse burst
boolean burst = false;
JsonNode burstJson = json.get("burst");
if (burstJson != null) {
burst = burstJson.asBoolean();
}
// parse unit type
String unit = nullIsIllegal(json.get(UNIT), UNIT + MISSING_MEMBER_MESSAGE).asText();
Meter.Unit meterUnit = null;
switch(unit) {
case "KB_PER_SEC":
meterUnit = Meter.Unit.KB_PER_SEC;
break;
case "PKTS_PER_SEC":
meterUnit = Meter.Unit.PKTS_PER_SEC;
break;
case "BYTES_PER_SEC":
meterUnit = Meter.Unit.BYTES_PER_SEC;
break;
default:
nullIsIllegal(meterUnit, "The requested unit " + unit + " is not defined for meter.");
}
// parse meter bands
List<Band> bandList = new ArrayList<>();
JsonNode bandsJson = json.get(BANDS);
checkNotNull(bandsJson);
if (bandsJson != null) {
IntStream.range(0, bandsJson.size()).forEach(i -> {
ObjectNode bandJson = get(bandsJson, i);
bandList.add(meterBandCodec.decode(bandJson, context));
});
}
// parse scope and index
JsonNode scopeJson = json.get(SCOPE);
MeterScope scope = null;
if (scopeJson != null && !isNullOrEmpty(scopeJson.asText())) {
scope = MeterScope.of(scopeJson.asText());
}
JsonNode indexJson = json.get(INDEX);
Long index = null;
if (indexJson != null && !isNullOrEmpty(indexJson.asText()) && scope != null) {
index = indexJson.asLong();
}
// build the final request
MeterRequest.Builder meterRequest = DefaultMeterRequest.builder();
if (scope != null) {
meterRequest.withScope(scope);
}
if (index != null) {
meterRequest.withIndex(index);
}
meterRequest.fromApp(applicationId).forDevice(deviceId).withUnit(meterUnit).withBands(bandList);
if (burst) {
meterRequest.burst();
}
return meterRequest.add();
}
use of org.onosproject.core.CoreService in project onos by opennetworkinglab.
the class IntentCodec method intentAttributes.
/**
* Extracts base intent specific attributes from a JSON object
* and adds them to a builder.
*
* @param json root JSON object
* @param context code context
* @param builder builder to use for storing the attributes
*/
public static void intentAttributes(ObjectNode json, CodecContext context, Intent.Builder builder) {
String appId = nullIsIllegal(json.get(IntentCodec.APP_ID), IntentCodec.APP_ID + IntentCodec.MISSING_MEMBER_MESSAGE).asText();
CoreService service = context.getService(CoreService.class);
builder.appId(nullIsNotFound(service.getAppId(appId), IntentCodec.E_APP_ID_NOT_FOUND));
JsonNode priorityJson = json.get(IntentCodec.PRIORITY);
if (priorityJson != null) {
builder.priority(priorityJson.asInt());
}
JsonNode keyJson = json.get(IntentCodec.KEY);
if (keyJson != null) {
String keyString = keyJson.asText();
if (keyString.startsWith("0x")) {
// The intent uses a LongKey
keyString = keyString.replaceFirst("0x", "");
builder.key(Key.of(Long.parseLong(keyString, 16), service.getAppId(appId)));
} else {
// The intent uses a StringKey
builder.key(Key.of(keyString, service.getAppId(appId)));
}
}
JsonNode resourceGroup = json.get(IntentCodec.RESOURCE_GROUP);
if (resourceGroup != null) {
String resourceGroupId = resourceGroup.asText();
builder.resourceGroup(ResourceGroup.of(Long.parseUnsignedLong(resourceGroupId.substring(2), 16)));
}
}
use of org.onosproject.core.CoreService in project onos by opennetworkinglab.
the class VirtualNetworkPathManagerTest method setUp.
@Before
public void setUp() throws Exception {
virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
CoreService coreService = new TestCoreService();
TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService);
TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
virtualNetworkManagerStore.activate();
manager = new VirtualNetworkManager();
manager.store = virtualNetworkManagerStore;
manager.coreService = coreService;
NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
testDirectory = new TestServiceDirectory();
TestUtils.setField(manager, "serviceDirectory", testDirectory);
manager.activate();
}
use of org.onosproject.core.CoreService in project onos by opennetworkinglab.
the class RemoveSpeakerCommand method doExecute.
@Override
protected void doExecute() {
NetworkConfigService configService = get(NetworkConfigService.class);
CoreService coreService = get(CoreService.class);
ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
BgpConfig config = configService.getConfig(appId, BgpConfig.class);
if (config == null || config.bgpSpeakers().isEmpty()) {
print(NO_CONFIGURATION);
return;
}
BgpConfig.BgpSpeakerConfig speaker = config.getSpeakerWithName(name);
if (speaker == null) {
print(SPEAKER_NOT_FOUND, name);
return;
} else {
if (!speaker.peers().isEmpty()) {
// Removal not allowed when peer connections exist.
print(PEERS_EXIST, name);
return;
}
}
removeSpeakerFromConf(config);
configService.applyConfig(appId, BgpConfig.class, config.node());
print(SPEAKER_REMOVE_SUCCESS);
}
use of org.onosproject.core.CoreService in project onos by opennetworkinglab.
the class AddSpeakerCommand method doExecute.
@Override
protected void doExecute() {
NetworkConfigService configService = get(NetworkConfigService.class);
CoreService coreService = get(CoreService.class);
ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
BgpConfig config = configService.addConfig(appId, BgpConfig.class);
if (name != null) {
BgpConfig.BgpSpeakerConfig speaker = config.getSpeakerWithName(name);
if (speaker != null) {
log.debug("Speaker already exists: {}", name);
return;
}
}
if (vlanId == null || vlanId.isEmpty()) {
vlanIdObj = VlanId.NONE;
} else {
vlanIdObj = VlanId.vlanId(Short.valueOf(vlanId));
}
addSpeakerToConf(config);
configService.applyConfig(appId, BgpConfig.class, config.node());
print(SPEAKER_ADD_SUCCESS);
}
Aggregations