From faa2d4e0cfabe320e66e943c6ff5bc81a8e236f1 Mon Sep 17 00:00:00 2001 From: poorva1209 Date: Fri, 5 Jan 2024 13:39:55 -0800 Subject: [PATCH 1/3] resolved bugs to start multiple simulations and services within multiple simulations --- .../pnnl/goss/gridappsd/service/ServiceManagerImpl.java | 2 -- .../goss/gridappsd/simulation/SimulationManagerImpl.java | 2 +- .../pnnl/goss/gridappsd/simulation/SimulationProcess.java | 8 ++++---- services/helicsgossbridge/service/helics_goss_bridge.py | 6 +++--- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/service/ServiceManagerImpl.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/service/ServiceManagerImpl.java index 230dae5c..53812bee 100755 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/service/ServiceManagerImpl.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/service/ServiceManagerImpl.java @@ -417,8 +417,6 @@ public List listRunningServices(String serviceId, String simula if(instance.getService_info().getId().equals(serviceId)){ if(simulationId!=null && instance.getSimulation_id().equals(simulationId)) result.add(instance); - else - result.add(instance); } } return result; diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/SimulationManagerImpl.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/SimulationManagerImpl.java index d8e28fbf..27f85d9d 100755 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/SimulationManagerImpl.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/SimulationManagerImpl.java @@ -158,7 +158,7 @@ public void resumeSimulation(String simulationId){ } @Override public void endSimulation(String simulationId){ - client.publish(GridAppsDConstants.topic_COSIM_input, "{\"command\": \"stop\"}"); + client.publish(GridAppsDConstants.topic_COSIM_input+"."+simulationId, "{\"command\": \"stop\"}"); } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/SimulationProcess.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/SimulationProcess.java index 3bfab0f3..81323d0f 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/SimulationProcess.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/SimulationProcess.java @@ -186,7 +186,7 @@ else if(simulationConfig.getSimulator().equals(gridlabdConstant)){ //Subscribe to fncs-goss-bridge output topic GossFncsResponseEvent gossFncsResponseEvent = new GossFncsResponseEvent(logManager, isInitialized, isFinished, simulationId); - client.subscribe("/topic/"+GridAppsDConstants.topic_COSIM_output, gossFncsResponseEvent); + client.subscribe("/topic/"+GridAppsDConstants.topic_COSIM_output+"."+simulationId, gossFncsResponseEvent); logManager.info(ProcessStatus.RUNNING, simulationId, "Checking if co-simulation is initialized, currently "+isInitialized.isInited); @@ -196,7 +196,7 @@ else if(simulationConfig.getSimulator().equals(gridlabdConstant)){ //TODO add limiting how long it checks for initialized, or cancel if the fncs process exits //This call would return true/false for initialization and simulation output of time step 0. - client.publish(GridAppsDConstants.topic_COSIM_input, "{\"command\": \"isInitialized\"}"); + client.publish(GridAppsDConstants.topic_COSIM_input+"."+simulationId, "{\"command\": \"isInitialized\"}"); initAttempts++; Thread.sleep(1000); @@ -217,7 +217,7 @@ else if(simulationConfig.getSimulator().equals(gridlabdConstant)){ } //call to stop the simulation - client.publish(GridAppsDConstants.topic_COSIM_input, "{\"command\": \"stop\"}"); + client.publish(GridAppsDConstants.topic_COSIM_input+"."+simulationId, "{\"command\": \"stop\"}"); logManager.info(ProcessStatus.COMPLETE, simulationId, "Simulation "+simulationId+" complete"); } catch(Exception e){ @@ -251,7 +251,7 @@ private void startSimulation(GossFncsResponseEvent gossEvent, SimulationConfig s // Send the start simulation command to the fncsgossbridge so that it runs it's time loop to move the fncs simulation forward logManager.debug(ProcessStatus.RUNNING, simulationId, "Sending start simulation to bridge."); String message = "{\"command\": \"StartSimulation\"}"; - client.publish(GridAppsDConstants.topic_COSIM_input, message); + client.publish(GridAppsDConstants.topic_COSIM_input+"."+simulationId, message); } private void watch(final Process process, String processName) { diff --git a/services/helicsgossbridge/service/helics_goss_bridge.py b/services/helicsgossbridge/service/helics_goss_bridge.py index 1df160cd..b9d0608f 100644 --- a/services/helicsgossbridge/service/helics_goss_bridge.py +++ b/services/helicsgossbridge/service/helics_goss_bridge.py @@ -414,7 +414,7 @@ def on_message(self, headers, msg): message['response'] = str(self._is_initialized) t_now = datetime.utcnow() message['timestamp'] = int(time.mktime(t_now.timetuple())) - self._gad_connection.send(self._simulation_manager_input_topic , json.dumps(message)) + self._gad_connection.send(self._simulation_manager_input_topic+"."+self._simulation_id , json.dumps(message)) elif json_msg.get('command', '') == 'update': json_msg['input']["time_received"] = time.perf_counter() message['command'] = 'update' @@ -606,7 +606,7 @@ def run_simulation(self): log.debug(f"Simulation finished in {time.perf_counter() - simulation_run_time_start} seconds.") message['command'] = 'simulationFinished' del message['output'] - self._gad_connection.send(self._simulation_manager_input_topic, json.dumps(message)) + self._gad_connection.send(self._simulation_manager_input_topic+"."+self._simulation_id, json.dumps(message)) log.info(f'Simulation {self._simulation_id} has finished.') self._gad_connection.send_simulation_status('COMPLETE', f'Simulation {self._simulation_id} has finished.', 'INFO') except Exception as e: @@ -630,7 +630,7 @@ def _register_with_goss(self): self._gad_connection = GridAPPSD(self._simulation_id) log.debug("Successfully registered with the GridAPPS-D platform.") self._gad_connection.subscribe(topics.simulation_input_topic(self._simulation_id), self.on_message) - self._gad_connection.subscribe("/topic/goss.gridappsd.cosim.input", self.on_message) + self._gad_connection.subscribe("/topic/goss.gridappsd.cosim.input."+self._simulation_id, self.on_message) except Exception as e: log.error("An error occurred when trying to register with the GridAPPS-D platform!", exc_info=True) From 344bebdd5fbbfee672fbf3c31c60724517e98375 Mon Sep 17 00:00:00 2001 From: Sharma Date: Fri, 5 Jan 2024 14:16:54 -0800 Subject: [PATCH 2/3] Auto-starting context_manager as service with cim-graph 1.0 --- .../distributed/FieldBusManagerImpl.java | 691 +++++++++--------- .../gridappsd/dto/field/RequestField.java | 27 + requirements.txt | 1 + services/context_manager.config | 14 + 4 files changed, 396 insertions(+), 337 deletions(-) create mode 100644 gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/field/RequestField.java create mode 100644 services/context_manager.config diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/distributed/FieldBusManagerImpl.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/distributed/FieldBusManagerImpl.java index f9347619..438c0647 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/distributed/FieldBusManagerImpl.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/distributed/FieldBusManagerImpl.java @@ -1,323 +1,362 @@ - package gov.pnnl.goss.gridappsd.distributed; - - import java.io.Serializable; - import java.util.ArrayList; - import java.util.Dictionary; - import java.util.HashMap; - import java.util.List; - import java.util.Map; - import java.util.stream.Collectors; - - import org.apache.felix.dm.annotation.api.Component; - import org.apache.felix.dm.annotation.api.ConfigurationDependency; - import org.apache.felix.dm.annotation.api.ServiceDependency; - import org.apache.felix.dm.annotation.api.Start; - import org.apache.http.auth.Credentials; - import org.apache.http.auth.UsernamePasswordCredentials; - import org.json.JSONArray; - import org.json.JSONObject; - - import com.google.gson.Gson; - import com.google.gson.JsonElement; - import com.google.gson.JsonObject; - import com.google.gson.JsonParser; - - import gov.pnnl.goss.gridappsd.api.DataManager; - import gov.pnnl.goss.gridappsd.api.FieldBusManager; - import gov.pnnl.goss.gridappsd.api.LogManager; - import gov.pnnl.goss.gridappsd.api.PowergridModelDataManager; - import gov.pnnl.goss.gridappsd.api.ServiceManager; - import gov.pnnl.goss.gridappsd.dto.LogMessage.ProcessStatus; - import gov.pnnl.goss.gridappsd.dto.PowergridModelDataRequest; - import gov.pnnl.goss.gridappsd.dto.ServiceInfo; - import gov.pnnl.goss.gridappsd.dto.field.Feeder; - import gov.pnnl.goss.gridappsd.dto.field.RequestFieldContext; - import gov.pnnl.goss.gridappsd.dto.field.Root; - import gov.pnnl.goss.gridappsd.dto.field.SecondaryArea; - import gov.pnnl.goss.gridappsd.dto.field.SwitchArea; - import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; - import pnnl.goss.core.Client; - import pnnl.goss.core.Client.PROTOCOL; - import pnnl.goss.core.ClientFactory; - import pnnl.goss.core.DataResponse; - import pnnl.goss.core.GossResponseEvent; - import pnnl.goss.core.Request.RESPONSE_FORMAT; - import pnnl.goss.core.Response; - import pnnl.goss.core.security.SecurityConfig; - - @Component - public class FieldBusManagerImpl implements FieldBusManager { - - private static final String CONFIG_PID = "pnnl.goss.gridappsd"; - private Dictionary configurationProperties; - - String topology_reponse; - String topicPrefix = "goss.gridappsd.process.request.field"; - String topicContext = topicPrefix + ".context"; - - @ServiceDependency - ClientFactory clientFactory; - - @ServiceDependency - private volatile DataManager dataManager; - - @ServiceDependency - private volatile ServiceManager serviceManager; - - @ServiceDependency - private volatile LogManager logManager; - - private volatile TopologyRequestProcess topology; - - @ServiceDependency - SecurityConfig securityConfig; - - @ServiceDependency - PowergridModelDataManager powergridModelDataManager; - - Client client; - - Map> messageBus_measIds_map = new HashMap>(); - //Map measId_messageBus_map = new HashMap(); - - String fieldModelId = null; - - // FileWriter writer = null; - - public FieldBusManagerImpl() { - System.out.println("Starting FieldBusManager"); - } - - @Start - public void start() { - - try { - - Credentials credentials = new UsernamePasswordCredentials(securityConfig.getManagerUser(), - securityConfig.getManagerPassword()); - client = clientFactory.create(PROTOCOL.STOMP, credentials, true); - - ServiceInfo serviceInfo = serviceManager.getService("gridappsd-topology-daemon"); - if (serviceInfo == null) { - logManager.warn(ProcessStatus.RUNNING, null, - "Topology deamon service is not available. Stopping FieldBusManager."); - return; - } - - fieldModelId = getFieldModelMrid(); - if (fieldModelId == null) { - logManager.warn(ProcessStatus.RUNNING, null, - "Field model mrid is not available. Stopping FieldBusManager. " - + "Check conf/pnnl.goss.gridappsd.cfg file and add field.model.mrid key with value of deployed field model mrid. "); - return; - } - - topology = new TopologyRequestProcess(fieldModelId, client, dataManager, securityConfig); - topology.start(); - - this.publishDeviceOutput(); - - } catch (Exception e) { - e.printStackTrace(); +package gov.pnnl.goss.gridappsd.distributed; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Dictionary; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.apache.felix.dm.annotation.api.Component; +import org.apache.felix.dm.annotation.api.ConfigurationDependency; +import org.apache.felix.dm.annotation.api.ServiceDependency; +import org.apache.felix.dm.annotation.api.Start; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import gov.pnnl.goss.gridappsd.api.DataManager; +import gov.pnnl.goss.gridappsd.api.FieldBusManager; +import gov.pnnl.goss.gridappsd.api.LogManager; +import gov.pnnl.goss.gridappsd.api.PowergridModelDataManager; +import gov.pnnl.goss.gridappsd.api.ServiceManager; +import gov.pnnl.goss.gridappsd.dto.LogMessage.ProcessStatus; +import gov.pnnl.goss.gridappsd.dto.PowergridModelDataRequest; +import gov.pnnl.goss.gridappsd.dto.ServiceInfo; +import gov.pnnl.goss.gridappsd.dto.field.Feeder; +import gov.pnnl.goss.gridappsd.dto.field.RequestField; +import gov.pnnl.goss.gridappsd.dto.field.Root; +import gov.pnnl.goss.gridappsd.dto.field.SecondaryArea; +import gov.pnnl.goss.gridappsd.dto.field.SwitchArea; +import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; +import pnnl.goss.core.Client; +import pnnl.goss.core.Client.PROTOCOL; +import pnnl.goss.core.ClientFactory; +import pnnl.goss.core.DataResponse; +import pnnl.goss.core.GossResponseEvent; +import pnnl.goss.core.Request.RESPONSE_FORMAT; +import pnnl.goss.core.Response; +import pnnl.goss.core.security.SecurityConfig; + +@Component +public class FieldBusManagerImpl implements FieldBusManager { + + private static final String CONFIG_PID = "pnnl.goss.gridappsd"; + private Dictionary configurationProperties; + + String topology_reponse; + String topicPrefix = "goss.gridappsd.process.request.field"; + + @ServiceDependency + ClientFactory clientFactory; + + @ServiceDependency + private volatile DataManager dataManager; + + @ServiceDependency + private volatile ServiceManager serviceManager; + + @ServiceDependency + private volatile LogManager logManager; + + private volatile TopologyRequestProcess topology; + + @ServiceDependency + SecurityConfig securityConfig; + + @ServiceDependency + PowergridModelDataManager powergridModelDataManager; + + Client client; + + Map> messageBus_measIds_map = new HashMap>(); + //Map measId_messageBus_map = new HashMap(); + + String fieldModelId = null; + + // FileWriter writer = null; + + public FieldBusManagerImpl() { + System.out.println("Starting FieldBusManager"); + } + + @Start + public void start() { + + try { + + Credentials credentials = new UsernamePasswordCredentials(securityConfig.getManagerUser(), + securityConfig.getManagerPassword()); + client = clientFactory.create(PROTOCOL.STOMP, credentials, true); + + ServiceInfo serviceInfo = serviceManager.getService("gridappsd-topology-daemon"); + if (serviceInfo == null) { + logManager.warn(ProcessStatus.RUNNING, null, + "Topology deamon service is not available. Stopping FieldBusManager."); + return; } - + + fieldModelId = getFieldModelMrid(); + if (fieldModelId == null) { + logManager.warn(ProcessStatus.RUNNING, null, + "Field model mrid is not available. Stopping FieldBusManager. " + + "Check conf/pnnl.goss.gridappsd.cfg file and add field.model.mrid key with value of deployed field model mrid. "); + return; + } + + topology = new TopologyRequestProcess(fieldModelId, client, dataManager, securityConfig); + topology.start(); + + this.publishDeviceOutput(); + + } catch (Exception e) { + e.printStackTrace(); } - - @Override - public Serializable handleRequest(String request_queue, Serializable request) { - - Feeder responseFeeder = null; - - if (request_queue.endsWith("context")) { - - RequestFieldContext requestFieldContext = RequestFieldContext.parse(request.toString()); - if (requestFieldContext.areaId == null) - return topology.root.feeders; - else if(requestFieldContext.areaId.equals(topology.root.feeders.message_bus_id)) - return topology.root.feeders; - else { - for (SwitchArea switchArea : topology.root.feeders.switch_areas) { - if (requestFieldContext.areaId.equals(switchArea.message_bus_id)) - return switchArea; - for (SecondaryArea secondaryArea : switchArea.secondary_areas) { - if (requestFieldContext.areaId.equals(secondaryArea.message_bus_id)) - return secondaryArea; - } + + } + + @Override + public Serializable handleRequest(String request_queue, Serializable request) { + + Feeder responseFeeder = null; + RequestField requestField = RequestField.parse(request.toString()); + + if (requestField.request_type.equals("get_context")) { + + if (requestField.areaId == null) + return topology.root.feeders; + else if(requestField.areaId.equals(topology.root.feeders.message_bus_id)) + return topology.root.feeders; + else { + for (SwitchArea switchArea : topology.root.feeders.switch_areas) { + if (requestField.areaId.equals(switchArea.message_bus_id)) + return switchArea; + for (SecondaryArea secondaryArea : switchArea.secondary_areas) { + if (requestField.areaId.equals(secondaryArea.message_bus_id)) + return secondaryArea; } } - } - return responseFeeder; } - - public void publishDeviceOutput() { - - client.subscribe(GridAppsDConstants.topic_simulationOutput + ".>", new GossResponseEvent() { - - @Override - public void onMessage(Serializable response) { - - DataResponse event = (DataResponse) response; - String simulationId = event.getDestination().substring(event.getDestination().lastIndexOf(".") + 1, - event.getDestination().length()); - String simOutputStr = event.getData().toString(); - JsonObject simOutputJsonObj = null; - - JsonParser parser = new JsonParser(); - JsonElement simOutputObject = parser.parse(simOutputStr); - - if (simOutputObject.isJsonObject()) { - simOutputJsonObj = simOutputObject.getAsJsonObject(); - } - - JsonObject tempObj = simOutputJsonObj.getAsJsonObject("message"); - Map expectedOutputMap = tempObj.getAsJsonObject("measurements").entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue())); - - try { - - for (String measurementMrid : expectedOutputMap.keySet()) { - - String messageBusId = null; - if (topology.measId_messageBus_map.get(measurementMrid) != null) { - messageBusId = "goss.gridappsd.field.simulation.output." + simulationId + "." - + topology.measId_messageBus_map.get(measurementMrid); - } else { - messageBusId = "goss.gridappsd.field.simulation.output." + simulationId + "." - + topology.root.feeders.message_bus_id; - } - - JsonObject obj = new JsonObject(); - obj.add(measurementMrid, expectedOutputMap.get(measurementMrid)); - - client.publish(messageBusId, obj.toString()); + else if (requestField.request_type.equals("is_initilized")) { + + JsonObject obj = new JsonObject(); + try { + if (topology.root.feeders != null){ + obj.addProperty("initialized",true); + } + else{ + obj.addProperty("initialized",false); + } + }catch(NullPointerException e){ + obj.addProperty("initialized",false); + return obj.toString(); + } + + return obj.toString(); + } + + return null; + } + + public void publishDeviceOutput() { + + client.subscribe(GridAppsDConstants.topic_simulationOutput + ".>", new GossResponseEvent() { + + @Override + public void onMessage(Serializable response) { + + DataResponse event = (DataResponse) response; + String simulationId = event.getDestination().substring(event.getDestination().lastIndexOf(".") + 1, + event.getDestination().length()); + String simOutputStr = event.getData().toString(); + JsonObject simOutputJsonObj = null; + + JsonParser parser = new JsonParser(); + JsonElement simOutputObject = parser.parse(simOutputStr); + + if (simOutputObject.isJsonObject()) { + simOutputJsonObj = simOutputObject.getAsJsonObject(); + } + + JsonObject tempObj = simOutputJsonObj.getAsJsonObject("message"); + Map expectedOutputMap = tempObj.getAsJsonObject("measurements").entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue())); + + try { + + for (String measurementMrid : expectedOutputMap.keySet()) { + + String messageBusId = null; + if (topology.measId_messageBus_map.get(measurementMrid) != null) { + messageBusId = "goss.gridappsd.field.simulation.output." + simulationId + "." + + topology.measId_messageBus_map.get(measurementMrid); + } else { + messageBusId = "goss.gridappsd.field.simulation.output." + simulationId + "." + + topology.root.feeders.message_bus_id; } - - } catch (Exception e1) { - e1.printStackTrace(); + + JsonObject obj = new JsonObject(); + obj.add(measurementMrid, expectedOutputMap.get(measurementMrid)); + + client.publish(messageBusId, obj.toString()); + } - - + + } catch (Exception e1) { + e1.printStackTrace(); } - - }); - - } - - @ConfigurationDependency(pid = CONFIG_PID) - public synchronized void updated(Dictionary config) { - this.configurationProperties = config; - } - - public String getFieldModelMrid() { - if (this.configurationProperties != null) { - Object value = this.configurationProperties.get("field.model.mrid"); - if (value != null) - return value.toString(); + + } - return null; - } - + + }); + } - - class TopologyRequest implements Serializable { - - private static final long serialVersionUID = 4279262793871885409L; - - String requestType = "GET_SWITCH_AREAS"; - String modelID = null; - String resultFormat = "JSON"; - - @Override - public String toString() { - Gson gson = new Gson(); - return gson.toJson(this); - } - + + @ConfigurationDependency(pid = CONFIG_PID) + public synchronized void updated(Dictionary config) { + this.configurationProperties = config; } - - class TopologyRequestProcess extends Thread { - - String fieldModelMrid; - Client client; - DataManager dataManager; - SecurityConfig securityConfig; - Root root = null; - Map> messageBus_measIds_map = new HashMap>(); - Map measId_messageBus_map = new HashMap(); - - public TopologyRequestProcess(String fieldModelMrid, Client client, DataManager dataManager, - SecurityConfig securityConfig) { - this.fieldModelMrid = fieldModelMrid; - this.client = client; - this.dataManager = dataManager; - this.securityConfig = securityConfig; + + public String getFieldModelMrid() { + if (this.configurationProperties != null) { + Object value = this.configurationProperties.get("field.model.mrid"); + if (value != null) + return value.toString(); } - - @Override - public void run() { - try { - Gson gson = new Gson(); - TopologyRequest request = new TopologyRequest(); - request.modelID = fieldModelMrid; - Serializable topoResponse = client.getResponse(request.toString(), "goss.gridappsd.request.data.topology", + return null; + } + +} + +class TopologyRequest implements Serializable { + + private static final long serialVersionUID = 4279262793871885409L; + + String requestType = "GET_SWITCH_AREAS"; + String modelID = null; + String resultFormat = "JSON"; + + @Override + public String toString() { + Gson gson = new Gson(); + return gson.toJson(this); + } + +} + +class TopologyRequestProcess extends Thread { + + String fieldModelMrid; + Client client; + DataManager dataManager; + SecurityConfig securityConfig; + Root root = null; + Map> messageBus_measIds_map = new HashMap>(); + Map measId_messageBus_map = new HashMap(); + + public TopologyRequestProcess(String fieldModelMrid, Client client, DataManager dataManager, + SecurityConfig securityConfig) { + this.fieldModelMrid = fieldModelMrid; + this.client = client; + this.dataManager = dataManager; + this.securityConfig = securityConfig; + } + + @Override + public void run() { + try { + Gson gson = new Gson(); + TopologyRequest request = new TopologyRequest(); + request.modelID = fieldModelMrid; + Serializable topoResponse = client.getResponse(request.toString(), "goss.gridappsd.request.data.topology", + RESPONSE_FORMAT.JSON); + int attempt = 1; + if (topoResponse == null && attempt < 6) { + // May have to wait for Topology processor to initialize + topoResponse = client.getResponse(request.toString(), "goss.gridappsd.request.data.topology", RESPONSE_FORMAT.JSON); - int attempt = 1; - if (topoResponse == null && attempt < 6) { - // May have to wait for Topology processor to initialize - topoResponse = client.getResponse(request.toString(), "goss.gridappsd.request.data.topology", - RESPONSE_FORMAT.JSON); - Thread.sleep(1000); - attempt++; - } - if (topoResponse != null && (topoResponse instanceof DataResponse)) { - String str = ((DataResponse) topoResponse).getData().toString(); - root = gson.fromJson(str, Root.class); - } else { - root = gson.fromJson(topoResponse.toString(), Root.class); + Thread.sleep(1000); + attempt++; + } + if (topoResponse != null && (topoResponse instanceof DataResponse)) { + String str = ((DataResponse) topoResponse).getData().toString(); + root = gson.fromJson(str, Root.class); + } else { + root = gson.fromJson(topoResponse.toString(), Root.class); + } + + /* + * feederList = root.feeders; if(root == null || feederList == null + * || feederList.size() == 0){ throw new + * Exception("No Feeder available to create field message bus"); } + */ + + root.feeders.message_bus_id = root.feeders.feeder_id; + int switch_area_index = 0; + for (SwitchArea switchArea : root.feeders.switch_areas) { + switchArea.message_bus_id = root.feeders.feeder_id + "." + switch_area_index; + int secondary_area_index = 0; + for (SecondaryArea secondaryArea : switchArea.secondary_areas) { + secondaryArea.message_bus_id = root.feeders.feeder_id + "." + switch_area_index + "." + + secondary_area_index; + secondary_area_index++; } - - /* - * feederList = root.feeders; if(root == null || feederList == null - * || feederList.size() == 0){ throw new - * Exception("No Feeder available to create field message bus"); } - */ - - root.feeders.message_bus_id = root.feeders.feeder_id; - int switch_area_index = 0; - for (SwitchArea switchArea : root.feeders.switch_areas) { - switchArea.message_bus_id = root.feeders.feeder_id + "." + switch_area_index; - int secondary_area_index = 0; - for (SecondaryArea secondaryArea : switchArea.secondary_areas) { - secondaryArea.message_bus_id = root.feeders.feeder_id + "." + switch_area_index + "." - + secondary_area_index; - secondary_area_index++; + switch_area_index++; + } + + this.getFieldMeasurementIds(fieldModelMrid); + + } catch (Exception e) { + e.printStackTrace(); + } + + } + + public void getFieldMeasurementIds(String fieldModelMrid) { + + PowergridModelDataRequest request = new PowergridModelDataRequest(); + request.modelId = fieldModelMrid; + request.requestType = PowergridModelDataRequest.RequestType.QUERY_OBJECT_MEASUREMENTS.toString(); + Response response = null; + List measurementList = new ArrayList(); + + try { + + // Get Feeder level measurement ids + for (String equipmentId : root.feeders.addressable_equipment) { + request.objectId = equipmentId; + response = dataManager.processDataRequest(request, "powergridmodel", null, null, + securityConfig.getManagerUser()); + if (response != null && (response instanceof DataResponse)) { + String str = ((DataResponse) response).getData().toString(); + JSONArray array = new JSONArray(str); + measurementList.clear(); + for (int i = 0; i < array.length(); i++) { + JSONObject object = array.getJSONObject(i); + String measid = object.getString("measid"); + measId_messageBus_map.put(measid, root.feeders.message_bus_id); + measurementList.add(measid); } - switch_area_index++; + messageBus_measIds_map.put(root.feeders.message_bus_id, measurementList); } - - this.getFieldMeasurementIds(fieldModelMrid); - - } catch (Exception e) { - e.printStackTrace(); + } - - } - - public void getFieldMeasurementIds(String fieldModelMrid) { - - PowergridModelDataRequest request = new PowergridModelDataRequest(); - request.modelId = fieldModelMrid; - request.requestType = PowergridModelDataRequest.RequestType.QUERY_OBJECT_MEASUREMENTS.toString(); - Response response = null; - List measurementList = new ArrayList(); - - try { - - // Get Feeder level measurement ids - for (String equipmentId : root.feeders.addressable_equipment) { + + // Get switch level measurement ids + for (SwitchArea switchArea : root.feeders.switch_areas) { + + for (String equipmentId : switchArea.addressable_equipment) { request.objectId = equipmentId; response = dataManager.processDataRequest(request, "powergridmodel", null, null, securityConfig.getManagerUser()); @@ -328,19 +367,17 @@ public void getFieldMeasurementIds(String fieldModelMrid) { for (int i = 0; i < array.length(); i++) { JSONObject object = array.getJSONObject(i); String measid = object.getString("measid"); - measId_messageBus_map.put(measid, root.feeders.message_bus_id); + measId_messageBus_map.put(measid, switchArea.message_bus_id); measurementList.add(measid); } - messageBus_measIds_map.put(root.feeders.message_bus_id, measurementList); + messageBus_measIds_map.put(switchArea.message_bus_id, measurementList); } - } - - // Get switch level measurement ids - for (SwitchArea switchArea : root.feeders.switch_areas) { - - for (String equipmentId : switchArea.addressable_equipment) { - request.objectId = equipmentId; + + // Get Secondary level measurement ids + for (SecondaryArea secondaryArea : switchArea.secondary_areas) { + for (String equipmentid : secondaryArea.addressable_equipment) { + request.objectId = equipmentid; response = dataManager.processDataRequest(request, "powergridmodel", null, null, securityConfig.getManagerUser()); if (response != null && (response instanceof DataResponse)) { @@ -350,39 +387,19 @@ public void getFieldMeasurementIds(String fieldModelMrid) { for (int i = 0; i < array.length(); i++) { JSONObject object = array.getJSONObject(i); String measid = object.getString("measid"); - measId_messageBus_map.put(measid, switchArea.message_bus_id); + measId_messageBus_map.put(measid, secondaryArea.message_bus_id); measurementList.add(measid); } - messageBus_measIds_map.put(switchArea.message_bus_id, measurementList); - } - } - - // Get Secondary level measurement ids - for (SecondaryArea secondaryArea : switchArea.secondary_areas) { - for (String equipmentid : secondaryArea.addressable_equipment) { - request.objectId = equipmentid; - response = dataManager.processDataRequest(request, "powergridmodel", null, null, - securityConfig.getManagerUser()); - if (response != null && (response instanceof DataResponse)) { - String str = ((DataResponse) response).getData().toString(); - JSONArray array = new JSONArray(str); - measurementList.clear(); - for (int i = 0; i < array.length(); i++) { - JSONObject object = array.getJSONObject(i); - String measid = object.getString("measid"); - measId_messageBus_map.put(measid, secondaryArea.message_bus_id); - measurementList.add(measid); - } - messageBus_measIds_map.put(secondaryArea.message_bus_id, measurementList); - } + messageBus_measIds_map.put(secondaryArea.message_bus_id, measurementList); } } - } - } catch (Exception e) { - e.printStackTrace(); + } - + } catch (Exception e) { + e.printStackTrace(); } - + } + +} diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/field/RequestField.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/field/RequestField.java new file mode 100644 index 00000000..01d19467 --- /dev/null +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/field/RequestField.java @@ -0,0 +1,27 @@ +package gov.pnnl.goss.gridappsd.dto.field; + +import java.io.Serializable; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; + +public class RequestField implements Serializable { + + private static final long serialVersionUID = 3088063820447700212L; + + public String request_type = null; + public String areaId = null; + + public static RequestField parse(String jsonString) throws JsonSyntaxException { + Gson gson = new Gson(); + RequestField obj = gson.fromJson(jsonString, RequestField.class); + return obj; + } + + @Override + public String toString() { + Gson gson = new Gson(); + return gson.toJson(this); + } + +} diff --git a/requirements.txt b/requirements.txt index dde65da1..93ec936b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ remote_pdb durable-rules gridappsd-python +gridappsd-field-bus diff --git a/services/context_manager.config b/services/context_manager.config new file mode 100644 index 00000000..65bcd7c6 --- /dev/null +++ b/services/context_manager.config @@ -0,0 +1,14 @@ +{ + "id": "context_manager", + "description": "GridAPPS-D Context Manager for distributed applications/agents", + "creator": "PNNL", + "inputs": [], + "outputs": [], + "static_args": [], + "execution_path": "context_manager", + "type": "EXE", + "launch_on_startup": "true", + "prereqs": ["gridappsd-topology-daemon"], + "multiple_instances": "false", + "environmentVariables": [{"envName":"GRIDAPPSD_APPLICATION_ID","envValue":"context_manager"}] +} From 2407dde55206324beee91a45d4b636af77dddc06 Mon Sep 17 00:00:00 2001 From: "C. Allwardt" <3979063+craig8@users.noreply.github.com> Date: Wed, 10 Jan 2024 10:33:11 -0800 Subject: [PATCH 3/3] Update build-gridappsd-container --- build-gridappsd-container | 70 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/build-gridappsd-container b/build-gridappsd-container index 89b0af39..3f842ca0 100755 --- a/build-gridappsd-container +++ b/build-gridappsd-container @@ -1,7 +1,67 @@ -docker pull gridappsd/gridappsd_base:master +#!/usr/bin/env python3 -./gradlew clean -rm -rf gov.pnnl.goss/gridappsd/generated -./gradlew export +""" +Script for building the gridappsd container. -docker build --no-cache --network=host -t gridappsd:local . +This script pulls the gridappsd_base docker image, cleans the project, exports the project, and builds the gridappsd container. + +Usage: + python build-gridappsd-container [--output_version ] + +Arguments: + base_version (str): The version of the gridappsd_base docker image. + output_version (str, optional): The output tag version for local development. Default is "local". + +Example: + python build-gridappsd-container.py 2.3.0 --output_version 2.3.1 + +""" + +import argparse +import subprocess +import shutil + + +def execute(cmd: str | list[str]): + """ + Executes a command in the shell and prints the output. + + Args: + cmd (str | list[str]): The command to execute. If a string, it will be split into a list of arguments. + + Raises: + subprocess.CalledProcessError: If the command returns a non-zero exit code. + + """ + if isinstance(cmd, str): + cmd = cmd.split(" ") + + with subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True) as p: + for line in iter(p.stdout.readline, ""): + if line.strip(): + print(line.strip()) + + p.wait() + if p.returncode: + raise subprocess.CalledProcessError(p.returncode, cmd=" ".join(cmd)) + + + +if __name__ == '__main__': + + parser = argparse.ArgumentParser() + + parser.add_argument("base_version", + help="The gridappsd_base docker image version") + parser.add_argument("--output_version", default="local", + help="The output tag version for local development") + + opts = parser.parse_args() + + base_version = opts.base_version + + execute(f"docker pull gridappsd/gridappsd_base:{opts.base_version}") + execute("./gradlew clean") + shutil.rmtree("gov.pnnl.goss/gridappsd/generated", ignore_errors=True) + execute("./gradlew export") + execute(f"docker build --build-arg GRIDAPPSD_BASE_VERSION=:{opts.base_version} --no-cache --network=host -t gridappsd:{opts.output_version} .")