Issue fixes for Restconf discovery node
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / restconfdiscovery / SvcLogicGraphInfo.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.onap.ccsdk.sli.core.sli.SvcLogicGraph;
26 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
27 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
28 import org.osgi.framework.Bundle;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.FrameworkUtil;
31 import org.osgi.framework.ServiceReference;
32
33 /**
34  * Holder to store callback directed graph info.
35  */
36 class SvcLogicGraphInfo {
37     private String module;
38     private String rpc;
39     private String mode;
40     private String version;
41
42     /**
43      * Creates an instance of SvcLogicGraphInfo.
44      *
45      * @param module module name of callback DG
46      * @param rpc rpc name of callback DG
47      * @param mode mode of callback DG
48      * @param version version of callback DG
49      */
50     public SvcLogicGraphInfo(String module, String rpc, String mode, String version) {
51         this.module = module;
52         this.rpc = rpc;
53         this.mode = mode;
54         this.version = version;
55     }
56
57     public SvcLogicGraphInfo() {}
58
59     /**
60      * Returns module name of callback DG.
61      *
62      * @return module name of callback DG
63      */
64     public String module() {
65         return module;
66     }
67
68     /**
69      * Sets module of callback DG.
70      *
71      * @param module module name of the DG
72      */
73     public void module(String module) {
74         this.module = module;
75     }
76
77     /**
78      * Returns rpc of callback DG.
79      *
80      * @return rpc of callback DG
81      */
82     public String rpc() {
83         return rpc;
84     }
85
86     /**
87      * Sets rpc of callback DG.
88      *
89      * @param rpc rpc attribute of the DG
90      */
91     public void rpc(String rpc) {
92         this.rpc = rpc;
93     }
94
95     /**
96      * Returns mode of callback DG.
97      *
98      * @return mode of callback DG
99      */
100     public String mode() {
101         return mode;
102     }
103
104     /**
105      * Sets mode of DG.
106      *
107      * @param mode mode of the DG
108      */
109     public void mode(String mode) {
110         this.mode = mode;
111     }
112
113     /**
114      * Returns version of callback DG.
115      *
116      * @return version of callback DG
117      */
118     public String version() {
119         return version;
120     }
121
122     /**
123      * Sets version of DG.
124      *
125      * @param version version of the DG
126      */
127     public void version(String version) {
128         this.version = version;
129     }
130
131     /**
132      * Executes call back DG.
133      *
134      * @param ctx service logic context
135      * @throws SvcLogicException service logic error
136      */
137     public void executeGraph(SvcLogicContext ctx) throws SvcLogicException {
138         SvcLogicService service = findSvcLogicService();
139         if (service == null) {
140             throw new SvcLogicException("\"Could not get SvcLogicService reference\"");
141         }
142
143         SvcLogicStore store = service.getStore();
144         if (store != null) {
145             SvcLogicGraph subGraph = store.fetch(module, rpc, mode, version);
146             if (subGraph != null) {
147                 ctx.setAttribute("subGraph", subGraph.toString());
148                 service.execute(subGraph, ctx);
149             } else {
150                 throw new SvcLogicException("Failed to call child [" + module +
151                                                     "," + rpc + "," + version +
152                                                     "," + mode + "] because" +
153                                                     " the" + " graph could" +
154                                                     " not be found");
155             }
156         } else {
157             throw new SvcLogicException("\"Could not get SvcLogicStore reference\"");
158         }
159     }
160
161     private static SvcLogicService findSvcLogicService() throws SvcLogicException {
162         Bundle bundle = FrameworkUtil.getBundle(SvcLogicService.class);
163         if (bundle == null) {
164             throw new SvcLogicException("Cannot find bundle reference for "
165                                                 + SvcLogicService.NAME);
166         }
167
168         BundleContext bctx = bundle.getBundleContext();
169         ServiceReference<SvcLogicService> sref = bctx.getServiceReference(
170                 SvcLogicService.class);
171         if (sref  != null) {
172             return bctx.getService(sref);
173         } else {
174             throw new SvcLogicException("Cannot find service reference for "
175                                                 + SvcLogicService.NAME);
176         }
177     }
178 }