Search in sources :

Example 11 with CoreService

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();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Meter(org.onosproject.net.meter.Meter) DeviceId(org.onosproject.net.DeviceId) ArrayList(java.util.ArrayList) CoreService(org.onosproject.core.CoreService) Band(org.onosproject.net.meter.Band) JsonNode(com.fasterxml.jackson.databind.JsonNode) MeterScope(org.onosproject.net.meter.MeterScope) MeterRequest(org.onosproject.net.meter.MeterRequest) DefaultMeterRequest(org.onosproject.net.meter.DefaultMeterRequest)

Example 12 with CoreService

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)));
    }
}
Also used : CoreService(org.onosproject.core.CoreService) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 13 with CoreService

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();
}
Also used : TestEventDispatcher(org.onosproject.common.event.impl.TestEventDispatcher) DistributedVirtualNetworkStore(org.onosproject.incubator.net.virtual.store.impl.DistributedVirtualNetworkStore) TestStorageService(org.onosproject.store.service.TestStorageService) TestServiceDirectory(org.onlab.osgi.TestServiceDirectory) CoreService(org.onosproject.core.CoreService) Before(org.junit.Before)

Example 14 with CoreService

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);
}
Also used : BgpConfig(org.onosproject.routing.config.BgpConfig) NetworkConfigService(org.onosproject.net.config.NetworkConfigService) CoreService(org.onosproject.core.CoreService) ApplicationId(org.onosproject.core.ApplicationId)

Example 15 with CoreService

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);
}
Also used : BgpConfig(org.onosproject.routing.config.BgpConfig) NetworkConfigService(org.onosproject.net.config.NetworkConfigService) CoreService(org.onosproject.core.CoreService) ApplicationId(org.onosproject.core.ApplicationId)

Aggregations

CoreService (org.onosproject.core.CoreService)71 Before (org.junit.Before)31 ApplicationId (org.onosproject.core.ApplicationId)30 FlowRuleService (org.onosproject.net.flow.FlowRuleService)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)12 ComponentConfigAdapter (org.onosproject.cfg.ComponentConfigAdapter)12 NetworkConfigService (org.onosproject.net.config.NetworkConfigService)12 TrafficSelector (org.onosproject.net.flow.TrafficSelector)11 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)11 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)10 DeviceId (org.onosproject.net.DeviceId)10 IntentExtensionService (org.onosproject.net.intent.IntentExtensionService)10 TestServiceDirectory (org.onlab.osgi.TestServiceDirectory)9 MockResourceService (org.onosproject.net.resource.MockResourceService)9 TestEventDispatcher (org.onosproject.common.event.impl.TestEventDispatcher)8 DistributedVirtualNetworkStore (org.onosproject.incubator.net.virtual.store.impl.DistributedVirtualNetworkStore)8 DeviceService (org.onosproject.net.device.DeviceService)8 TestStorageService (org.onosproject.store.service.TestStorageService)8 ArrayList (java.util.ArrayList)7 DomainService (org.onosproject.net.domain.DomainService)6