Skip to content

Commit bccd56e

Browse files
committed
seems that there has been a change somewhere in the service and the
oauth it't behaving as expected, adding some test stuff
1 parent 90832eb commit bccd56e

6 files changed

Lines changed: 60 additions & 17 deletions

File tree

IoTDBJDBC/src/main/java/com/oracle/demo/timg/iot/iotdbjdbc/messagehandler/outputs/http/normalizeddata/timeseriesdb/TimeSeriesDBOutputOTLP.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Software and the Larger Work(s), and to sublicense the foregoing rights on
3737
package com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.outputs.http.normalizeddata.timeseriesdb;
3838

3939
import java.sql.SQLException;
40+
import java.time.LocalDateTime;
4041

4142
import com.oracle.demo.timg.iot.iotdbjdbc.aqdata.NormalizedData;
4243
import com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.NormalizedDataMessageHandler;
@@ -45,11 +46,14 @@ Software and the Larger Work(s), and to sublicense the foregoing rights on
4546
import com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.iotdbutils.MissingModelException;
4647
import com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.outputs.http.normalizeddata.timeseriesdb.endpoints.TimeSeriesEndpointsQueryParams;
4748
import com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.outputs.http.normalizeddata.timeseriesdb.endpoints.TimeSeriesEndpointsRetriever;
49+
import com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.outputs.http.normalizeddata.timeseriesdb.oauth.OAuthTokenRetrievalException;
50+
import com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.outputs.http.normalizeddata.timeseriesdb.oauth.TimeSeriesDBOAuthTokenRetriever;
4851
import com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.outputs.http.normalizeddata.timeseriesdb.otlp.MetricsData;
4952
import com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.outputs.http.normalizeddata.timeseriesdb.otlp.NormalizedDataMetricsDataBuilder;
5053
import com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.outputs.http.normalizeddata.timeseriesdb.otlp.OtlpMetricsClient;
5154
import com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.outputs.http.normalizeddata.timeseriesdb.otlp.OtlpProperties;
5255

56+
import io.micronaut.context.BeanProvider;
5357
import io.micronaut.context.annotation.Property;
5458
import io.micronaut.context.annotation.Requires;
5559
import io.micronaut.context.event.StartupEvent;
@@ -81,11 +85,18 @@ public class TimeSeriesDBOutputOTLP implements NormalizedDataMessageHandler {
8185
@Property(name = TimeSeriesDBProperties.TIME_SERIES_PROPERTY_METRICS_PATH, defaultValue = "/tel/v1/metrics")
8286
private String metricsPath;
8387

88+
// for debugging if we're not uploading we can still trigger a oauth check, that
89+
// way any oauth debugging will happen
90+
@Inject
91+
private BeanProvider<TimeSeriesDBOAuthTokenRetriever> tokenRetriever;
92+
private final boolean noUpload;
93+
8494
@Inject
8595
public TimeSeriesDBOutputOTLP(DeviceModelInstancesCache deviceModelInstancesCache, OtlpMetricsClient metricsClient,
8696
TimeSeriesEndpointsRetriever timeSeriesEndpointsRetriever, ObjectMapper mapper,
8797
@Property(name = TimeSeriesDBProperties.TIME_SERIES_PROPERTY_ORDER) int order,
88-
@Property(name = TimeSeriesDBProperties.TIME_SERIES_PROPERTY_SENT_DATA_IS_COMPLETED, defaultValue = "true") boolean sentDataIsCompleted) {
98+
@Property(name = TimeSeriesDBProperties.TIME_SERIES_PROPERTY_SENT_DATA_IS_COMPLETED, defaultValue = "true") boolean sentDataIsCompleted,
99+
@Property(name = TimeSeriesDBProperties.TIME_SERIES_PROPERTY_DEBUG_NO_UPLOAD, defaultValue = "false") boolean noUpload) {
89100
this.deviceModelInstancesCache = deviceModelInstancesCache;
90101
this.metricsClient = metricsClient;
91102
TimeSeriesEndpointsQueryParams queryParams = timeSeriesEndpointsRetriever.getQueryParams();
@@ -95,6 +106,7 @@ public TimeSeriesDBOutputOTLP(DeviceModelInstancesCache deviceModelInstancesCach
95106
this.mapper = mapper;
96107
this.order = order;
97108
this.sentDataIsCompleted = sentDataIsCompleted;
109+
this.noUpload = noUpload;
98110
}
99111

100112
@Override
@@ -129,10 +141,14 @@ public NormalizedData[] processNormalizedData(NormalizedData normalizedData) thr
129141
log.info(() -> "About to upload to time series db " + metricsDataString);
130142
log.info(() -> "Uploading url=" + metricsClientUrl + ", path=" + metricsPath + ", queryX=" + queryX
131143
+ ", queryY=" + queryY);
144+
if (noUpload) {
145+
log.info("noUpload is true, skipping upload");
132146

133-
HttpResponse<String> resp = metricsClient.uploadMetrics(queryX, queryY, metricsDataString);
134-
log.info("Upload to time series DB response is " + resp.getStatus().getCode() + "("
135-
+ resp.getStatus().toString() + ") with body " + resp.getBody().orElse("No response data"));
147+
} else {
148+
HttpResponse<String> resp = metricsClient.uploadMetrics(queryX, queryY, metricsDataString);
149+
log.info("Upload to time series DB response is " + resp.getStatus().getCode() + "("
150+
+ resp.getStatus().toString() + ") with body " + resp.getBody().orElse("No response data"));
151+
}
136152
return sentDataIsCompleted ? new NormalizedData[0] : new NormalizedData[] { normalizedData };
137153
}
138154

@@ -168,5 +184,18 @@ private void addResourceAttributes(NormalizedDataMetricsDataBuilder builder, Nor
168184
public void onStartup(StartupEvent event) {
169185
log.info("Startup event received for TimeSeriesDBOutputOTLP, queryX=" + queryX + ", queryY=" + queryY
170186
+ ", Uploading to " + metricsClientUrl + " with path " + metricsPath);
187+
if (noUpload) {
188+
// trigger the oauth provider to do it's stuff
189+
try {
190+
log.info("noUpload set, forcing an OAuth operation");
191+
String token = tokenRetriever.get().getToken();
192+
String tokenType = tokenRetriever.get().getTokenType();
193+
LocalDateTime tokenExpire = tokenRetriever.get().getCurrentTokenRenewTime();
194+
log.info("Token type=" + tokenType + ", token expiry=" + tokenExpire + ", token=" + token);
195+
} catch (OAuthTokenRetrievalException e) {
196+
// TODO Auto-generated catch block
197+
log.severe("noUpload set, Problem getting the OAuth token " + e.getLocalizedMessage());
198+
}
199+
}
171200
}
172201
}

IoTDBJDBC/src/main/java/com/oracle/demo/timg/iot/iotdbjdbc/messagehandler/outputs/http/normalizeddata/timeseriesdb/TimeSeriesDBProperties.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ public class TimeSeriesDBProperties {
6464
public static final String TIME_SERIES_PROPERTY_METRICS_QUERY_PARAMS_Y = TIME_SERIES_PROPERTY_METRICS_QUERY_PARAMS
6565
+ ".y";
6666
public static final String TIME_SERIES_PROPERTY_METRICS_PATH = TIME_SERIES_PROPERTY_METRICS + ".path";
67+
68+
public static final String TIME_SERIES_PROPERTY_DEBUG = TIME_SERIES_PROPERTY_PREFIX + ".debug";
69+
public static final String TIME_SERIES_PROPERTY_DEBUG_OAUTH = TIME_SERIES_PROPERTY_DEBUG + ".oauth";
70+
public static final String TIME_SERIES_PROPERTY_DEBUG_NO_UPLOAD = TIME_SERIES_PROPERTY_DEBUG + ".noupload";
6771
}

IoTDBJDBC/src/main/java/com/oracle/demo/timg/iot/iotdbjdbc/messagehandler/outputs/http/normalizeddata/timeseriesdb/oauth/TimeSeriesDBOAuthClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public interface TimeSeriesDBOAuthClient {
5858
@Consumes(MediaType.APPLICATION_JSON)
5959
@Produces(MediaType.APPLICATION_JSON)
6060
@Post("${" + TimeSeriesDBProperties.TIME_SERIES_PROPERTY_OAUTH_PATH + ":/tel/token}")
61-
public OAuthTokenResponse getOAuthToken(@QueryValue("x") String queryParamX, @QueryValue("y") String queryParamY,
61+
public String getOAuthToken(@QueryValue("x") String queryParamX, @QueryValue("y") String queryParamY,
6262
@Body String timeSeriesDBCredentials) throws HttpClientException;
6363

6464
}

IoTDBJDBC/src/main/java/com/oracle/demo/timg/iot/iotdbjdbc/messagehandler/outputs/http/normalizeddata/timeseriesdb/oauth/TimeSeriesDBOAuthTokenRequestFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Software and the Larger Work(s), and to sublicense the foregoing rights on
4848
import io.micronaut.http.annotation.RequestFilter;
4949
import io.micronaut.runtime.event.annotation.EventListener;
5050
import jakarta.inject.Inject;
51+
import jakarta.inject.Singleton;
5152
import lombok.extern.java.Log;
5253

5354
/*
@@ -56,6 +57,7 @@ Software and the Larger Work(s), and to sublicense the foregoing rights on
5657
// @ClientFilter(patterns = { "${" + TimeSeriesDBProperties.TIME_SERIES_PROPERTY_OAUTH_PATH + "}" })
5758
@Requires(property = TimeSeriesDBProperties.TIME_SERIES_PROPERTY_ENABLED, value = "true", defaultValue = "false")
5859
@ClientFilter(patterns = "${" + TimeSeriesDBProperties.TIME_SERIES_PROPERTY_OAUTH_PATH + ":/tel/token}")
60+
@Singleton
5961
@Log
6062
public class TimeSeriesDBOAuthTokenRequestFilter {
6163
public final static String HEADER_REQUEST_ID = "Request-Id";

IoTDBJDBC/src/main/java/com/oracle/demo/timg/iot/iotdbjdbc/messagehandler/outputs/http/normalizeddata/timeseriesdb/oauth/TimeSeriesDBOAuthTokenRetriever.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,35 @@ Software and the Larger Work(s), and to sublicense the foregoing rights on
5858

5959
@Singleton
6060
@Requires(property = TimeSeriesDBProperties.TIME_SERIES_PROPERTY_ENABLED, value = "true", defaultValue = "false")
61-
//@Requires(property = TimeSeriesDBProperties.TIME_SERIES_PROPERTY_OAUTH_QUERY_PARAMS_X)
62-
//@Requires(property = TimeSeriesDBProperties.TIME_SERIES_PROPERTY_OAUTH_QUERY_PARAMS_Y)
6361
@Log
6462
public class TimeSeriesDBOAuthTokenRetriever {
6563
@Inject
6664
private ObjectMapper mapper;
67-
@Inject
68-
private TimeSeriesDBCredentials tsDBuserCredentials;
65+
private final TimeSeriesDBCredentials tsDBuserCredentials;
6966
@Property(name = TimeSeriesDBProperties.TIME_SERIES_PROPERTY_OAUTH_RENEWAL_PREEMPT, defaultValue = "PT60S")
7067
private Duration renewalPreempt;
71-
// @Property(name =
72-
// TimeSeriesDBProperties.TIME_SERIES_PROPERTY_OAUTH_QUERY_PARAMS_X)
7368
private final String queryX;
74-
// @Property(name =
75-
// TimeSeriesDBProperties.TIME_SERIES_PROPERTY_OAUTH_QUERY_PARAMS_Y)
7669
private final String queryY;
7770
@Getter
7871
private LocalDateTime currentTokenRenewTime = null;
7972

8073
private String currentToken = null;
8174
@Getter
8275
private String tokenType;
76+
@Property(name = TimeSeriesDBProperties.TIME_SERIES_PROPERTY_DEBUG_OAUTH, defaultValue = "false")
77+
boolean debugOauth = false;
8378

8479
@Inject
85-
public TimeSeriesDBOAuthTokenRetriever(TimeSeriesEndpointsRetriever endpointsRetriever) {
80+
public TimeSeriesDBOAuthTokenRetriever(TimeSeriesEndpointsRetriever endpointsRetriever,
81+
TimeSeriesDBCredentials tsDBuserCredentials) {
8682
TimeSeriesEndpointsQueryParams endpointsQueryParams = endpointsRetriever.getQueryParams();
8783
this.queryX = endpointsQueryParams.getOauthQueryX();
8884
this.queryY = endpointsQueryParams.getOauthQueryY();
85+
log.info("Constructor params for OAUTH queryX=" + queryX + ", queryY=" + queryY);
86+
this.tsDBuserCredentials = tsDBuserCredentials;
87+
if (debugOauth) {
88+
log.info("Oauth retrieve credentials are " + tsDBuserCredentials);
89+
}
8990
}
9091

9192
@Inject
@@ -116,13 +117,17 @@ public synchronized String getToken() throws OAuthTokenRetrievalException {
116117
log.info("Retrieveing oauth token from time series DB");
117118
try {
118119
String credentials = mapper.writeValueAsString(tsDBuserCredentials);
119-
log.fine("Setting body to " + credentials);
120-
atr = authClient.getOAuthToken(queryX, queryY, credentials);
120+
log.fine(() -> "Setting body to " + credentials);
121+
String oauthRespStr = authClient.getOAuthToken(queryX, queryY, credentials);
122+
log.fine(() -> "OAuth response is " + oauthRespStr);
123+
atr = mapper.readValue(oauthRespStr, OAuthTokenResponse.class);
121124
} catch (HttpClientException e) {
125+
log.warning("Problem getting the OAuth token " + e.getLocalizedMessage());
122126
throw new OAuthTokenRetrievalException("Problem getting the OAuth token " + e.getLocalizedMessage(), e);
123127
} catch (IOException e) {
128+
log.warning("IOException in mapping, this should not happen " + e.getLocalizedMessage());
124129
throw new OAuthTokenRetrievalException(
125-
"IOException building mapping, this should not happen " + e.getLocalizedMessage(), e);
130+
"IOException in mapping, this should not happen " + e.getLocalizedMessage(), e);
126131
}
127132
this.currentToken = atr.getAccessToken();
128133
this.tokenType = atr.getTokenType();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
// register SLF4JBridgeHandler as handler for the j.u.l. root logger
22
handlers = org.slf4j.bridge.SLF4JBridgeHandler
3+
4+
com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.outputs.http.normalizeddata.timeseriesdb.oauth.TimeSeriesDBOAuthTokenRequestFilter.level = FINER
5+
com.oracle.demo.timg.iot.iotdbjdbc.messagehandler.outputs.http.normalizeddata.timeseriesdb.TimeSeriesDBOutputOTLP = FINER

0 commit comments

Comments
 (0)