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