Skip to content

Commit

Permalink
758 created RegionsConfigUtility.java to configure cell module's regi…
Browse files Browse the repository at this point in the history
…ons.json file with GammaRandomDelay
  • Loading branch information
iwillitried committed Feb 26, 2024
1 parent 2767799 commit 1b94b6b
Showing 1 changed file with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright (c) 2024 Fraunhofer FOKUS and others. All rights reserved.
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contact: [email protected]
*/

package org.eclipse.mosaic.fed.cell.utility;

import org.eclipse.mosaic.fed.cell.config.CRegion;
import org.eclipse.mosaic.fed.cell.config.gson.RegionsNamingStrategy;
import org.eclipse.mosaic.fed.cell.config.model.CNetworkProperties;
import org.eclipse.mosaic.fed.cell.config.util.ConfigurationReader;
import org.eclipse.mosaic.lib.geo.GeoPoint;
import org.eclipse.mosaic.lib.geo.GeoPolygon;
import org.eclipse.mosaic.lib.model.delay.GammaRandomDelay;
import org.eclipse.mosaic.rti.api.InternalFederateException;

import com.google.common.base.Charsets;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonWriter;

/**
* Helper that modifies a region.json config file and writes it back to the source file.
*/
public class RegionsConfigUtility {
public static CRegion getRegionsConfig(String regionConfigurationPath) {
CRegion regionConfig = null;
try {
regionConfig = ConfigurationReader.importRegionConfig(regionConfigurationPath);
} catch (InternalFederateException e) {
throw new RuntimeException(e);
}

return regionConfig;
}

/**
* Sets all transmission delays (uplink, downlink.multicst, downlink.unicast) to 'GammaRandomDelay'.
*
* @param cRegion The region configuration object
* @param minDelay The minimum delay value (ms)
* @param expDelay The expected delay value (ms)
*/
public static void setGammaRandomDelay(CRegion cRegion, long minDelay, long expDelay) {
GammaRandomDelay delay = new GammaRandomDelay();
delay.expDelay = expDelay;
delay.minDelay = minDelay;

for (CNetworkProperties region : cRegion.regions) {
region.downlink.multicast.delay = delay;
region.downlink.unicast.delay = delay;
region.uplink.delay = delay;
}
}

/**
* Writes the region configuration to a .json file.
*
* @param regionConfigurationPath The path to the output file (*.json)
* @param cRegion The region configuration object
*/
public static void exportRegionsConfig(String regionConfigurationPath, CRegion cRegion) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(GeoPoint.class, new GeoPolygonAdapter())
.setFieldNamingStrategy(new RegionsNamingStrategy())
.setPrettyPrinting().create();
try (Writer writer = new OutputStreamWriter(new FileOutputStream(regionConfigurationPath), Charsets.UTF_8)) {
gson.toJson(cRegion, writer);
} catch (IOException e) {
throw new RuntimeException("Error exporting regions config. \n"
+ "The file exists but is a directory rather than a regular file, does not exist but cannot be created, "
+ "or cannot be opened for any other reason. \n"
+ "Path: " + regionConfigurationPath);
}

}

public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Usage: RegionsConfigUtility <path-to-regions.json> <minDelay> <expDelay>");
}

String regionConfigurationPath = args[0];
long minDelay = Long.parseLong(args[1]);
long expDelay = Long.parseLong(args[2]);

CRegion cRegion = getRegionsConfig(regionConfigurationPath);
setGammaRandomDelay(cRegion, minDelay, expDelay);
exportRegionsConfig(regionConfigurationPath, cRegion);
}


static class GeoPolygonAdapter extends TypeAdapter<GeoPoint> {
@Override
public void write(com.google.gson.stream.JsonWriter out, GeoPoint point) throws IOException {
out.beginObject();
out.name("lon");
out.value(point.getLongitude());
out.name("lat");
out.value(point.getLatitude());
out.endObject();
}

@Override
public GeoPoint read(JsonReader in) throws IOException {
return null;
}
}
}

0 comments on commit 1b94b6b

Please sign in to comment.