beb14e359a47e6c76009f7ce3977c0dd52837064
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.adapter.netconf.odlconnector;
27
28 import org.apache.http.HttpStatus;
29 import org.onap.appc.adapter.netconf.NetconfClient;
30 import org.onap.appc.adapter.netconf.NetconfClientRestconf;
31 import org.onap.appc.adapter.netconf.NetconfConnectionDetails;
32 import org.onap.appc.adapter.netconf.util.Constants;
33 import org.onap.appc.exceptions.APPCException;
34 import org.onap.appc.util.httpClient;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37
38 import java.util.Properties;
39
40 public class NetconfClientRestconfImpl implements NetconfClient, NetconfClientRestconf {
41
42     private EELFLogger logger = EELFManager.getInstance().getLogger(NetconfClientRestconfImpl.class);
43
44     private NetconfConnectionDetails connectionDetails;
45
46     public NetconfClientRestconfImpl(){
47         //constructor
48     }
49
50     //restconf client impl
51
52     @SuppressWarnings("deprecation")
53     @Override
54     public void configure(String configuration, String deviceMountPointName, String moduleName, String nodeName) throws APPCException {
55
56         logger.info("Configuring device " + deviceMountPointName + " with configuration " + configuration);
57
58         int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,
59                 getModuleConfigurePath(deviceMountPointName, moduleName, nodeName), configuration, "application/json");
60
61         if (httpCode != HttpStatus.SC_OK) {
62             logger.error("Configuration request failed. throwing Exception !");
63             throw new APPCException("Error configuring node :" + nodeName + ", of Module :" + moduleName +
64                     ", in device :" + deviceMountPointName);
65         }
66     }
67
68     @Override
69     public void connect(String deviceMountPointName, String payload) throws APPCException{
70
71         logger.info("Connecting device " + deviceMountPointName);
72
73         int httpCode = httpClient.postMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
74                 getConnectPath(), payload, "application/json");
75
76         if(httpCode != HttpStatus.SC_NO_CONTENT){
77             logger.error("Connect request failed with code " + httpCode + ". throwing Exception !");
78             throw new APPCException("Error connecting device :" + deviceMountPointName);
79         }
80     }
81
82     @Override
83     public boolean checkConnection(String deviceMountPointName) throws APPCException {
84         logger.info("Checking device " + deviceMountPointName + " connectivity");
85
86         String result = httpClient.getMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP,
87                 Constants.CONTROLLER_PORT, getCheckConnectivityPath(deviceMountPointName), "application/json");
88
89         return result != null;
90     }
91
92     @Override
93     public void disconnect(String deviceMountPointName) throws APPCException {
94         logger.info("Disconnecting " + deviceMountPointName);
95
96         int httpCode = httpClient.deleteMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
97                 getDisconnectPath(deviceMountPointName), "application/json");
98
99         if(httpCode != HttpStatus.SC_OK){
100             logger.error("Disconnection of device " + deviceMountPointName + " failed!");
101             throw new APPCException("Disconnection of device " + deviceMountPointName + " failed!");
102         }
103     }
104
105     @Override
106     public String getConfiguration(String deviceMountPointName, String moduleName, String nodeName) throws APPCException{
107         logger.info("Getting configuration of device " + deviceMountPointName);
108
109         String result = httpClient.getMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
110                 getModuleConfigurePath(deviceMountPointName, moduleName, nodeName), "application/json");
111
112         if (result == null) {
113             logger.error("Configuration request failed. throwing Exception !");
114             throw new APPCException("Error getting configuration of node :" + nodeName + ", of Module :" + moduleName +
115                     ", in device :" + deviceMountPointName);
116         }
117
118         return result;
119     }
120
121     //netconf client impl
122
123     @Override
124     public void connect(NetconfConnectionDetails connectionDetails) throws APPCException {
125         if(connectionDetails == null){
126             throw new APPCException("Invalid connection details - null value");
127         }
128         this.connectionDetails = connectionDetails;
129         this.connect(connectionDetails.getHost(), getPayload());
130     }
131
132     @Override
133     public String exchangeMessage(String message) throws APPCException {
134         // TODO implement
135         return null;
136     }
137
138     @Override
139     public void configure(String configuration) throws APPCException {
140         if(connectionDetails == null){
141             throw new APPCException("Invalid connection details - null value");
142         }
143
144         Properties props = connectionDetails.getAdditionalProperties();
145         if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")) {
146             throw new APPCException("Invalid properties!");
147         }
148
149         String moduleName = props.getProperty("module.name");
150         String nodeName = props.getProperty("node.name");
151         String deviceMountPointName = connectionDetails.getHost();
152
153         int httpCode = httpClient.putMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
154                 getModuleConfigurePath(deviceMountPointName, moduleName, nodeName), configuration, "application/xml");
155
156         if (httpCode != HttpStatus.SC_OK) {
157             logger.error("Configuration request failed. throwing Exception !");
158             throw new APPCException("Error configuring node :" + nodeName + ", of Module :" + moduleName +
159                     ", in device :" + deviceMountPointName);
160         }
161     }
162
163     @Override
164     public String getConfiguration() throws APPCException {
165         if(connectionDetails == null){
166             throw new APPCException("Invalid connection details - null value");
167         }
168
169         Properties props = connectionDetails.getAdditionalProperties();
170         if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")) {
171             throw new APPCException("Invalid properties!");
172         }
173
174         return this.getConfiguration(connectionDetails.getHost(), props.getProperty("module.name"),
175                 props.getProperty("node.name"));
176     }
177
178     @Override
179     public void disconnect() throws APPCException {
180         if(connectionDetails == null){
181             throw new APPCException("Invalid connection details - null value");
182         }
183         this.disconnect(connectionDetails.getHost());
184     }
185
186     //private methods
187     private String getModuleConfigurePath(String deviceMountPointName, String moduleName, String nodeName){
188
189         String deviceSpecificPath = deviceMountPointName + "/yang-ext:mount/" + moduleName + ":" + nodeName;
190
191         return Constants.CONFIGURE_PATH + deviceSpecificPath;
192     }
193
194     private String getConnectPath(){
195
196         return Constants.CONNECT_PATH;
197     }
198
199     private String getCheckConnectivityPath(String deviceMountPointName) {
200         return Constants.CHECK_CONNECTION_PATH + deviceMountPointName;
201     }
202
203     private String getDisconnectPath(String deviceMountPointName) {
204         return Constants.DISCONNECT_PATH + deviceMountPointName;
205     }
206
207     private String getPayload() {
208         return "{\n" +
209                 "    \"config:module\":\n" +
210                 "        {\n" +
211                 "        \"type\":\"odl-sal-netconf-connector-cfg:sal-netconf-connector\",\n" +
212                 "        \"netconf-northbound-ssh\\odl-sal-netconf-connector-cfg:name\":"+connectionDetails.getHost()+",\n" +
213                 "        \"odl-sal-netconf-connector-cfg:address\":"+connectionDetails.getHost()+",\n" +
214                 "        \"odl-sal-netconf-connector-cfg:port\":"+connectionDetails.getPort()+",\n" +
215                 "        \"odl-sal-netconf-connector-cfg:username\":"+connectionDetails.getUsername()+",\n" +
216                 "        \"odl-sal-netconf-connector-cfg:password\":"+connectionDetails.getPassword()+",\n" +
217                 "        \"tcp-only\":\"false\",\n" +
218                 "        \"odl-sal-netconf-connector-cfg:event-executor\":\n" +
219                 "            {\n" +
220                 "            \"type\":\"netty:netty-event-executor\",\n" +
221                 "            \"name\":\"global-event-executor\"\n" +
222                 "            },\n" +
223                 "        \"odl-sal-netconf-connector-cfg:binding-registry\":\n" +
224                 "            {\n" +
225                 "            \"type\":\"opendaylight-md-sal-binding:binding-broker-osgi-registry\",\n" +
226                 "            \"name\":\"binding-osgi-broker\"\n" +
227                 "            },\n" +
228                 "        \"odl-sal-netconf-connector-cfg:dom-registry\":\n" +
229                 "            {\n" +
230                 "            \"type\":\"opendaylight-md-sal-dom:dom-broker-osgi-registry\",\n" +
231                 "            \"name\":\"dom-broker\"\n" +
232                 "            },\n" +
233                 "        \"odl-sal-netconf-connector-cfg:client-dispatcher\":\n" +
234                 "            {\n" +
235                 "            \"type\":\"odl-netconf-cfg:netconf-client-dispatcher\",\n" +
236                 "            \"name\":\"global-netconf-dispatcher\"\n" +
237                 "            },\n" +
238                 "        \"odl-sal-netconf-connector-cfg:processing-executor\":\n" +
239                 "            {\n" +
240                 "            \"type\":\"threadpool:threadpool\",\n" +
241                 "            \"name\":\"global-netconf-processing-executor\"\n" +
242                 "        }\n" +
243                 "    }\n" +
244                 "}";
245     }
246 }