Release version 1.1.0 of sli/plugins
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / restconfdiscovery / EventProcessor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CCSDK
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6  *
7  * Modifications Copyright © 2018 IBM
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.ccsdk.sli.plugins.restconfdiscovery;
24
25 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
26 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
27 import org.slf4j.Logger;
28
29 import java.util.Map;
30
31 import static org.onap.ccsdk.sli.plugins.restapicall.JsonParser.convertToProperties;
32 import static org.slf4j.LoggerFactory.getLogger;
33
34 /**
35  * Processes the events from event queue and executes callback DG.
36  */
37 class EventProcessor implements Runnable {
38
39     private static final Logger log = getLogger(EventProcessor.class);
40     private RestconfDiscoveryNode node;
41
42     private static final String EVENT_SUBSCRIPTION_ID = "notification." +
43             "push-change-update.subscription-id";
44
45     public EventProcessor(RestconfDiscoveryNode node) {
46         this.node = node;
47     }
48
49     @Override
50     public void run() {
51         while(true) {
52             try {
53                 String payload = node.eventQueue().take();
54                 Map<String, String> param = convertToProperties(payload);
55                 String id = param.get(EVENT_SUBSCRIPTION_ID);
56                 SubscriptionInfo info = node.subscriptionInfoMap().get(id);
57                 if (info != null) {
58                     SvcLogicContext ctx = setContext(param);
59                     SvcLogicGraphInfo callbackDG = info.callBackDG();
60                     callbackDG.executeGraph(ctx);
61                 }
62             } catch (InterruptedException | SvcLogicException e) {
63                 log.error("Interrupted!", e);
64                 Thread.currentThread().interrupt();
65             }
66         }
67     }
68
69     private SvcLogicContext setContext(Map<String, String> param) {
70         SvcLogicContext ctx = new SvcLogicContext();
71         for (Map.Entry<String, String> entry : param.entrySet()) {
72             ctx.setAttribute(entry.getKey(), entry.getValue());
73         }
74         return ctx;
75     }
76 }