Skip to content

Commit 3dad0a3

Browse files
rebase from master repo (#4)
* Implement Subscription Handler (#8) * [DURACI-6697]-Implement Subscription Handler * fix use of embeded mongo db * fix mongodbhandler for test * Implemented Review comments * Fixed PathVariable annotations for input parameters in RestApi. * Fixed PathVariable annotations for input parameters in RestApi. (#9) * Added a fix for Block Origin in webbrowsers while accessing EI RestApi. * Implement Historical ER Query Service (#6) *Implement historical Event Repository query Service * add posibility to query downstream and upstreamfrom ER and test for ER query method * modify unit test
1 parent 2360715 commit 3dad0a3

File tree

13 files changed

+1166
-37
lines changed

13 files changed

+1166
-37
lines changed

pom.xml

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<version>1.5.2.RELEASE</version>
1212
</parent>
1313

14-
<properties>
14+
<properties>
1515
<java.version>1.8</java.version>
1616
<plugin-version>0.8.6</plugin-version>
1717
<output-relative-path>src/main/java</output-relative-path>
@@ -57,6 +57,11 @@
5757
<artifactId>spring-boot-starter-amqp</artifactId>
5858
</dependency>
5959

60+
<dependency>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-starter-mail</artifactId>
63+
</dependency>
64+
6065
<dependency>
6166
<groupId>org.springframework.boot</groupId>
6267
<artifactId>spring-boot-starter-test</artifactId>
@@ -101,13 +106,8 @@
101106
<artifactId>commons-io</artifactId>
102107
<version>1.3.2</version>
103108
</dependency>
104-
<!--
105-
<dependency>
106-
<groupId>org.slf4j</groupId>
107-
<artifactId>slf4j-log4j12</artifactId>
108-
<version>1.7.25</version>
109-
</dependency>
110-
-->
109+
<!-- <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId>
110+
<version>1.7.25</version> </dependency> -->
111111

112112
<!-- https://mvnrepository.com/artifact/org.json/json -->
113113
<dependency>
@@ -146,19 +146,19 @@
146146
<version>2.6.1</version>
147147
<scope>compile</scope>
148148
</dependency>
149-
<dependency>
150-
<groupId>org.apache.qpid</groupId>
151-
<artifactId>qpid-broker</artifactId>
152-
<version>6.1.3</version>
153-
<scope>test</scope>
154-
</dependency>
149+
<dependency>
150+
<groupId>org.apache.qpid</groupId>
151+
<artifactId>qpid-broker</artifactId>
152+
<version>6.1.3</version>
153+
<scope>test</scope>
154+
</dependency>
155155

156-
<dependency>
157-
<groupId>de.flapdoodle.embed</groupId>
158-
<artifactId>de.flapdoodle.embed.mongo</artifactId>
159-
<version>2.0.0</version>
160-
<scope>test</scope>
161-
</dependency>
156+
<dependency>
157+
<groupId>de.flapdoodle.embed</groupId>
158+
<artifactId>de.flapdoodle.embed.mongo</artifactId>
159+
<version>2.0.0</version>
160+
<scope>test</scope>
161+
</dependency>
162162

163163
</dependencies>
164164
<build>
@@ -168,7 +168,7 @@
168168
<artifactId>spring-boot-maven-plugin</artifactId>
169169
</plugin>
170170

171-
<!-- PhoenixNAP RAML Code Generator plugin used to generate sources
171+
<!-- PhoenixNAP RAML Code Generator plugin used to generate sources
172172
from raml -->
173173
<plugin>
174174
<groupId>com.phoenixnap.oss</groupId>
@@ -232,18 +232,18 @@
232232
</plugin>
233233

234234
<plugin>
235-
<groupId>org.apache.maven.plugins</groupId>
236-
<artifactId>maven-surefire-plugin</artifactId>
237-
<version>2.20</version>
238-
<configuration>
239-
<excludes>
240-
<exclude>${someModule.test.excludes}</exclude>
241-
</excludes>
242-
<includes>
243-
<include>${someModule.test.includes}</include>
244-
</includes>
245-
</configuration>
246-
</plugin>
235+
<groupId>org.apache.maven.plugins</groupId>
236+
<artifactId>maven-surefire-plugin</artifactId>
237+
<version>2.20</version>
238+
<configuration>
239+
<excludes>
240+
<exclude>${someModule.test.excludes}</exclude>
241+
</excludes>
242+
<includes>
243+
<include>${someModule.test.includes}</include>
244+
</includes>
245+
</configuration>
246+
</plugin>
247247

248248
</plugins>
249249
</build>

src/main/java/com/ericsson/ei/controller/SubscriptionControllerImpl.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.springframework.http.HttpStatus;
1818
import org.springframework.http.ResponseEntity;
1919
import org.springframework.stereotype.Component;
20+
import org.springframework.web.bind.annotation.CrossOrigin;
21+
import org.springframework.web.bind.annotation.PathVariable;
2022
import org.springframework.web.bind.annotation.RequestBody;
2123

2224
import com.ericsson.ei.controller.model.Subscription;
@@ -28,6 +30,7 @@
2830
import io.swagger.annotations.ApiOperation;
2931

3032
@Component
33+
@CrossOrigin
3134
@Api(value = "subscription", description = "The Subscription API for the store and retrieve the subscriptions from the database")
3235
public class SubscriptionControllerImpl implements SubscriptionController {
3336

@@ -37,6 +40,7 @@ public class SubscriptionControllerImpl implements SubscriptionController {
3740
private static final Logger LOG = LoggerFactory.getLogger(SubscriptionControllerImpl.class);
3841

3942
@Override
43+
@CrossOrigin
4044
@ApiOperation(value = "Creates the subscription")
4145
public ResponseEntity<SubscriptionResponse> createSubscription(@RequestBody Subscription subscription) {
4246
SubscriptionResponse subscriptionResponse = new SubscriptionResponse();
@@ -54,8 +58,9 @@ public ResponseEntity<SubscriptionResponse> createSubscription(@RequestBody Subs
5458
}
5559

5660
@Override
61+
@CrossOrigin
5762
@ApiOperation(value = "Returns the subscription rules for given subscription name")
58-
public ResponseEntity<Subscription> getSubscriptionById(String subscriptionName) {
63+
public ResponseEntity<Subscription> getSubscriptionById(@PathVariable String subscriptionName) {
5964
Subscription subscription = null;
6065
try {
6166
LOG.info("Subscription :" + subscriptionName + " fetch started");
@@ -70,8 +75,9 @@ public ResponseEntity<Subscription> getSubscriptionById(String subscriptionName)
7075
}
7176

7277
@Override
78+
@CrossOrigin
7379
@ApiOperation(value = "Update the existing subscription by the subscription name")
74-
public ResponseEntity<SubscriptionResponse> updateSubscriptionById(String subscriptionName, @RequestBody Subscription subscription) {
80+
public ResponseEntity<SubscriptionResponse> updateSubscriptionById(@PathVariable String subscriptionName, @RequestBody Subscription subscription) {
7581
LOG.info("Subscription :" + subscriptionName + " update started");
7682
SubscriptionResponse subscriptionResponse = new SubscriptionResponse();
7783
if (!subscriptionService.isDuplicatedSubscription(subscription.getSubscriptionName())) {
@@ -89,8 +95,9 @@ public ResponseEntity<SubscriptionResponse> updateSubscriptionById(String subscr
8995
}
9096

9197
@Override
98+
@CrossOrigin
9299
@ApiOperation(value = "Removes the subscription from the database")
93-
public ResponseEntity<SubscriptionResponse> deleteSubscriptionById(String subscriptionName) {
100+
public ResponseEntity<SubscriptionResponse> deleteSubscriptionById(@PathVariable String subscriptionName) {
94101
SubscriptionResponse subscriptionResponse = new SubscriptionResponse();
95102
LOG.info("Subscription :" + subscriptionName + " delete started");
96103
if (subscriptionService.deleteSubscription(subscriptionName)) {
@@ -106,6 +113,7 @@ public ResponseEntity<SubscriptionResponse> deleteSubscriptionById(String subscr
106113
}
107114

108115
@Override
116+
@CrossOrigin
109117
@ApiOperation(value = "Retrieve all the subscriptions")
110118
public ResponseEntity<List<Subscription>> getSubscriptions() {
111119
LOG.info("Subscription : get all records started");
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
Copyright 2017 Ericsson AB.
3+
For a full list of individual contributors, please see the commit history.
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
package com.ericsson.ei.erqueryservice;
15+
16+
import java.io.IOException;
17+
import java.util.ArrayList;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import javax.annotation.PostConstruct;
23+
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
import org.springframework.beans.factory.annotation.Value;
27+
import org.springframework.boot.web.client.RestTemplateBuilder;
28+
import org.springframework.http.HttpEntity;
29+
import org.springframework.http.HttpHeaders;
30+
import org.springframework.http.HttpMethod;
31+
import org.springframework.http.MediaType;
32+
import org.springframework.http.ResponseEntity;
33+
import org.springframework.stereotype.Component;
34+
import org.springframework.util.LinkedMultiValueMap;
35+
import org.springframework.web.client.RestOperations;
36+
import org.springframework.web.util.UriComponentsBuilder;
37+
38+
import com.fasterxml.jackson.core.JsonProcessingException;
39+
import com.fasterxml.jackson.databind.JsonNode;
40+
import com.fasterxml.jackson.databind.ObjectMapper;
41+
import com.fasterxml.jackson.databind.node.ArrayNode;
42+
import com.fasterxml.jackson.databind.node.ObjectNode;
43+
44+
/**
45+
* @author evasiba
46+
*
47+
*/
48+
@Component
49+
public class ERQueryService {
50+
51+
static Logger log = (Logger) LoggerFactory.getLogger(ERQueryService.class);
52+
53+
private RestOperations rest;
54+
55+
public final static int DOWNSTREAM = 0;
56+
public final static int UPSTREAM = 1;
57+
public final static int DOWNANDUPSTREAM = 2;
58+
59+
@Value("${er.url}")
60+
private String url;
61+
62+
public String getUrl() {
63+
return url;
64+
}
65+
66+
public ERQueryService(RestTemplateBuilder builder) {
67+
rest = builder.build();
68+
}
69+
70+
public void setRest(RestOperations rest) {
71+
this.rest = rest;
72+
}
73+
74+
/**
75+
* This method only extracts the event information from ER2.0 based on the
76+
* eventID.
77+
*
78+
* @param eventId
79+
* @return ResponseEntity
80+
*/
81+
public ResponseEntity getEventDataById(String eventId) {
82+
String erUrl = url.trim() + "{id}";
83+
log.info("The url is : " + erUrl);
84+
Map<String, String> params = new HashMap<String, String>();
85+
params.put("id", eventId);
86+
ResponseEntity<String> response = null;
87+
log.info("The ID parameter is set");
88+
try {
89+
response = rest.getForEntity(erUrl, String.class, params);
90+
log.info("The response is : " + response.toString());
91+
} catch (Exception e) {
92+
log.error(e.getMessage(), e);
93+
}
94+
return response;
95+
}
96+
97+
/**
98+
* This method is used to fetch only the upstream or downstream or both
99+
* event information from ER2.0 based on the eventID and searchAction
100+
* conditions.
101+
*
102+
* @param eventId
103+
* @param searchAction
104+
* @param limitParam
105+
* @param levelsParam
106+
* @param tree
107+
* @return ResponseEntity
108+
*/
109+
110+
public ResponseEntity getEventStreamDataById(String eventId, int searchAction, int limitParam,
111+
int levelsParam, boolean tree) {
112+
113+
String erUrl = url.trim() + eventId;
114+
log.info("The url is : " + erUrl);
115+
116+
// Request Body parameters
117+
JsonNode uriParams = getSearchParameters(searchAction);
118+
119+
// Add query parameter
120+
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(erUrl).queryParam("limit", limitParam)
121+
.queryParam("levels", levelsParam).queryParam("tree", tree);
122+
123+
HttpHeaders headers = new HttpHeaders();
124+
headers.setContentType(MediaType.APPLICATION_JSON);
125+
126+
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity(uriParams, headers);
127+
log.info("The request is : " + builder.buildAndExpand(uriParams).toUri().toString());
128+
129+
ResponseEntity response = rest.exchange(builder.buildAndExpand(uriParams).toUri(), HttpMethod.POST,
130+
requestEntity, JsonNode.class);
131+
return response;
132+
}
133+
134+
/** Generates the json object used as body for downstream/upstream
135+
* query requests
136+
* @param searchAction - one of DOWNSTREAM, UPSTREAM or DOWNANDUPSTREAM
137+
* @return
138+
*/
139+
public JsonNode getSearchParameters(int searchAction) {
140+
JsonNode uriParams = null;
141+
ObjectMapper objectmapper = new ObjectMapper();
142+
143+
String[] linkTypes = {"ALL"};
144+
145+
try {
146+
uriParams = objectmapper.readTree("{}");
147+
if (searchAction == DOWNSTREAM) {
148+
putSearchParameter(uriParams, "dlt", linkTypes);
149+
} else if (searchAction == UPSTREAM) {
150+
putSearchParameter(uriParams, "ult", linkTypes);
151+
} else if (searchAction == DOWNANDUPSTREAM) {
152+
putSearchParameter(uriParams, "dlt", linkTypes);
153+
putSearchParameter(uriParams, "ult", linkTypes);
154+
}
155+
} catch (Exception e) {
156+
log.error(e.getMessage(), e);
157+
}
158+
return uriParams;
159+
}
160+
161+
/** Create an array node with link types for upstream or downstream query
162+
* @param params
163+
* @param actionString
164+
* @param linkTypes
165+
*/
166+
public void putSearchParameter(JsonNode params, String actionString, String[] linkTypes) {
167+
ArrayNode node =((ObjectNode) params).putArray(actionString);
168+
for (String string : linkTypes) {
169+
node.add(string);
170+
}
171+
}
172+
173+
@PostConstruct
174+
public void init() {
175+
log.debug("The url parameter is : " + url);
176+
}
177+
}

0 commit comments

Comments
 (0)