cd242e8ff4418a54238cce7bae2251c1392e77ab
[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 package org.onap.nbi.apis.hub.service;
17
18 import org.aspectj.lang.JoinPoint;
19 import org.aspectj.lang.annotation.AfterReturning;
20 import org.aspectj.lang.annotation.Aspect;
21 import org.onap.nbi.apis.hub.model.Event;
22 import org.onap.nbi.apis.hub.model.EventType;
23 import org.onap.nbi.apis.hub.repository.SubscriberRepository;
24 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
25 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
26 import org.onap.nbi.apis.serviceorder.model.StateType;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.beans.factory.annotation.Configurable;
29 import org.springframework.stereotype.Component;
30
31 @Aspect
32 @Component
33 @Configurable
34 public class NotificationAspect {
35
36     @Autowired
37     private SubscriberRepository subscriberRepository;
38
39     @Autowired
40     private NotifierService notifier;
41
42     @AfterReturning(value = "execution(* org.onap.nbi.apis.serviceorder.service.ServiceOrderService" +
43             ".createServiceOrder(..))", returning = "serviceOrderCreated")
44     public void whenCreateServiceOrder(ServiceOrder serviceOrderCreated) {
45         if(StateType.ACKNOWLEDGED.equals(serviceOrderCreated.getState())) {
46             processEvent(EventFactory.getEvent(EventType.SERVICE_ORDER_CREATION, serviceOrderCreated, null));
47         }
48     }
49
50     @AfterReturning(value = "execution(* org.onap.nbi.apis.serviceorder.service.ServiceOrderService" +
51             ".updateOrderState(..))", returning = "serviceOrderUpdated")
52     public void whenUpdateServiceOrderState(ServiceOrder serviceOrderUpdated) {
53         if(StateType.COMPLETED.equals(serviceOrderUpdated.getState())||
54                 StateType.FAILED.equals(serviceOrderUpdated.getState())) {
55             processEvent(EventFactory.getEvent(EventType.SERVICE_ORDER_STATE_CHANGE, serviceOrderUpdated, null));
56         }
57     }
58
59     @AfterReturning(value = "execution(* org.onap.nbi.apis.serviceorder.service.ServiceOrderService" +
60             ".updateOrderItemState(..))")
61     public void whenUpdateServiceOrderItemState(JoinPoint joinPoint) {
62         Object[] signatureArgs = joinPoint.getArgs();
63
64         if(signatureArgs != null && signatureArgs.length == 3) {
65             ServiceOrder serviceOrder = (ServiceOrder) signatureArgs[0];
66             ServiceOrderItem serviceOrderItem = (ServiceOrderItem) signatureArgs[1];
67             StateType serviceOrderItemState = (StateType) signatureArgs[2];
68
69             processEvent(EventFactory.getEvent(EventType.SERVICE_ORDER_ITEM_STATE_CHANGE, serviceOrder,
70                     serviceOrderItem));
71         }
72     }
73
74     /**
75      * Retreive subscribers that match an event and fire notification
76      * asynchronously
77      * @param event
78      */
79     private void processEvent(Event event) {
80         subscriberRepository
81                 .findSubscribersUsingEvent(event)
82                 .forEach(sub -> notifier.run(sub, event));
83     }
84 }