diff --git a/.travis/main.sh b/.travis/main.sh index d393c31f..b6cf36e4 100755 --- a/.travis/main.sh +++ b/.travis/main.sh @@ -34,9 +34,12 @@ setup_dependencies() { sudo apt install realpath python python-pip -y sudo apt install --only-upgrade docker-ce -y - sudo pip install docker-compose || true - docker info + + sudo rm /usr/local/bin/docker-compose + curl -L https://github.com/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` > docker-compose + chmod +x docker-compose + sudo mv docker-compose /usr/local/bin docker-compose --version } diff --git a/Dockerfile b/Dockerfile index 611a4755..ddc87c52 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,14 +5,14 @@ ARG TIMESTAMP # Get the gridappsd-python from the proper repository RUN cd ${TEMP_DIR} \ - && git clone https://github.com/GRIDAPPSD/gridappsd-python -b master \ + && git clone https://github.com/GRIDAPPSD/gridappsd-python -b develop \ && cd gridappsd-python \ && pip3 install . \ && rm -rf /root/.cache/pip/wheels # Get the gridappsd-sensor-simulator from the proper repository RUN cd ${TEMP_DIR} \ - && git clone https://github.com/GRIDAPPSD/gridappsd-sensor-simulator -b master \ + && git clone https://github.com/GRIDAPPSD/gridappsd-sensor-simulator -b develop \ && cd gridappsd-sensor-simulator \ && pip3 install -r requirements.txt \ && mkdir -p /gridappsd/services/gridappsd-sensor-simulator \ @@ -21,7 +21,7 @@ RUN cd ${TEMP_DIR} \ && cp /gridappsd/services/gridappsd-sensor-simulator/sensor_simulator.config /gridappsd/services/ \ && rm -rf /root/.cache/pip/wheels -# Get the gridappsd-sensor-simulator from the proper repository +# Get the gridappsd-voltage-violation from the proper repository RUN cd ${TEMP_DIR} \ && git clone https://github.com/GRIDAPPSD/gridappsd-voltage-violation -b develop \ && cd gridappsd-voltage-violation \ @@ -30,6 +30,15 @@ RUN cd ${TEMP_DIR} \ && cp -r * /gridappsd/services/gridappsd-voltage-violation \ && cp /gridappsd/services/gridappsd-voltage-violation/voltage-violation.config /gridappsd/services/ +# Get the gridappsd-dnp3 from the proper repository +RUN cd ${TEMP_DIR} \ + && git clone https://github.com/GRIDAPPSD/gridappsd-dnp3 -b develop \ + && cd gridappsd-dnp3 \ + && mkdir -p /gridappsd/services/gridappsd-dnp3 \ + && rm .git -rf \ + && cp -r dnp3/* /gridappsd/services/gridappsd-dnp3 \ + && cp /gridappsd/services/gridappsd-dnp3/dnp3.config /gridappsd/services/ + # Get the gridappsd-alarms from the proper repository RUN cd ${TEMP_DIR} \ && git clone https://github.com/GRIDAPPSD/gridappsd-alarms -b develop \ @@ -39,7 +48,6 @@ RUN cd ${TEMP_DIR} \ && cp -r * /gridappsd/services/gridappsd-alarms \ && cp /gridappsd/services/gridappsd-alarms/gridappsd-alarms.config /gridappsd/services/ - # Copy initial applications and services into the container. # # NOTE: developers should mount a volume over the top of these or diff --git a/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/AppManagerTest.java b/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/AppManagerTest.java index ca2123a6..49abfb04 100644 --- a/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/AppManagerTest.java +++ b/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/AppManagerTest.java @@ -97,7 +97,7 @@ public void testConnect(){ //Step1: Create GOSS Client Credentials credentials = new UsernamePasswordCredentials( - GridAppsDConstants.username, GridAppsDConstants.password); + TestConstants.username, TestConstants.password); client = clientFactory.create(PROTOCOL.STOMP, credentials); //Create Request Simulation object diff --git a/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/BGPGModelManagerTest.java b/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/BGPGModelManagerTest.java index 4ca905f7..67c94904 100644 --- a/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/BGPGModelManagerTest.java +++ b/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/BGPGModelManagerTest.java @@ -322,7 +322,7 @@ Client getClient() throws Exception{ //Step1: Create GOSS Client Credentials credentials = new UsernamePasswordCredentials( - GridAppsDConstants.username, GridAppsDConstants.password); + TestConstants.username, TestConstants.password); // client = clientFactory.create(PROTOCOL.OPENWIRE, credentials); client = clientFactory.create(PROTOCOL.STOMP, credentials); } diff --git a/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/ConfigurationManagerTest.java b/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/ConfigurationManagerTest.java index 9a8cd3a3..5ea445d7 100644 --- a/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/ConfigurationManagerTest.java +++ b/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/ConfigurationManagerTest.java @@ -383,7 +383,7 @@ Client getClient() throws Exception{ //Step1: Create GOSS Client Credentials credentials = new UsernamePasswordCredentials( - GridAppsDConstants.username, GridAppsDConstants.password); + TestConstants.username, TestConstants.password); // client = clientFactory.create(PROTOCOL.OPENWIRE, credentials); client = clientFactory.create(PROTOCOL.STOMP, credentials); } diff --git a/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/TestConstants.java b/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/TestConstants.java index af9830a2..4ab557cd 100644 --- a/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/TestConstants.java +++ b/gov.pnnl.goss.gridappsd.test/src/gov/pnnl/goss/gridappsd/test/TestConstants.java @@ -65,5 +65,7 @@ public class TestConstants { public static final String FNCS_BRIDGE_RESPONSE = "{\"command\":\"isInitialized\",\"response\":\"true\",\"output\":\"Any messages from simulator regarding initialization\"}"; + public static final String username = "system"; + public static final String password = "password"; } diff --git a/gov.pnnl.goss.gridappsd/bnd.bnd b/gov.pnnl.goss.gridappsd/bnd.bnd index ee2d6e27..986b77e7 100644 --- a/gov.pnnl.goss.gridappsd/bnd.bnd +++ b/gov.pnnl.goss.gridappsd/bnd.bnd @@ -4,12 +4,12 @@ org.apache.felix.dependencymanager.runtime,\ biz.aQute.bnd.annotation,\ pnnl.goss.core.core-api;version=latest,\ - pnnl.goss.core.goss-core-security,\ - pnnl.goss.core.goss-core-server-api,\ + pnnl.goss.core.goss-core-security;version=latest,\ + pnnl.goss.core.goss-core-server-api;version=latest,\ pnnl.goss.core.goss-client;version=latest,\ - pnnl.goss.core.goss-core-server,\ - pnnl.goss.core.goss-core-server-web,\ - pnnl.goss.core.goss-core-server-registry,\ + pnnl.goss.core.goss-core-server;version=latest,\ + pnnl.goss.core.goss-core-server-web;version=latest,\ + pnnl.goss.core.goss-core-server-registry;version=latest,\ com.springsource.javax.jms,\ slf4j.api,\ org.apache.activemq.activemq-osgi,\ @@ -29,7 +29,7 @@ osgi.enroute.base.api,\ org.mockito.mockito-all,\ httpcore,\ - blazegraph.cim2glm;version=12.0.0,\ + blazegraph.cim2glm;version=18.0.0,\ httpclient,\ com.bigdata.rdf,\ proven-client;version=0.2.3,\ diff --git a/gov.pnnl.goss.gridappsd/conf/org.ops4j.pax.logging.cfg b/gov.pnnl.goss.gridappsd/conf/org.ops4j.pax.logging.cfg index 64546590..51f29f96 100644 --- a/gov.pnnl.goss.gridappsd/conf/org.ops4j.pax.logging.cfg +++ b/gov.pnnl.goss.gridappsd/conf/org.ops4j.pax.logging.cfg @@ -12,7 +12,7 @@ log4j.appender.file.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | % #%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.layout.ConversionPattern=%t %p [%c] - %m%n +log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %t %p [%c] - %m%n log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.threshold=INFO diff --git a/gov.pnnl.goss.gridappsd/conf/pnnl.goss.core.security.propertyfile.cfg b/gov.pnnl.goss.gridappsd/conf/pnnl.goss.core.security.propertyfile.cfg index 8df6bac1..8ef8d1e9 100644 --- a/gov.pnnl.goss.gridappsd/conf/pnnl.goss.core.security.propertyfile.cfg +++ b/gov.pnnl.goss.gridappsd/conf/pnnl.goss.core.security.propertyfile.cfg @@ -1,26 +1,18 @@ -user1=123,queue:*,topic:ActiveMQ.Advisory.*,temp-queue:*,fusion:*:read,fusion:CapacityRequirement:write,fusion:InterfacesViolation:write,fusion:VoltageStabilityViolation:write -user2=123,queue:*,topic:ActiveMQ.Advisory.*,temp-queue:*,fusion:*:read,fusion:GeneratorData:write -user3=123,queue:*,topic:ActiveMQ.Advisory.*,temp-queue:*,fusion:*:read -tony=tony,queue:*,topic:ActiveMQ.Advisory.*,temp-queue:*,fusion:*:read,fusion:CapacityRequirement:write,fusion:InterfacesViolation:write,fusion:VoltageStabilityViolation:write -pavel=pavel,queue:*,topic:ActiveMQ.Advisory.*,topic:/goss/fusion/*,temp-queue:*,fusion:*:read,fusion:*:write -erin=erin,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -yousu=yousu,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -poorva=123,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,topic:synthdata/request/*,topic:synthdata/poorva/*,fusion:*:read -admin=admin12345,queue:*,topic:*,temp-queue:* system=manager,queue:*,topic:*,temp-queue:*,fusion:*:read,fusion:*:write -user1=user1,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user2=user2,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user3=user3,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user4=user4,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user5=user5,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user6=user6,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user7=user7,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user8=user8,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user9=user9,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user10=user10,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user11=user11,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user12=user12,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user13=user13,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user14=user14,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -user15=user15,queue:*,topic:ActiveMQ.Advisory.*,topic:goss/fusion/*,temp-queue:*,fusion:*:read -fncs=fncs,queue:*,topic:*,temp-queue:*,fusion:*:read,fusion:*:write +operator1=operator,queue:*,topic:ActiveMQ.Advisory.*,topic:goss.gridappsd.process.request.status.*,topic:goss.gridappsd.simulation.*,temp-queue:*,fusion:*:read +operator2=operator,queue:*,topic:ActiveMQ.Advisory.*,topic:goss.gridappsd.process.request.status.*,topic:goss.gridappsd.simulation.*,temp-queue:*,fusion:*:read +operator3=operator,queue:*,topic:ActiveMQ.Advisory.*,topic:goss.gridappsd.process.request.status.*,topic:goss.gridappsd.simulation.*,temp-queue:*,fusion:*:read +evaluator1=evaluator,queue:*,topic:ActiveMQ.Advisory.*,topic:goss.gridappsd.process.request.status.*,topic:goss.gridappsd.simulation.*,temp-queue:*,fusion:*:read +evaluator2=evaluator,queue:*,topic:ActiveMQ.Advisory.*,topic:goss.gridappsd.process.request.status.*,topic:goss.gridappsd.simulation.*,temp-queue:*,fusion:*:read +testmanager1=testmanager,queue:*,topic:ActiveMQ.Advisory.*,topic:goss.gridappsd.process.request.status.*,topic:goss.gridappsd.simulation.*,temp-queue:*,fusion:*:read +testmanager2=testmanager,queue:*,topic:ActiveMQ.Advisory.*,topic:goss.gridappsd.process.request.status.*,topic:goss.gridappsd.simulation.*,temp-queue:*,fusion:*:read +application1=application,queue:*,topic:ActiveMQ.Advisory.*,topic:goss.gridappsd.process.request.status.*,topic:goss.gridappsd.simulation.*,temp-queue:*,fusion:*:read +application2=application,queue:*,topic:ActiveMQ.Advisory.*,topic:goss.gridappsd.process.request.status.*,topic:goss.gridappsd.simulation.*,temp-queue:*,fusion:*:read +service1=service,queue:*,topic:ActiveMQ.Advisory.*,topic:goss.gridappsd.process.request.status.*,topic:goss.gridappsd.simulation.*,temp-queue:*,fusion:*:read +service2=service,queue:*,topic:ActiveMQ.Advisory.*,topic:goss.gridappsd.process.request.status.*,topic:goss.gridappsd.simulation.*,temp-queue:*,fusion:*:read + + + + + + diff --git a/gov.pnnl.goss.gridappsd/conf/pnnl.goss.core.server.cfg b/gov.pnnl.goss.gridappsd/conf/pnnl.goss.core.server.cfg index dce33f97..aac9fe9e 100644 --- a/gov.pnnl.goss.gridappsd/conf/pnnl.goss.core.server.cfg +++ b/gov.pnnl.goss.gridappsd/conf/pnnl.goss.core.server.cfg @@ -3,10 +3,6 @@ # compilation 'mvn compile' the place holders @@ will be # replaced with values from a global goss.properties file. -# This user is equivelant to root. Must have '*' -# permissions on the message bus. -goss.system.manager = system -goss.system.manager.password = manager # The following are used for the core-client connection. goss.openwire.uri = tcp://0.0.0.0:61616 diff --git a/gov.pnnl.goss.gridappsd/conf/pnnl.goss.gridappsd.security.rolefile.cfg b/gov.pnnl.goss.gridappsd/conf/pnnl.goss.gridappsd.security.rolefile.cfg new file mode 100644 index 00000000..2389ba34 --- /dev/null +++ b/gov.pnnl.goss.gridappsd/conf/pnnl.goss.gridappsd.security.rolefile.cfg @@ -0,0 +1,12 @@ +system=admin,operator,evaluator,testmanager,application,service +operator1=operator +operator2=operator +operator3=operator +evaluator1=evaluator,operator +evaluator2=evaluator,operator +testmanager1=testmanager +testmanager2=testmanager +application1=application +application2=application +service1=service +service2=service \ No newline at end of file diff --git a/gov.pnnl.goss.gridappsd/conf/pnnl.goss.security.cfg b/gov.pnnl.goss.gridappsd/conf/pnnl.goss.security.cfg new file mode 100644 index 00000000..30475a79 --- /dev/null +++ b/gov.pnnl.goss.gridappsd/conf/pnnl.goss.security.cfg @@ -0,0 +1,17 @@ +# Configuration for the goss-core module. Each jar in +# goss-core can be configured using the following. During +# compilation 'mvn compile' the place holders @@ will be +# replaced with values from a global goss.properties file. + +# This user is equivelant to root. Must have '*' +# permissions on the message bus. +goss.system.manager = system +goss.system.manager.password = manager + +# Used in the core-ssl.bndrun file +client.keystore = ${client.keystore} +client.keystore.password = ${client.keystore.password} +client.truststore = ${client.truststore} +client.truststore.password = ${client.truststore.password} +ssl.enabled = false + diff --git a/gov.pnnl.goss.gridappsd/gridappsd.bnd b/gov.pnnl.goss.gridappsd/gridappsd.bnd index 21988ab3..00bd17c1 100644 --- a/gov.pnnl.goss.gridappsd/gridappsd.bnd +++ b/gov.pnnl.goss.gridappsd/gridappsd.bnd @@ -2,6 +2,7 @@ Bundle-Version: 0.0.1.${tstamp} Private-Package: \ gov.pnnl.goss.gridappsd.configuration,\ + gov.pnnl.goss.gridappsd.role,\ gov.pnnl.goss.gridappsd.data.conversion,\ gov.pnnl.goss.gridappsd.data.handlers,\ gov.pnnl.goss.gridappsd.data,\ diff --git a/gov.pnnl.goss.gridappsd/run.bnd.bndrun b/gov.pnnl.goss.gridappsd/run.bnd.bndrun index a8d8c178..8dc7d919 100644 --- a/gov.pnnl.goss.gridappsd/run.bnd.bndrun +++ b/gov.pnnl.goss.gridappsd/run.bnd.bndrun @@ -74,7 +74,7 @@ httpcore,\ httpclient,\ xml-apis,\ - blazegraph.cim2glm;version=12.0.0,\ + blazegraph.cim2glm;version=18.0.0,\ org.eclipse.jetty.aggregate.jetty-all-server;version=7.6.9,\ javax.servlet-api,\ com.bigdata.rdf,\ diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/api/LogManager.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/api/LogManager.java index 6ddbbc08..600603c5 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/api/LogManager.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/api/LogManager.java @@ -66,7 +66,7 @@ public interface LogManager { * @param topic * Message should be published on this topic if topic is not null. */ - void log(LogMessage message, String topic); +// void log(LogMessage message, String topic); /** * Implementation of this method should call an implementation of diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/api/RoleManager.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/api/RoleManager.java new file mode 100644 index 00000000..a7c7256c --- /dev/null +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/api/RoleManager.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2017, Battelle Memorial Institute All rights reserved. + * Battelle Memorial Institute (hereinafter Battelle) hereby grants permission to any person or entity + * lawfully obtaining a copy of this software and associated documentation files (hereinafter the + * Software) to redistribute and use the Software in source and binary forms, with or without modification. + * Such person or entity may use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and may permit others to do so, subject to the following conditions: + * Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimers. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or other materials provided with the distribution. + * Other than as used herein, neither the name Battelle Memorial Institute or Battelle may be used in any + * form whatsoever without the express written consent of Battelle. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * BATTELLE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * General disclaimer for use with OSS licenses + * + * This material was prepared as an account of work sponsored by an agency of the United States Government. + * Neither the United States Government nor the United States Department of Energy, nor Battelle, nor any + * of their employees, nor any jurisdiction or organization that has cooperated in the development of these + * materials, makes any warranty, express or implied, or assumes any legal liability or responsibility for + * the accuracy, completeness, or usefulness or any information, apparatus, product, software, or process + * disclosed, or represents that its use would not infringe privately owned rights. + * + * Reference herein to any specific commercial product, process, or service by trade name, trademark, manufacturer, + * or otherwise does not necessarily constitute or imply its endorsement, recommendation, or favoring by the United + * States Government or any agency thereof, or Battelle Memorial Institute. The views and opinions of authors expressed + * herein do not necessarily state or reflect those of the United States Government or any agency thereof. + * + * PACIFIC NORTHWEST NATIONAL LABORATORY operated by BATTELLE for the + * UNITED STATES DEPARTMENT OF ENERGY under Contract DE-AC05-76RL01830 + ******************************************************************************/ +package gov.pnnl.goss.gridappsd.api; + +import java.util.List; + + +public interface RoleManager { + + List getRoles(String userName) throws Exception; + + boolean hasRole(String userName, String roleName) throws Exception; + + //TODO + //addRole(user, role) + //removeRole(user, role) +} diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/api/ServiceManager.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/api/ServiceManager.java index 4084a2d8..61dc2d87 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/api/ServiceManager.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/api/ServiceManager.java @@ -90,7 +90,7 @@ public interface ServiceManager { * Start service instance * @param service_id Registered ID of the desired service * @param runtimeOptions Runtime options for the service instance, in most cases these will be passed in on the command-line - * @return String containing service instance ID + * @return String containing service instance ID or null if service is already started for that simulation */ String startService(String service_id, HashMap runtimeOptions); //may also need input/output topics or simulation id @@ -111,9 +111,10 @@ public interface ServiceManager { /** * Lists currently running service instances for the requested service ID * @param serviceId Registered ID of the service to list + * @param simulationId Id of the simulation * @return List of ServiceInstance objects */ - List listRunningServices(String serviceId); + List listRunningServices(String serviceId, String simulationId); /** * Stops service instance diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/app/AppManagerImpl.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/app/AppManagerImpl.java index ed8bb82a..6f955129 100755 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/app/AppManagerImpl.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/app/AppManagerImpl.java @@ -74,6 +74,7 @@ import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; + import com.google.gson.JsonSyntaxException; import gov.pnnl.goss.gridappsd.api.AppManager; @@ -96,6 +97,7 @@ import pnnl.goss.core.ClientFactory; import pnnl.goss.core.DataError; import pnnl.goss.core.DataResponse; +import pnnl.goss.core.security.SecurityConfig; /** * This class implements subset of functionalities for Internal Functions 405 @@ -119,6 +121,9 @@ public class AppManagerImpl implements AppManager { @ServiceDependency private volatile LogManager logManager; + + @ServiceDependency + private volatile SecurityConfig securityConfig; private Dictionary configurationProperties; @@ -146,12 +151,14 @@ public AppManagerImpl(LogManager logManager, public void process(int processId, DataResponse event, Serializable message) throws Exception { - // TODO:Get username from message's metadata e.g. event.getUserName() - username = GridAppsDConstants.username; + // Get username from message's metadata e.g. event.getUserName() + username = event.getUsername(); - if (client == null) { + + + if (client == null) { Credentials credentials = new UsernamePasswordCredentials( - GridAppsDConstants.username, GridAppsDConstants.password); + securityConfig.getManagerUser(), securityConfig.getManagerPassword()); client = clientFactory.create(PROTOCOL.STOMP, credentials); } Destination replyDestination = event.getReplyDestination(); @@ -258,7 +265,7 @@ public void start(){ "Starting "+this.getClass().getName(), LogLevel.INFO, ProcessStatus.RUNNING, - true),GridAppsDConstants.username, + true),securityConfig.getManagerUser(), GridAppsDConstants.topic_platformLog); scanForApps(); @@ -269,7 +276,7 @@ public void start(){ String.format("Found %s applications", apps.size()), LogLevel.INFO, ProcessStatus.RUNNING, - true),GridAppsDConstants.username); + true),securityConfig.getManagerUser(),GridAppsDConstants.topic_platformLog); }catch(Exception e){ e.printStackTrace(); } @@ -548,6 +555,7 @@ else if (AppType.PYTHON.equals(appInfo.getType())) { simulationId, new Date().getTime(), "Starting app with command "+ String.join(" ",commands), LogLevel.INFO, ProcessStatus.RUNNING, true), + securityConfig.getManagerUser(), GridAppsDConstants.topic_simulationLog+simulationId); try { process = processAppBuilder.start(); diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/BaseConfigurationHandler.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/BaseConfigurationHandler.java index 0afc5066..ebc1bf38 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/BaseConfigurationHandler.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/BaseConfigurationHandler.java @@ -64,7 +64,8 @@ public abstract class BaseConfigurationHandler implements ConfigurationHandler // private volatile LogManager logManager; - + public static final String MODELSTATE = "model_state"; + public BaseConfigurationHandler() { } @@ -82,7 +83,7 @@ void logRunning(String message, String simulationID, String username, LogManager } void logRunning(String message, String simulationID, String username, LogManager logManager, LogLevel logLevel){ logManager.log( - new LogMessage(this.getClass().getName(), new Integer( + new LogMessage(this.getClass().getSimpleName(), new Integer( simulationID).toString(), new Date().getTime(), message, logLevel, ProcessStatus.RUNNING, false), username, diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/CIMDictionaryConfigurationHandler.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/CIMDictionaryConfigurationHandler.java index dc1272d6..4796881b 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/CIMDictionaryConfigurationHandler.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/CIMDictionaryConfigurationHandler.java @@ -50,7 +50,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.Gson; + import gov.pnnl.goss.cim2glm.CIMImporter; +import gov.pnnl.goss.cim2glm.dto.ModelState; import gov.pnnl.goss.cim2glm.queryhandler.QueryHandler; import gov.pnnl.goss.gridappsd.api.ConfigurationHandler; import gov.pnnl.goss.gridappsd.api.ConfigurationManager; @@ -60,6 +63,7 @@ import gov.pnnl.goss.gridappsd.api.SimulationManager; import gov.pnnl.goss.gridappsd.data.handlers.BlazegraphQueryHandler; import gov.pnnl.goss.gridappsd.dto.LogMessage.LogLevel; +import gov.pnnl.goss.gridappsd.dto.ConfigurationRequest; import gov.pnnl.goss.gridappsd.dto.SimulationContext; import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; import pnnl.goss.core.Client; @@ -114,8 +118,8 @@ public void generateConfig(Properties parameters, PrintWriter out, String proces String simulationId = GridAppsDConstants.getStringProperty(parameters, SIMULATIONID, null); boolean useHouses = false; - if(parameters.containsKey(USEHOUSES)) - useHouses = GridAppsDConstants.getBooleanProperty(parameters, USEHOUSES, false); + if(parameters.containsKey(USEHOUSES)) + useHouses = GridAppsDConstants.getBooleanProperty(parameters, USEHOUSES, false); File configFile = null; if(simulationId!=null){ SimulationContext simulationContext = simulationManager.getSimulationContextForId(simulationId); @@ -132,15 +136,21 @@ public void generateConfig(Properties parameters, PrintWriter out, String proces } } - - + ModelState modelState = new ModelState(); + String modelStateStr = GridAppsDConstants.getStringProperty(parameters, MODELSTATE, null); + if(modelStateStr==null || modelStateStr.trim().length()==0){ + logRunning("No "+MODELSTATE+" parameter provided", processId, username, logManager); + } else { + Gson gson = new Gson(); + modelState = gson.fromJson(modelStateStr, ModelState.class); + } + String modelId = GridAppsDConstants.getStringProperty(parameters, MODELID, null); if(modelId==null || modelId.trim().length()==0){ logError("No "+MODELID+" parameter provided", processId, username, logManager); throw new Exception("Missing parameter "+MODELID); } - String bgHost = configManager.getConfigurationProperty(GridAppsDConstants.BLAZEGRAPH_HOST_PATH); if(bgHost==null || bgHost.trim().length()==0){ bgHost = BlazegraphQueryHandler.DEFAULT_ENDPOINT; @@ -153,9 +163,9 @@ public void generateConfig(Properties parameters, PrintWriter out, String proces CIMImporter cimImporter = new CIMImporter(); //If the simulation info is available also write to file if(configFile!=null){ - cimImporter.generateDictionaryFile(queryHandler, new PrintWriter(new FileWriter(configFile)),useHouses); + cimImporter.generateDictionaryFile(queryHandler, new PrintWriter(new FileWriter(configFile)),useHouses,modelState); } else { - cimImporter.generateDictionaryFile(queryHandler, out, useHouses); + cimImporter.generateDictionaryFile(queryHandler, out, useHouses,modelState); } if(configFile!=null){ //config was written to file, so return that diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/ConfigurationManagerImpl.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/ConfigurationManagerImpl.java index 7fa89fca..b8cbd1c7 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/ConfigurationManagerImpl.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/ConfigurationManagerImpl.java @@ -99,6 +99,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager{ private HashMap configHandlers = new HashMap(); + public ConfigurationManagerImpl() { } @@ -178,6 +179,12 @@ public void generateConfiguration(String type, Properties parameters, PrintWrite GridAppsDConstants.topic_platformLog); throw new Exception("No configuration handler registered for '"+type+"'"); } + + + } + + + } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/DSSAllConfigurationHandler.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/DSSAllConfigurationHandler.java index a86e3f56..e31e8d09 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/DSSAllConfigurationHandler.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/DSSAllConfigurationHandler.java @@ -40,6 +40,7 @@ package gov.pnnl.goss.gridappsd.configuration; import gov.pnnl.goss.cim2glm.CIMImporter; +import gov.pnnl.goss.cim2glm.dto.ModelState; import gov.pnnl.goss.cim2glm.queryhandler.QueryHandler; import gov.pnnl.goss.gridappsd.api.ConfigurationHandler; import gov.pnnl.goss.gridappsd.api.ConfigurationManager; @@ -49,6 +50,7 @@ import gov.pnnl.goss.gridappsd.api.SimulationManager; import gov.pnnl.goss.gridappsd.data.handlers.BlazegraphQueryHandler; import gov.pnnl.goss.gridappsd.dto.SimulationContext; +import gov.pnnl.goss.gridappsd.dto.LogMessage.LogLevel; import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; import java.io.File; @@ -61,6 +63,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.Gson; + import pnnl.goss.core.Client; @@ -186,6 +190,16 @@ public void generateConfig(Properties parameters, PrintWriter out, String proces }catch (Exception e) { logError("Simulation ID not a valid integer "+simulationID+", defaulting to "+simId, simulationID, username, logManager); } + + ModelState modelState = new ModelState(); + String modelStateStr = GridAppsDConstants.getStringProperty(parameters, MODELSTATE, null); + if(modelStateStr==null || modelStateStr.trim().length()==0){ + logRunning("No "+MODELSTATE+" parameter provided", processId, username, logManager); + } else { + Gson gson = new Gson(); + modelState = gson.fromJson(modelStateStr, ModelState.class); + } + long simulationStartTime = GridAppsDConstants.getLongProperty(parameters, SIMULATIONSTARTTIME, -1); if(simulationStartTime<0){ logError("No "+SIMULATIONSTARTTIME+" parameter provided", processId, username, logManager); @@ -208,11 +222,13 @@ public void generateConfig(Properties parameters, PrintWriter out, String proces boolean useHouses = GridAppsDConstants.getBooleanProperty(parameters, USEHOUSES, false); + boolean bHaveEventGen = true; + //TODO add climate //CIM2GLM utility uses CIMImporter cimImporter = new CIMImporter(); - cimImporter.start(queryHandler, CONFIGTARGET, fRoot, scheduleName, loadScale, bWantSched, bWantZip, bWantRandomFractions, useHouses, zFraction, iFraction, pFraction); + cimImporter.start(queryHandler, CONFIGTARGET, fRoot, scheduleName, loadScale, bWantSched, bWantZip, bWantRandomFractions, useHouses, zFraction, iFraction, pFraction, bHaveEventGen, modelState, false); logRunning("Finished generating all DSS configuration files.", processId, username, logManager); diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDAllConfigurationHandler.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDAllConfigurationHandler.java index 888e57ae..868913d9 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDAllConfigurationHandler.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDAllConfigurationHandler.java @@ -56,20 +56,24 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import gov.pnnl.goss.cim2glm.CIMImporter; +import gov.pnnl.goss.cim2glm.dto.ModelState; import gov.pnnl.goss.cim2glm.queryhandler.QueryHandler; import gov.pnnl.goss.gridappsd.api.ConfigurationHandler; import gov.pnnl.goss.gridappsd.api.ConfigurationManager; import gov.pnnl.goss.gridappsd.api.DataManager; import gov.pnnl.goss.gridappsd.api.LogManager; import gov.pnnl.goss.gridappsd.api.PowergridModelDataManager; +import gov.pnnl.goss.gridappsd.api.SimulationManager; import gov.pnnl.goss.gridappsd.data.ProvenTimeSeriesDataManagerImpl; import gov.pnnl.goss.gridappsd.data.conversion.ProvenWeatherToGridlabdWeatherConverter; import gov.pnnl.goss.gridappsd.data.handlers.BlazegraphQueryHandler; import gov.pnnl.goss.gridappsd.dto.LogMessage.LogLevel; import gov.pnnl.goss.gridappsd.dto.RequestTimeseriesData; +import gov.pnnl.goss.gridappsd.dto.SimulationContext; import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; import pnnl.goss.core.Client; import pnnl.goss.core.DataResponse; @@ -86,6 +90,8 @@ public class GLDAllConfigurationHandler extends BaseConfigurationHandler impleme @ServiceDependency private volatile PowergridModelDataManager powergridModelManager; @ServiceDependency + private volatile SimulationManager simulationManager; + @ServiceDependency volatile LogManager logManager; @ServiceDependency volatile DataManager dataManager; @@ -110,9 +116,11 @@ public class GLDAllConfigurationHandler extends BaseConfigurationHandler impleme public static final String SIMULATIONBROKERPORT = "simulation_broker_port"; public static final String STARTTIME_FILTER = "startTime"; public static final String ENDTIME_FILTER = "endTime"; + public static final String MODEL_STATE = "model_state"; public static final int TIMEFILTER_YEAR = 2013; - public static final String CONFIGTARGET = "glm"; +// public static final String CONFIGTARGET = "glm"; + public static final String CONFIGTARGET = "both"; //will build files for both glm and dss public static final String CIM2GLM_PREFIX = "model"; public static final String BASE_FILENAME = CIM2GLM_PREFIX+"_base.glm"; @@ -198,6 +206,16 @@ public void generateConfig(Properties parameters, PrintWriter out, String proces }catch (Exception e) { logError("Simulation ID not a valid integer "+simulationID+", defaulting to "+simId, simulationID, username, logManager); } + + ModelState modelState = new ModelState(); + String modelStateStr = GridAppsDConstants.getStringProperty(parameters, MODELSTATE, null); + if(modelStateStr==null || modelStateStr.trim().length()==0){ + logRunning("No "+MODELSTATE+" parameter provided", processId, username, logManager); + } else { + Gson gson = new Gson(); + modelState = gson.fromJson(modelStateStr, ModelState.class); + } + long simulationStartTime = GridAppsDConstants.getLongProperty(parameters, SIMULATIONSTARTTIME, -1); if(simulationStartTime<0){ logError("No "+SIMULATIONSTARTTIME+" parameter provided", processId, username, logManager); @@ -222,10 +240,12 @@ public void generateConfig(Properties parameters, PrintWriter out, String proces boolean useHouses = GridAppsDConstants.getBooleanProperty(parameters, USEHOUSES, false); //TODO boolean useClimate = true;//GridAppsDConstants.getBooleanProperty(parameters, USECLIMATE, false); + + boolean bHaveEventGen = true; //CIM2GLM utility uses CIMImporter cimImporter = new CIMImporter(); - cimImporter.start(queryHandler, CONFIGTARGET, fRoot, scheduleName, loadScale, bWantSched, bWantZip, bWantRandomFractions, useHouses, zFraction, iFraction, pFraction); + cimImporter.start(queryHandler, CONFIGTARGET, fRoot, scheduleName, loadScale, bWantSched, bWantZip, bWantRandomFractions, useHouses, zFraction, iFraction, pFraction, bHaveEventGen, modelState, false); String tempDataPath = dir.getAbsolutePath(); //If use climate, then generate gridlabd weather data file @@ -387,6 +407,9 @@ protected void generateStartupFile(Properties parameters, String tempDataPath, P startupFileWriter.println("module connection;"); startupFileWriter.println("module generators;"); startupFileWriter.println("module tape;"); + startupFileWriter.println("module reliability {"); + startupFileWriter.println(" report_event_log false;"); + startupFileWriter.println("};"); startupFileWriter.println("module powerflow {"); startupFileWriter.println(" line_capacitance TRUE;"); startupFileWriter.println(" solver_method "+solverMethod+";"); diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDBaseConfigurationHandler.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDBaseConfigurationHandler.java index 3e96f63c..dcfa6213 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDBaseConfigurationHandler.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDBaseConfigurationHandler.java @@ -177,13 +177,15 @@ public void generateConfig(Properties parameters, PrintWriter out, String proces boolean useHouses = GridAppsDConstants.getBooleanProperty(parameters, USEHOUSES, false); + boolean bHaveEventGen = true; + CIMImporter cimImporter = new CIMImporter(); //If the simulation info is available also write to file if(configFile!=null){ - cimImporter.generateGLMFile(queryHandler, new PrintWriter(new FileWriter(configFile)), scheduleName, loadScale, bWantSched, bWantZip, bWantRandomFractions, useHouses, zFraction, iFraction, pFraction); + cimImporter.generateGLMFile(queryHandler, new PrintWriter(new FileWriter(configFile)), scheduleName, loadScale, bWantSched, bWantZip, bWantRandomFractions, useHouses, zFraction, iFraction, pFraction, bHaveEventGen); } else { - cimImporter.generateGLMFile(queryHandler, out, scheduleName, loadScale, bWantSched, bWantZip, bWantRandomFractions, useHouses,zFraction, iFraction, pFraction); + cimImporter.generateGLMFile(queryHandler, out, scheduleName, loadScale, bWantSched, bWantZip, bWantRandomFractions, useHouses,zFraction, iFraction, pFraction, bHaveEventGen); } if(configFile!=null){ //config was written to file, so return that diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDSimulationOutputConfigurationHandler.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDSimulationOutputConfigurationHandler.java index afd1e1cb..3262a557 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDSimulationOutputConfigurationHandler.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDSimulationOutputConfigurationHandler.java @@ -68,6 +68,7 @@ import com.google.gson.JsonPrimitive; import gov.pnnl.goss.cim2glm.CIMImporter; +import gov.pnnl.goss.cim2glm.dto.ModelState; import gov.pnnl.goss.cim2glm.queryhandler.QueryHandler; import gov.pnnl.goss.gridappsd.api.ConfigurationHandler; import gov.pnnl.goss.gridappsd.api.ConfigurationManager; @@ -157,6 +158,14 @@ public void generateConfig(Properties parameters, PrintWriter out, String proces throw new Exception("Missing parameter "+MODELID); } + ModelState modelState = new ModelState(); + String modelStateStr = GridAppsDConstants.getStringProperty(parameters, MODELSTATE, null); + if(modelStateStr==null || modelStateStr.trim().length()==0){ + logRunning("No "+MODELSTATE+" parameter provided", processId, username, logManager); + } else { + Gson gson = new Gson(); + modelState = gson.fromJson(modelStateStr, ModelState.class); + } //If passed in, use location of dictionary file, otherwise it will attempt to generate it String dictFilePath = GridAppsDConstants.getStringProperty(parameters, DICTIONARY_FILE, null); if(dictFilePath!=null){ @@ -182,7 +191,7 @@ public void generateConfig(Properties parameters, PrintWriter out, String proces StringWriter dictionaryStringOutput = new StringWriter(); PrintWriter dictionaryOutput = new PrintWriter(dictionaryStringOutput); - cimImporter.generateDictionaryFile(queryHandler, dictionaryOutput, useHouses); + cimImporter.generateDictionaryFile(queryHandler, dictionaryOutput, useHouses, modelState); String dictOut = dictionaryStringOutput.toString(); measurementFileReader = null; if(dictFile!=null && dictFile.getName().length()>0 && !dictFile.exists()){ diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDZiploadScheduleConfigurationHandler.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDZiploadScheduleConfigurationHandler.java index bbd479b9..bcc7ccb8 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDZiploadScheduleConfigurationHandler.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/GLDZiploadScheduleConfigurationHandler.java @@ -66,6 +66,7 @@ import pnnl.goss.core.Client; import pnnl.goss.core.DataResponse; +import pnnl.goss.core.security.SecurityConfig; @Component public class GLDZiploadScheduleConfigurationHandler extends @@ -79,6 +80,8 @@ public class GLDZiploadScheduleConfigurationHandler extends volatile LogManager logManager; @ServiceDependency volatile DataManager dataManager; + @ServiceDependency + volatile SecurityConfig securityConfig; public static final String TYPENAME = "GridLAB-D Zipload Schedule"; public static final String DIRECTORY = "directory"; @@ -117,6 +120,7 @@ public void start() { LogLevel.WARN, ProcessStatus.ERROR, true), + securityConfig.getManagerUser(), GridAppsDConstants.topic_platformLog); } } @@ -153,16 +157,12 @@ public void generateConfig(Properties parameters, PrintWriter out, processId, username, logManager); throw new Exception("Missing parameter " + SIMULATIONDURATION); } - long simulationEndTime = simulationStartTime - + (1000 * simulationDuration); - File dir = new File(directory); if (!dir.exists()) { dir.mkdirs(); } String tempDataPath = dir.getAbsolutePath(); - String fRoot = dir.getAbsolutePath() + File.separator + CIM2GLM_PREFIX; - + String simulationID = GridAppsDConstants.getStringProperty(parameters, SIMULATIONID, null); int simId = -1; if(simulationID==null || simulationID.trim().length()==0){ @@ -181,19 +181,14 @@ public void generateConfig(Properties parameters, PrintWriter out, Map queryFilter = new HashMap(); Calendar c = Calendar.getInstance(); - // For both the start and end time, set the year to the one that - // currently has data in the database - // TODO either we need more weather data in the database, or make this - // more flexible where we only have to search by month/day c.setTime(new Date(simulationStartTime*1000)); + int simulationYear = c.get(Calendar.YEAR); c.set(Calendar.YEAR, TIMEFILTER_YEAR); - // Convert to UTC time until the input time is correct - // //TODO this will be changed in the future - // c.add(Calendar.HOUR, 6); queryFilter.put(STARTTIME_FILTER, "" + c.getTimeInMillis()); c.add(Calendar.SECOND, new Long(simulationDuration).intValue()); queryFilter.put(ENDTIME_FILTER, "" + c.getTimeInMillis()); request.setQueryFilter(queryFilter); + request.setSimulationYear(simulationYear); DataResponse resp = (DataResponse) dataManager.processDataRequest( request, ProvenTimeSeriesDataManagerImpl.DATA_MANAGER_TYPE, simId, diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/YBusExportConfigurationHandler.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/YBusExportConfigurationHandler.java index 4ffcd41b..8533267d 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/YBusExportConfigurationHandler.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/configuration/YBusExportConfigurationHandler.java @@ -152,10 +152,13 @@ simulationId, new Date().getTime(), ProcessStatus.RUNNING, true), username, GridAppsDConstants.topic_simulationLog+simulationId); - //Create DSS base file - PrintWriter basePrintWriter = new PrintWriter(new StringWriter()); - DSSAllConfigurationHandler baseConfigurationHandler = new DSSAllConfigurationHandler(logManager,simulationManager,configManager); - baseConfigurationHandler.generateConfig(parameters, basePrintWriter, simulationId, username); + if(!dssBaseFile.exists()) { + //Create DSS base file if it doesn't already exist + PrintWriter basePrintWriter = new PrintWriter(new StringWriter()); + DSSAllConfigurationHandler baseConfigurationHandler = new DSSAllConfigurationHandler(logManager,simulationManager,configManager); + baseConfigurationHandler.generateConfig(parameters, basePrintWriter, simulationId, username); + } + if(!dssBaseFile.exists()) throw new Exception("Error: Could not create DSS base file to export YBus matrix"); diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/BGPowergridModelDataManagerImpl.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/BGPowergridModelDataManagerImpl.java index b0231b6c..d5f5256e 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/BGPowergridModelDataManagerImpl.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/BGPowergridModelDataManagerImpl.java @@ -199,7 +199,7 @@ public String query(String modelId, String query, String resultFormat, String pr public ResultSet queryResultSet(String modelId, String query, String processId, String username) { String endpoint = getEndpointURL(modelId); BlazegraphQueryHandler queryHandler = new BlazegraphQueryHandler(endpoint, logManager, processId, username); - return queryHandler.query(query); + return queryHandler.query(query, null); } @@ -231,7 +231,7 @@ public ResultSet queryObjectResultSet(String modelId, String mrid, String proces String query = "select ?property ?value where {<"+getEndpointNS(mrid)+"> ?property ?value}"; BlazegraphQueryHandler queryHandler = new BlazegraphQueryHandler(getEndpointURL(modelId), logManager, processId, username); - ResultSet rs = queryHandler.query(query); + ResultSet rs = queryHandler.query(query, null); return rs; } @@ -251,7 +251,7 @@ public List queryObjectTypeList(String modelId, String processId, String } query = query + "}"; BlazegraphQueryHandler queryHandler = new BlazegraphQueryHandler(getEndpointURL(modelId), logManager, processId, username); - ResultSet rs = queryHandler.query(query); + ResultSet rs = queryHandler.query(query, null); List objectTypes = new ArrayList(); while(rs.hasNext()){ diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/LogDataManagerMySQL.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/LogDataManagerMySQL.java index bb227d73..73b71986 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/LogDataManagerMySQL.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/LogDataManagerMySQL.java @@ -44,7 +44,6 @@ import gov.pnnl.goss.gridappsd.dto.LogMessage.LogLevel; import gov.pnnl.goss.gridappsd.dto.LogMessage.ProcessStatus; import gov.pnnl.goss.gridappsd.dto.RequestLogMessage; -import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; import java.io.Serializable; import java.sql.Connection; @@ -72,6 +71,7 @@ import pnnl.goss.core.Client; import pnnl.goss.core.Client.PROTOCOL; import pnnl.goss.core.ClientFactory; +import pnnl.goss.core.security.SecurityConfig; @Component @@ -83,9 +83,14 @@ public class LogDataManagerMySQL implements LogDataManager, DataManagerHandler { @ServiceDependency ClientFactory clientFactory; + @ServiceDependency + SecurityConfig securityConfig; + private Connection connection; Client client; + + public static final String DATA_MANAGER_TYPE = "log"; public LogDataManagerMySQL(){ @@ -99,7 +104,7 @@ public void start(){ try { Credentials credentials = new UsernamePasswordCredentials( - GridAppsDConstants.username, GridAppsDConstants.password); + securityConfig.getManagerUser(), securityConfig.getManagerPassword()); client = clientFactory.create(PROTOCOL.STOMP,credentials); connection = dataSources.getDataSourceByKey("gridappsd").getConnection(); @@ -143,6 +148,7 @@ public void store(String source, String processId, long timestamp, preparedStatement.executeUpdate(); } catch (Exception e) { + e.printStackTrace(); log.error("Error while storing log:"); log.error("error = " + e.getMessage()); log.error("source = " + source); @@ -177,6 +183,8 @@ public void storeExpectedResults(String test_id, String processId, long simulati preparedStatement.executeUpdate(); } catch (DataTruncation e) { + e.printStackTrace(); + log.error("Error while storing log:"); log.error("error = " + e.getMessage()); } catch (SQLException e) { diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/ProvenTimeSeriesDataManagerImpl.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/ProvenTimeSeriesDataManagerImpl.java index 69e3a0bf..57913715 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/ProvenTimeSeriesDataManagerImpl.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/ProvenTimeSeriesDataManagerImpl.java @@ -36,6 +36,7 @@ import pnnl.goss.core.ClientFactory; import pnnl.goss.core.DataResponse; import pnnl.goss.core.GossResponseEvent; +import pnnl.goss.core.security.SecurityConfig; import com.google.gson.Gson; @@ -63,6 +64,9 @@ public class ProvenTimeSeriesDataManagerImpl implements TimeseriesDataManager, D @ServiceDependency private volatile AppManager appManager; + @ServiceDependency + private volatile SecurityConfig securityConfig; + public static final String DATA_MANAGER_TYPE = "timeseries"; public static int count = 0; @@ -76,8 +80,8 @@ public class ProvenTimeSeriesDataManagerImpl implements TimeseriesDataManager, D ProvenProducer provenQueryProducer = new ProvenProducer(); ProvenProducer provenWriteProducer = new ProvenProducer(); - Credentials credentials = new UsernamePasswordCredentials( - GridAppsDConstants.username, GridAppsDConstants.password); +// Credentials credentials = new UsernamePasswordCredentials( +// GridAppsDConstants.username, GridAppsDConstants.password); @Start public void start(){ @@ -86,6 +90,7 @@ public void start(){ logManager.log(new LogMessage(this.getClass().getSimpleName(), null, new Date().getTime(), "Starting "+this.getClass().getSimpleName(), LogLevel.DEBUG, ProcessStatus.RUNNING, true), + securityConfig.getManagerUser(), GridAppsDConstants.topic_platformLog); dataManager.registerDataManagerHandler(this, DATA_MANAGER_TYPE); @@ -129,7 +134,7 @@ public Serializable query(RequestTimeseriesData requestTimeseriesData) throws Ex DataFormatConverter converter = dataManager.getConverter(origFormat, responseFormat); if(converter!=null){ StringWriter sw = new StringWriter(); - converter.convert(response.data.toString(), new PrintWriter(sw)); + converter.convert(response.data.toString(), new PrintWriter(sw), requestTimeseriesData); return sw.toString(); } @@ -163,6 +168,9 @@ public void storeAllData(SimulationContext simulationContext) throws Exception{ private void subscribeAndStoreDataFromTopic(String topic, String appOrServiceid, String instanceId) throws Exception{ + + Credentials credentials = new UsernamePasswordCredentials( + securityConfig.getManagerUser(), securityConfig.getManagerPassword()); Client inputClient = clientFactory.create(PROTOCOL.STOMP,credentials); inputClient.subscribe(topic, new GossResponseEvent() { @Override @@ -180,6 +188,7 @@ public void onMessage(Serializable message) { logManager.log(new LogMessage(this.getClass().getSimpleName(), null, new Date().getTime(), "Error storing timeseries data for message at "+event.getDestination()+" : "+sStackTrace, LogLevel.DEBUG, ProcessStatus.RUNNING, true), + event.getUsername(), GridAppsDConstants.topic_platformLog); } } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/conversion/DataFormatConverter.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/conversion/DataFormatConverter.java index 2a0b1984..22c561b9 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/conversion/DataFormatConverter.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/conversion/DataFormatConverter.java @@ -1,10 +1,12 @@ package gov.pnnl.goss.gridappsd.data.conversion; +import gov.pnnl.goss.gridappsd.dto.RequestTimeseriesData; + import java.io.InputStream; import java.io.PrintWriter; public interface DataFormatConverter { - public void convert(String inputContent, PrintWriter outputContent) throws Exception; - public void convert(InputStream inputContent, PrintWriter outputContent) throws Exception; + public void convert(String inputContent, PrintWriter outputContent, RequestTimeseriesData request) throws Exception; + public void convert(InputStream inputContent, PrintWriter outputContent, RequestTimeseriesData request) throws Exception; } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/conversion/ProvenLoadScheduleToGridlabdLoadScheduleConverter.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/conversion/ProvenLoadScheduleToGridlabdLoadScheduleConverter.java index 50da1683..14451bb2 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/conversion/ProvenLoadScheduleToGridlabdLoadScheduleConverter.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/conversion/ProvenLoadScheduleToGridlabdLoadScheduleConverter.java @@ -5,6 +5,7 @@ import gov.pnnl.goss.gridappsd.dto.LogMessage; import gov.pnnl.goss.gridappsd.dto.LogMessage.LogLevel; import gov.pnnl.goss.gridappsd.dto.LogMessage.ProcessStatus; +import gov.pnnl.goss.gridappsd.dto.RequestTimeseriesData; import gov.pnnl.goss.gridappsd.dto.TimeSeriesEntryResult; import gov.pnnl.goss.gridappsd.dto.TimeSeriesKeyValuePair; import gov.pnnl.goss.gridappsd.dto.TimeSeriesMeasurementResult; @@ -53,19 +54,11 @@ public class ProvenLoadScheduleToGridlabdLoadScheduleConverter implements DataFo private volatile DataManager dataManager; @ServiceDependency private volatile LogManager logManager; - int simulationYear; - - - /*static{ - sdfIn.setTimeZone(TimeZone.getTimeZone("UTC")); - sdfOut.setTimeZone(TimeZone.getTimeZone("UTC")); - }*/ public ProvenLoadScheduleToGridlabdLoadScheduleConverter(){} - public ProvenLoadScheduleToGridlabdLoadScheduleConverter(LogManager logManager, DataManager dataManager, int simulationYear) { + public ProvenLoadScheduleToGridlabdLoadScheduleConverter(LogManager logManager, DataManager dataManager) { this.logManager = logManager; this.dataManager = dataManager; - this.simulationYear = simulationYear; } @Start @@ -74,9 +67,7 @@ public void start(){ dataManager.registerConverter(INPUT_FORMAT, OUTPUT_FORMAT, this); } else { - //TODO send log message and exception if(logManager!=null){ - //log.warn("No Data manager available for "+getClass()); logManager.log( new LogMessage(this.getClass().getName(), new Integer( 0).toString(), new Date().getTime(), @@ -89,16 +80,16 @@ public void start(){ @Override - public void convert(String inputContent, PrintWriter outputContent) throws Exception { + public void convert(String inputContent, PrintWriter outputContent, RequestTimeseriesData request) throws Exception { TimeSeriesEntryResult resultObj = TimeSeriesEntryResult.parse(inputContent); boolean isFirstRecord = true; Calendar c = Calendar.getInstance(); - + int year = request.getSimulationYear(); for(HashMap map : resultObj.getData()){ if(isFirstRecord){ long longTime = new Double(map.get("time").toString()).longValue(); c.setTime(new Date(longTime*1000)); - c.set(Calendar.YEAR, 2019); + c.set(Calendar.YEAR, year); outputContent.print(sdfOut.format(c.getTime())); outputContent.print(" UTC,"); outputContent.println(map.get("value")); @@ -112,18 +103,19 @@ public void convert(String inputContent, PrintWriter outputContent) throws Excep } @Override - public void convert(InputStream inputContent, PrintWriter outputContent) throws Exception { + public void convert(InputStream inputContent, PrintWriter outputContent, RequestTimeseriesData request) throws Exception { String strContent = IOUtils.toString(inputContent); TimeSeriesEntryResult resultObj = TimeSeriesEntryResult.parse(strContent); boolean isFirstRecord = true; Calendar c = Calendar.getInstance(); - + int year = request.getSimulationYear(); for(HashMap map : resultObj.getData()){ if(isFirstRecord){ long longTime = new Double(map.get("time").toString()).longValue(); c.setTime(new Date(longTime*1000)); - c.set(Calendar.YEAR, 2013); - outputContent.print(sdfOut.format(c.getTime())+","); + c.set(Calendar.YEAR, year); + outputContent.print(sdfOut.format(c.getTime())); + outputContent.print(" UTC,"); outputContent.println(map.get("value")); isFirstRecord = false; } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/conversion/ProvenWeatherToGridlabdWeatherConverter.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/conversion/ProvenWeatherToGridlabdWeatherConverter.java index e4507a82..4ef0eb46 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/conversion/ProvenWeatherToGridlabdWeatherConverter.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/conversion/ProvenWeatherToGridlabdWeatherConverter.java @@ -5,12 +5,14 @@ import gov.pnnl.goss.gridappsd.dto.LogMessage; import gov.pnnl.goss.gridappsd.dto.LogMessage.LogLevel; import gov.pnnl.goss.gridappsd.dto.LogMessage.ProcessStatus; +import gov.pnnl.goss.gridappsd.dto.RequestTimeseriesData; import gov.pnnl.goss.gridappsd.dto.TimeSeriesEntryResult; import gov.pnnl.goss.gridappsd.dto.TimeSeriesKeyValuePair; import gov.pnnl.goss.gridappsd.dto.TimeSeriesMeasurementResult; import gov.pnnl.goss.gridappsd.dto.TimeSeriesResult; import gov.pnnl.goss.gridappsd.dto.TimeSeriesRowResult; import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; +import pnnl.goss.core.security.SecurityConfig; import java.io.InputStream; import java.io.PrintWriter; @@ -26,6 +28,7 @@ import org.apache.felix.dm.annotation.api.ServiceDependency; import org.apache.felix.dm.annotation.api.Start; + @Component public class ProvenWeatherToGridlabdWeatherConverter implements DataFormatConverter { protected static SimpleDateFormat sdfIn = new SimpleDateFormat("MM/dd/yyyy HH:mm"); @@ -53,6 +56,8 @@ public class ProvenWeatherToGridlabdWeatherConverter implements DataFormatConver private volatile DataManager dataManager; @ServiceDependency private volatile LogManager logManager; + @ServiceDependency + private volatile SecurityConfig securityConfig; /*static{ sdfIn.setTimeZone(TimeZone.getTimeZone("UTC")); @@ -78,7 +83,7 @@ public void start(){ new LogMessage(this.getClass().getName(), new Integer( 0).toString(), new Date().getTime(), "No Data manager available for "+getClass(), LogLevel.WARN, - ProcessStatus.RUNNING, false), "system", + ProcessStatus.RUNNING, false), securityConfig.getManagerUser(), GridAppsDConstants.topic_platformLog); } } @@ -86,7 +91,7 @@ public void start(){ @Override - public void convert(String inputContent, PrintWriter outputContent) throws Exception { + public void convert(String inputContent, PrintWriter outputContent, RequestTimeseriesData request) throws Exception { boolean headerPrinted = false; TimeSeriesEntryResult resultObj = TimeSeriesEntryResult.parse(inputContent); //for(TimeSeriesKeyValuePair record: resultObj.getEntryMap()){ @@ -99,7 +104,7 @@ public void convert(String inputContent, PrintWriter outputContent) throws Excep } @Override - public void convert(InputStream inputContent, PrintWriter outputContent) throws Exception { + public void convert(InputStream inputContent, PrintWriter outputContent, RequestTimeseriesData request) throws Exception { boolean headerPrinted = false; String strContent = IOUtils.toString(inputContent); @@ -246,7 +251,7 @@ public static void main(String[] args) { + "\"value\":\"0.0\"},{\"key\":\"time\",\"value\":\"1970-01-16T16:57:28.86Z\"},{\"key\":\"place\"," + "\"value\":\"\\\"Solar Radiation Research Laboratory\\\"\"},{\"key\":\"lat\",\"value\":\"\\\"39.74 N\\\"\"}]}}]}]}"; - new ProvenWeatherToGridlabdWeatherConverter().convert(provenInput, new PrintWriter(System.out)); + new ProvenWeatherToGridlabdWeatherConverter().convert(provenInput, new PrintWriter(System.out), null); } catch (Exception e) { e.printStackTrace(); } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/handlers/BlazegraphQueryHandler.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/handlers/BlazegraphQueryHandler.java index 7d77f70f..666a9615 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/handlers/BlazegraphQueryHandler.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/data/handlers/BlazegraphQueryHandler.java @@ -91,7 +91,7 @@ public void setLogManager(LogManager logManager) { this.logManager = logManager; } @Override - public ResultSetCloseable query(String szQuery) { + public ResultSetCloseable query(String szQuery, String szTag) { Query query = QueryFactory.create (Q_PREFIX + szQuery); GridAppsDConstants.logMessage(logManager, this.getClass().getName(), "Executing query "+szQuery, processID, username, LogLevel.DEBUG); diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/ModelCreationConfig.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/ModelCreationConfig.java index f9ca8d16..5b622c22 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/ModelCreationConfig.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/ModelCreationConfig.java @@ -44,6 +44,8 @@ import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; +import gov.pnnl.goss.cim2glm.dto.ModelState; + public class ModelCreationConfig implements Serializable{ private static final long serialVersionUID = 1L; @@ -61,7 +63,8 @@ public class ModelCreationConfig implements Serializable{ public double p_fraction = 0; // allowed values {0....1} constant P portion (defaults to 0 for CIM-defined, maps to -p public boolean randomize_zipload_fractions = false; // should randomize the zipload fraction values (eg. z, i, p_fractions) public boolean use_houses = false; - + public ModelState model_state; + @@ -150,6 +153,13 @@ public String toString() { return gson.toJson(this); } + public ModelState getModel_state() { + return model_state; + } + public void setModel_state(ModelState model_state) { + this.model_state = model_state; + } + public static ModelCreationConfig parse(String jsonString){ Gson gson = new Gson(); ModelCreationConfig obj = gson.fromJson(jsonString, ModelCreationConfig.class); @@ -158,5 +168,4 @@ public static ModelCreationConfig parse(String jsonString){ return obj; } - } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/RequestTimeseriesData.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/RequestTimeseriesData.java index 239d0108..5bd5e7c8 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/RequestTimeseriesData.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/RequestTimeseriesData.java @@ -17,6 +17,7 @@ public class RequestTimeseriesData implements Serializable { //ResultFormat responseFormat = ResultFormat.JSON; String responseFormat ="JSON"; private String queryType = "time-series"; + int simulationYear; public String getQueryMeasurement() { return queryMeasurement; @@ -41,6 +42,14 @@ public String getResponseFormat() { public void setResponseFormat(String responseFormat) { this.responseFormat = responseFormat; } + + public int getSimulationYear() { + return simulationYear; + } + + public void setSimulationYear(int simulationYear) { + this.simulationYear = simulationYear; + } @Override public String toString() { diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/RoleList.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/RoleList.java new file mode 100644 index 00000000..08ef2543 --- /dev/null +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/RoleList.java @@ -0,0 +1,31 @@ +package gov.pnnl.goss.gridappsd.dto; + +import java.util.List; + +import com.google.gson.Gson; + +public class RoleList { + public List roles; + + public List getRoles() { + return roles; + } + + public void setRoles(List roles) { + this.roles = roles; + } + + + @Override + public String toString() { + Gson gson = new Gson(); + return gson.toJson(this); + } + + + public static RoleList parse(String jsonString){ + Gson gson = new Gson(); + RoleList obj = gson.fromJson(jsonString, RoleList.class); + return obj; + } +} diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/RoleRequest.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/RoleRequest.java new file mode 100644 index 00000000..a367a163 --- /dev/null +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/RoleRequest.java @@ -0,0 +1,34 @@ +package gov.pnnl.goss.gridappsd.dto; + +import java.io.Serializable; +import com.google.gson.Gson; + +public class RoleRequest implements Serializable{ + + private static final long serialVersionUID = -3277794121736133832L; + + + public RoleRequest(){} + + public String user; + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + @Override + public String toString() { + Gson gson = new Gson(); + return gson.toJson(this); + } + + public static RoleRequest parse(String jsonString){ + Gson gson = new Gson(); + RoleRequest obj = gson.fromJson(jsonString, RoleRequest.class); + return obj; + } +} diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/SimulationConfig.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/SimulationConfig.java index 34e21eba..17b1f3dc 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/SimulationConfig.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/SimulationConfig.java @@ -44,6 +44,7 @@ import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; + public class SimulationConfig implements Serializable { private static final long serialVersionUID = -2995486912804104569L; @@ -65,6 +66,7 @@ public class SimulationConfig implements Serializable { //Slow simulator down to realtime if true. If false it will run as fast as the simulator allows public boolean run_realtime = true; + //eg "simulation_output": [{"name":"objectname", "properties": ["prop1","prop2"]},{"name":"object2name","properties":["prop1","prop2"]}] public SimulationOutput simulation_output = new SimulationOutput(); @@ -169,6 +171,7 @@ public String toString() { return gson.toJson(this); } + public static SimulationConfig parse(String jsonString){ Gson gson = new Gson(); SimulationConfig obj = gson.fromJson(jsonString, SimulationConfig.class); diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/SimulationContext.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/SimulationContext.java index 9f03f787..687afbe4 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/SimulationContext.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/SimulationContext.java @@ -3,6 +3,7 @@ import java.io.Serializable; import java.util.List; + public class SimulationContext implements Serializable { private static final long serialVersionUID = 1L; @@ -16,7 +17,7 @@ public class SimulationContext implements Serializable { public String simulatorPath; public List appInstanceIds; public List serviceInstanceIds; - + public String simulationUser; public String getSimulationId() { return simulationId; @@ -93,7 +94,14 @@ public void setServiceInstanceIds(List serviceInstanceIds) { public void addServiceInstanceIds(String serviceInstanceId) { this.serviceInstanceIds.add(serviceInstanceId); } - - + + public String getSimulationUser() { + return simulationUser; + } + + public void setSimulationUser(String simulationUser) { + this.simulationUser = simulationUser; + } + } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/events/packageinfo b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/events/packageinfo new file mode 100644 index 00000000..9ad81f6f --- /dev/null +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/dto/events/packageinfo @@ -0,0 +1 @@ +version 1.0.0 diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/log/LogManagerImpl.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/log/LogManagerImpl.java index 888e32a8..1d371561 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/log/LogManagerImpl.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/log/LogManagerImpl.java @@ -46,6 +46,7 @@ import org.apache.felix.dm.annotation.api.Component; 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.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,6 +60,7 @@ import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; import pnnl.goss.core.Client; import pnnl.goss.core.Client.PROTOCOL; +import pnnl.goss.core.security.SecurityConfig; import pnnl.goss.core.ClientFactory; import pnnl.goss.core.DataResponse; import pnnl.goss.core.GossResponseEvent; @@ -81,6 +83,9 @@ public class LogManagerImpl implements LogManager { @ServiceDependency ClientFactory clientFactory; + + @ServiceDependency + SecurityConfig securityConfig; Client client; @@ -104,8 +109,8 @@ private void logIncomingMessage(Serializable message) { public void start() { LogMessage logMessage = new LogMessage(); try { - UsernamePasswordCredentials credentials = new UsernamePasswordCredentials( - GridAppsDConstants.username, GridAppsDConstants.password); + Credentials credentials = new UsernamePasswordCredentials( + securityConfig.getManagerUser(), securityConfig.getManagerPassword()); client = clientFactory.create(PROTOCOL.STOMP, credentials); @@ -205,10 +210,10 @@ public void log(LogMessage message, String username, String topic) { } - @Override - public void log(LogMessage message, String topic) { - this.log(message, GridAppsDConstants.username, topic); - } +// @Override +// public void log(LogMessage message, String topic) { +// this.log(message, securityConfig.getManagerUser(), topic); +// } private void store(String source, String requestId, long timestamp, String log_message, LogLevel log_level, ProcessStatus process_status, String username, String process_type) { diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/process/ProcessEvent.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/process/ProcessEvent.java index 55577772..5d060a7a 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/process/ProcessEvent.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/process/ProcessEvent.java @@ -43,6 +43,7 @@ import gov.pnnl.goss.gridappsd.api.ConfigurationManager; import gov.pnnl.goss.gridappsd.api.DataManager; import gov.pnnl.goss.gridappsd.api.LogManager; +import gov.pnnl.goss.gridappsd.api.RoleManager; import gov.pnnl.goss.gridappsd.api.ServiceManager; import gov.pnnl.goss.gridappsd.api.SimulationManager; import gov.pnnl.goss.gridappsd.api.TestManager; @@ -55,17 +56,15 @@ import gov.pnnl.goss.gridappsd.dto.RequestSimulation; import gov.pnnl.goss.gridappsd.dto.RequestSimulation.SimulationRequestType; import gov.pnnl.goss.gridappsd.dto.RequestSimulationResponse; -import gov.pnnl.goss.gridappsd.dto.RuntimeTypeAdapterFactory; +import gov.pnnl.goss.gridappsd.dto.RoleList; import gov.pnnl.goss.gridappsd.dto.YBusExportResponse; -import gov.pnnl.goss.gridappsd.dto.events.CommOutage; -import gov.pnnl.goss.gridappsd.dto.events.Event; -import gov.pnnl.goss.gridappsd.dto.events.Fault; import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; import java.io.PrintWriter; import java.io.Serializable; import java.io.StringWriter; import java.util.Date; +import java.util.List; import javax.jms.Destination; @@ -76,7 +75,6 @@ import pnnl.goss.core.Response; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; /** @@ -104,13 +102,14 @@ public class ProcessEvent implements GossResponseEvent { ServiceManager serviceManager; DataManager dataManager; TestManager testManager; + RoleManager roleManager; public ProcessEvent(ProcessManagerImpl processManager, Client client, ProcessNewSimulationRequest newSimulationProcess, ConfigurationManager configurationManager, SimulationManager simulationManager, AppManager appManager, LogManager logManager, ServiceManager serviceManager, - DataManager dataManager, TestManager testManager){ + DataManager dataManager, TestManager testManager, RoleManager roleManager){ this.client = client; this.processManger = processManager; this.newSimulationProcess = newSimulationProcess; @@ -121,6 +120,7 @@ public ProcessEvent(ProcessManagerImpl processManager, this.serviceManager = serviceManager; this.dataManager = dataManager; this.testManager = testManager; + this.roleManager = roleManager; } @@ -128,10 +128,10 @@ public ProcessEvent(ProcessManagerImpl processManager, public void onMessage(Serializable message) { DataResponse event = (DataResponse)message; - String username = GridAppsDConstants.username; - + String username = event.getUsername(); + int processId = ProcessManagerImpl.generateProcessId(); - this.debug(processId, "Received message: "+ event.getData() +" on topic "+event.getDestination()+" from user "+username, event.getDestination()); + this.debug(processId, "Received message: "+ event.getData() +" on topic "+event.getDestination()+" from user "+username, event.getDestination(), username); try{ @@ -156,10 +156,10 @@ public void onMessage(Serializable message) { }catch(JsonSyntaxException e){ e.printStackTrace(); //TODO log error - sendError(client, event.getReplyDestination(), e.getMessage(), processId); + sendError(client, event.getReplyDestination(), e.getMessage(), processId, username); } } else { - sendError(client, event.getReplyDestination(), "Simulation request is null", processId); + sendError(client, event.getReplyDestination(), "Simulation request is null", processId, username); } } if(simRequest!=null){ @@ -173,7 +173,7 @@ public void onMessage(Serializable message) { client.publish(event.getReplyDestination(), response); //TODO also verify that we have the correct sub-configurations as part of the request //newSimulationProcess.process(configurationManager, simulationManager, processId, event, event.getData(), appManager, serviceManager); - newSimulationProcess.process(configurationManager, simulationManager, processId, simRequest,processManger.assignSimulationPort(processId), appManager,serviceManager, testManager, dataManager); + newSimulationProcess.process(configurationManager, simulationManager, processId, simRequest,processManger.assignSimulationPort(processId), appManager,serviceManager, testManager, dataManager, username); } else if (simRequest.simulation_request_type.equals(SimulationRequestType.PAUSE)) { //if pause simulationManager.pauseSimulation(simRequest.getSimulation_id()); } else if (simRequest.simulation_request_type.equals(SimulationRequestType.RESUME)) { //if play @@ -181,10 +181,10 @@ public void onMessage(Serializable message) { } else if (simRequest.simulation_request_type.equals(SimulationRequestType.STOP)) { //if stop simulationManager.endSimulation(simRequest.getSimulation_id()); } else{ - sendError(client, event.getReplyDestination(), "Simulation request type not recognized: "+simRequest.simulation_request_type, processId); + sendError(client, event.getReplyDestination(), "Simulation request type not recognized: "+simRequest.simulation_request_type, processId, username); } } else { - sendError(client, event.getReplyDestination(), "Simulation request could not be parsed", processId); + sendError(client, event.getReplyDestination(), "Simulation request could not be parsed", processId, username); } } else if(event.getDestination().contains(GridAppsDConstants.topic_requestApp )){ appManager.process(processId, event, message); @@ -201,7 +201,7 @@ public void onMessage(Serializable message) { } String type = requestTopicExtension; - this.debug(processId, "Received data request of type: "+type,null); + this.debug(processId, "Received data request of type: "+type,null, username); Serializable request; if (message instanceof DataResponse){ @@ -212,7 +212,7 @@ public void onMessage(Serializable message) { Response r = dataManager.processDataRequest(request, type, processId, configurationManager.getConfigurationProperty(GridAppsDConstants.GRIDAPPSD_TEMP_PATH), username); //client.publish(event.getReplyDestination(), r); - sendData(client, event.getReplyDestination(), ((DataResponse)r).getData(), processId); + sendData(client, event.getReplyDestination(), ((DataResponse)r).getData(), processId, username); } else if(event.getDestination().contains(GridAppsDConstants.topic_requestConfig)){ @@ -232,7 +232,7 @@ public void onMessage(Serializable message) { configRequest = ConfigurationRequest.parse(request.toString()); }catch(JsonSyntaxException e){ //TODO log error - sendError(client, event.getReplyDestination(), e.getMessage(), processId); + sendError(client, event.getReplyDestination(), e.getMessage(), processId, username); } } if(configRequest!=null){ @@ -244,8 +244,8 @@ public void onMessage(Serializable message) { StringWriter sww = new StringWriter(); PrintWriter pw = new PrintWriter(sww); e.printStackTrace(pw); - this.error(processId,sww.toString()); - sendError(client, event.getReplyDestination(), e.getMessage(), processId); + this.error(processId,sww.toString(), username); + sendError(client, event.getReplyDestination(), e.getMessage(), processId, username); } String result = sw.toString(); @@ -253,14 +253,14 @@ public void onMessage(Serializable message) { configRequest.getConfigurationType().equals("Vnom Export")){ Gson gson = new Gson(); YBusExportResponse response = gson.fromJson(result, YBusExportResponse.class); - sendData(client, event.getReplyDestination(), response, processId); + sendData(client, event.getReplyDestination(), response, processId, username); } else - sendData(client, event.getReplyDestination(), result, processId); + sendData(client, event.getReplyDestination(), result, processId, username); } else { - this.error(processId, "No valid configuration request received, request: "+request); - sendError(client, event.getReplyDestination(), "No valid configuration request received, request: "+request, processId); + this.error(processId, "No valid configuration request received, request: "+request, username); + sendError(client, event.getReplyDestination(), "No valid configuration request received, request: "+request, processId, username); } @@ -277,19 +277,29 @@ public void onMessage(Serializable message) { if(request.isServiceInstances()) platformStatus.setServiceInstances(serviceManager.listRunningServices()); client.publish(event.getReplyDestination(), platformStatus); + + } else if (event.getDestination().contains(GridAppsDConstants.topic_requestMyRoles)){ + List roles = roleManager.getRoles(username); + RoleList roleListResult = new RoleList(); + roleListResult.setRoles(roles); + sendData(client, event.getReplyDestination(), roleListResult.toString(), processId, username); } }catch(Exception e ){ StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); - this.error(processId,sw.toString()); - sendError(client, event.getReplyDestination(), sw.toString(), processId); + this.error(processId,sw.toString(), username); + sendError(client, event.getReplyDestination(), sw.toString(), processId, username); } } - private void sendData(Client client, Destination replyDestination, Serializable data, int processId){ + private void sendData(Client client, Destination replyDestination, Serializable data, int processId, String username){ try { + //Make sure it is sending back something in the data field for valid json (or if it is null maybe it should send error response instead???) + if(data==null || data.toString().length()==0){ + data = "{}"; + } String r = "{\"data\":"+data+",\"responseComplete\":true,\"id\":\""+processId+"\"}"; /*DataResponse r = new DataResponse(); r.setData(data); @@ -300,13 +310,13 @@ private void sendData(Client client, Destination replyDestination, Serializable StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); - this.error(processId,sw.toString()); + this.error(processId,sw.toString(), username); //TODO log error and send error response } } - private void sendError(Client client, Destination replyDestination, String error, int processId){ + private void sendError(Client client, Destination replyDestination, String error, int processId, String username){ try { DataResponse r = new DataResponse(); r.setError(new DataError(error)); @@ -316,12 +326,12 @@ private void sendError(Client client, Destination replyDestination, String error StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); - this.error(processId,sw.toString()); + this.error(processId,sw.toString(), username); } } - private void debug(int processId, String message, String process_type) { + private void debug(int processId, String message, String process_type, String username) { LogMessage logMessage = new LogMessage(); logMessage.setSource(this.getClass().getSimpleName()); @@ -333,11 +343,11 @@ private void debug(int processId, String message, String process_type) { logMessage.setTimestamp(new Date().getTime()); if(process_type!=null) logMessage.setProcess_type(process_type); - logManager.log(logMessage, GridAppsDConstants.topic_platformLog); + logManager.log(logMessage, username, GridAppsDConstants.topic_platformLog); } - private void error(int processId, String message) { + private void error(int processId, String message, String username) { LogMessage logMessage = new LogMessage(); logMessage.setSource(this.getClass().getSimpleName()); @@ -348,7 +358,7 @@ private void error(int processId, String message) { logMessage.setStoreToDb(true); logMessage.setTimestamp(new Date().getTime()); - logManager.log(logMessage, GridAppsDConstants.topic_platformLog); + logManager.log(logMessage, username, GridAppsDConstants.topic_platformLog); } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/process/ProcessManagerImpl.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/process/ProcessManagerImpl.java index 5cc2be2d..f1266827 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/process/ProcessManagerImpl.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/process/ProcessManagerImpl.java @@ -55,6 +55,7 @@ import gov.pnnl.goss.gridappsd.api.DataManager; import gov.pnnl.goss.gridappsd.api.LogManager; import gov.pnnl.goss.gridappsd.api.ProcessManager; +import gov.pnnl.goss.gridappsd.api.RoleManager; import gov.pnnl.goss.gridappsd.api.ServiceManager; import gov.pnnl.goss.gridappsd.api.SimulationManager; import gov.pnnl.goss.gridappsd.api.TestManager; @@ -65,6 +66,7 @@ import pnnl.goss.core.Client; import pnnl.goss.core.Client.PROTOCOL; import pnnl.goss.core.ClientFactory; +import pnnl.goss.core.security.SecurityConfig; @@ -94,12 +96,18 @@ public class ProcessManagerImpl implements ProcessManager { @ServiceDependency private volatile ServiceManager serviceManager; + + @ServiceDependency + private volatile SecurityConfig securityConfig; @ServiceDependency private volatile DataManager dataManager; @ServiceDependency private volatile TestManager testManager; + + @ServiceDependency + private volatile RoleManager roleManager; ProcessNewSimulationRequest newSimulationProcess = null; @@ -132,9 +140,13 @@ public void start(){ LogMessage logMessageObj = new LogMessage(); try{ + + + Credentials credentials = new UsernamePasswordCredentials( + securityConfig.getManagerUser(), securityConfig.getManagerPassword()); - Credentials credentials = new UsernamePasswordCredentials( - GridAppsDConstants.username, GridAppsDConstants.password); +// Credentials credentials = new UsernamePasswordCredentials( +// GridAppsDConstants.username, GridAppsDConstants.password); Client client = clientFactory.create(PROTOCOL.STOMP,credentials); logMessageObj.setLogLevel(LogLevel.DEBUG); @@ -156,14 +168,14 @@ public void start(){ client.subscribe(GridAppsDConstants.topic_process_prefix+".>", new ProcessEvent(this, - client, newSimulationProcess, configurationManager, simulationManager, appManager, logManager, serviceManager, dataManager, testManager)); + client, newSimulationProcess, configurationManager, simulationManager, appManager, logManager, serviceManager, dataManager, testManager, roleManager)); } catch(Exception e){ e.printStackTrace(); logMessageObj.setTimestamp(new Date().getTime()); logMessageObj.setLogLevel(LogLevel.ERROR); logMessageObj.setLogMessage(e.getMessage()); - logManager.log(logMessageObj, GridAppsDConstants.username); + logManager.log(logMessageObj, securityConfig.getManagerUser(), GridAppsDConstants.topic_platformLog); } } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/process/ProcessNewSimulationRequest.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/process/ProcessNewSimulationRequest.java index d8f68152..b4ad6ed7 100755 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/process/ProcessNewSimulationRequest.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/process/ProcessNewSimulationRequest.java @@ -74,6 +74,8 @@ import java.util.Map; import java.util.Properties; +import com.google.gson.Gson; + import pnnl.goss.core.DataResponse; public class ProcessNewSimulationRequest { @@ -91,20 +93,19 @@ public void process(ConfigurationManager configurationManager, SimulationManager simulationManager, int simulationId, DataResponse event, RequestSimulation simRequest, AppManager appManager, ServiceManager serviceManager, TestManager testManager, - DataManager dataManager) { + DataManager dataManager, String username) { process(configurationManager, simulationManager, simulationId, simRequest, SimulationConfig.DEFAULT_SIMULATION_BROKER_PORT, appManager, - serviceManager, testManager, dataManager); + serviceManager, testManager, dataManager, username); } public void process(ConfigurationManager configurationManager, SimulationManager simulationManager, int simulationId, RequestSimulation simRequest, int simulationPort, AppManager appManager, - ServiceManager serviceManager, TestManager testManager,DataManager dataManager) { + ServiceManager serviceManager, TestManager testManager,DataManager dataManager, String username) { try { - String username = GridAppsDConstants.username; String source = this.getClass().getSimpleName(); String simId = new Integer(simulationId).toString(); String simulationLogTopic = GridAppsDConstants.topic_simulationLog @@ -135,7 +136,7 @@ public void process(ConfigurationManager configurationManager, "Creating simulation and power grid model files for simulation Id " + simulationId, LogLevel.INFO, - ProcessStatus.RUNNING,true), simulationLogTopic); + ProcessStatus.RUNNING,true), username, simulationLogTopic); // StringWriter simulationConfigDirOut = new StringWriter(); @@ -170,6 +171,7 @@ public void process(ConfigurationManager configurationManager, simContext.simulationPort = simulationPort; simContext.simulationDir = tempDataPathDir.getAbsolutePath(); simContext.startupFile = tempDataPathDir.getAbsolutePath()+File.separator+"model_startup.glm"; + simContext.simulationUser = username; try{ simContext.simulatorPath = serviceManager.getService(simRequest.getSimulation_config().getSimulator()).getExecution_path(); }catch(NullPointerException e){ @@ -178,20 +180,19 @@ public void process(ConfigurationManager configurationManager, simId, new Date().getTime(), "Cannot find service with id ="+simRequest.getSimulation_config().getSimulator(), - LogLevel.DEBUG, ProcessStatus.RUNNING, true), GridAppsDConstants.topic_simulationLog+simulationId); + LogLevel.DEBUG, ProcessStatus.RUNNING, true), username, GridAppsDConstants.topic_simulationLog+simulationId); }else if(serviceManager.getService(simRequest.getSimulation_config().getSimulator()).getExecution_path() == null){ logManager.log(new LogMessage(this.getClass().getSimpleName(), simId, new Date().getTime(), "Cannot find execution path for service ="+simRequest.getSimulation_config().getSimulator(), - LogLevel.DEBUG, ProcessStatus.RUNNING, true), GridAppsDConstants.topic_simulationLog+simulationId); + LogLevel.DEBUG, ProcessStatus.RUNNING, true), username, GridAppsDConstants.topic_simulationLog+simulationId); } e.printStackTrace(); } - String simulator = simRequest.getSimulation_config().getSimulator(); //generate config files for requested simulator //if requested simulator is opendss @@ -210,7 +211,7 @@ public void process(ConfigurationManager configurationManager, logManager .log(new LogMessage(source, simId,new Date().getTime(), "Simulation and power grid model files generated for simulation Id ",LogLevel.DEBUG, ProcessStatus.RUNNING,true), - simulationLogTopic); + username, simulationLogTopic); // Start Apps and Services @@ -230,13 +231,13 @@ public void process(ConfigurationManager configurationManager, simId, new Date().getTime(), "Cannot find service with id ="+simRequest.getSimulation_config().getSimulator(), - LogLevel.WARN, ProcessStatus.RUNNING, true), GridAppsDConstants.topic_simulationLog+simulationId); + LogLevel.WARN, ProcessStatus.RUNNING, true), username, GridAppsDConstants.topic_simulationLog+simulationId); }else if(serviceManager.getService(simRequest.getSimulation_config().getSimulator()).getExecution_path() == null){ logManager.log(new LogMessage(this.getClass().getSimpleName(), simId, new Date().getTime(), "Cannot find execution path for service ="+simRequest.getSimulation_config().getSimulator(), - LogLevel.DEBUG, ProcessStatus.RUNNING, true), GridAppsDConstants.topic_simulationLog+simulationId); + LogLevel.DEBUG, ProcessStatus.RUNNING, true), username, GridAppsDConstants.topic_simulationLog+simulationId); } e.printStackTrace(); } @@ -250,13 +251,15 @@ public void process(ConfigurationManager configurationManager, simId, new Date().getTime(), "No services found in request ="+simRequest.getSimulation_config().getSimulator(), - LogLevel.WARN, ProcessStatus.RUNNING, true), GridAppsDConstants.topic_simulationLog+simulationId); + LogLevel.WARN, ProcessStatus.RUNNING, true), username, GridAppsDConstants.topic_simulationLog+simulationId); } else{ for(ServiceConfig serviceConfig : simRequest.service_configs){ String serviceInstanceId = serviceManager.startServiceForSimultion(serviceConfig.getId(), null, simulationContext); - connectServiceInstanceIds.add(serviceInstanceId); - connectServiceIds.add(serviceConfig.getId()); + if(serviceInstanceId!=null){ + connectServiceInstanceIds.add(serviceInstanceId); + connectServiceIds.add(serviceConfig.getId()); + } } } @@ -267,7 +270,7 @@ public void process(ConfigurationManager configurationManager, simId, new Date().getTime(), "No applications found in request ="+simRequest.getSimulation_config().getSimulator(), - LogLevel.WARN, ProcessStatus.RUNNING, true), GridAppsDConstants.topic_simulationLog+simulationId); + LogLevel.WARN, ProcessStatus.RUNNING, true), username, GridAppsDConstants.topic_simulationLog+simulationId); } else { for (ApplicationObject app : simRequest.application_config @@ -277,7 +280,7 @@ public void process(ConfigurationManager configurationManager, logManager.log(new LogMessage(this.getClass().getSimpleName(), String.valueOf(simulationId), new Date().getTime(), "Cannot start application "+ app.getName() +". Application not available", - LogLevel.ERROR, ProcessStatus.ERROR, true), GridAppsDConstants.topic_simulationLog + LogLevel.ERROR, ProcessStatus.ERROR, true), username, GridAppsDConstants.topic_simulationLog + simulationId); throw new RuntimeException("Cannot start application "+ app.getName() +". Application not available"); @@ -288,14 +291,18 @@ public void process(ConfigurationManager configurationManager, List prereqsList = appManager.getApp(app.getName()) .getPrereqs(); for (String prereqs : prereqsList) { + if(!connectServiceIds.contains(prereqs)){ String serviceInstanceId = serviceManager.startServiceForSimultion(prereqs, null,simulationContext); - connectServiceInstanceIds.add(serviceInstanceId); - logManager.log(new LogMessage(source, simId, new Date().getTime(),"Started " + if(serviceInstanceId!=null){ + connectServiceInstanceIds.add(serviceInstanceId); + logManager.log(new LogMessage(source, simId, new Date().getTime(),"Started " + prereqs + " with instance id " + serviceInstanceId,LogLevel.DEBUG, ProcessStatus.RUNNING, true), + username, GridAppsDConstants.topic_simulationLog + simulationId); + } } } @@ -306,6 +313,7 @@ public void process(ConfigurationManager configurationManager, new LogMessage(source, simId, new Date().getTime(),"Started " + app.getName() + " with instance id " + appInstanceId, LogLevel.DEBUG, ProcessStatus.RUNNING, true), + username, GridAppsDConstants.topic_simulationLog + simulationId); } @@ -321,10 +329,12 @@ public void process(ConfigurationManager configurationManager, // start simulation logManager.log(new LogMessage(source, simId,new Date().getTime(), "Starting simulation for id " + simulationId,LogLevel.DEBUG, ProcessStatus.RUNNING,true), + username, simulationLogTopic); simulationManager.startSimulation(simulationId, simRequest.getSimulation_config(),simContext, simulationContext); logManager.log(new LogMessage(source, simId,new Date().getTime(), "Started simulation for id " + simulationId,LogLevel.DEBUG, ProcessStatus.RUNNING,true), + username, simulationLogTopic); } catch (Exception e) { @@ -336,7 +346,7 @@ public void process(ConfigurationManager configurationManager, "Process Initialization error: " + e.getMessage(), LogLevel.ERROR, ProcessStatus.ERROR, false), - GridAppsDConstants.username, + username, GridAppsDConstants.topic_platformLog); } catch (Exception e1) { @@ -375,7 +385,11 @@ Properties generateSimulationParameters(RequestSimulation requestSimulation){ params.put(GLDAllConfigurationHandler.SIMULATIONSTARTTIME, requestSimulation.getSimulation_config().start_time); params.put(GLDAllConfigurationHandler.SIMULATIONDURATION, new Integer(requestSimulation.getSimulation_config().duration).toString()); - + + if(modelConfig.getModel_state()!=null){ + Gson gson = new Gson(); + params.put(GLDAllConfigurationHandler.MODEL_STATE, gson.toJson(modelConfig.getModel_state())); + } return params; } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/role/RoleManagerImpl.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/role/RoleManagerImpl.java new file mode 100644 index 00000000..e510945d --- /dev/null +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/role/RoleManagerImpl.java @@ -0,0 +1,66 @@ +package gov.pnnl.goss.gridappsd.role; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Dictionary; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; + +import org.apache.commons.lang.StringUtils; +import org.apache.felix.dm.annotation.api.Component; +import org.apache.felix.dm.annotation.api.ConfigurationDependency; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.northconcepts.exception.SystemException; + +import gov.pnnl.goss.gridappsd.api.RoleManager; + + +@Component +public class RoleManagerImpl implements RoleManager { + + private static final Logger log = LoggerFactory.getLogger(RoleManagerImpl.class); + private static final String CONFIG_PID = "pnnl.goss.gridappsd.security.rolefile"; + + private HashMap> roles = new HashMap>(); + + + @Override + public List getRoles(String userName) throws Exception { + if(!roles.containsKey(userName)){ + throw new Exception("No roles specified for user "+userName); + } + + return roles.get(userName); + } + + @Override + public boolean hasRole(String userName, String roleName) throws Exception { + if(!roles.containsKey(userName)){ + throw new Exception("No roles specified for user "+userName); + } + + List groups = roles.get(userName); + return groups.contains(roleName); + } + + + + @ConfigurationDependency(pid=CONFIG_PID) + public synchronized void updated(Dictionary properties) throws SystemException { + if (properties != null) { + Enumeration keys = properties.keys(); + + while(keys.hasMoreElements()){ + String user = keys.nextElement(); + String groups = properties.get(user).toString(); + System.out.println("Registering user roles: "+user+" -- "+groups); + List groupList = new ArrayList<>(Arrays.asList(StringUtils.split(groups, ","))); + roles.put(user, groupList); + } + } + } + +} 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 21045a95..e5a1bf48 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 @@ -64,7 +64,6 @@ import gov.pnnl.goss.gridappsd.api.LogManager; import gov.pnnl.goss.gridappsd.api.ServiceManager; -import gov.pnnl.goss.gridappsd.dto.AppInstance; import gov.pnnl.goss.gridappsd.dto.EnvironmentVariable; import gov.pnnl.goss.gridappsd.dto.LogMessage; import gov.pnnl.goss.gridappsd.dto.UserOptions; @@ -73,9 +72,9 @@ import gov.pnnl.goss.gridappsd.dto.ServiceInfo; import gov.pnnl.goss.gridappsd.dto.ServiceInfo.ServiceType; import gov.pnnl.goss.gridappsd.dto.ServiceInstance; -import gov.pnnl.goss.gridappsd.dto.SimulationContext; import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; import pnnl.goss.core.ClientFactory; +import pnnl.goss.core.security.SecurityConfig; @Component public class ServiceManagerImpl implements ServiceManager{ @@ -88,6 +87,9 @@ public class ServiceManagerImpl implements ServiceManager{ @ServiceDependency private volatile ClientFactory clientFactory; + @ServiceDependency + private volatile SecurityConfig securityConfig; + private HashMap services = new HashMap(); private Dictionary configurationProperties; @@ -126,7 +128,7 @@ public void start(){ "Starting "+this.getClass().getName(), LogLevel.INFO, ProcessStatus.RUNNING, - true),GridAppsDConstants.username, + true),securityConfig.getManagerUser(), GridAppsDConstants.topic_platformLog); scanForServices(); @@ -137,7 +139,7 @@ public void start(){ String.format("Found %s services", services.size()), LogLevel.INFO, ProcessStatus.RUNNING, - true),GridAppsDConstants.username); + true),securityConfig.getManagerUser(),GridAppsDConstants.topic_platformLog); }catch(Exception e){ e.printStackTrace(); } @@ -260,9 +262,15 @@ public String startServiceForSimultion(String serviceId, HashMap throw new RuntimeException("Service not found: "+serviceId); } - // are multiple allowed? if not check to see if it is already running, if it is then fail - if(!serviceInfo.isMultiple_instances() && listRunningServices(serviceId).size()>0){ - throw new RuntimeException("Service is already running and multiple instances are not allowed: "+serviceId); + // are multiple allowed? if not check to see if it is already running, if it is then send warning message + if(!serviceInfo.isMultiple_instances() && listRunningServices(serviceId, simulationId).size()>0){ + logManager.log(new LogMessage(this.getClass().getSimpleName(), + simulationId, new Date().getTime(), + serviceId + " service is already running and multiple instances are not allowed for single simulation", + LogLevel.WARN, ProcessStatus.RUNNING, true), + securityConfig.getManagerUser(), + GridAppsDConstants.topic_simulationLog+simulationId); + return null; } File serviceDirectory = new File(getServiceConfigDirectory().getAbsolutePath() @@ -330,6 +338,7 @@ public String startServiceForSimultion(String serviceId, HashMap simulationId, new Date().getTime(), "Starting service with command "+ String.join(" ",commands), LogLevel.INFO, ProcessStatus.RUNNING, true), + securityConfig.getManagerUser(), GridAppsDConstants.topic_simulationLog+simulationId); process = processServiceBuilder.start(); @@ -345,6 +354,7 @@ simulationId, new Date().getTime(), simulationId, new Date().getTime(), "Starting service with command "+ String.join(" ",commands), LogLevel.DEBUG, ProcessStatus.RUNNING, true), + securityConfig.getManagerUser(), GridAppsDConstants.topic_simulationLog+simulationId); process = processServiceBuilder.start(); @@ -360,6 +370,7 @@ simulationId, new Date().getTime(), simulationId, new Date().getTime(), "Starting service with command "+ String.join(" ",commands), LogLevel.DEBUG, ProcessStatus.RUNNING, true), + securityConfig.getManagerUser(), GridAppsDConstants.topic_simulationLog+simulationId); process = processServiceBuilder.start(); @@ -394,6 +405,7 @@ simulationId, new Date().getTime(), LogLevel.ERROR, ProcessStatus.ERROR, true), + securityConfig.getManagerUser(), GridAppsDConstants.topic_simulationLog+simulationId); logManager.log(new LogMessage(this.getClass().getSimpleName(), simulationId, @@ -402,6 +414,7 @@ simulationId, new Date().getTime(), LogLevel.ERROR, ProcessStatus.ERROR, true), + securityConfig.getManagerUser(), GridAppsDConstants.topic_simulationLog+simulationId); } @@ -426,12 +439,15 @@ public List listRunningServices() { } @Override - public List listRunningServices(String serviceId) { + public List listRunningServices(String serviceId, String simulationId) { List result = new ArrayList(); for(String instanceId: serviceInstances.keySet()){ ServiceInstance instance = serviceInstances.get(instanceId); if(instance.getService_info().getId().equals(serviceId)){ - result.add(instance); + if(simulationId!=null && instance.getSimulation_id().equals(simulationId)) + result.add(instance); + else + result.add(instance); } } return result; @@ -440,7 +456,7 @@ public List listRunningServices(String serviceId) { @Override public void stopService(String serviceId) { serviceId = serviceId.trim(); - for(ServiceInstance instance: listRunningServices(serviceId)){ + for(ServiceInstance instance: listRunningServices(serviceId, null)){ if(instance.getService_info().getId().equals(serviceId)){ stopServiceInstance(instance.getInstance_id()); } @@ -485,12 +501,12 @@ public void run() { String line = null; try { while ((line = input.readLine()) != null) { - logManager.log(new LogMessage(this.getClass().getName(),serviceInstance.getInstance_id(), new Date().getTime(), line, LogLevel.DEBUG, ProcessStatus.RUNNING, false), GridAppsDConstants.username, GridAppsDConstants.topic_simulationLog+simulationId); + logManager.log(new LogMessage(this.getClass().getSimpleName(),serviceInstance.getInstance_id(), new Date().getTime(), line, LogLevel.DEBUG, ProcessStatus.RUNNING, false), securityConfig.getManagerUser(), GridAppsDConstants.topic_simulationLog+simulationId); } } catch (IOException e) { if(!(e.getMessage().contains("Stream closed"))){ e.printStackTrace(); - logManager.log(new LogMessage(this.getClass().getName(),serviceInstance.getInstance_id(), new Date().getTime(), e.getMessage(), LogLevel.ERROR, ProcessStatus.ERROR, false), GridAppsDConstants.username, GridAppsDConstants.topic_simulationLog+simulationId); + logManager.log(new LogMessage(this.getClass().getName(),serviceInstance.getInstance_id(), new Date().getTime(), e.getMessage(), LogLevel.ERROR, ProcessStatus.ERROR, false), securityConfig.getManagerUser(), GridAppsDConstants.topic_simulationLog+simulationId); } } } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/FNCSOutputEvent.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/FNCSOutputEvent.java index 1bbf308d..1d896f20 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/FNCSOutputEvent.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/FNCSOutputEvent.java @@ -48,6 +48,7 @@ import pnnl.goss.core.Client; import pnnl.goss.core.Client.PROTOCOL; +import pnnl.goss.core.security.SecurityConfig; import pnnl.goss.core.ClientFactory; import pnnl.goss.core.GossResponseEvent; import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; @@ -62,7 +63,8 @@ public class FNCSOutputEvent implements GossResponseEvent { @ServiceDependency private volatile ClientFactory clientFactory; - + @ServiceDependency + private volatile SecurityConfig securityConfig; /** * message is in the JSON string format * {} @@ -72,7 +74,7 @@ public void onMessage(Serializable message) { try { Credentials credentials = new UsernamePasswordCredentials( - GridAppsDConstants.username, GridAppsDConstants.password); + securityConfig.getManagerUser(), securityConfig.getManagerPassword()); Client client = clientFactory.create(PROTOCOL.STOMP,credentials); diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/SimulationEvent.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/SimulationEvent.java index a9550ae4..58ea55e4 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/SimulationEvent.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/simulation/SimulationEvent.java @@ -48,8 +48,10 @@ import pnnl.goss.core.Client; import pnnl.goss.core.ClientFactory; +import pnnl.goss.core.DataResponse; import pnnl.goss.core.GossResponseEvent; import pnnl.goss.core.Client.PROTOCOL; +import pnnl.goss.core.security.SecurityConfig; import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; import gov.pnnl.goss.gridappsd.utils.RunCommandLine; @@ -68,6 +70,8 @@ public class SimulationEvent implements GossResponseEvent { @ServiceDependency private volatile ClientFactory clientFactory; + @ServiceDependency + private volatile SecurityConfig securityConfig; /** * message is in the JSON string format @@ -77,8 +81,11 @@ public class SimulationEvent implements GossResponseEvent { public void onMessage(Serializable message) { try { + DataResponse event = (DataResponse)message; + String username = event.getUsername(); + Credentials credentials = new UsernamePasswordCredentials( - GridAppsDConstants.username, GridAppsDConstants.password); + securityConfig.getManagerUser(), securityConfig.getManagerPassword()); Client client = clientFactory.create(PROTOCOL.STOMP,credentials); 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 74756ad6..b15a7285 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 @@ -65,6 +65,7 @@ import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; import pnnl.goss.core.Client; import pnnl.goss.core.Client.PROTOCOL; +import pnnl.goss.core.security.SecurityConfig; import pnnl.goss.core.ClientFactory; import pnnl.goss.core.server.ServerControl; @@ -95,6 +96,9 @@ public class SimulationManagerImpl implements SimulationManager{ @ServiceDependency private volatile AppManager appManager; + @ServiceDependency + private volatile SecurityConfig securityConfig; + @ServiceDependency LogManager logManager; @@ -114,7 +118,7 @@ public SimulationManagerImpl(ClientFactory clientFactory, ServerControl serverCo public void start() throws Exception{ Credentials credentials = new UsernamePasswordCredentials( - GridAppsDConstants.username, GridAppsDConstants.password); + securityConfig.getManagerUser(), securityConfig.getManagerPassword()); client = clientFactory.create(PROTOCOL.STOMP,credentials); client.publish("goss.gridappsd.log.platform", new LogMessage(this.getClass().getSimpleName(), null, @@ -141,7 +145,7 @@ public void startSimulation(int simulationId, SimulationConfig simulationConfig, "Starting simulation "+simulationId, LogLevel.INFO, ProcessStatus.STARTING, - true),GridAppsDConstants.username, + true),simContext.getSimulationUser(), GridAppsDConstants.topic_platformLog); } catch (Exception e2) { log.warn("Error while reporting status "+e2.getMessage()); @@ -152,7 +156,7 @@ public void startSimulation(int simulationId, SimulationConfig simulationConfig, startServiceDependencies(simulationConfig, simContext, simulationContext); SimulationProcess simProc = new SimulationProcess(simContext, serviceManager, - simulationConfig, simulationId, logManager, appManager, client); + simulationConfig, simulationId, logManager, appManager, client, securityConfig); // simProcesses.put(simContext.getSimulationId(), simProc); simProc.start(); } @@ -204,7 +208,8 @@ public void startServiceDependencies(SimulationConfig simulationConfig, Simulati List serviceDependencies = simulationServiceInfo.getService_dependencies(); for(String service : serviceDependencies) { String serviceInstanceId = serviceManager.startServiceForSimultion(service, null, simulationContext); - simContext.addServiceInstanceIds(serviceInstanceId); + if(serviceInstanceId!=null) + simContext.addServiceInstanceIds(serviceInstanceId); } } 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 32e44b8f..1be31837 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 @@ -1,5 +1,18 @@ package gov.pnnl.goss.gridappsd.simulation; +import gov.pnnl.goss.gridappsd.api.AppManager; +import gov.pnnl.goss.gridappsd.api.LogManager; +import gov.pnnl.goss.gridappsd.api.ServiceManager; +import gov.pnnl.goss.gridappsd.configuration.GLDAllConfigurationHandler; +import gov.pnnl.goss.gridappsd.dto.FncsBridgeResponse; +import gov.pnnl.goss.gridappsd.dto.LogMessage; +import gov.pnnl.goss.gridappsd.dto.LogMessage.LogLevel; +import gov.pnnl.goss.gridappsd.dto.LogMessage.ProcessStatus; +import gov.pnnl.goss.gridappsd.dto.SimulationConfig; +import gov.pnnl.goss.gridappsd.dto.SimulationContext; +import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; +import gov.pnnl.goss.gridappsd.utils.RunCommandLine; + import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -12,23 +25,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.gson.Gson; - -import gov.pnnl.goss.gridappsd.api.AppManager; -import gov.pnnl.goss.gridappsd.api.LogManager; -import gov.pnnl.goss.gridappsd.api.ServiceManager; -import gov.pnnl.goss.gridappsd.configuration.GLDAllConfigurationHandler; -import gov.pnnl.goss.gridappsd.dto.FncsBridgeResponse; -import gov.pnnl.goss.gridappsd.dto.LogMessage; -import gov.pnnl.goss.gridappsd.dto.SimulationConfig; -import gov.pnnl.goss.gridappsd.dto.LogMessage.LogLevel; -import gov.pnnl.goss.gridappsd.dto.LogMessage.ProcessStatus; -import gov.pnnl.goss.gridappsd.dto.SimulationContext; -import gov.pnnl.goss.gridappsd.utils.GridAppsDConstants; -import gov.pnnl.goss.gridappsd.utils.RunCommandLine; import pnnl.goss.core.Client; import pnnl.goss.core.DataResponse; import pnnl.goss.core.GossResponseEvent; +import pnnl.goss.core.security.SecurityConfig; + +import com.google.gson.Gson; public class SimulationProcess extends Thread { private static Logger log = LoggerFactory.getLogger(SimulationProcess.class); @@ -60,10 +62,11 @@ class SimulationTracker { LogManager logManager; AppManager appManager; Client client; + SecurityConfig securityConfig; public SimulationProcess(SimulationContext simContext, ServiceManager serviceManager, SimulationConfig simulationConfig, int simulationId, LogManager logManager, - AppManager appManager, Client client){ + AppManager appManager, Client client, SecurityConfig securityConfig){ this.simContext = simContext; this.serviceManager = serviceManager; this.simulationConfig = simulationConfig; @@ -71,6 +74,7 @@ public SimulationProcess(SimulationContext simContext, ServiceManager serviceMan this.logManager = logManager; this.appManager = appManager; this.client = client; + this.securityConfig = securityConfig; } @@ -83,7 +87,7 @@ public void run() { SimulationTracker isFinished = new SimulationTracker(); try{ - File simulationFile = new File(simContext.getStartupFile()); + File simulationFile = new File(simContext.getStartupFile()); if(simulationConfig!=null && simulationConfig.model_creation_config!=null && simulationConfig.model_creation_config.schedule_name!=null && simulationConfig.model_creation_config.schedule_name.trim().length()>0){ File serviceDir = serviceManager.getServiceConfigDirectory(); @@ -106,7 +110,7 @@ public void run() { simContext.getSimulatorPath()+" "+simulationFile, LogLevel.INFO, ProcessStatus.RUNNING, - true),GridAppsDConstants.username, + true),simContext.getSimulationUser(), GridAppsDConstants.topic_platformLog); ProcessBuilder simulatorBuilder = new ProcessBuilder(simContext.getSimulatorPath(), simulationFile.getAbsolutePath()); simulatorBuilder.redirectErrorStream(true); @@ -115,7 +119,7 @@ public void run() { simulatorBuilder.directory(simulationFile.getParentFile()); simulatorProcess = simulatorBuilder.start(); // Watch the process - watch(simulatorProcess, "Simulator"); + watch(simulatorProcess, "Simulator-"+simulationId); //TODO: check if GridLAB-D is started correctly and send publish simulation status accordingly @@ -126,7 +130,7 @@ public void run() { "GridLAB-D started", LogLevel.INFO, ProcessStatus.RUNNING, - true),GridAppsDConstants.username, + true),simContext.getSimulationUser(), GridAppsDConstants.topic_platformLog); //Subscribe to fncs-goss-bridge output topic @@ -139,7 +143,7 @@ public void run() { "Checking fncs is initialized, currently "+isInitialized.isInited, LogLevel.INFO, ProcessStatus.RUNNING, - true),GridAppsDConstants.username, + true),simContext.getSimulationUser(), GridAppsDConstants.topic_platformLog); int initAttempts = 0; @@ -161,7 +165,7 @@ public void run() { "FNCS Initialized", LogLevel.INFO, ProcessStatus.RUNNING, - true),GridAppsDConstants.username, + true),simContext.getSimulationUser(), GridAppsDConstants.topic_platformLog); @@ -174,7 +178,7 @@ public void run() { "Checking if FNCS simulation is finished, currently "+isFinished.isFinished, LogLevel.DEBUG, ProcessStatus.RUNNING, - true),GridAppsDConstants.username, + true),simContext.getSimulationUser(), GridAppsDConstants.topic_platformLog); Thread.sleep(1000); } @@ -185,7 +189,7 @@ public void run() { "FNCS Initialization Failed", LogLevel.ERROR, ProcessStatus.ERROR, - true),GridAppsDConstants.username, + true),simContext.getSimulationUser(), GridAppsDConstants.topic_platformLog); } @@ -198,7 +202,7 @@ public void run() { "Simulation "+simulationId+" complete", LogLevel.INFO, ProcessStatus.COMPLETE, - true),GridAppsDConstants.username, + true),simContext.getSimulationUser(), GridAppsDConstants.topic_platformLog); } catch(Exception e){ @@ -210,7 +214,7 @@ public void run() { "Simulation error: "+e.getMessage(), LogLevel.ERROR, ProcessStatus.ERROR, - true),GridAppsDConstants.username, + true),simContext.getSimulationUser(), GridAppsDConstants.topic_platformLog); } catch (Exception e1) { log.error("Error while reporting error status", e); @@ -243,7 +247,7 @@ private void startSimulation(GossFncsResponseEvent gossEvent, SimulationConfig s "Sending start simulation to bridge.", LogLevel.DEBUG, ProcessStatus.RUNNING, - true),GridAppsDConstants.username, + true),simContext.getSimulationUser(), GridAppsDConstants.topic_platformLog); String message = "{\"command\": \"StartSimulation\"}"; client.publish(GridAppsDConstants.topic_FNCS_input, message); @@ -256,11 +260,36 @@ public void run() { String line = null; try { while ((line = input.readLine()) != null) { - log.info(processName+": "+line.substring(0,Math.min(200, line.length()))); + if(!line.trim().isEmpty()){ + LogLevel level = LogLevel.INFO; + if(line.contains("DEBUG")) + level = LogLevel.DEBUG; + else if(line.contains("ERROR")) + level = LogLevel.ERROR; + else if(line.contains("FATAL") && !line.contains("INFO")) + level = LogLevel.FATAL; + else if(line.contains("WARN")) + level = LogLevel.WARN; + logManager.log(new LogMessage(this.getClass().getSimpleName(), + processName, + new Date().getTime(), + line, + level, + ProcessStatus.RUNNING, + true), + securityConfig.getManagerUser(), GridAppsDConstants.topic_simulationLog+simulationId); + } } } catch (IOException e) { if(!(e.getMessage().contains("Stream closed"))) - log.error("Error on process "+processName, e); + logManager.log(new LogMessage(this.getClass().getName(), + processName, + new Date().getTime(), + "Error reading input stream of simulator process: "+e.getMessage(), + LogLevel.ERROR, + ProcessStatus.ERROR, + true), + securityConfig.getManagerUser(), GridAppsDConstants.topic_simulationLog+simulationId); } } }.start(); @@ -294,7 +323,7 @@ public void onMessage(Serializable response) { "FNCS-GOSS Bridge response:"+dataResponse.getData(), LogLevel.DEBUG, ProcessStatus.RUNNING, - true),GridAppsDConstants.username, + true),simContext.getSimulationUser(), GridAppsDConstants.topic_platformLog); Gson gson = new Gson(); diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/HistoricalComparison.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/HistoricalComparison.java index 65be701c..7d0b5b48 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/HistoricalComparison.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/HistoricalComparison.java @@ -22,9 +22,11 @@ public class HistoricalComparison { DataManager dataManager; + String username; - public HistoricalComparison(DataManager dataManager) { + public HistoricalComparison(DataManager dataManager, String username) { this.dataManager = dataManager; + this.username = username; } public TestResultSeries test_proven(String simulationId, JsonObject expected_output_series){ @@ -70,7 +72,7 @@ public String timeSeriesQuery(String simulationId, String hasMrid, try { response = dataManager.processDataRequest(request, "timeseries", Integer.parseInt(simulationId), null, - GridAppsDConstants.username); + username); } catch (Exception e) { // TODO: Log error - excpetion } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/ProcessEvents.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/ProcessEvents.java index e322f813..1140e5f3 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/ProcessEvents.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/ProcessEvents.java @@ -61,19 +61,22 @@ public List getEvents() { private String simulationID; private long start_time; private int duration; + private String username; - public ProcessEvents(LogManager logManager, List events, long start_time, int duration){ + public ProcessEvents(LogManager logManager, List events, long start_time, int duration, String username){ this.logManager = logManager; + this.username = username; addEvents(events); this.duration = duration; this.start_time = start_time; } - public ProcessEvents(LogManager logManager, Client client, String simulationID, SimulationManager simulationManager){ + public ProcessEvents(LogManager logManager, Client client, String simulationID, SimulationManager simulationManager, String username){ System.out.println("New " + this.getClass().getSimpleName()); this.logManager = logManager; this.simulationID = simulationID; this.simulationManager = simulationManager; + this.username = username; processEvents(client); } @@ -344,7 +347,7 @@ public void logMessage(String msgStr, String simulationId) { logMessageObj.setStoreToDb(true); logMessageObj.setTimestamp(new Date().getTime()); logMessageObj.setLogMessage(msgStr); - logManager.log(logMessageObj,GridAppsDConstants.username,GridAppsDConstants.topic_platformLog); + logManager.log(logMessageObj,username,GridAppsDConstants.topic_platformLog); } } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/TestManagerImpl.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/TestManagerImpl.java index 9cd66a85..fd5eeab7 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/TestManagerImpl.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/TestManagerImpl.java @@ -85,6 +85,7 @@ import pnnl.goss.core.Client; import pnnl.goss.core.Client.PROTOCOL; +import pnnl.goss.core.security.SecurityConfig; import pnnl.goss.core.ClientFactory; import pnnl.goss.core.DataResponse; import pnnl.goss.core.GossResponseEvent; @@ -116,6 +117,9 @@ public class TestManagerImpl implements TestManager { @ServiceDependency private volatile SimulationManager simulationManager; + @ServiceDependency + private volatile SecurityConfig securityConfig; + private Hashtable rulePorts = new Hashtable(); private Random randPort = new Random(); @@ -159,7 +163,7 @@ public void start() { // Log - "Starting "+this.getClass().getName()); Credentials credentials = new UsernamePasswordCredentials( - GridAppsDConstants.username, GridAppsDConstants.password); + "testmanager1", "testmanager"); client = clientFactory.create(PROTOCOL.STOMP, credentials); client.subscribe(GridAppsDConstants.topic_simulationTestInput+">", new GossResponseEvent() { @@ -251,7 +255,7 @@ public List sendEventsToSimulation(List events, String simulationI private ProcessEvents getProcessEvents(Client client, String simulationId) { ProcessEvents pe; if(! processEventsMap.containsKey(simulationId) ){ - pe = processEventsMap.getOrDefault(simulationId, new ProcessEvents(logManager, client, simulationId, simulationManager)); + pe = processEventsMap.getOrDefault(simulationId, new ProcessEvents(logManager, client, simulationId, simulationManager, securityConfig.getManagerUser())); processEventsMap.putIfAbsent(simulationId, pe); } pe = processEventsMap.get(simulationId); @@ -269,9 +273,9 @@ public void updateEventForSimulation(List events, String simulationId){ public void sendEventStatus(String simulationId, Destination replyDestination){ // { // "data": [ -// {“faultMRID" : String, +// {"faultMRID" : String, // "simulation_id": int, -// “faultType:”: String, +// "faultType": String, // "fault": , // "timeInitiated":long, // "timeCleared":long, @@ -302,7 +306,7 @@ public void sendEventStatus(String simulationId, Destination replyDestination){ @Override public void compareSimulations(String simulationIdOne, String simulationIdTwo){ - HistoricalComparison hc = new HistoricalComparison(dataManger); + HistoricalComparison hc = new HistoricalComparison(dataManger, securityConfig.getManagerUser()); //TODO: Remove expected results from this method TestResultSeries testResultsSeries = null; //hc.test_proven(simulationIdTwo, expectedResultObject); client.publish(testOutputTopic+simulationIdOne, testResultsSeries); diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/TestManagerQueryFactory.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/TestManagerQueryFactory.java index b5749652..411dfa88 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/TestManagerQueryFactory.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/testmanager/TestManagerQueryFactory.java @@ -24,7 +24,7 @@ public String getFeeder() { QueryHandler queryHandler = getQueryHandler(); String lineQuery = "SELECT ?name WHERE { " + "?s r:type c:Feeder. " + "?s c:IdentifiedObject.name ?name} " + "ORDER by ?name"; - ResultSet results = queryHandler.query(lineQuery); + ResultSet results = queryHandler.query(lineQuery, null); String feederName = getResultName(results); return feederName; } @@ -34,7 +34,7 @@ public String getGeographicalRegion() { String geoRegionQuery = "SELECT ?name WHERE { " + "?s r:type c:GeographicalRegion. " + "?s c:IdentifiedObject.name ?name} " + "ORDER by ?name"; - ResultSet results = queryHandler.query(geoRegionQuery); + ResultSet results = queryHandler.query(geoRegionQuery, null); String geoName = getResultName(results); return geoName; } @@ -43,7 +43,7 @@ public String getSubGeographicalRegion() { QueryHandler queryHandler = getQueryHandler(); String subGeoRegionQuery = "SELECT ?name WHERE { " + "?s r:type c:SubGeographicalRegion. " + "?s c:IdentifiedObject.name ?name} " + "ORDER by ?name"; - ResultSet results = queryHandler.query(subGeoRegionQuery); + ResultSet results = queryHandler.query(subGeoRegionQuery, null); String subGeoName = getResultName(results); return subGeoName; } @@ -108,7 +108,7 @@ public void getMeasurements(){ "?s c:Measurement.phases ?phsraw ."+ " {bind(strafter(str(?phsraw),\"PhaseCode.\") as ?phases)}"+ "} ORDER BY ?class ?type ?name"; - ResultSet results = queryHandler.query(query); + ResultSet results = queryHandler.query(query, null); String subGeoName = getResultName(results); System.out.println(subGeoName); } diff --git a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/utils/GridAppsDConstants.java b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/utils/GridAppsDConstants.java index 14fb71de..6d754265 100644 --- a/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/utils/GridAppsDConstants.java +++ b/gov.pnnl.goss.gridappsd/src/gov/pnnl/goss/gridappsd/utils/GridAppsDConstants.java @@ -52,9 +52,6 @@ public class GridAppsDConstants { - //user credentials - public static final String username = "system"; - public static final String password = "manager"; //topics public static final String topic_prefix = "goss.gridappsd"; @@ -73,7 +70,8 @@ public class GridAppsDConstants { public static final String topic_app_register_remote = topic_requestApp+".remote.register"; public static final String topic_requestSimulationStatus = topic_process_prefix+".request.status.simulation"; public static final String topic_requestPlatformStatus = topic_process_prefix+".request.status.platform"; - + public static final String topic_requestMyRoles = topic_process_prefix+".request.roles"; + // Remote Application topics public static final String topic_remoteapp_prefix = topic_prefix+".remoteapp"; public static final String topic_remoteapp_heartbeat = topic_remoteapp_prefix+".heartbeat"; diff --git a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/LogManagerTests.java b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/LogManagerTests.java index c3f58cab..de738d99 100644 --- a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/LogManagerTests.java +++ b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/LogManagerTests.java @@ -90,7 +90,7 @@ public void storeCalledWhen_logStoreToDBTrueInObject() throws ParseException{ message.setStoreToDb(true); message.setTimestamp(GridAppsDConstants.SDF_SIMULATION_REQUEST.parse("11/11/11 11:11:11").getTime()); - logManager.log(message,GridAppsDConstants.username,GridAppsDConstants.topic_platformLog); + logManager.log(message,TestConstants.SYSTEM_USER_NAME,GridAppsDConstants.topic_platformLog); @@ -124,7 +124,7 @@ public void storeCalledWhen_logStoreToDBTrueInString() throws ParseException{ + "\"logMessage\":\"Testing LogManager\"," + "\"timestamp\": "+GridAppsDConstants.SDF_SIMULATION_REQUEST.parse("8/14/17 2:22:22").getTime()+"}"; - logManager.log(LogMessage.parse(message), GridAppsDConstants.username,GridAppsDConstants.topic_testLog); + logManager.log(LogMessage.parse(message), TestConstants.SYSTEM_USER_NAME,GridAppsDConstants.topic_testLog); Mockito.verify(logDataManager).store(argCaptor.capture(),argCaptor.capture(), argLongCaptor.capture(), argCaptor.capture(), diff --git a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ProcessEventTests.java b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ProcessEventTests.java index 95fa0744..15d6957a 100644 --- a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ProcessEventTests.java +++ b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ProcessEventTests.java @@ -44,6 +44,7 @@ import gov.pnnl.goss.gridappsd.api.DataManager; import gov.pnnl.goss.gridappsd.api.LogDataManager; import gov.pnnl.goss.gridappsd.api.LogManager; +import gov.pnnl.goss.gridappsd.api.RoleManager; import gov.pnnl.goss.gridappsd.api.ServiceManager; import gov.pnnl.goss.gridappsd.api.SimulationManager; import gov.pnnl.goss.gridappsd.api.TestManager; @@ -94,6 +95,8 @@ public class ProcessEventTests { Client client; @Mock TestManager testManager; + @Mock + RoleManager roleManager; @Captor ArgumentCaptor argCaptor; @@ -112,7 +115,7 @@ public void testWhen_RequestPGQueryRequestSent() throws ParseException{ DataManager dataManager = new DataManagerImpl(clientFactory, logManager); ProcessEvent pe = new ProcessEvent(processManager, client, - newSimulationProcess, configurationManager, simulationManager, appManager, logManager, serviceManager, dataManager, testManager); + newSimulationProcess, configurationManager, simulationManager, appManager, logManager, serviceManager, dataManager, testManager,roleManager); PowergridModelDataRequest pgDataRequest = new PowergridModelDataRequest(); String queryString = "select ?line_name ?subregion_name ?region_name WHERE {?line rdf:type cim:Line."+ @@ -143,7 +146,7 @@ public void testWhen_RequestPGQueryObjectTypesRequestSent() throws ParseExceptio DataManager dataManager = new DataManagerImpl(clientFactory, logManager); ProcessEvent pe = new ProcessEvent(processManager, client, - newSimulationProcess, configurationManager, simulationManager, appManager, logManager, serviceManager, dataManager, testManager); + newSimulationProcess, configurationManager, simulationManager, appManager, logManager, serviceManager, dataManager, testManager,roleManager); PowergridModelDataRequest pgDataRequest = new PowergridModelDataRequest(); @@ -167,7 +170,7 @@ public void testWhen_RequestPGQueryModelNamesRequestSent() throws ParseException DataManager dataManager = new DataManagerImpl(clientFactory, logManager); ProcessEvent pe = new ProcessEvent(processManager, client, - newSimulationProcess, configurationManager, simulationManager, appManager, logManager, serviceManager, dataManager, testManager); + newSimulationProcess, configurationManager, simulationManager, appManager, logManager, serviceManager, dataManager, testManager,roleManager); PowergridModelDataRequest pgDataRequest = new PowergridModelDataRequest(); diff --git a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ProcessManagerComponentTests.java b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ProcessManagerComponentTests.java index 08828fca..f5e29c58 100644 --- a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ProcessManagerComponentTests.java +++ b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ProcessManagerComponentTests.java @@ -333,7 +333,7 @@ public void processStartedWhen_simulationTopicSent(){ ArgumentCaptor argCaptorSerializable= ArgumentCaptor.forClass(Serializable.class) ; Mockito.verify(newSimulationProcess).process(Mockito.any(), Mockito.any(), - Mockito.anyInt(),Mockito.any(),Mockito.any(), Mockito.any(),Mockito.any(), Mockito.any(),Mockito.any()); + Mockito.anyInt(),Mockito.any(),Mockito.any(), Mockito.any(),Mockito.any(), Mockito.any(),Mockito.any(),Mockito.any()); String messageString = argCaptorSerializable.getValue().toString(); assertNotNull(RequestSimulation.parse(messageString)); diff --git a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ProcessNewSimulationRequestComponentTests.java b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ProcessNewSimulationRequestComponentTests.java index 0f0a02a4..85e3f50a 100644 --- a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ProcessNewSimulationRequestComponentTests.java +++ b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ProcessNewSimulationRequestComponentTests.java @@ -112,7 +112,7 @@ public void callsMadeWhen_processStarted(){ int simulationId = Math.abs(new Random().nextInt()); ProcessNewSimulationRequest request = new ProcessNewSimulationRequest(logManager); RequestSimulation requestSimulation = RequestSimulation.parse(REQUEST_SIMULATION_CONFIG); - request.process(configurationManager, simulationManager, simulationId, event, requestSimulation,appManager, serviceManager, testManager,dataManager); + request.process(configurationManager, simulationManager, simulationId, event, requestSimulation,appManager, serviceManager, testManager,dataManager,TestConstants.SYSTEM_USER_NAME); // request simulation object parsed successfully and first log info call made Mockito.verify(logManager, Mockito.times(3)).log(argCaptorLogMessage.capture(), argCaptor.capture(),argCaptor.capture()); //GridAppsDConstants.username); @@ -164,7 +164,7 @@ public void callsMadeWhen_processError(){ ProcessNewSimulationRequest request = new ProcessNewSimulationRequest(logManager); RequestSimulation requestSimulation = RequestSimulation.parse(REQUEST_SIMULATION_CONFIG); requestSimulation.getPower_system_config().setGeographicalRegion_name("Bad"); - request.process(configurationManager, simulationManager, simulationId, event, requestSimulation, appManager, serviceManager, testManager,dataManager); + request.process(configurationManager, simulationManager, simulationId, event, requestSimulation, appManager, serviceManager, testManager,dataManager,TestConstants.SYSTEM_USER_NAME); // try { // Mockito.verify(statusReporter).reportStatus(Mockito.any(), argCaptor.capture()); @@ -200,7 +200,7 @@ public void callsMadeWhen_processErrorBecauseNullConfig(){ int simulationId = Math.abs(new Random().nextInt()); ProcessNewSimulationRequest request = new ProcessNewSimulationRequest(logManager); - request.process(configurationManager, simulationManager, simulationId, event, null, appManager, serviceManager, testManager,dataManager); + request.process(configurationManager, simulationManager, simulationId, event, null, appManager, serviceManager, testManager,dataManager,TestConstants.SYSTEM_USER_NAME); // try { // Mockito.verify(statusReporter).reportStatus(Mockito.any(), argCaptor.capture()); @@ -238,7 +238,7 @@ public void callsMadeWhen_processErrorBecauseNullSimulationFile(){ int simulationId = Math.abs(new Random().nextInt()); ProcessNewSimulationRequest request = new ProcessNewSimulationRequest(logManager); RequestSimulation requestSimulation = RequestSimulation.parse(REQUEST_SIMULATION_CONFIG); - request.process(configurationManager, simulationManager, simulationId, event, requestSimulation,appManager, serviceManager, testManager, dataManager); + request.process(configurationManager, simulationManager, simulationId, event, requestSimulation,appManager, serviceManager, testManager, dataManager,TestConstants.SYSTEM_USER_NAME); // request error log call made diff --git a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ServiceManagerTests.java b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ServiceManagerTests.java index c9fca4eb..3395402d 100644 --- a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ServiceManagerTests.java +++ b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/ServiceManagerTests.java @@ -43,6 +43,7 @@ import gov.pnnl.goss.gridappsd.service.ServiceManagerImpl; import java.io.File; +import java.util.HashMap; import java.util.Hashtable; import java.util.Map; @@ -90,7 +91,11 @@ public void testPythonServiceStart_WithNoDependencyNoSimulation(){ @Test public void testPythonServiceStart_WithDependencyAndSimulation(){ - serviceManager.startService("fncsgossbridge", "simulation_1"); + + HashMap props = new HashMap(); + props.put("simulationId", "simulation_1"); + + serviceManager.startService("fncsgossbridge", props); } @Test diff --git a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/TestConstants.java b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/TestConstants.java index fbf2d1fa..65846a71 100644 --- a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/TestConstants.java +++ b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/TestConstants.java @@ -64,5 +64,6 @@ public class TestConstants { public static final String FNCS_BRIDGE_RESPONSE = "{\"timestamp\":0,\"command\":\"isInitialized\",\"response\":\"true\",\"output\":\"Any messages from simulator regarding initialization\"}"; + public static final String SYSTEM_USER_NAME = "system"; } diff --git a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/TestManagerEventsTest.java b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/TestManagerEventsTest.java index 31bc2aa8..f84ba336 100644 --- a/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/TestManagerEventsTest.java +++ b/gov.pnnl.goss.gridappsd/test/gov/pnnl/goss/gridappsd/TestManagerEventsTest.java @@ -83,7 +83,7 @@ public class TestManagerEventsTest { @Before public void beforeTests(){ List events = new ArrayList(); - processEvent = new ProcessEvents(logManager, events, start_time, 120); + processEvent = new ProcessEvents(logManager, events, start_time, 120, TestConstants.SYSTEM_USER_NAME); GsonBuilder gsonBuilder = new GsonBuilder(); RuntimeTypeAdapterFactory commandAdapterFactory = RuntimeTypeAdapterFactory.of(Event.class, "event_type") .registerSubtype(CommOutage.class,"CommOutage").registerSubtype(Fault.class, "Fault").registerSubtype(ScheduledCommandEvent.class, "ScheduledCommandEvent"); diff --git a/services/GridLAB-D.config b/services/GridLAB-D.config index 8d3e9426..1eb29e69 100644 --- a/services/GridLAB-D.config +++ b/services/GridLAB-D.config @@ -8,7 +8,7 @@ "execution_path": "gridlabd.sh", "type": "EXE", "launch_on_startup": "false", - "service_dependencies": ["fncs","fncsgossbridge"], - "multiple_instances": "true", + "service_dependencies": ["fncs","fncsgossbridge","gridappsd-alarms","gridappsd-voltage-violation"], + "multiple_instances": false, "environmentVariables":[] } diff --git a/services/fncs.config b/services/fncs.config index 4fe44e05..ce5cbb39 100644 --- a/services/fncs.config +++ b/services/fncs.config @@ -9,6 +9,6 @@ "type": "EXE", "launch_on_startup": "false", "prereqs": [], - "multiple_instances": "true", + "multiple_instances": false, "environmentVariables":[{"envName":"FNCS_FATAL","envValue":false},{"envName":"FNCS_BROKER","envValue":"tcp://*:(simulationPort)"}] -} \ No newline at end of file +} diff --git a/services/fncsgossbridge.config b/services/fncsgossbridge.config index f34bcb8b..498083f8 100644 --- a/services/fncsgossbridge.config +++ b/services/fncsgossbridge.config @@ -9,6 +9,6 @@ "type": "PYTHON", "launch_on_startup": "false", "prereqs": ["fncs"], - "multiple_instances": "true", + "multiple_instances": false, "environmentVariables":[{"envName":"FNCS_FATAL","envValue":false}] } diff --git a/services/fncsgossbridge/service/fncs_goss_bridge.py b/services/fncsgossbridge/service/fncs_goss_bridge.py index 5beb3a02..fee59c50 100644 --- a/services/fncsgossbridge/service/fncs_goss_bridge.py +++ b/services/fncsgossbridge/service/fncs_goss_bridge.py @@ -49,13 +49,16 @@ import argparse import cmath from datetime import datetime +import gzip import json +import logging import math import os try: from Queue import Queue except: from queue import Queue +import sqlite3 import sys import time @@ -79,6 +82,7 @@ output_to_simulation_manager = 'goss.gridappsd.fncs.output' output_to_goss_topic = '/topic/goss.gridappsd.simulation.output.' #this should match GridAppsDConstants.topic_FNCS_output simulation_input_topic = '/topic/goss.gridappsd.simulation.input.' +_log = logging.getLogger(__name__) goss_connection= None is_initialized = False @@ -199,6 +203,83 @@ } } +db_connection = None +measurement_cache = {} + + +def create_schema(conn): + sql = """ + CREATE TABLE IF NOT EXISTS measurement_props ( + measurement_id text primary key + ); + + CREATE TABLE IF NOT EXISTS measurements ( + measurement_id text, + timestamp int, + property_key text, + property_value value, + FOREIGN KEY (measurement_id) + REFERENCES measurement_props (measurement_id) + ); + + CREATE INDEX timestamp_indx ON measurements(timestamp); + """ + cursor = db_connection.cursor() + for d in sql.split(';'): + _log.debug("Executing {d}".format(d=d)) + cursor.execute(d) + cursor.close() + db_connection.commit() + + +def write_db_archive(timestamp, measurements): + global db_connection + global measurement_cache + _log.debug("Inserting measurements for timestamp: {timestamp}".format(timestamp=timestamp)) + + if db_connection is None: + raise ValueError("Invalid process, must create connection first.") + + sql_insert_measurement_props = """ + INSERT INTO measurement_props (measurement_id) VALUES(?); + """ + sql_insert_measurement = """ + INSERT INTO measurements(measurement_id, timestamp, property_key, property_value) + VALUES (?,?,?,?);""" + + needs_insert_measurement = [] + bulk_insert_measurements = [] + for k, v in measurements.items(): + if k not in measurement_cache: + needs_insert_measurement.append((k,)) + measurement_cache[k] = True + measurement_mrid = k + for prop, value in v.items(): + # the measurement_mrid is already handled by each of the properties. as long + # as there is at least one measurement for that mrid. + if prop == "measurement_mrid": + continue + bulk_insert_measurements.append((measurement_mrid, timestamp, prop, value)) + + if needs_insert_measurement: + cur = db_connection.cursor() + cur.executemany(sql_insert_measurement_props, needs_insert_measurement) + db_connection.commit() + cur.close() + + cur = db_connection.cursor() + cur.executemany(sql_insert_measurement, bulk_insert_measurements) + db_connection.commit() + cur.close() + + +def create_db_connection(filename): + global db_connection + _log.debug("Creating sqlite filename: {filename}".format(filename=filename)) + db_connection = sqlite3.connect(filename) + create_schema(db_connection) + + class GOSSListener(object): def __init__(self, sim_length, sim_start): @@ -216,10 +297,14 @@ def __init__(self, sim_length, sim_start): self.filter_all_measurements = False self.message_id_list = [] - def run_simulation(self,run_realtime): + def run_simulation(self,run_realtime, archive_db_file, archive_file, only_archive): + targz_file = None try: + if archive_file: + targz_file = gzip.open(archive_file, "wb") + if archive_db_file: + create_db_connection(archive_db_file) message = {} - current_time = 0; message['command'] = 'nextTimeStep' for current_time in range(self.simulation_length): while self.pause_simulation == True: @@ -235,14 +320,24 @@ def run_simulation(self,run_realtime): else: message['output'] = {} response_msg = json.dumps(message['output']) + if message['output']!={}: - goss_connection.send(output_to_goss_topic + "{}".format(simulation_id) , response_msg) + if not only_archive: + goss_connection.send(output_to_goss_topic + "{}".format(simulation_id) , response_msg) + if archive_db_file: + ts = message['output']['message']['timestamp'] + meas = message['output']['message']['measurements'] + _log.debug("Passing timestamp {ts} to write_db_archive".format(ts=ts)) + write_db_archive(ts, meas) + if targz_file: + targz_file.write((response_msg+"\n").encode('utf-8')) + #forward messages from GOSS to FNCS while not self.goss_to_fncs_message_queue.empty(): _publish_to_fncs_bus(simulation_id, self.goss_to_fncs_message_queue.get(), self.command_filter) _done_with_time_step(current_time) #current_time is incrementing integer 0 ,1, 2.... representing seconds - message_str = 'done with timestep '+str(current_time) - _send_simulation_status('RUNNING', message_str, 'DEBUG') + #message_str = 'done with timestep '+str(current_time) + #_send_simulation_status('RUNNING', message_str, 'DEBUG') message_str = 'incrementing to '+str(current_time + 1) _send_simulation_status('RUNNING', message_str, 'DEBUG') if run_realtime == True: @@ -251,13 +346,17 @@ def run_simulation(self,run_realtime): message['command'] = 'simulationFinished' del message['output'] goss_connection.send(output_to_simulation_manager, json.dumps(message)) - _send_simulation_status('COMPLETE', 'Simulation {} has finsihed.'.format(simulation_id), 'INFO') + _send_simulation_status('COMPLETE', 'Simulation {} has finished.'.format(simulation_id), 'INFO') except Exception as e: message_str = 'Error in run simulation '+str(e) + _log.exception(message_str) _send_simulation_status('ERROR', message_str, 'ERROR') self.stop_simulation = True if fncs.is_initialized(): fncs.die() + finally: + if targz_file: + targz_file.close() def on_message(self, headers, msg): @@ -713,8 +812,8 @@ def _get_fncs_bus_messages(simulation_id, measurement_filter): raise ValueError( 'simulation_id must be a nonempty string.\n' + 'simulation_id = {0}'.format(simulation_id)) - message_str = 'about to get fncs events' - _send_simulation_status('RUNNING', message_str, 'DEBUG') + #message_str = 'about to get fncs events' + #_send_simulation_status('RUNNING', message_str, 'DEBUG') message_events = fncs.get_events() message_str = 'fncs events '+str(message_events) _send_simulation_status('RUNNING', message_str, 'DEBUG') @@ -849,18 +948,18 @@ def _done_with_time_step(current_time): ValueError() """ try: - message_str = 'Done with timestep '+str(current_time) - _send_simulation_status('RUNNING', message_str, 'DEBUG') + #message_str = 'Done with timestep '+str(current_time) + #_send_simulation_status('RUNNING', message_str, 'DEBUG') if current_time == None or type(current_time) != int: raise ValueError( 'current_time must be an integer.\n' + 'current_time = {0}'.format(current_time)) time_request = current_time + 1 - message_str = 'calling time_request '+str(time_request) - _send_simulation_status('RUNNING', message_str, 'DEBUG') + #message_str = 'calling time_request '+str(time_request) + #_send_simulation_status('RUNNING', message_str, 'DEBUG') time_approved = fncs.time_request(time_request) - message_str = 'time approved '+str(time_approved) - _send_simulation_status('RUNNING', message_str, 'DEBUG') + #message_str = 'time approved '+str(time_approved) + #_send_simulation_status('RUNNING', message_str, 'DEBUG') if time_approved != time_request: raise RuntimeError( 'The time approved from fncs_broker is not the time requested.\n' @@ -1254,22 +1353,23 @@ def _byteify(data, ignore_dicts = False): return data -def _keep_alive(is_realtime): +def _keep_alive(is_realtime, archive_db_file, archive_file, only_archive): simulation_ran = False while goss_listener_instance.stop_simulation == False: time.sleep(0.1) if goss_listener_instance.start_simulation == True and simulation_ran == False: - goss_listener_instance.run_simulation(is_realtime) + goss_listener_instance.run_simulation(is_realtime, archive_db_file, archive_file, only_archive) simulation_ran = True -def _main(simulation_id, simulation_broker_location='tcp://localhost:5570', measurement_map_dir='', is_realtime=True, sim_duration=86400, sim_start=0): +def _main(simulation_id, simulation_broker_location='tcp://localhost:5570', measurement_map_dir='', is_realtime=True, + sim_duration=86400, sim_start=0, archive_db_file=None, archive_file=None, only_archive=False): measurement_map_file=str(measurement_map_dir)+"model_dict.json" _register_with_goss(simulation_id,'system','manager','127.0.0.1','61613', sim_duration, sim_start) _register_with_fncs_broker(simulation_broker_location) _create_cim_object_map(measurement_map_file) - _keep_alive(is_realtime) + _keep_alive(is_realtime, archive_db_file, archive_file, only_archive) def _get_opts(): parser = argparse.ArgumentParser() @@ -1285,10 +1385,31 @@ def _get_opts(): #stomp_port, username and password as commmand line arguments opts = _get_opts() simulation_id = opts.simulation_id + # logging within the context of the container. + logfile = "/tmp/gridappsd_tmp/{simulation_id}/fncs_goss_bridge.log".format(simulation_id=simulation_id) + logging.basicConfig(level=logging.INFO, filename=logfile) + sim_broker_location = opts.broker_location sim_dir = opts.simulation_directory sim_request = json.loads(opts.simulation_request.replace("\'","")) run_realtime = sim_request["simulation_config"]["run_realtime"] sim_duration = sim_request["simulation_config"]["duration"] sim_start_str = int(sim_request["simulation_config"]["start_time"]) - _main(simulation_id, sim_broker_location, sim_dir, run_realtime, sim_duration, sim_start_str) + # New archiving variables set here + # Once the simulation_config is sent directly from the ui, then we can use these, + # Until then you can change the archive to have a default value for either the + # archive or the db_archive. These will be off by default as is the current + # setup. + make_db_archive = sim_request['simulation_config'].get("make_db_archive", False) + make_archive = sim_request["simulation_config"].get("make_archive", False) + only_archive = sim_request["simulation_config"].get("only_archive", False) + archive_db_file = None + if make_db_archive: + archive_db_file = "/tmp/gridappsd_tmp/{simulation_id}/archive.sqlite".format(simulation_id=simulation_id) + archive_file = None + if make_archive: + archive_file = "/tmp/gridappsd_tmp/{simulation_id}/archive.tar.gz".format(simulation_id=simulation_id) + _log.debug("Archive settings:\n\tarchive_db_file: {}\n\tarchive_file: {}\n\tonly_archive: {}".format( + archive_db_file, archive_file, only_archive)) + _main(simulation_id, sim_broker_location, sim_dir, run_realtime, sim_duration, sim_start_str, + archive_db_file, archive_file, only_archive)