diff options
Diffstat (limited to 'src/main/java/org/onap/nbi/apis/hub')
6 files changed, 90 insertions, 166 deletions
diff --git a/src/main/java/org/onap/nbi/apis/hub/ExtApiClientForHub.java b/src/main/java/org/onap/nbi/apis/hub/ExtApiClientForHub.java deleted file mode 100644 index 10d6f7c..0000000 --- a/src/main/java/org/onap/nbi/apis/hub/ExtApiClientForHub.java +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Copyright (c) 2019 Amdocs - * - * 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 org.onap.nbi.apis.hub; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.onap.nbi.apis.hub.model.Subscription; -import org.onap.nbi.exceptions.BackendFunctionalException; -import org.onap.nbi.exceptions.TechnicalException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.*; -import org.springframework.stereotype.Service; -import org.springframework.web.client.ResourceAccessException; -import org.springframework.web.client.RestTemplate; - -@Service -public class ExtApiClientForHub { - - @Autowired - RestTemplate restTemplate; - - @Value("${external.nbi.url}") - private String externalNbiUrl; - - @Value("${nbi.url}") - private String nbiUrl; - - private static final Logger LOGGER = LoggerFactory.getLogger(ExtApiClientForHub.class); - - private static final ObjectMapper mapper = new ObjectMapper(); - - public ResponseEntity<Object> postEventSubscription(Subscription subscription, String targetURL) { - try { - - - String url = externalNbiUrl.replace("{targetUrl}", targetURL) + "/hub"; - LOGGER.debug("Sending create event notification request to " + url); - String nbiListenerUrl = nbiUrl + "/listener"; - subscription.setCallback(nbiListenerUrl); - String subscriptionAsBody = mapper.writeValueAsString(subscription); - ResponseEntity<Object> response = postRequest(url, subscriptionAsBody, buildRequestHeaders()); - LOGGER.info("Received response from " + targetURL + "with status : " + response.getStatusCode()); - LOGGER.debug("Response Body: " + response.getBody()); - return response; - }catch(JsonProcessingException ex) { - LOGGER.error("error occurred while parsing service order to Json: " + ex); - throw new TechnicalException("error occurred while parsing service order to Json:"+ HttpStatus.INTERNAL_SERVER_ERROR); - } - } - - private HttpHeaders buildRequestHeaders() { - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.add("Accept", "application/json"); - httpHeaders.add("Content-Type", "application/json"); - return httpHeaders; - } - - - private ResponseEntity<Object> postRequest(String url, String body, HttpHeaders httpHeaders) { - try { - ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(body, httpHeaders), Object.class); - return response; - }catch(BackendFunctionalException ex) { - LOGGER.error("error on calling " + url + " ," + ex); - return new ResponseEntity<>("problem calling onap services", ex.getHttpStatus()); - }catch (ResourceAccessException ex) { - LOGGER.error("error on calling " + url + " ," + ex); - return new ResponseEntity<>("unable to reach onap services", HttpStatus.INTERNAL_SERVER_ERROR); - - } - - } -} - diff --git a/src/main/java/org/onap/nbi/apis/hub/HubResource.java b/src/main/java/org/onap/nbi/apis/hub/HubResource.java index 28e1313..3f14b8a 100755 --- a/src/main/java/org/onap/nbi/apis/hub/HubResource.java +++ b/src/main/java/org/onap/nbi/apis/hub/HubResource.java @@ -13,19 +13,23 @@ */ package org.onap.nbi.apis.hub; +import java.util.LinkedHashMap; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; +import org.onap.nbi.OnapComponentsUrlPaths; import org.onap.nbi.apis.hub.model.Subscriber; import org.onap.nbi.apis.hub.model.Subscription; import org.onap.nbi.apis.hub.service.dmaap.CheckDMaaPEventsManager; import org.onap.nbi.apis.hub.service.SubscriptionService; +import org.onap.nbi.commons.EWInterfaceUtils; import org.onap.nbi.commons.JsonRepresentation; import org.onap.nbi.commons.MultiCriteriaRequestBuilder; import org.onap.nbi.commons.ResourceManagement; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Query; import org.springframework.http.HttpHeaders; @@ -39,14 +43,14 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.bind.annotation.RequestHeader; @RestController -@RequestMapping("/hub") +@RequestMapping(OnapComponentsUrlPaths.HUB_PATH) @EnableScheduling public class HubResource extends ResourceManagement { @@ -65,27 +69,40 @@ public class HubResource extends ResourceManagement { CheckDMaaPEventsManager checkDMaaPEventMAnager; @Autowired - ExtApiClientForHub extApiClientForHub; + EWInterfaceUtils ewInterfaceUtils; + + @Value("${nbi.public.url}") + private String nbiPublicUrl; @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Object> createEventSubscription(@RequestBody Subscription subscription, - @RequestParam MultiValueMap<String, String> params, @RequestHeader(required = false) String targetURL) { + @RequestParam MultiValueMap<String, String> params, @RequestHeader(value="Target",required = false)String targetUrl) { logger.debug("POST request for subscription : {}", subscription); - if(targetURL != null) { - Subscriber subscriber = subscriptionService.createSubscription(subscription); - return extApiClientForHub.postEventSubscription(subscription,targetURL); - }else { - Subscriber subscriber = subscriptionService.createSubscription(subscription); - JsonRepresentation filter = new JsonRepresentation(params); - return this.createResponse(Subscription.createFromSubscriber(subscriber), filter); + if (targetUrl != null) { + targetUrl = targetUrl + OnapComponentsUrlPaths.HUB_PATH; + String originalCallback = subscription.getCallback(); + subscription.setCallback(nbiPublicUrl + OnapComponentsUrlPaths.LISTENER_PATH); + ResponseEntity ewResponse = ewInterfaceUtils.callPostRequestTarget(subscription, targetUrl); + if (ewResponse.getStatusCode() == HttpStatus.CREATED) { + subscription.setCallback(originalCallback); + subscription.setEwHost(targetUrl); + subscription.setEwId(((LinkedHashMap)ewResponse.getBody()).get( "id" ).toString()); + } else { + return ewResponse; + } } + Subscriber subscriber = subscriptionService.createSubscription(subscription); + JsonRepresentation filter = new JsonRepresentation(params); + return this.createResponse(Subscription.createFromSubscriber(subscriber), filter); + } @GetMapping(value = "/{subscriptionId}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Subscription> getSubscription(@PathVariable String subscriptionId) { - Optional<Subscriber> optionalSubscriber = subscriptionService.findSubscriptionById(subscriptionId); + Optional<Subscriber> optionalSubscriber = + subscriptionService.findSubscriptionById(subscriptionId); if (!optionalSubscriber.isPresent()) { return ResponseEntity.notFound().build(); } @@ -93,7 +110,8 @@ public class HubResource extends ResourceManagement { } @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<Object> findSubscribers(@RequestParam MultiValueMap<String, String> params) { + public ResponseEntity<Object> findSubscribers( + @RequestParam MultiValueMap<String, String> params) { Query query = multiCriteriaRequestBuilder.buildRequest(params); List<Subscriber> subscribers = mongoTemplate.find(query, Subscriber.class); @@ -102,7 +120,8 @@ public class HubResource extends ResourceManagement { HttpHeaders headers = new HttpHeaders(); headers.add("X-Total-Count", String.valueOf(totalCount)); headers.add("X-Result-Count", String.valueOf(subscribers.size())); - List<Subscription> subscriptions = subscribers.stream().map(Subscription::createFromSubscriber).collect(Collectors.toList()); + List<Subscription> subscriptions = + subscribers.stream().map(Subscription::createFromSubscriber).collect(Collectors.toList()); return this.findResponse(subscriptions, filter, headers); @@ -121,7 +140,19 @@ public class HubResource extends ResourceManagement { @ResponseStatus(HttpStatus.NO_CONTENT) public void deleteSubscription(@PathVariable String subscriptionId) { logger.debug("DELETE request for subscription id #{}", subscriptionId); + Optional<Subscriber> optionalSubscriber= subscriptionService.findSubscriptionById(subscriptionId); subscriptionService.deleteSubscription(subscriptionId); + String ewHost=optionalSubscriber.get().getEwHost(); + String ewId=optionalSubscriber.get().getEwId(); + logger.info("POST delete for ewHost : {}", ewHost); + logger.info("POST delete for ewId : {}", ewId); + if ( ewHost !=null && ewId !=null ) + { + logger.info("POST deleteIF for ewHost : {}", ewHost); + String targetUrl = ewHost+ "/" + ewId; + ewInterfaceUtils.callDeleteRequestTarget(targetUrl); + logger.info("POST deleteIF for ewHost is : {}", targetUrl); + } } } diff --git a/src/main/java/org/onap/nbi/apis/hub/ListenerResource.java b/src/main/java/org/onap/nbi/apis/hub/ListenerResource.java deleted file mode 100644 index aa236d1..0000000 --- a/src/main/java/org/onap/nbi/apis/hub/ListenerResource.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright (c) 2019 Amdocs - * - * 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 org.onap.nbi.apis.hub; - -import org.onap.nbi.apis.hub.service.NotificationAspect; -import org.onap.nbi.commons.ResourceManagement; -import org.onap.nbi.exceptions.BackendFunctionalException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import org.onap.nbi.apis.hub.model.Event; - - - -@RestController -@RequestMapping("/listener") -public class ListenerResource extends ResourceManagement { - - @Autowired - NotificationAspect notificationAspect; - - private static final Logger LOGGER = LoggerFactory.getLogger(ListenerResource.class); - - @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) - public void receiveNotification(@RequestBody Event event) { - try { - LOGGER.debug("Received notification from external NBI || Sending it to original listener"); - notificationAspect.forwardNotificationToOriginalListener(event); - }catch(BackendFunctionalException ex) { - LOGGER.error("Unable to send the recieved notification to original Listener"); - - } - } -} diff --git a/src/main/java/org/onap/nbi/apis/hub/model/Subscriber.java b/src/main/java/org/onap/nbi/apis/hub/model/Subscriber.java index ab2b05f..582b14b 100755 --- a/src/main/java/org/onap/nbi/apis/hub/model/Subscriber.java +++ b/src/main/java/org/onap/nbi/apis/hub/model/Subscriber.java @@ -34,6 +34,26 @@ public class Subscriber implements Resource { private String id; private String callback; + public String getEwId() { + return ewId; + } + + public void setEwId(String ewId) { + this.ewId = ewId; + } + + private String ewId; + + public String getEwHost() { + return ewHost; + } + + public void setEwHost(String ewHost) { + this.ewHost = ewHost; + } + + private String ewHost; + private Map<String, String[]> query = new HashMap<>(); public String getId() { @@ -59,6 +79,8 @@ public class Subscriber implements Resource { public static Subscriber createFromSubscription(Subscription request) { Subscriber sub = new Subscriber(); sub.setCallback(request.getCallback()); + sub.setEwId( request.getEwId()); + sub.setEwHost( request.getEwHost()); Stream.of(request.getQuery().split("&")) .map(q -> q.split("=")) diff --git a/src/main/java/org/onap/nbi/apis/hub/model/Subscription.java b/src/main/java/org/onap/nbi/apis/hub/model/Subscription.java index 34e23e1..646d39b 100755 --- a/src/main/java/org/onap/nbi/apis/hub/model/Subscription.java +++ b/src/main/java/org/onap/nbi/apis/hub/model/Subscription.java @@ -26,6 +26,26 @@ public class Subscription implements Resource{ private String callback; private String query; + private String ewId; + private String ewHost; + + public void setEwHost(String ewHost) { + this.ewHost = ewHost; + } + + public String getEwId() { + return ewId; + } + + public void setEwId(String ewId) { + this.ewId = ewId; + } + + + + public String getEwHost() { + return ewHost; + } public Subscription(){ @@ -65,6 +85,9 @@ public class Subscription implements Resource{ Subscription sub = new Subscription(); sub.setId(subscriber.getId()); sub.setCallback(subscriber.getCallback()); + sub.setEwId( subscriber.getEwId()); + sub.setEwHost( subscriber.getEwHost()); + String query = subscriber.getQuery().entrySet() .stream() diff --git a/src/main/java/org/onap/nbi/apis/hub/service/NotificationAspect.java b/src/main/java/org/onap/nbi/apis/hub/service/NotificationAspect.java index 3033404..cd242e8 100755 --- a/src/main/java/org/onap/nbi/apis/hub/service/NotificationAspect.java +++ b/src/main/java/org/onap/nbi/apis/hub/service/NotificationAspect.java @@ -27,7 +27,6 @@ import org.onap.nbi.apis.serviceorder.model.StateType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.stereotype.Component; -import org.onap.nbi.exceptions.TechnicalException; @Aspect @Component @@ -72,14 +71,6 @@ public class NotificationAspect { } } - public void forwardNotificationToOriginalListener(Event event) { - if(event != null) { - processEvent(event); - }else{ - throw new TechnicalException("Received null event from external NBI"); - } - } - /** * Retreive subscribers that match an event and fire notification * asynchronously |