Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery
/
mde
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Registry
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
6a7e8284
authored
Mar 28, 2017
by
Adi Amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
support cisco login and parking
parent
4d6225d0
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
525 additions
and
29 deletions
docker-compose.yml
src/main/java/logic/CiscoManager.java
src/main/java/logic/GEManager.java
src/main/java/logic/MdeManager.java
docker-compose.yml
View file @
6a7e8284
mde
:
mde
:
environment
:
environment
:
IPG_ENV_PARAMS
:
"
-Dds.IpAddress=172.16.1.151:8012#
\
IPG_ENV_PARAMS
:
"
-Dds.IpAddress=172.16.1.151:8012#
\
-Dredis.host=172.16.1.151"
-Dredis.host=172.16.1.151#
\
-Dmde.cisco.username=devoperator11@cdp.com#
\
-Dmde.cisco.password=z9E+H=9$J#
\
-Dmde.cisco.clientId=a27b18484c3c4e08a7c193e42c639347#
\
-Dmde.cisco.clientSecret=b863de8f453c4a05A88126F45B958CF1"
USE_DEBUG
:
"
yes"
USE_DEBUG
:
"
yes"
image
:
172.16.1.212:5050/mcx/mde
image
:
172.16.1.212:5050/mcx/mde
ports
:
ports
:
...
...
src/main/java/logic/CiscoManager.java
0 → 100644
View file @
6a7e8284
package
logic
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.node.ArrayNode
;
import
com.fasterxml.jackson.databind.node.NullNode
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
com.google.gdata.util.common.base.PercentEscaper
;
import
http.simpleHttpClient.SimpleHttpClient
;
import
http.simpleHttpClient.SimpleHttpRequest
;
import
http.simpleHttpClient.SimpleHttpResponse
;
import
microservice.MicroserviceClient
;
import
microservice.RequestContext
;
import
microservice.io.iface.ILogger
;
import
microservice.params.RestClientParams
;
import
microservice.types.BaseRestResponse
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.client.CookieStore
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.conn.scheme.PlainSocketFactory
;
import
org.apache.http.conn.scheme.Scheme
;
import
org.apache.http.conn.scheme.SchemeRegistry
;
import
org.apache.http.conn.ssl.SSLSocketFactory
;
import
org.apache.http.impl.client.DefaultHttpClient
;
import
org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
/**
* Created by gil on 01/03/17.
*/
public
class
CiscoManager
{
// consts
public
static
String
CISCO_DOMAIN
=
"10.10.20.6"
;
public
static
String
API_URI
=
"/apigw/devnetlabapi/cdp/v1/"
;
public
static
String
API_TOKEN_URI
=
"/apigw/devnetlabtokenapi/"
;
//tools
private
static
PercentEscaper
pes
=
new
PercentEscaper
(
"-_.!~*\'()@:$,;/?:"
,
false
);
// members
private
ILogger
logger
;
private
ObjectMapper
objMapper
=
new
ObjectMapper
();
private
SimpleHttpClient
httpClient
=
null
;
private
MicroserviceClient
geClient
=
null
;
private
RestClientParams
geClientParams
=
null
;
private
LoginInfo
loginInfo
=
new
LoginInfo
();
// private types
private
class
LoginInfo
{
Boolean
loginSuccessfull
=
false
;
String
clientId
=
null
;
String
clientSecret
=
null
;
String
username
=
null
;
String
password
=
null
;
String
appAccessToken
=
null
;
String
apiAccessToken
=
null
;
String
appRefreshToken
=
null
;
String
userId
=
null
;
String
parentUserId
=
null
;
}
public
CiscoManager
(
ILogger
logger
)
throws
Exception
{
this
.
logger
=
logger
;
initHttpClient
();
LoadLoginInfo
();
}
private
void
initHttpClient
()
{
httpClient
=
new
SimpleHttpClient
();
httpClient
.
Initialize
(
100
);
httpClient
.
addMimeType
(
"video/mp4"
);
}
private
void
LoadLoginInfo
()
{
loginInfo
.
username
=
System
.
getProperty
(
"mde.cisco.username"
,
""
);
loginInfo
.
password
=
System
.
getProperty
(
"mde.cisco.password"
,
""
);
loginInfo
.
clientId
=
System
.
getProperty
(
"mde.cisco.clientId"
,
""
);
loginInfo
.
clientSecret
=
System
.
getProperty
(
"mde.cisco.clientSecret"
,
""
);
}
public
BaseRestResponse
executeRequest
(
String
serviceId
,
String
[]
apiIdAsParams
,
RequestContext
requestContext
)
{
String
error
=
null
;
BaseRestResponse
brr
=
new
BaseRestResponse
(
false
,
null
);
long
tStart
=
System
.
currentTimeMillis
();
logger
.
info
(
"cisco.executeRequest() started: "
+
getApiIdString
(
apiIdAsParams
));
// login for the first time
//if (loginInfo.loginSuccessfull == false) {
brr
=
login
(
loginInfo
);
if
(
brr
.
success
==
false
)
return
brr
;
//}
if
(
apiIdAsParams
.
length
==
1
&&
apiIdAsParams
[
0
].
equals
(
"traffic-lanes"
))
{
brr
=
getTrafficLanes
();
}
else
if
(
apiIdAsParams
.
length
==
1
&&
apiIdAsParams
[
0
].
equals
(
"spots"
))
{
brr
=
getParkingSpots
();
}
else
{
String
err
=
"cisco.executeRequest() failed. Unknown api: "
+
getApiIdString
(
apiIdAsParams
);
brr
=
errorResponse
(
err
);
}
// if the request failed due to invalid token - fetch a new one & try again
if
(
isAccessDenied
(
brr
))
{
logger
.
info
(
"cisco.executeRequest() - access denied. fetch a new token & try again"
);
brr
=
reLogin
(
loginInfo
);
if
(
brr
.
success
)
{
brr
=
executeRequest
(
serviceId
,
apiIdAsParams
,
requestContext
);
}
else
{
brr
=
errorResponse
(
"cisco.executeRequest() failed to obtain access token"
);
}
}
long
tEnd
=
System
.
currentTimeMillis
();
double
elapsedTime
=
((
double
)(
tEnd
-
tStart
))
/
1000.0
;
logger
.
info
(
"cisco.executeRequest finished. time= "
+
elapsedTime
+
" success="
+
brr
.
success
+
" api="
+
getApiIdString
(
apiIdAsParams
));
return
brr
;
}
/*** A P I ***/
private
BaseRestResponse
login
(
LoginInfo
loginInfo
)
{
SimpleHttpRequest
req
=
null
;
SimpleHttpResponse
httpResp
=
null
;
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
StringBuilder
sb
=
new
StringBuilder
(
"cisco.login() - login for the first time. "
);
sb
.
append
(
"clientId="
).
append
(
loginInfo
.
clientId
)
.
append
(
"clientSecret="
).
append
(
loginInfo
.
clientSecret
)
.
append
(
"username="
).
append
(
loginInfo
.
username
)
.
append
(
"password="
).
append
(
loginInfo
.
password
);
logger
.
info
(
sb
.
toString
());
try
{
// step 1 - login ...
req
=
buildLoginRequest
();
httpResp
=
httpClient
.
processRequest
(
req
);
if
(
httpResp
.
getStatusCode
()
==
200
)
brr
=
parseLoginResp
(
httpResp
);
else
{
logHttpError
(
"cisco.login/login"
,
httpResp
);
return
errorHttpResponse
(
httpResp
);
}
// step 2 - user info
req
=
buildUserInfoRequest
();
httpResp
=
httpClient
.
processRequest
(
req
);
if
(
httpResp
.
getStatusCode
()
==
200
)
brr
=
parseUserInfoResp
(
httpResp
);
else
{
logHttpError
(
"cisco.login/userInfo"
,
httpResp
);
return
errorHttpResponse
(
httpResp
);
}
}
catch
(
Exception
e
)
{
String
err
=
"cisco.login failed with exception. e="
+
e
.
toString
();
logger
.
error
(
err
);
return
errorResponse
(
err
);
}
if
(
brr
.
success
)
loginInfo
.
loginSuccessfull
=
true
;
return
brr
;
}
// e.g: http://10.10.20.6/apigw/devnetlabapi/cdp/v1/devices/parking?UserKey=500111&SensorCustomerKey=500050&AppKey=CDP-App
private
BaseRestResponse
getParkingSpots
()
{
SimpleHttpRequest
req
=
null
;
SimpleHttpResponse
httpResp
=
null
;
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
logger
.
info
(
"cisco.getParkingSpots() activated"
);
try
{
// get parking
req
=
buildParkingSpotsRequest
();
httpResp
=
httpClient
.
processRequest
(
req
);
if
(
httpResp
.
getStatusCode
()
==
200
)
brr
=
parseParkingSpotsResp
(
httpResp
);
else
{
logHttpError
(
"getParkingSpots"
,
httpResp
);
return
errorHttpResponse
(
httpResp
);
}
}
catch
(
Exception
e
)
{
String
err
=
"ge.getCameraList() failed with exception. e="
+
e
.
toString
();
logger
.
error
(
err
);
return
errorResponse
(
err
);
}
return
brr
;
}
private
BaseRestResponse
reLogin
(
LoginInfo
loginInfo
)
{
return
null
;
}
private
BaseRestResponse
getTrafficLanes
()
{
return
null
;
}
/*** H E L P E R S ***/
private
SimpleHttpRequest
buildLoginRequest
()
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
request
.
setProtocol
(
"https"
);
request
.
setDomain
(
CISCO_DOMAIN
);
request
.
setProtocol
(
"http"
);
request
.
setPort
(
80
);
request
.
setMethod
(
SimpleHttpRequest
.
Method
.
POST
);
// path
String
path
=
API_TOKEN_URI
+
"login"
;
request
.
setPath
(
path
);
// content
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"username="
).
append
(
pes
.
escape
(
loginInfo
.
username
)).
append
(
"&"
)
.
append
(
"password="
).
append
(
pes
.
escape
(
loginInfo
.
password
)).
append
(
"&"
)
.
append
(
"client_id="
).
append
(
pes
.
escape
(
loginInfo
.
clientId
)).
append
(
"&"
)
.
append
(
"client_secret="
).
append
(
pes
.
escape
(
loginInfo
.
clientSecret
)).
append
(
"&"
)
.
append
(
"grant_type=client_credentials"
);
String
content
=
sb
.
toString
();
request
.
setContent
(
content
);
// headers
request
.
addHeader
(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
request
.
addHeader
(
"Cache-Control"
,
"no-cache"
);
return
request
;
}
// {
// "scope": "default",
// "token_type": "bearer",
// "app_access_token": "b746d143ba4f9fae868b1e7efa533161",
// "app_refresh_token": "57de744cb5f932dc480887f426b63a0",
// "app_expires_in": 3600,
// "api_access_token": "LuGy7w5FvQTRULXEEtCgWFnXZjPl",
// "api_expires_in": 3599
// }
private
BaseRestResponse
parseLoginResp
(
SimpleHttpResponse
httpResp
)
{
try
{
JsonNode
contentObj
=
objMapper
.
readTree
(
httpResp
.
getContent
());
loginInfo
.
appAccessToken
=
contentObj
.
get
(
"app_access_token"
).
asText
();
loginInfo
.
apiAccessToken
=
contentObj
.
get
(
"api_access_token"
).
asText
();
loginInfo
.
appRefreshToken
=
contentObj
.
get
(
"app_refresh_token"
).
asText
();
return
successResponse
(
null
);
}
catch
(
Exception
e
)
{
String
err
=
"parseLoginResp failed with exception. e: "
+
e
.
toString
();
return
errorResponse
(
err
);
}
}
private
SimpleHttpRequest
buildUserInfoRequest
()
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
request
.
setProtocol
(
"https"
);
request
.
setDomain
(
CISCO_DOMAIN
);
request
.
setProtocol
(
"http"
);
request
.
setPort
(
80
);
request
.
setMethod
(
SimpleHttpRequest
.
Method
.
GET
);
// path
String
path
=
API_URI
+
"accounts/username"
;
request
.
setPath
(
path
);
// query string
String
queryString
=
"loginName="
+
loginInfo
.
username
;
request
.
setQueryString
(
queryString
);
// headers
// WSO2-Authorization
String
WSO2_Authorization
=
"oAuth Bearer "
+
loginInfo
.
appAccessToken
;
request
.
addHeader
(
"WSO2-Authorization"
,
WSO2_Authorization
);
// Authorization
String
Authorization
=
"Bearer "
+
loginInfo
.
apiAccessToken
;
request
.
addHeader
(
"Authorization"
,
Authorization
);
// accept
String
Accept
=
"application/json"
;
request
.
addHeader
(
"Accept"
,
Accept
);
return
request
;
}
private
BaseRestResponse
parseUserInfoResp
(
SimpleHttpResponse
httpResp
)
{
try
{
JsonNode
contentObj
=
objMapper
.
readTree
(
httpResp
.
getContent
());
loginInfo
.
userId
=
contentObj
.
get
(
"id"
).
asText
();
loginInfo
.
parentUserId
=
contentObj
.
at
(
"/parentInfo/id"
).
asText
();
return
successResponse
(
null
);
}
catch
(
Exception
e
)
{
String
err
=
"parseUserInfoResp failed with exception. e: "
+
e
.
toString
();
return
errorResponse
(
err
);
}
}
private
SimpleHttpRequest
buildParkingSpotsRequest
()
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
request
.
setProtocol
(
"https"
);
request
.
setDomain
(
CISCO_DOMAIN
);
request
.
setProtocol
(
"http"
);
request
.
setPort
(
80
);
request
.
setMethod
(
SimpleHttpRequest
.
Method
.
POST
);
// path
String
path
=
API_URI
+
"devices/parking"
;
request
.
setPath
(
path
);
// query string
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"UserKey="
).
append
(
loginInfo
.
userId
).
append
(
"&"
)
.
append
(
"SensorCustomerKey="
).
append
(
loginInfo
.
parentUserId
).
append
(
"&"
)
.
append
(
"AppKey=CDP-App"
);
String
queryString
=
sb
.
toString
();
request
.
setQueryString
(
queryString
);
// content
String
content
=
"{\"Query\":{\"Find\": {\"ParkingSpot\": {\"sid\": {\"ne\": \"\" }}}}}"
;
request
.
setContent
(
content
);
// headers
// WSO2-Authorization
String
WSO2_Authorization
=
"oAuth Bearer "
+
loginInfo
.
appAccessToken
;
request
.
addHeader
(
"WSO2-Authorization"
,
WSO2_Authorization
);
// Authorization
String
Authorization
=
"Bearer "
+
loginInfo
.
apiAccessToken
;
request
.
addHeader
(
"Authorization"
,
Authorization
);
// accept
String
Accept
=
"application/json"
;
request
.
addHeader
(
"Accept"
,
Accept
);
// Content-Type
String
Content_Type
=
"application/json"
;
request
.
addHeader
(
"Content-Type"
,
Content_Type
);
return
request
;
}
private
BaseRestResponse
parseParkingSpotsResp
(
SimpleHttpResponse
httpResp
)
{
try
{
JsonNode
contentObj
=
objMapper
.
readTree
(
httpResp
.
getContent
());
ArrayNode
csParkingSpots
=
(
ArrayNode
)
contentObj
.
at
(
"/Find/Result"
);
ArrayNode
parkingSpotsResult
=
objMapper
.
createArrayNode
();
for
(
JsonNode
csParkingSpot:
csParkingSpots
)
{
ObjectNode
ps
=
objMapper
.
createObjectNode
();
// location uid
ps
.
put
(
"location-uid"
,
csParkingSpot
.
at
(
"/ParkingSpot/parkingSpaceId"
).
asText
());
// coordinates
JsonNode
csCoordinates
=
csParkingSpot
.
at
(
"/ParkingSpot/geocoordinates"
);
String
p1Loc
=
csCoordinates
.
at
(
"/latitude"
).
asText
()
+
","
+
csCoordinates
.
at
(
"/longitude"
).
asText
();
ObjectNode
p1
=
objMapper
.
createObjectNode
();
p1
.
put
(
"P1"
,
p1Loc
);
ps
.
put
(
"coordinates"
,
p1
);
// properties
ObjectNode
properties
=
objMapper
.
createObjectNode
();
ObjectNode
availability
=
objMapper
.
createObjectNode
();
availability
.
put
(
"timestamp"
,
0
);
availability
.
put
(
"mde-key"
,
csParkingSpot
.
at
(
"/ParkingSpot/sid"
).
asText
());
String
occupied
=
csParkingSpot
.
at
(
"/ParkingSpot/state/occupied"
).
asText
();
availability
.
put
(
"value"
,
occupied
);
properties
.
put
(
"availability"
,
availability
);
ps
.
put
(
"properties"
,
properties
);
parkingSpotsResult
.
add
(
ps
);
}
return
successResponse
(
parkingSpotsResult
);
}
catch
(
Exception
e
)
{
String
err
=
"parseUserInfoResp failed with exception. e: "
+
e
.
toString
();
return
errorResponse
(
err
);
}
}
private
Boolean
parseLoginRequestParameters
(
RequestContext
requestContext
)
{
return
true
;
}
private
BaseRestResponse
testGetImage
(
RequestContext
requestContext
,
String
[]
apiIds
)
{
try
{
// init http client
System
.
setProperty
(
"org.apache.commons.logging.Log"
,
"org.apache.commons.logging.impl.Jdk14Logger"
);
SchemeRegistry
registry
=
new
SchemeRegistry
();
registry
.
register
(
new
Scheme
(
"http"
,
80
,
PlainSocketFactory
.
getSocketFactory
()));
registry
.
register
(
new
Scheme
(
"https"
,
443
,
SSLSocketFactory
.
getSocketFactory
()));
ThreadSafeClientConnManager
connManager
=
new
ThreadSafeClientConnManager
(
registry
);
connManager
.
setDefaultMaxPerRoute
(
100
);
connManager
.
setMaxTotal
(
100
);
DefaultHttpClient
defaulthttpClient
=
new
DefaultHttpClient
(
connManager
);
CookieStore
cookieStore
=
defaulthttpClient
.
getCookieStore
();
String
uri
=
"https://ie-cities-media.run.asv-pr-pub.ice.predix.io/v2/file/CAMERA-STG-HYP1052-CAM-L_1487807853000_0_IMAGE.JPG"
;
HttpGet
getRequest
=
new
HttpGet
(
uri
);
getRequest
.
addHeader
(
"Authorization"
,
"Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImxlZ2FjeS10b2tlbi1rZXkiLCJ0eXAiOiJKV1QifQ.eyJqdGkiOiI4MDdlMzQxMTk5MzY0MDVlOTkyNTk1YTA5ZjhhMTExYyIsInN1YiI6ImlwZ2FsbGVyeSIsInNjb3BlIjpbImllLWN1cnJlbnQuaWNzLUlFLVBVQkxJQy1TQUZFVFkuSUUtUFVCTElDLVNBRkVUWS5MSU1JVEVELkRFVkVMT1AiLCJ1YWEucmVzb3VyY2UiLCJpZS1jdXJyZW50Lmljcy1JRS1FTlZJUk9OTUVOVEFMLklFLUVOVklST05NRU5UQUwuRlJFRS5ERVZFTE9QIiwiaWUtY3VycmVudC5pY3MtSUUtUEFSS0lORy5JRS1QQVJLSU5HLkxJTUlURUQuREVWRUxPUCIsImllLWN1cnJlbnQuaWNzLUlFLVRSQUZGSUMuSUUtVFJBRkZJQy5MSU1JVEVELkRFVkVMT1AiLCJpZS1jdXJyZW50Lmljcy1JRS1QRURFU1RSSUFOLklFLVBFREVTVFJJQU4uTElNSVRFRC5ERVZFTE9QIl0sImNsaWVudF9pZCI6ImlwZ2FsbGVyeSIsImNpZCI6ImlwZ2FsbGVyeSIsImF6cCI6ImlwZ2FsbGVyeSIsImdyYW50X3R5cGUiOiJjbGllbnRfY3JlZGVudGlhbHMiLCJyZXZfc2lnIjoiZDBmMjJiMTciLCJpYXQiOjE0ODg3MDE1NjksImV4cCI6MTQ4OTMwNjM2OSwiaXNzIjoiaHR0cHM6Ly84NTUzNDgyYy0xZDMyLTRkMzgtODU5Ny0yZTU2YWI2NDJkZDMucHJlZGl4LXVhYS5ydW4uYXN2LXByLmljZS5wcmVkaXguaW8vb2F1dGgvdG9rZW4iLCJ6aWQiOiI4NTUzNDgyYy0xZDMyLTRkMzgtODU5Ny0yZTU2YWI2NDJkZDMiLCJhdWQiOlsidWFhIiwiaWUtY3VycmVudC5pY3MtSUUtUEFSS0lORy5JRS1QQVJLSU5HLkxJTUlURUQiLCJpZS1jdXJyZW50Lmljcy1JRS1UUkFGRklDLklFLVRSQUZGSUMuTElNSVRFRCIsImllLWN1cnJlbnQuaWNzLUlFLVBFREVTVFJJQU4uSUUtUEVERVNUUklBTi5MSU1JVEVEIiwiaXBnYWxsZXJ5IiwiaWUtY3VycmVudC5pY3MtSUUtRU5WSVJPTk1FTlRBTC5JRS1FTlZJUk9OTUVOVEFMLkZSRUUiLCJpZS1jdXJyZW50Lmljcy1JRS1QVUJMSUMtU0FGRVRZLklFLVBVQkxJQy1TQUZFVFkuTElNSVRFRCJdfQ.tbz0Xp1ie4m5yp--bgfNW8F0SXUf1EsIJHKM6jemBI9kNKzSBIj5ef1BmyBrXPQg6Eia_553_RlobTV8UJx2tDJaiRmHnin3NxCEOTdWYicjrBFLDmP31EMhr8gTGw3xx-a540JQDp1voMysQCqGx1eUj7oIfMbD__vCYko7MRLlPtOssVkvsqUu3DVpo3lof6M2vhbwlcnXCaG6O0ScFP-lpkapzEewnvH8wWjLelntbzlE2c5IS6rhXqNlFBykwyVa6drFUxVhZf8bYwrIrfHuKrfbX0_r9AnhOoh_TGj4S8AniF8gXxS8GWg0LkXWaiqDaxmfCKvIHwiJS8LQwA"
);
getRequest
.
addHeader
(
"Predix-Zone-Id"
,
"ics-IE-PUBLIC-SAFETY"
);
HttpResponse
resp
=
defaulthttpClient
.
execute
(
getRequest
);
InputStream
is
=
resp
.
getEntity
().
getContent
();
byte
[]
baContent
=
IOUtils
.
toByteArray
(
is
);
// write to file
String
fileName
=
"/opt/mcx/config/test1.jpeg"
;
Path
file
=
Paths
.
get
(
fileName
);
Files
.
write
(
file
,
baContent
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
private
String
getApiIdString
(
String
[]
apiIdAsParams
)
{
String
apiString
=
""
;
for
(
String
p:
apiIdAsParams
)
apiString
+=
p
+
"/"
;
return
apiString
;
}
private
BaseRestResponse
errorHttpResponse
(
SimpleHttpResponse
httpResp
)
{
BaseRestResponse
errResp
=
new
BaseRestResponse
(
false
,
null
);
ObjectNode
objectNode
=
objMapper
.
createObjectNode
();
objectNode
.
put
(
"statusCode"
,
httpResp
.
getStatusCode
());
objectNode
.
put
(
"content"
,
httpResp
.
getContent
());
errResp
.
objectNode
=
objectNode
;
return
errResp
;
}
private
BaseRestResponse
errorResponse
(
String
error
)
{
BaseRestResponse
errResp
=
new
BaseRestResponse
(
false
,
error
);
return
errResp
;
}
private
BaseRestResponse
successResponse
(
JsonNode
objectNode
)
{
BaseRestResponse
successResp
=
new
BaseRestResponse
(
true
,
null
);
successResp
.
objectNode
=
objectNode
;
return
successResp
;
}
private
Boolean
isAccessDenied
(
BaseRestResponse
brr
)
{
if
(
brr
.
success
==
false
&&
brr
.
objectNode
!=
null
)
{
int
statusCode
=
brr
.
objectNode
.
get
(
"statusCode"
).
asInt
();
Boolean
bAccessDenied
=
(
statusCode
==
403
||
statusCode
==
401
);
return
bAccessDenied
;
}
else
return
false
;
}
private
void
logHttpError
(
String
funcName
,
SimpleHttpResponse
httpResp
)
{
logger
.
error
(
funcName
+
" failed:"
+
" statusCode="
+
httpResp
.
getStatusCode
()
+
" content="
+
httpResp
.
getContent
());
}
}
src/main/java/logic/GEManager.java
View file @
6a7e8284
...
@@ -77,7 +77,7 @@ public class GEManager {
...
@@ -77,7 +77,7 @@ public class GEManager {
BaseRestResponse
brr
=
new
BaseRestResponse
(
false
,
null
);
BaseRestResponse
brr
=
new
BaseRestResponse
(
false
,
null
);
long
tStart
=
System
.
currentTimeMillis
();
long
tStart
=
System
.
currentTimeMillis
();
logger
.
info
(
"executeRequest() started: "
+
getApiIdString
(
apiIdAsParams
));
logger
.
info
(
"
ge.
executeRequest() started: "
+
getApiIdString
(
apiIdAsParams
));
if
(
apiIdAsParams
.
length
==
1
&&
apiIdAsParams
[
0
].
equals
(
"cameras"
))
{
if
(
apiIdAsParams
.
length
==
1
&&
apiIdAsParams
[
0
].
equals
(
"cameras"
))
{
brr
=
getCameraList
(
requestContext
);
brr
=
getCameraList
(
requestContext
);
...
@@ -98,24 +98,24 @@ public class GEManager {
...
@@ -98,24 +98,24 @@ public class GEManager {
brr
=
getSensorLatestEvent
(
apiIdAsParams
[
1
]
/*sensorId*/
,
requestContext
);
brr
=
getSensorLatestEvent
(
apiIdAsParams
[
1
]
/*sensorId*/
,
requestContext
);
}
}
else
{
else
{
String
err
=
"executeRequest() failed. Unknown api: "
+
getApiIdString
(
apiIdAsParams
);
String
err
=
"
ge.
executeRequest() failed. Unknown api: "
+
getApiIdString
(
apiIdAsParams
);
brr
=
errorResponse
(
err
);
brr
=
errorResponse
(
err
);
}
}
// if the request failed due to invalid token - fetch a new one & try again
// if the request failed due to invalid token - fetch a new one & try again
if
(
isAccessDenied
(
brr
))
{
if
(
isAccessDenied
(
brr
))
{
logger
.
info
(
"executeRequest() - access denied. fetch a new token & try again"
);
logger
.
info
(
"
ge.
executeRequest() - access denied. fetch a new token & try again"
);
if
(
getNewAccessToken
()
==
true
)
{
if
(
getNewAccessToken
()
==
true
)
{
brr
=
executeRequest
(
serviceId
,
apiIdAsParams
,
requestContext
);
brr
=
executeRequest
(
serviceId
,
apiIdAsParams
,
requestContext
);
}
}
else
{
else
{
brr
=
errorResponse
(
"executeRequest() failed to obtain access token"
);
brr
=
errorResponse
(
"
ge.
executeRequest() failed to obtain access token"
);
}
}
}
}
long
tEnd
=
System
.
currentTimeMillis
();
long
tEnd
=
System
.
currentTimeMillis
();
double
elapsedTime
=
((
double
)(
tEnd
-
tStart
))
/
1000.0
;
double
elapsedTime
=
((
double
)(
tEnd
-
tStart
))
/
1000.0
;
logger
.
info
(
"executeRequest finished. time= "
+
elapsedTime
+
" success="
+
brr
.
success
+
" api="
+
getApiIdString
(
apiIdAsParams
));
logger
.
info
(
"
ge.
executeRequest finished. time= "
+
elapsedTime
+
" success="
+
brr
.
success
+
" api="
+
getApiIdString
(
apiIdAsParams
));
return
brr
;
return
brr
;
}
}
...
@@ -129,7 +129,7 @@ public class GEManager {
...
@@ -129,7 +129,7 @@ public class GEManager {
BaseRestResponse
brr
=
null
;
BaseRestResponse
brr
=
null
;
JsonNode
contentObj
=
null
;
JsonNode
contentObj
=
null
;
logger
.
info
(
"getLatestMediaFile() activated. camId="
+
camId
);
logger
.
info
(
"ge
.ge
tLatestMediaFile() activated. camId="
+
camId
);
// execute the request
// execute the request
// example: ie-cities-media.run.asv-pr-pub.ice.predix.io/assets/{camId}/media/latest?mediaType=IMAGE
// example: ie-cities-media.run.asv-pr-pub.ice.predix.io/assets/{camId}/media/latest?mediaType=IMAGE
...
@@ -139,7 +139,7 @@ public class GEManager {
...
@@ -139,7 +139,7 @@ public class GEManager {
SimpleHttpRequest
httpRequest
=
buildLatestMediaFileUrlRequest
(
camId
,
mediaType
);
SimpleHttpRequest
httpRequest
=
buildLatestMediaFileUrlRequest
(
camId
,
mediaType
);
SimpleHttpResponse
httpFileUrlResp
=
httpClient
.
processRequest
(
httpRequest
);
SimpleHttpResponse
httpFileUrlResp
=
httpClient
.
processRequest
(
httpRequest
);
if
(
httpFileUrlResp
.
getStatusCode
()
!=
200
)
{
if
(
httpFileUrlResp
.
getStatusCode
()
!=
200
)
{
logHttpError
(
"getLatestMediaFile/getLatestMediaURL"
,
httpFileUrlResp
);
logHttpError
(
"ge
.ge
tLatestMediaFile/getLatestMediaURL"
,
httpFileUrlResp
);
return
errorHttpResponse
(
httpFileUrlResp
);
return
errorHttpResponse
(
httpFileUrlResp
);
}
}
contentObj
=
objMapper
.
readTree
(
httpFileUrlResp
.
getContent
());
contentObj
=
objMapper
.
readTree
(
httpFileUrlResp
.
getContent
());
...
@@ -150,7 +150,7 @@ public class GEManager {
...
@@ -150,7 +150,7 @@ public class GEManager {
httpRequest
=
buildDownloadLatestMediaFileRequest
(
url
);
httpRequest
=
buildDownloadLatestMediaFileRequest
(
url
);
SimpleHttpResponse
httpFileDataResp
=
httpClient
.
processRequest
(
httpRequest
);
SimpleHttpResponse
httpFileDataResp
=
httpClient
.
processRequest
(
httpRequest
);
if
(
httpFileDataResp
.
getStatusCode
()
!=
200
)
{
if
(
httpFileDataResp
.
getStatusCode
()
!=
200
)
{
logHttpError
(
"getLatestMediaFile/downloadLatestMediaFile"
,
httpFileDataResp
);
logHttpError
(
"ge
.ge
tLatestMediaFile/downloadLatestMediaFile"
,
httpFileDataResp
);
return
errorHttpResponse
(
httpFileDataResp
);
return
errorHttpResponse
(
httpFileDataResp
);
}
}
String
fileData
=
httpFileDataResp
.
getContent
();
String
fileData
=
httpFileDataResp
.
getContent
();
...
@@ -169,7 +169,7 @@ public class GEManager {
...
@@ -169,7 +169,7 @@ public class GEManager {
brr
=
buildLatestMediaFileResp
(
fileData
,
url
);
brr
=
buildLatestMediaFileResp
(
fileData
,
url
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
String
err
=
"getLatestMediaFile() failed with exception. camId="
+
camId
+
" e="
+
e
.
toString
();
String
err
=
"ge
.ge
tLatestMediaFile() failed with exception. camId="
+
camId
+
" e="
+
e
.
toString
();
logger
.
error
(
err
);
logger
.
error
(
err
);
return
errorResponse
(
err
);
return
errorResponse
(
err
);
}
}
...
@@ -182,7 +182,7 @@ public class GEManager {
...
@@ -182,7 +182,7 @@ public class GEManager {
SimpleHttpResponse
httpResp
=
null
;
SimpleHttpResponse
httpResp
=
null
;
BaseRestResponse
brr
=
new
BaseRestResponse
(
false
,
null
);
BaseRestResponse
brr
=
new
BaseRestResponse
(
false
,
null
);
logger
.
info
(
"getCamerasList() activated."
);
logger
.
info
(
"ge
.ge
tCamerasList() activated."
);
// execute the request ...
// execute the request ...
req
=
buildCameraListRequest
(
requestContext
);
req
=
buildCameraListRequest
(
requestContext
);
...
@@ -196,7 +196,7 @@ public class GEManager {
...
@@ -196,7 +196,7 @@ public class GEManager {
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
String
err
=
"getCameraList() failed with exception. e="
+
e
.
toString
();
String
err
=
"ge
.ge
tCameraList() failed with exception. e="
+
e
.
toString
();
logger
.
error
(
err
);
logger
.
error
(
err
);
return
errorResponse
(
err
);
return
errorResponse
(
err
);
}
}
...
@@ -208,7 +208,7 @@ public class GEManager {
...
@@ -208,7 +208,7 @@ public class GEManager {
SimpleHttpResponse
httpResp
=
null
;
SimpleHttpResponse
httpResp
=
null
;
BaseRestResponse
brr
=
new
BaseRestResponse
(
false
,
null
);
BaseRestResponse
brr
=
new
BaseRestResponse
(
false
,
null
);
logger
.
info
(
"getCameraDetails() activated. camId="
+
cameraId
);
logger
.
info
(
"ge
.ge
tCameraDetails() activated. camId="
+
cameraId
);
// execute the request ...
// execute the request ...
req
=
buildCameraDetailsRequest
(
cameraId
);
req
=
buildCameraDetailsRequest
(
cameraId
);
...
@@ -217,17 +217,17 @@ public class GEManager {
...
@@ -217,17 +217,17 @@ public class GEManager {
if
(
httpResp
.
getStatusCode
()
==
200
)
if
(
httpResp
.
getStatusCode
()
==
200
)
brr
=
buildCameraDetailsResp
(
httpResp
);
brr
=
buildCameraDetailsResp
(
httpResp
);
else
{
else
{
logHttpError
(
"getCameraDetails"
,
httpResp
);
logHttpError
(
"ge
.ge
tCameraDetails"
,
httpResp
);
return
errorHttpResponse
(
httpResp
);
return
errorHttpResponse
(
httpResp
);
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
String
err
=
"getCameraDetails() failed with exception. e="
+
e
.
toString
();
String
err
=
"ge
.ge
tCameraDetails() failed with exception. e="
+
e
.
toString
();
logger
.
error
(
err
);
logger
.
error
(
err
);
return
errorResponse
(
err
);
return
errorResponse
(
err
);
}
}
logger
.
info
(
"getCameraDetails() successfull. camId="
+
cameraId
);
logger
.
info
(
"ge
.ge
tCameraDetails() successfull. camId="
+
cameraId
);
return
brr
;
return
brr
;
}
}
private
BaseRestResponse
getSensorLatestEvent
(
String
sensorId
,
RequestContext
requestContext
)
{
private
BaseRestResponse
getSensorLatestEvent
(
String
sensorId
,
RequestContext
requestContext
)
{
...
@@ -235,7 +235,7 @@ public class GEManager {
...
@@ -235,7 +235,7 @@ public class GEManager {
SimpleHttpResponse
httpResp
=
null
;
SimpleHttpResponse
httpResp
=
null
;
BaseRestResponse
brr
=
new
BaseRestResponse
(
false
,
null
);
BaseRestResponse
brr
=
new
BaseRestResponse
(
false
,
null
);
logger
.
info
(
"getSensorLatestEvent() activated. camId="
+
sensorId
);
logger
.
info
(
"ge
.ge
tSensorLatestEvent() activated. camId="
+
sensorId
);
// execute the request ...
// execute the request ...
// envType
// envType
...
@@ -264,12 +264,12 @@ public class GEManager {
...
@@ -264,12 +264,12 @@ public class GEManager {
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
String
err
=
"getSensorLatestEvent() failed with exception. e="
+
e
.
toString
();
String
err
=
"ge
.ge
tSensorLatestEvent() failed with exception. e="
+
e
.
toString
();
logger
.
error
(
err
);
logger
.
error
(
err
);
return
errorResponse
(
err
);
return
errorResponse
(
err
);
}
}
logger
.
info
(
"getSensorLatestEvent() successfull. camId="
+
sensorId
);
logger
.
info
(
"ge
.ge
tSensorLatestEvent() successfull. camId="
+
sensorId
);
return
brr
;
return
brr
;
}
}
...
@@ -318,7 +318,7 @@ public class GEManager {
...
@@ -318,7 +318,7 @@ public class GEManager {
SimpleHttpRequest
req
=
null
;
SimpleHttpRequest
req
=
null
;
SimpleHttpResponse
httpResp
=
null
;
SimpleHttpResponse
httpResp
=
null
;
logger
.
info
(
"getNewAccessToken() activated."
);
logger
.
info
(
"ge
.ge
tNewAccessToken() activated."
);
req
=
buildGetTokenRequest
();
req
=
buildGetTokenRequest
();
try
{
try
{
...
@@ -331,11 +331,11 @@ public class GEManager {
...
@@ -331,11 +331,11 @@ public class GEManager {
return
true
;
return
true
;
}
}
else
{
else
{
logHttpError
(
"getNewAccessToken"
,
httpResp
);
logHttpError
(
"ge
.ge
tNewAccessToken"
,
httpResp
);
return
false
;
return
false
;
}
}
}
catch
(
Exception
e
){
}
catch
(
Exception
e
){
logger
.
error
(
"getNewAccessToken() failed with exception. e: "
+
e
.
toString
());
logger
.
error
(
"ge
.ge
tNewAccessToken() failed with exception. e: "
+
e
.
toString
());
return
false
;
return
false
;
}
}
...
@@ -346,7 +346,7 @@ public class GEManager {
...
@@ -346,7 +346,7 @@ public class GEManager {
SimpleHttpResponse
httpResp
=
null
;
SimpleHttpResponse
httpResp
=
null
;
BaseRestResponse
brr
=
new
BaseRestResponse
(
false
,
null
);
BaseRestResponse
brr
=
new
BaseRestResponse
(
false
,
null
);
logger
.
info
(
"getSensorList() activated."
);
logger
.
info
(
"ge
.ge
tSensorList() activated."
);
// execute the request ...
// execute the request ...
req
=
buildSensorListRequest
(
requestContext
);
req
=
buildSensorListRequest
(
requestContext
);
...
@@ -360,7 +360,7 @@ public class GEManager {
...
@@ -360,7 +360,7 @@ public class GEManager {
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
String
err
=
"getSensorList() failed with exception. e="
+
e
.
toString
();
String
err
=
"ge
.ge
tSensorList() failed with exception. e="
+
e
.
toString
();
logger
.
error
(
err
);
logger
.
error
(
err
);
return
errorResponse
(
err
);
return
errorResponse
(
err
);
}
}
...
@@ -398,7 +398,7 @@ public class GEManager {
...
@@ -398,7 +398,7 @@ public class GEManager {
// String[3]: fileName
// String[3]: fileName
String
[]
urlParts
=
getMediaFileName
(
url
,
"latest"
);
String
[]
urlParts
=
getMediaFileName
(
url
,
"latest"
);
if
(
urlParts
==
null
)
{
if
(
urlParts
==
null
)
{
String
err
=
"buildLatestMediaFileResp() - failed to parse file url: "
+
url
;
String
err
=
"
ge.
buildLatestMediaFileResp() - failed to parse file url: "
+
url
;
logger
.
error
(
err
);
logger
.
error
(
err
);
return
new
BaseRestResponse
(
false
,
err
);
return
new
BaseRestResponse
(
false
,
err
);
}
}
...
@@ -410,7 +410,7 @@ public class GEManager {
...
@@ -410,7 +410,7 @@ public class GEManager {
brr
.
objectNode
=
dataObj
;
brr
.
objectNode
=
dataObj
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
String
err
=
"buildLatestMediaFileResp() failed with exception. e: "
+
e
.
toString
();
String
err
=
"
ge.
buildLatestMediaFileResp() failed with exception. e: "
+
e
.
toString
();
logger
.
error
(
err
);
logger
.
error
(
err
);
return
new
BaseRestResponse
(
false
,
err
);
return
new
BaseRestResponse
(
false
,
err
);
}
}
...
...
src/main/java/logic/MdeManager.java
View file @
6a7e8284
...
@@ -36,6 +36,7 @@ public class MdeManager {
...
@@ -36,6 +36,7 @@ public class MdeManager {
private
ServicesRepository
servicesRepository
;
private
ServicesRepository
servicesRepository
;
private
GEManager
geManager
=
null
;
private
GEManager
geManager
=
null
;
private
CiscoManager
ciscoManager
=
null
;
public
MdeManager
(
ILogger
logger
)
throws
Exception
{
public
MdeManager
(
ILogger
logger
)
throws
Exception
{
...
@@ -45,6 +46,7 @@ public class MdeManager {
...
@@ -45,6 +46,7 @@ public class MdeManager {
this
.
logger
=
logger
;
this
.
logger
=
logger
;
geManager
=
new
GEManager
(
logger
);
geManager
=
new
GEManager
(
logger
);
ciscoManager
=
new
CiscoManager
(
logger
);
LoadServices
(
logger
);
LoadServices
(
logger
);
loadAdapters
(
logger
);
loadAdapters
(
logger
);
ValidityCheck
();
ValidityCheck
();
...
@@ -76,13 +78,15 @@ public class MdeManager {
...
@@ -76,13 +78,15 @@ public class MdeManager {
serviceId
=
getServiceId
(
requestContext
,
offset
);
serviceId
=
getServiceId
(
requestContext
,
offset
);
String
[]
apiIdAsParams
=
getApiIdAsParams
(
requestContext
,
offset
);
String
[]
apiIdAsParams
=
getApiIdAsParams
(
requestContext
,
offset
);
String
ver
=
serviceId
.
split
(
"\\."
)[
0
];
String
[]
idTokens
=
serviceId
.
split
(
"\\."
);
if
(
ver
.
equals
(
"v2"
))
String
verAndTenant
=
idTokens
[
0
]
+
"."
+
idTokens
[
1
];
if
(
verAndTenant
.
equals
(
"v2.ge"
))
brr
=
geManager
.
executeRequest
(
serviceId
,
apiIdAsParams
,
requestContext
);
brr
=
geManager
.
executeRequest
(
serviceId
,
apiIdAsParams
,
requestContext
);
else
if
(
verAndTenant
.
equals
(
"v1.cisco"
))
brr
=
ciscoManager
.
executeRequest
(
serviceId
,
apiIdAsParams
,
requestContext
);
else
else
brr
=
executeRequest
(
serviceId
,
apiIdAsParams
,
requestContext
,
null
,
isMaintenanceRequest
);
brr
=
executeRequest
(
serviceId
,
apiIdAsParams
,
requestContext
,
null
,
isMaintenanceRequest
);
return
brr
;
return
brr
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment