d02530fe602bb960cb418943f73a600e43416e0e
[ccsdk/sli/adaptors.git] / mdsal-resource / provider / src / main / java / org / onap / ccsdk / sli / adaptors / resource / mdsal / ConfigResource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.adaptors.resource.mdsal;
23
24 import java.util.Map;
25
26 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
27 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Document;
33
34 public class ConfigResource implements SvcLogicResource {
35
36     private static final Logger LOG = LoggerFactory.getLogger(ConfigResource.class);
37
38     private RestService restService;
39
40     public ConfigResource(String sdncProtocol, String sdncHost, String sdncPort, String sdncUser, String sdncPasswd)
41     {
42         restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, RestService.PayloadType.XML);
43     }
44
45     public ConfigResource(RestService restService) {
46                 this.restService = restService;
47     }
48
49     @Override
50     public QueryStatus isAvailable(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
51     {
52         return(query(resource, false, null, key, prefix, null, null));
53     }
54
55     @Override
56     public QueryStatus exists(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
57     {
58
59         return(query(resource, false, null, key, prefix, null, null));
60
61     }
62
63     @Override
64     public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix,
65             String orderBy, SvcLogicContext ctx) throws SvcLogicException {
66
67
68         String module = resource;
69         StringBuffer restQuery = new StringBuffer();
70
71         String[] keyParts = key.split("/");
72
73         for (String keyPart : keyParts) {
74             if (restQuery.length() > 0) {
75                 restQuery.append("/");
76             }
77             if (keyPart.startsWith("$")) {
78
79                 restQuery.append(ctx.resolve(keyPart.substring(1)));
80             } else {
81                 restQuery.append(keyPart);
82             }
83         }
84
85         String restQueryStr = restQuery.toString();
86         if ((restQueryStr.startsWith("'") && restQueryStr.endsWith("'")) ||
87                 (restQueryStr.startsWith("\"") && restQueryStr.endsWith("\""))) {
88             restQueryStr = restQueryStr.substring(1, restQueryStr.length()-1);
89         }
90
91         String urlString = "restconf/config/" + module + ":" + restQueryStr;
92
93                 LOG.info("Querying resource: " + resource + ". At URL: " + urlString);
94
95         Document results = restService.get(urlString);
96
97
98         if (results == null) {
99             return(QueryStatus.NOT_FOUND);
100         } else {
101
102             if (ctx != null) {
103                 ctx.mergeDocument(prefix, results);
104             }
105             return(QueryStatus.SUCCESS);
106         }
107
108     }
109
110     @Override
111     public QueryStatus reserve(String resource, String select, String key, String prefix,
112             SvcLogicContext ctx) throws SvcLogicException {
113
114
115         return(QueryStatus.SUCCESS);
116
117     }
118
119     @Override
120     public QueryStatus release(String resource, String key, SvcLogicContext ctx) throws SvcLogicException {
121
122         return(QueryStatus.SUCCESS);
123     }
124
125     @Override
126     public QueryStatus delete(String arg0, String arg1, SvcLogicContext arg2)
127             throws SvcLogicException {
128         // TODO Auto-generated method stub
129         return(QueryStatus.SUCCESS);
130     }
131
132     @Override
133     public QueryStatus save(String arg0, boolean arg1, boolean localOnly, String arg2,
134             Map<String, String> arg3, String arg4, SvcLogicContext arg5)
135             throws SvcLogicException {
136         // TODO Auto-generated method stub
137         return(QueryStatus.SUCCESS);
138     }
139
140     @Override
141     public QueryStatus notify(String resource, String action, String key,
142             SvcLogicContext ctx) throws SvcLogicException {
143         return(QueryStatus.SUCCESS);
144     }
145
146
147     public QueryStatus update(String resource, String key,
148             Map<String, String> parms, String prefix, SvcLogicContext ctx)
149             throws SvcLogicException {
150         return(QueryStatus.SUCCESS);
151     }
152
153
154
155 }