[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / mdsal-resource / provider / src / main / java / org / openecomp / sdnc / sli / resource / mdsal / ConfigResource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP 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.openecomp.sdnc.sli.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         @Override
46         public QueryStatus isAvailable(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
47         {
48                 return(query(resource, false, null, key, prefix, null, null));
49         }
50
51         @Override
52         public QueryStatus exists(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
53         {
54
55                 return(query(resource, false, null, key, prefix, null, null));
56
57         }
58
59         @Override
60         public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix,
61                         String orderBy, SvcLogicContext ctx) throws SvcLogicException {
62
63
64                 String module = resource;
65                 StringBuffer restQuery = new StringBuffer();
66
67                 String[] keyParts = key.split("/");
68
69                 for (String keyPart : keyParts) {
70                         if (restQuery.length() > 0) {
71                                 restQuery.append("/");
72                         }
73                         if (keyPart.startsWith("$")) {
74
75                                 restQuery.append(ctx.resolve(keyPart.substring(1)));
76                         } else {
77                                 restQuery.append(keyPart);
78                         }
79                 }
80
81                 String restQueryStr = restQuery.toString();
82                 if ((restQueryStr.startsWith("'") && restQueryStr.endsWith("'")) ||
83                                 (restQueryStr.startsWith("\"") && restQueryStr.endsWith("\""))) {
84                         restQueryStr = restQueryStr.substring(1, restQueryStr.length()-1);
85                 }
86
87                 String urlString = "restconf/config/" + module + ":" + restQueryStr;
88
89                 LOG.info("Querying resource: " + resource + ". At URL: " + urlString);
90
91                 Document results = restService.get(urlString);
92
93
94                 if (results == null) {
95                         return(QueryStatus.NOT_FOUND);
96                 } else {
97
98                         if (ctx != null) {
99                                 ctx.mergeDocument(prefix, results);
100                         }
101                         return(QueryStatus.SUCCESS);
102                 }
103
104         }
105
106         @Override
107         public QueryStatus reserve(String resource, String select, String key, String prefix,
108                         SvcLogicContext ctx) throws SvcLogicException {
109
110
111                 return(QueryStatus.SUCCESS);
112
113         }
114
115         @Override
116         public QueryStatus release(String resource, String key, SvcLogicContext ctx) throws SvcLogicException {
117
118                 return(QueryStatus.SUCCESS);
119         }
120
121         @Override
122         public QueryStatus delete(String arg0, String arg1, SvcLogicContext arg2)
123                         throws SvcLogicException {
124                 // TODO Auto-generated method stub
125                 return(QueryStatus.SUCCESS);
126         }
127
128         @Override
129         public QueryStatus save(String arg0, boolean arg1, boolean localOnly, String arg2,
130                         Map<String, String> arg3, String arg4, SvcLogicContext arg5)
131                         throws SvcLogicException {
132                 // TODO Auto-generated method stub
133                 return(QueryStatus.SUCCESS);
134         }
135
136         @Override
137         public QueryStatus notify(String resource, String action, String key,
138                         SvcLogicContext ctx) throws SvcLogicException {
139                 return(QueryStatus.SUCCESS);
140         }
141
142
143         public QueryStatus update(String resource, String key,
144                         Map<String, String> parms, String prefix, SvcLogicContext ctx)
145                         throws SvcLogicException {
146                 return(QueryStatus.SUCCESS);
147         }
148
149
150
151 }