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