diff options
author | aosull01 <adrian.osullivan@huawei.com> | 2019-02-25 16:09:30 +0000 |
---|---|---|
committer | aosull01 <adrian.osullivan@huawei.com> | 2019-03-01 15:02:00 +0000 |
commit | d6df1fa47283fd52c8b1970d16c994919203f59e (patch) | |
tree | 73c0aa638aea28328de145326b60f43856f1105f /src/main/java/org/onap | |
parent | 44a7f7d91b329a26fb814428af4a230d35ceff50 (diff) |
Add DMaaP Integration to retrieve AAI-EVENT
Change-Id: I94e5eec12fe22b1785a00de530e080a842f37a40
Issue-ID: EXTAPI-201
Signed-off-by: aosull01 <adrian.osullivan@huawei.com>
Diffstat (limited to 'src/main/java/org/onap')
9 files changed, 545 insertions, 142 deletions
diff --git a/src/main/java/org/onap/nbi/OnapComponentsUrlPaths.java b/src/main/java/org/onap/nbi/OnapComponentsUrlPaths.java index 9060249..80563d5 100644 --- a/src/main/java/org/onap/nbi/OnapComponentsUrlPaths.java +++ b/src/main/java/org/onap/nbi/OnapComponentsUrlPaths.java @@ -50,4 +50,8 @@ public final class OnapComponentsUrlPaths { public static final String MSO_CREATE_E2ESERVICE_INSTANCE_PATH = "/onap/so/infra/e2eServiceInstances/v3"; public static final String MSO_DELETE_E2ESERVICE_INSTANCE_PATH = "/onap/so/infra/e2eServiceInstances/v3/"; public static final String MSO_GET_E2EREQUEST_STATUS_PATH = "/onap/so/infra/e2eServiceInstances/v3/$serviceId/operations/$operationId"; -} + + // DMaaP Message Router REST Client + public static final String DMAAP_CONSUME_EVENTS = + "/events/$topic/$consumergroup/$consumerid?timeout=$timeout"; +}
\ No newline at end of file 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 0f94802..ac073c1 100755 --- a/src/main/java/org/onap/nbi/apis/hub/HubResource.java +++ b/src/main/java/org/onap/nbi/apis/hub/HubResource.java @@ -1,23 +1,24 @@ /** * Copyright (c) 2018 Orange * - * 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 + * 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. + * 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 java.net.URI; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import org.onap.nbi.apis.hub.model.Subscriber; import org.onap.nbi.apis.hub.model.Subscription; +import org.onap.nbi.apis.hub.service.CheckDMaaPEventsManager; import org.onap.nbi.apis.hub.service.SubscriptionService; import org.onap.nbi.commons.JsonRepresentation; import org.onap.nbi.commons.MultiCriteriaRequestBuilder; @@ -42,67 +43,80 @@ 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.servlet.support.ServletUriComponentsBuilder; @RestController @RequestMapping("/hub") @EnableScheduling public class HubResource extends ResourceManagement { - Logger logger = LoggerFactory.getLogger(HubResource.class); + Logger logger = LoggerFactory.getLogger(HubResource.class); - @Autowired - MongoTemplate mongoTemplate; + @Autowired + MongoTemplate mongoTemplate; - @Autowired - SubscriptionService subscriptionService; + @Autowired + SubscriptionService subscriptionService; - @Autowired - MultiCriteriaRequestBuilder multiCriteriaRequestBuilder; + @Autowired + MultiCriteriaRequestBuilder multiCriteriaRequestBuilder; - @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<Object> createEventSubscription(@RequestBody Subscription subscription, - @RequestParam MultiValueMap<String, String> params) { - logger.debug("POST request for subscription : {}", subscription); - 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) { + @Autowired + CheckDMaaPEventsManager checkDMaaPEventMAnager; + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<Object> createEventSubscription(@RequestBody Subscription subscription, + @RequestParam MultiValueMap<String, String> params) { + logger.debug("POST request for subscription : {}", subscription); + Subscriber subscriber = subscriptionService.createSubscription(subscription); + JsonRepresentation filter = new JsonRepresentation(params); + return this.createResponse(Subscription.createFromSubscriber(subscriber), filter); - Optional<Subscriber> optionalSubscriber = subscriptionService.findSubscriptionById(subscriptionId); - if (!optionalSubscriber.isPresent()) { - return ResponseEntity.notFound().build(); - } - return ResponseEntity.ok(Subscription.createFromSubscriber(optionalSubscriber.get())); - } - - @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<Object> findSubscribers(@RequestParam MultiValueMap<String, String> params) { + } - Query query = multiCriteriaRequestBuilder.buildRequest(params); - List<Subscriber> subscribers = mongoTemplate.find(query, Subscriber.class); - JsonRepresentation filter = new JsonRepresentation(params); - long totalCount = subscriptionService.countSubscription(); - 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()); + @GetMapping(value = "/{subscriptionId}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<Subscription> getSubscription(@PathVariable String subscriptionId) { - return this.findResponse(subscriptions, filter, headers); + Optional<Subscriber> optionalSubscriber = + subscriptionService.findSubscriptionById(subscriptionId); + if (!optionalSubscriber.isPresent()) { + return ResponseEntity.notFound().build(); } + return ResponseEntity.ok(Subscription.createFromSubscriber(optionalSubscriber.get())); + } + + @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<Object> findSubscribers( + @RequestParam MultiValueMap<String, String> params) { + + Query query = multiCriteriaRequestBuilder.buildRequest(params); + List<Subscriber> subscribers = mongoTemplate.find(query, Subscriber.class); + JsonRepresentation filter = new JsonRepresentation(params); + long totalCount = subscriptionService.countSubscription(); + 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()); + + return this.findResponse(subscriptions, filter, headers); + + } + + /* + * Resource to test for DMaaP Integration for subscribing to AAI-EVENTs + */ + @GetMapping("/testaaievents") + @ResponseStatus(HttpStatus.OK) + public void testAAIEventListener() { + checkDMaaPEventMAnager.checkForDMaaPAAIEvents(); + } + + @DeleteMapping("/{subscriptionId}") + @ResponseStatus(HttpStatus.NO_CONTENT) + public void deleteSubscription(@PathVariable String subscriptionId) { + logger.debug("DELETE request for subscription id #{}", subscriptionId); + subscriptionService.deleteSubscription(subscriptionId); + } - @DeleteMapping("/{subscriptionId}") - @ResponseStatus(HttpStatus.NO_CONTENT) - public void deleteSubscription(@PathVariable String subscriptionId) { - logger.debug("DELETE request for subscription id #{}", subscriptionId); - subscriptionService.deleteSubscription(subscriptionId); - } } diff --git a/src/main/java/org/onap/nbi/apis/hub/model/EventType.java b/src/main/java/org/onap/nbi/apis/hub/model/EventType.java index 1702347..b4e1f1a 100644 --- a/src/main/java/org/onap/nbi/apis/hub/model/EventType.java +++ b/src/main/java/org/onap/nbi/apis/hub/model/EventType.java @@ -1,17 +1,15 @@ /** - * Copyright (c) 2018 Orange + * Copyright (c) 2018 Orange * - * 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 + * 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 + * 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. + * 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.model; @@ -20,36 +18,42 @@ import com.fasterxml.jackson.annotation.JsonValue; public enum EventType { - SERVICE_ORDER_CREATION("ServiceOrderCreationNotification"), + SERVICE_ORDER_CREATION("ServiceOrderCreationNotification"), - SERVICE_ORDER_STATE_CHANGE("ServiceOrderStateChangeNotification"), + SERVICE_ORDER_STATE_CHANGE("ServiceOrderStateChangeNotification"), - SERVICE_ORDER_ITEM_STATE_CHANGE("ServiceOrderItemStateChangeNotification"); + SERVICE_ORDER_ITEM_STATE_CHANGE("ServiceOrderItemStateChangeNotification"), - private String value; + SERVICE_CREATION("ServiceCreationNotification"), - EventType(String value) { - this.value = value; - } + SERVICE_ATTRIBUTE_VALUE_CHANGE("ServiceAttributeValueChangeNotification"), - @Override - public String toString() { - return String.valueOf(value); - } + SERVICE_REMOVE("ServiceRemoveNotification"); - @JsonCreator - public static EventType fromValue(String text) { - for (EventType b : EventType.values()) { - if (String.valueOf(b.value).equals(text)) { - return b; - } - } - return null; - } + private String value; - @JsonValue - public String value() { - return this.value; + EventType(String value) { + this.value = value; + } + + @Override + public String toString() { + return String.valueOf(value); + } + + @JsonCreator + public static EventType fromValue(String text) { + for (EventType b : EventType.values()) { + if (String.valueOf(b.value).equals(text)) { + return b; + } } + return null; + } + + @JsonValue + public String value() { + return this.value; + } } diff --git a/src/main/java/org/onap/nbi/apis/hub/model/ServiceInstanceEvent.java b/src/main/java/org/onap/nbi/apis/hub/model/ServiceInstanceEvent.java new file mode 100644 index 0000000..4bbafdc --- /dev/null +++ b/src/main/java/org/onap/nbi/apis/hub/model/ServiceInstanceEvent.java @@ -0,0 +1,90 @@ +/** + * Copyright (c) 2019 Huawei + * + * 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.model; + +import org.onap.nbi.apis.serviceorder.model.RelatedParty; +import org.springframework.data.annotation.Id; +import com.fasterxml.jackson.annotation.JsonProperty; + +public class ServiceInstanceEvent { + + @Id + @JsonProperty("id") + private String id = null; + + @JsonProperty("href") + private String href = null; + + @JsonProperty("name") + private String name = null; + + @JsonProperty("type") + private String type = "service-instance"; + + @JsonProperty("state") + private String state = null; + + @JsonProperty("relatedParty") + private RelatedParty relatedParty = null; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getHref() { + return href; + } + + public void setHref(String href) { + this.href = href; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public RelatedParty getRelatedParty() { + return relatedParty; + } + + public void setRelatedParty(RelatedParty relatedParty) { + this.relatedParty = relatedParty; + } + +} diff --git a/src/main/java/org/onap/nbi/apis/hub/service/CheckDMaaPEventsManager.java b/src/main/java/org/onap/nbi/apis/hub/service/CheckDMaaPEventsManager.java new file mode 100644 index 0000000..6e2811f --- /dev/null +++ b/src/main/java/org/onap/nbi/apis/hub/service/CheckDMaaPEventsManager.java @@ -0,0 +1,199 @@ +/** + * Copyright (c) 2019 Huawei + * + * 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.service; + +import java.io.IOException; +import java.net.URI; +import java.util.List; +import javax.annotation.PostConstruct; +import org.onap.nbi.OnapComponentsUrlPaths; +import org.onap.nbi.apis.hub.model.Event; +import org.onap.nbi.apis.hub.model.EventType; +import org.onap.nbi.apis.hub.model.ServiceInstanceEvent; +import org.onap.nbi.apis.hub.repository.SubscriberRepository; +import org.onap.nbi.apis.serviceorder.model.RelatedParty; +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.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + + +@Service +public class CheckDMaaPEventsManager { + + public static final String RESPONSE_STATUS = "response status : "; + public static final String RETURNS = " returns "; + public static final String ERROR_ON_CALLING = "error on calling "; + + @Autowired + private RestTemplate restTemplate; + + @Autowired + private SubscriberRepository subscriberRepository; + + @Autowired + private NotifierService notifier; + + @Value("${dmaap.host}") + private String dmaapHostname; + + @Value("${dmaap.topic}") + private String topic; + + @Value("${dmaap.consumergroup}") + private String consumerGroup; + + @Value("${dmaap.consumerid}") + private String consumerId; + + @Value("${dmaap.timeout}") + private String timeout; + + private final Logger logger = LoggerFactory.getLogger(CheckDMaaPEventsManager.class); + + private String dmaapGetEventsUrl; + + @PostConstruct + private void setUpAndLogDMaaPUrl() { + dmaapGetEventsUrl = new StringBuilder().append(dmaapHostname) + .append(OnapComponentsUrlPaths.DMAAP_CONSUME_EVENTS).toString(); + logger.info("DMaaP Get Events url : " + dmaapGetEventsUrl); + } + + public void checkForDMaaPAAIEvents() { + ObjectMapper mapper = new ObjectMapper(); + + + String dmaapGetEventsUrlFormated = dmaapGetEventsUrl.replace("$topic", topic); + dmaapGetEventsUrlFormated = dmaapGetEventsUrlFormated.replace("$consumergroup", consumerGroup); + dmaapGetEventsUrlFormated = dmaapGetEventsUrlFormated.replace("$consumerid", consumerId); + dmaapGetEventsUrlFormated = dmaapGetEventsUrlFormated.replace("$timeout", timeout); + + List<String> dmaapResponse = callDMaaPGetEvents(dmaapGetEventsUrlFormated); + if (!CollectionUtils.isEmpty(dmaapResponse)) { + for (int i = 0; i < dmaapResponse.size(); i++) { + String aaiEventString = dmaapResponse.get(i); + if (logger.isDebugEnabled()) { + logger.debug("aai event returned was {}", aaiEventString); + } + try { + JsonNode jsonNode = mapper.readValue(aaiEventString, JsonNode.class); + JsonNode eventHeader = jsonNode.get("event-header"); + String aaiEventEntityType = eventHeader.get("entity-type").asText(); + String action = eventHeader.get("action").asText(); + if (logger.isDebugEnabled()) { + logger.debug("aaiEventEntityType is {} and action is {}", aaiEventEntityType, action); + } + if (aaiEventEntityType.equals("service-instance")) { + { + // parse the AAI-EVENT service-instance tree + ServiceInstanceEvent serviceInstanceEvent = new ServiceInstanceEvent(); + RelatedParty relatedParty = new RelatedParty(); + JsonNode entity = jsonNode.get("entity"); + relatedParty.setId(entity.get("global-customer-id").asText()); + relatedParty.setName(entity.get("subscriber-name").asText()); + serviceInstanceEvent.setRelatedParty(relatedParty); + JsonNode childServiceSubscription = entity.get("service-subscriptions"); + JsonNode serviceSubscriptions = childServiceSubscription.get("service-subscription"); + JsonNode serviceSubscription = serviceSubscriptions.get(0); + String serviceSubscriptionPrint = serviceSubscription.toString(); + JsonNode childserviceInstances = serviceSubscription.get("service-instances"); + JsonNode serviceInstances = childserviceInstances.get("service-instance"); + JsonNode serviceInstance = serviceInstances.get(0); + serviceInstanceEvent.setId(serviceInstance.get("service-instance-id").asText()); + serviceInstanceEvent.setState(serviceInstance.get("orchestration-status").asText()); + if (action.equals("CREATE")) { + if (logger.isDebugEnabled()) { + logger.debug("sending service inventory event to listeners"); + } + processEvent( + EventFactory.getEvent(EventType.SERVICE_CREATION, serviceInstanceEvent)); + } else if (action.equals("DELETE")) { + processEvent(EventFactory.getEvent(EventType.SERVICE_REMOVE, serviceInstanceEvent)); + } else if (action.equals("UPDATE")) { + processEvent(EventFactory.getEvent(EventType.SERVICE_ATTRIBUTE_VALUE_CHANGE, + serviceInstanceEvent)); + } + + + } + + } + + } catch (JsonParseException e) { + logger.error(" unable to Parse AAI Event JSON String {}, exception is", aaiEventString, + e.getMessage()); + } catch (JsonMappingException e) { + logger.error(" unable to Map AAI Event JSON String {} to Java Pojo, exception is", + aaiEventString, e.getMessage()); + } catch (IOException e) { + logger.error("IO Error when parsing AAI Event JSON String {} ", aaiEventString, + e.getMessage()); + } + } + } + } + + public List<String> callDMaaPGetEvents(String dmaapGetEventsUrlFormated) { + + if (logger.isDebugEnabled()) { + logger.debug("Calling DMaaP Url : " + dmaapGetEventsUrlFormated); + } + UriComponentsBuilder callURI = UriComponentsBuilder.fromHttpUrl(dmaapGetEventsUrlFormated); + ResponseEntity<Object> response = callDMaaP(callURI.build().encode().toUri()); + return (List<String>) response.getBody(); + + } + + private ResponseEntity<Object> callDMaaP(URI callURI) { + ResponseEntity<Object> response = + restTemplate.exchange(callURI, HttpMethod.GET, buildRequestHeader(), Object.class); + + if (logger.isDebugEnabled()) { + logger.debug("response body : {} ", response.getBody().toString()); + logger.debug("response status : {}", response.getStatusCodeValue()); + } + return response; + } + + private HttpEntity<String> buildRequestHeader() { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Accept", "application/json"); + httpHeaders.add("Content-Type", "application/json"); + return new HttpEntity<>("parameters", httpHeaders); + } + + /** + * Retrieve subscribers that match an event and fire notification asynchronously + * + * @param event + */ + private void processEvent(Event event) { + subscriberRepository.findSubscribersUsingEvent(event).forEach(sub -> notifier.run(sub, event)); + } + +} diff --git a/src/main/java/org/onap/nbi/apis/hub/service/DMaaPEventsScheduler.java b/src/main/java/org/onap/nbi/apis/hub/service/DMaaPEventsScheduler.java new file mode 100644 index 0000000..20bc2d9 --- /dev/null +++ b/src/main/java/org/onap/nbi/apis/hub/service/DMaaPEventsScheduler.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2019 Huawei + * + * 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.service; + +import java.io.FileNotFoundException; +import java.io.IOException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +@Profile("default") +@Service +@EnableScheduling +public class DMaaPEventsScheduler { + + @Autowired + CheckDMaaPEventsManager checkDMaaPEventsManager; + + @Scheduled(fixedDelayString = "${dmaapCheck.schedule}", + initialDelayString = "${dmaapCheck.initial}") + private void processDMaaPEvents() { + checkDMaaPEventsManager.checkForDMaaPAAIEvents(); + + } +} + diff --git a/src/main/java/org/onap/nbi/apis/hub/service/EventFactory.java b/src/main/java/org/onap/nbi/apis/hub/service/EventFactory.java index e935a1c..ed34322 100644 --- a/src/main/java/org/onap/nbi/apis/hub/service/EventFactory.java +++ b/src/main/java/org/onap/nbi/apis/hub/service/EventFactory.java @@ -1,75 +1,95 @@ /** * Copyright (c) 2018 Orange * - * 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 + * 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. + * 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.service; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.MappingJsonFactory; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; import org.onap.nbi.apis.hub.model.Event; import org.onap.nbi.apis.hub.model.EventType; +import org.onap.nbi.apis.hub.model.ServiceInstanceEvent; import org.onap.nbi.apis.serviceorder.model.ServiceOrder; import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem; import org.onap.nbi.commons.JacksonFilter; import org.onap.nbi.commons.JsonRepresentation; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.MappingJsonFactory; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; public class EventFactory { - private static final ObjectMapper mapper = new ObjectMapper(new MappingJsonFactory()); + private static final ObjectMapper mapper = new ObjectMapper(new MappingJsonFactory()); - public static Event getEvent(EventType eventType, ServiceOrder serviceOrder, ServiceOrderItem serviceOrderItem) { - Event event = new Event(); - event.setEventId(UUID.randomUUID().toString()); - event.setEventDate(new Date()); - event.setEventType(eventType.value()); + public static Event getEvent(EventType eventType, ServiceOrder serviceOrder, + ServiceOrderItem serviceOrderItem) { + Event event = new Event(); + event.setEventId(UUID.randomUUID().toString()); + event.setEventDate(new Date()); + event.setEventType(eventType.value()); - DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); - mapper.setDateFormat(df); + DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + mapper.setDateFormat(df); - JsonNode serviceOrderJson = mapper.valueToTree(filterServiceOrder(serviceOrder)); + JsonNode serviceOrderJson = mapper.valueToTree(filterServiceOrder(serviceOrder)); - if (EventType.SERVICE_ORDER_ITEM_STATE_CHANGE.equals(eventType)) { - JsonNode serviceOrderItemJson = mapper.valueToTree(serviceOrderItem); - ((ObjectNode) serviceOrderJson).putArray("orderItem").add(serviceOrderItemJson); - } + if (EventType.SERVICE_ORDER_ITEM_STATE_CHANGE.equals(eventType)) { + JsonNode serviceOrderItemJson = mapper.valueToTree(serviceOrderItem); + ((ObjectNode) serviceOrderJson).putArray("orderItem").add(serviceOrderItemJson); + } - event.setEvent(serviceOrderJson); + event.setEvent(serviceOrderJson); - return event; - } + return event; + } + + public static Event getEvent(EventType eventType, ServiceInstanceEvent serviceInstanceEvent) { + Event event = new Event(); + event.setEventId(UUID.randomUUID().toString()); + event.setEventDate(new Date()); + event.setEventType(eventType.value()); + + DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + mapper.setDateFormat(df); + JsonNode serviceInstanceJson = mapper.valueToTree(serviceInstanceEvent); - /** - * Filter ServiceOrderObject to produce a lightweight object that fit the eventBody specification - * @param serviceOrder - * @return - */ - private static Object filterServiceOrder(final ServiceOrder serviceOrder) { + event.setEvent(serviceInstanceJson); - Object filteredServiceOrder = null; + return event; + } - if (serviceOrder != null) { - JsonRepresentation jsonRepresentation = new JsonRepresentation(); - jsonRepresentation.add("id").add("href").add("externalId").add("state").add("orderDate").add - ("completionDateTime").add("orderItem"); - filteredServiceOrder = JacksonFilter.createNode(serviceOrder, jsonRepresentation); - } + /** + * Filter ServiceOrderObject to produce a lightweight object that fit the eventBody specification + * + * @param serviceOrder + * @return + */ + private static Object filterServiceOrder(final ServiceOrder serviceOrder) { - return filteredServiceOrder; + Object filteredServiceOrder = null; + + if (serviceOrder != null) { + JsonRepresentation jsonRepresentation = new JsonRepresentation(); + jsonRepresentation.add("id").add("href").add("externalId").add("state").add("orderDate") + .add("completionDateTime").add("orderItem"); + + filteredServiceOrder = JacksonFilter.createNode(serviceOrder, jsonRepresentation); } + + return filteredServiceOrder; + } } diff --git a/src/main/java/org/onap/nbi/apis/hub/service/NotifierService.java b/src/main/java/org/onap/nbi/apis/hub/service/NotifierService.java index 90cc7a4..80b6331 100755 --- a/src/main/java/org/onap/nbi/apis/hub/service/NotifierService.java +++ b/src/main/java/org/onap/nbi/apis/hub/service/NotifierService.java @@ -1,14 +1,15 @@ /** * Copyright (c) 2018 Orange * - * 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 + * 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. + * 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.service; @@ -26,19 +27,19 @@ import org.springframework.web.client.RestTemplate; @Service public class NotifierService { - private final Logger logger = LoggerFactory.getLogger(NotifierService.class); + private final Logger logger = LoggerFactory.getLogger(NotifierService.class); - @Autowired - RestTemplate restTemplate; - - @Async - public void run(Subscriber subscriber, @Valid Event event) { - try { - restTemplate.postForEntity(subscriber.getCallback(), event, Object.class); - } catch (BackendFunctionalException e) { - logger.error(" unable to post event to {} , receive {}, {}", subscriber.getCallback(), e.getHttpStatus(), - e.getBodyResponse()); - } + @Autowired + RestTemplate restTemplate; + @Async + public void run(Subscriber subscriber, @Valid Event event) { + try { + restTemplate.postForEntity(subscriber.getCallback(), event, Object.class); + } catch (BackendFunctionalException e) { + logger.error(" unable to post event to {} , receive {}, {}", subscriber.getCallback(), + e.getHttpStatus(), e.getBodyResponse()); } + + } } diff --git a/src/main/java/org/onap/nbi/configuration/AppContext.java b/src/main/java/org/onap/nbi/configuration/AppContext.java new file mode 100644 index 0000000..fb26799 --- /dev/null +++ b/src/main/java/org/onap/nbi/configuration/AppContext.java @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2019 Huawei + * + * 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.configuration; + +import java.util.concurrent.Executor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.SimpleAsyncTaskExecutor; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; + +@Configuration +@EnableAsync +public class AppContext extends WebMvcConfigurationSupport { + @Bean + public Executor taskExecutor() { + return new SimpleAsyncTaskExecutor(); + } +} |