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