-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from AO-StreetArt/dev
Update Project Structure & Addition of unit tests
- Loading branch information
Showing
13 changed files
with
725 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,35 @@ | ||
language: java | ||
jdk: | ||
- oraclejdk8 | ||
- oraclejdk8 | ||
services: | ||
- docker | ||
- docker | ||
before_install: | ||
- docker network create dvs | ||
- docker run -d -p 8500:8500 --name=registry --network=dvs consul agent --dev -ui -client=0.0.0.0 | ||
- docker exec -t registry curl -X PUT -d 'cache--6379----2--5--0' http://localhost:8500/v1/kv/ivan/RedisConnectionString | ||
- docker exec -t registry curl -X PUT -d 'neo4j://graph-db:7687' http://localhost:8500/v1/kv/ivan/DB_ConnectionString | ||
- docker exec -t registry curl -X PUT -d 'True' http://localhost:8500/v1/kv/ivan/StampTransactionId | ||
- docker exec -t registry curl -X PUT -d 'True' http://localhost:8500/v1/kv/ivan/AtomicTransactions | ||
- docker exec -t registry curl -X PUT -d 'Json' http://localhost:8500/v1/kv/ivan/Data_Format_Type | ||
- docker run -d --publish=7474:7474 --publish=7687:7687 --env=NEO4J_AUTH=none --volume=/home/abarry/neo4j/data:/data --network=dvs --name=graph-db neo4j | ||
- docker run --network=dvs --name=cache -d redis | ||
- docker run --name document-db -d --network=dvs mongo | ||
- sleep 3 | ||
- docker run --name crazyivan --network=dvs -p 5555:5555 -d aostreetart/crazyivan:latest -consul-addr=registry:8500 -ip=localhost -port=5555 -log-conf=CrazyIvan/log4cpp.properties | ||
- sleep 3 | ||
- docker logs --tail 50 registry | ||
- docker logs --tail 50 cache | ||
- docker logs --tail 50 document-db | ||
- docker logs --tail 50 graph-db | ||
- docker logs --tail 150 crazyivan | ||
- docker ps -a | ||
- docker network create dvs | ||
- docker run -d -p 8500:8500 --name=registry --network=dvs consul agent --dev -ui | ||
-client=0.0.0.0 | ||
- docker exec -t registry curl -X PUT -d 'cache--6379----2--5--0' http://localhost:8500/v1/kv/ivan/RedisConnectionString | ||
- docker exec -t registry curl -X PUT -d 'neo4j://graph-db:7687' http://localhost:8500/v1/kv/ivan/DB_ConnectionString | ||
- docker exec -t registry curl -X PUT -d 'True' http://localhost:8500/v1/kv/ivan/StampTransactionId | ||
- docker exec -t registry curl -X PUT -d 'True' http://localhost:8500/v1/kv/ivan/AtomicTransactions | ||
- docker exec -t registry curl -X PUT -d 'Json' http://localhost:8500/v1/kv/ivan/Data_Format_Type | ||
- docker run -d --publish=7474:7474 --publish=7687:7687 --env=NEO4J_AUTH=none --volume=/home/abarry/neo4j/data:/data | ||
--network=dvs --name=graph-db neo4j | ||
- docker run --network=dvs --name=cache -d redis | ||
- docker run --name document-db -d --network=dvs mongo | ||
- sleep 3 | ||
- docker run --name crazyivan --network=dvs -p 5555:5555 -d aostreetart/crazyivan:latest | ||
-consul-addr=registry:8500 -ip=localhost -port=5555 -log-conf=CrazyIvan/log4cpp.properties | ||
- sleep 3 | ||
- docker logs --tail 50 registry | ||
- docker logs --tail 50 cache | ||
- docker logs --tail 50 document-db | ||
- docker logs --tail 50 graph-db | ||
- docker logs --tail 150 crazyivan | ||
- docker ps -a | ||
install: | ||
- gradle assemble | ||
- gradle assemble | ||
script: | ||
- gradle check | ||
- gradle check | ||
notifications: | ||
slack: | ||
secure: AfMTl+Si4xHctYyrB8GaV5a5fJYEuX18Ow5/NRwpZuRoGfcFU/CkX/SKNHNMSpUyXdMDiTXuylnt3KtscwSTn2pJdfqlx91n5wuIcx7uO+Hv0pLsxKhHJ/3WLYVrPK9GKEVHbsKpR+gxw8Bu/HTGqLBL8k9TAjGl97m1h39k3ovIbAjxzKhSlUSVlxcdqmD18CEnD2szH1qSZ4IlsIraYJ0F7rZtr8mdAESPPU9Q9BAWSVNF2wQ40c3PPYZb6l/zaKEYN3BqHsBGEJHRzH+Aa+ZXtFfgtj6voqiigMVw3FTPl66jPaDx7TI8mJFFBWUGOOImrwNYliIiGyo/b+q3N4OYVWADHp1RaLQEZqwQzCqRfoqgIUCagDF5wjOP6hQCobBT61o2wDSE2xHT73yKnr03AHp7An0flD0+/SYl9J2OOIe9vLgluVUEPa/jyiApldpKoH+JH0ZhvdMhz40Eu3iV9XtBdl0emT9gSeY5cKu3LSuA1AHIaGQz4VpsEokF14ebVSbxKx6rYjaNOViM56vwpBOT0eS9lD/NNduu5OBcijoifpBc5oAAUuK1t4VuoXDtBMaMdoqcxExRcfuRK/H63pUgDt1RkXKRysvg68EJMPLZosCrBwit5jsVbyWn9svLH7D1Kew437rHccD6KXsFjUOrp2Vc+0ppuDAvSCk= |
154 changes: 154 additions & 0 deletions
154
src/main/java/adrestia/controller/scene/RegistrationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
Apache2 License Notice | ||
Copyright 2017 Alex Barry | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package adrestia; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerationException; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.Semaphore; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import javax.annotation.PreDestroy; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.discovery.DiscoveryClient; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* Rest Controller defining the Scene Registration API. | ||
* Responsible for handling and responding to all Scene API Requests. | ||
*/ | ||
@RestController | ||
@RequestMapping(path = "/v1/scene") | ||
public class RegistrationController { | ||
|
||
// DVS Manager, DAO Object allowing access to dependent services | ||
@Autowired | ||
DvsManager serviceManager; | ||
|
||
// Utility Provider, providing us with basic utility methods | ||
@Autowired | ||
UtilityProvider utils; | ||
|
||
// Scene Controller Logger | ||
private static final Logger logger = | ||
LogManager.getLogger("adrestia.RegistrationController"); | ||
|
||
// Execute a registration transaction with Crazy Ivan | ||
private SceneList ivanRegistrationTransaction(String sceneName, | ||
String deviceId, Transform inpTransform, int registerMsgType) { | ||
logger.debug("Scene Registration Name: " + sceneName); | ||
logger.debug("Scene Registration Device " + deviceId); | ||
|
||
// Construct a User Device array | ||
UserDevice ud = new UserDevice(); | ||
ud.setKey(deviceId); | ||
// Pass the transform to the user device, if one is passed in | ||
if (inpTransform != null) { | ||
logger.debug("Input Transform detected"); | ||
ud.setTransform(inpTransform); | ||
} | ||
UserDevice[] devices = {ud}; | ||
// Construct a scene array | ||
Scene scn = new Scene(); | ||
scn.setName(sceneName); | ||
scn.setDevices(devices); | ||
Scene[] scnArray = {scn}; | ||
// Construct a Scene List, which we will then convert to JSON | ||
SceneList inpSceneList = new SceneList(registerMsgType, scnArray); | ||
// Send the Scene List to Crazy Ivan and get the response | ||
return serviceManager.ivanTransaction(inpSceneList); | ||
} | ||
|
||
// Process a registration request | ||
private ResponseEntity<Scene> processRegistration(String name, | ||
String device, Transform inpTransform, int registrationMsgType) { | ||
Scene returnScn = new Scene(); | ||
HttpStatus returnCode = HttpStatus.OK; | ||
|
||
SceneList ivanResponse = ivanRegistrationTransaction( | ||
name, device, inpTransform, registrationMsgType); | ||
|
||
// If we have a successful response, then we pull the first value | ||
if (ivanResponse.getNumRecords() > 0 | ||
&& ivanResponse.getErrorCode() == 100) { | ||
returnScn = ivanResponse.getSceneList()[0]; | ||
returnCode = utils.translateIvanError(ivanResponse.getErrorCode()); | ||
} else { | ||
returnCode = HttpStatus.INTERNAL_SERVER_ERROR; | ||
logger.debug("Failure Registered. Ivan Response Error Code and Length:"); | ||
logger.debug(ivanResponse.getNumRecords()); | ||
logger.debug(ivanResponse.getErrorCode()); | ||
} | ||
|
||
// Set up a response header to return a valid HTTP Response | ||
HttpHeaders responseHeaders = new HttpHeaders(); | ||
responseHeaders.set("Content-Type", "application/json"); | ||
|
||
// Create and return the new HTTP Response | ||
return new ResponseEntity<Scene>(returnScn, responseHeaders, returnCode); | ||
} | ||
|
||
/** | ||
* Scene Registration. | ||
*/ | ||
@RequestMapping(path = "/{name}/registration", method = RequestMethod.PUT) | ||
public ResponseEntity<Scene> register(@PathVariable("name") String name, | ||
@RequestParam(value = "device_id", defaultValue = "") String device, | ||
@RequestBody(required = false) Transform inpTransform) { | ||
logger.info("Responding to Scene Registration Request"); | ||
return processRegistration(name, device, inpTransform, 4); | ||
} | ||
|
||
/** | ||
* Scene De-Registration. | ||
*/ | ||
@RequestMapping(path = "/{name}/registration", method = RequestMethod.DELETE) | ||
public ResponseEntity<Scene> deregister(@PathVariable("name") String name, | ||
@RequestParam(value = "device_id", defaultValue = "") String device) { | ||
logger.info("Responding to Scene De-Registration Request"); | ||
return processRegistration(name, device, null, 5); | ||
} | ||
|
||
/** | ||
* Scene-Device Synchronization. | ||
*/ | ||
@RequestMapping(path = "/{name}/registration", method = RequestMethod.POST) | ||
public ResponseEntity<Scene> synchronize(@PathVariable("name") String name, | ||
@RequestParam(value = "device_id", defaultValue = "") String device, | ||
@RequestBody(required = false) Transform inpTransform) { | ||
logger.info("Responding to Scene Synchronization Request"); | ||
return processRegistration(name, device, inpTransform, 6); | ||
} | ||
|
||
} |
Oops, something went wrong.