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