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