e2e9bc2305bcfdd2efa03c8b0ba46243902f00ed
[ccsdk/sli.git] /
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 import java.util.Properties;
26
27 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.w3c.dom.Document;
34
35 public class ConfigResource implements SvcLogicResource {
36
37     private static final Logger LOG = LoggerFactory.getLogger(ConfigResource.class);
38
39     private RestService restService;
40
41     public ConfigResource(MdsalResourcePropertiesProvider propProvider) {
42         LOG.info("Loading ConfigResource using property provider");
43         Properties props = propProvider.getProperties();
44
45         String sdncUser = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-user", "admin");
46         String sdncPasswd = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-passwd", "admin");
47         String sdncHost = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-host", "localhost");
48         String sdncProtocol = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-protocol", "https");
49         String sdncPort = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-port", "8443");
50
51         restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, "XML", "XML");
52     }
53
54     public ConfigResource(String sdncProtocol, String sdncHost, String sdncPort, String sdncUser, String sdncPasswd)
55     {
56         restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, "XML", "XML");
57     }
58
59     public ConfigResource(RestService restService) {
60         this.restService = restService;
61     }
62
63     @Override
64     public QueryStatus isAvailable(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
65     {
66         return(query(resource, false, null, key, prefix, null, null));
67     }
68
69     @Override
70     public QueryStatus exists(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException
71     {
72         return(query(resource, false, null, key, prefix, null, null));
73     }
74
75     @Override
76     public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix,
77                              String orderBy, SvcLogicContext ctx) throws SvcLogicException {
78         String module = resource;
79         StringBuffer restQuery = new StringBuffer();
80
81         String[] keyParts = key.split("/");
82         for (String keyPart : keyParts) {
83             if (restQuery.length() > 0) {
84                 restQuery.append("/");
85             }
86             if (keyPart.startsWith("$")) {
87                 restQuery.append(ctx.resolve(keyPart.substring(1)));
88             } else {
89                 restQuery.append(keyPart);
90             }
91         }
92
93         String restQueryStr = restQuery.toString();
94         if ((restQueryStr.startsWith("'") && restQueryStr.endsWith("'")) ||
95             (restQueryStr.startsWith("\"") && restQueryStr.endsWith("\""))) {
96             restQueryStr = restQueryStr.substring(1, restQueryStr.length()-1);
97         }
98
99         String urlString = "restconf/config/" + module + ":" + restQueryStr;
100         LOG.info("Querying resource: " + resource + ". At URL: " + urlString);
101
102         Document results = restService.get(urlString);
103         if (results == null) {
104             return(QueryStatus.NOT_FOUND);
105         } else {
106             if (ctx != null) {
107                 ctx.mergeDocument(prefix, results);
108             }
109             return(QueryStatus.SUCCESS);
110         }
111     }
112
113     @Override
114     public QueryStatus reserve(String resource, String select, String key, String prefix,
115                                SvcLogicContext ctx) throws SvcLogicException {
116         return(QueryStatus.SUCCESS);
117
118     }
119
120     @Override
121     public QueryStatus release(String resource, String key, SvcLogicContext ctx) throws SvcLogicException {
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 }