Format Java code with respect to ONAP Code Style
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / hub / service / NotificationAspect.java
1 /**
2  *     Copyright (c) 2018 Orange
3  *
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  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *     Unless required by applicable law or agreed to in writing, software
11  *     distributed under the License is distributed on an "AS IS" BASIS,
12  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *     See the License for the specific language governing permissions and
14  *     limitations under the License.
15  */
16
17 package org.onap.nbi.apis.hub.service;
18
19 import org.aspectj.lang.JoinPoint;
20 import org.aspectj.lang.annotation.AfterReturning;
21 import org.aspectj.lang.annotation.Aspect;
22 import org.onap.nbi.apis.hub.model.Event;
23 import org.onap.nbi.apis.hub.model.EventType;
24 import org.onap.nbi.apis.hub.repository.SubscriberRepository;
25 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
26 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
27 import org.onap.nbi.apis.serviceorder.model.StateType;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.beans.factory.annotation.Configurable;
30 import org.springframework.stereotype.Component;
31
32 @Aspect
33 @Component
34 @Configurable
35 public class NotificationAspect {
36
37     @Autowired
38     private SubscriberRepository subscriberRepository;
39
40     @Autowired
41     private NotifierService notifier;
42
43     @AfterReturning(
44             value = "execution(* org.onap.nbi.apis.serviceorder.service.ServiceOrderService"
45                     + ".createServiceOrder(..))",
46             returning = "serviceOrderCreated")
47     public void whenCreateServiceOrder(ServiceOrder serviceOrderCreated) {
48         if (StateType.ACKNOWLEDGED.equals(serviceOrderCreated.getState())) {
49             processEvent(EventFactory.getEvent(EventType.SERVICE_ORDER_CREATION, serviceOrderCreated, null));
50         }
51     }
52
53     @AfterReturning(
54             value = "execution(* org.onap.nbi.apis.serviceorder.service.ServiceOrderService" + ".updateOrderState(..))",
55             returning = "serviceOrderUpdated")
56     public void whenUpdateServiceOrderState(ServiceOrder serviceOrderUpdated) {
57         if (StateType.COMPLETED.equals(serviceOrderUpdated.getState())
58                 || StateType.FAILED.equals(serviceOrderUpdated.getState())) {
59             processEvent(EventFactory.getEvent(EventType.SERVICE_ORDER_STATE_CHANGE, serviceOrderUpdated, null));
60         }
61     }
62
63     @AfterReturning(
64             value = "execution(* org.onap.nbi.apis.serviceorder.service.ServiceOrderService"
65                     + ".updateOrderItemState(..))")
66     public void whenUpdateServiceOrderItemState(JoinPoint joinPoint) {
67         Object[] signatureArgs = joinPoint.getArgs();
68
69         if (signatureArgs != null && signatureArgs.length == 3) {
70             ServiceOrder serviceOrder = (ServiceOrder) signatureArgs[0];
71             ServiceOrderItem serviceOrderItem = (ServiceOrderItem) signatureArgs[1];
72             StateType serviceOrderItemState = (StateType) signatureArgs[2];
73
74             processEvent(
75                     EventFactory.getEvent(EventType.SERVICE_ORDER_ITEM_STATE_CHANGE, serviceOrder, serviceOrderItem));
76         }
77     }
78
79     /**
80      * Retreive subscribers that match an event and fire notification
81      * asynchronously
82      * 
83      * @param event
84      */
85     private void processEvent(Event event) {
86         subscriberRepository.findSubscribersUsingEvent(event).forEach(sub -> notifier.run(sub, event));
87     }
88 }