aboutsummaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-netconf-adapter/appc-netconf-adapter-bundle/src/main/java/org/onap/appc/adapter/netconf/odlconnector/NetconfClientRestconfImpl.java
blob: beb14e359a47e6c76009f7ce3977c0dd52837064 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * ================================================================================
 * Modifications Copyright (C) 2019 Ericsson
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.adapter.netconf.odlconnector;

import org.apache.http.HttpStatus;
import org.onap.appc.adapter.netconf.NetconfClient;
import org.onap.appc.adapter.netconf.NetconfClientRestconf;
import org.onap.appc.adapter.netconf.NetconfConnectionDetails;
import org.onap.appc.adapter.netconf.util.Constants;
import org.onap.appc.exceptions.APPCException;
import org.onap.appc.util.httpClient;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

import java.util.Properties;

public class NetconfClientRestconfImpl implements NetconfClient, NetconfClientRestconf {

    private EELFLogger logger = EELFManager.getInstance().getLogger(NetconfClientRestconfImpl.class);

    private NetconfConnectionDetails connectionDetails;

    public NetconfClientRestconfImpl(){
        //constructor
    }

    //restconf client impl

    @SuppressWarnings("deprecation")
    @Override
    public void configure(String configuration, String deviceMountPointName, String moduleName, String nodeName) throws APPCException {

        logger.info("Configuring device " + deviceMountPointName + " with configuration " + configuration);

        int httpCode = httpClient.putMethod(Constants.PROTOCOL,Constants.CONTROLLER_IP,Constants.CONTROLLER_PORT,
                getModuleConfigurePath(deviceMountPointName, moduleName, nodeName), configuration, "application/json");

        if (httpCode != HttpStatus.SC_OK) {
            logger.error("Configuration request failed. throwing Exception !");
            throw new APPCException("Error configuring node :" + nodeName + ", of Module :" + moduleName +
                    ", in device :" + deviceMountPointName);
        }
    }

    @Override
    public void connect(String deviceMountPointName, String payload) throws APPCException{

        logger.info("Connecting device " + deviceMountPointName);

        int httpCode = httpClient.postMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
                getConnectPath(), payload, "application/json");

        if(httpCode != HttpStatus.SC_NO_CONTENT){
            logger.error("Connect request failed with code " + httpCode + ". throwing Exception !");
            throw new APPCException("Error connecting device :" + deviceMountPointName);
        }
    }

    @Override
    public boolean checkConnection(String deviceMountPointName) throws APPCException {
        logger.info("Checking device " + deviceMountPointName + " connectivity");

        String result = httpClient.getMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP,
                Constants.CONTROLLER_PORT, getCheckConnectivityPath(deviceMountPointName), "application/json");

        return result != null;
    }

    @Override
    public void disconnect(String deviceMountPointName) throws APPCException {
        logger.info("Disconnecting " + deviceMountPointName);

        int httpCode = httpClient.deleteMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
                getDisconnectPath(deviceMountPointName), "application/json");

        if(httpCode != HttpStatus.SC_OK){
            logger.error("Disconnection of device " + deviceMountPointName + " failed!");
            throw new APPCException("Disconnection of device " + deviceMountPointName + " failed!");
        }
    }

    @Override
    public String getConfiguration(String deviceMountPointName, String moduleName, String nodeName) throws APPCException{
        logger.info("Getting configuration of device " + deviceMountPointName);

        String result = httpClient.getMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
                getModuleConfigurePath(deviceMountPointName, moduleName, nodeName), "application/json");

        if (result == null) {
            logger.error("Configuration request failed. throwing Exception !");
            throw new APPCException("Error getting configuration of node :" + nodeName + ", of Module :" + moduleName +
                    ", in device :" + deviceMountPointName);
        }

        return result;
    }

    //netconf client impl

    @Override
    public void connect(NetconfConnectionDetails connectionDetails) throws APPCException {
        if(connectionDetails == null){
            throw new APPCException("Invalid connection details - null value");
        }
        this.connectionDetails = connectionDetails;
        this.connect(connectionDetails.getHost(), getPayload());
    }

    @Override
    public String exchangeMessage(String message) throws APPCException {
        // TODO implement
        return null;
    }

    @Override
    public void configure(String configuration) throws APPCException {
        if(connectionDetails == null){
            throw new APPCException("Invalid connection details - null value");
        }

        Properties props = connectionDetails.getAdditionalProperties();
        if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")) {
            throw new APPCException("Invalid properties!");
        }

        String moduleName = props.getProperty("module.name");
        String nodeName = props.getProperty("node.name");
        String deviceMountPointName = connectionDetails.getHost();

        int httpCode = httpClient.putMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
                getModuleConfigurePath(deviceMountPointName, moduleName, nodeName), configuration, "application/xml");

        if (httpCode != HttpStatus.SC_OK) {
            logger.error("Configuration request failed. throwing Exception !");
            throw new APPCException("Error configuring node :" + nodeName + ", of Module :" + moduleName +
                    ", in device :" + deviceMountPointName);
        }
    }

    @Override
    public String getConfiguration() throws APPCException {
        if(connectionDetails == null){
            throw new APPCException("Invalid connection details - null value");
        }

        Properties props = connectionDetails.getAdditionalProperties();
        if(props == null || !props.containsKey("module.name") || !props.containsKey("node.name")) {
            throw new APPCException("Invalid properties!");
        }

        return this.getConfiguration(connectionDetails.getHost(), props.getProperty("module.name"),
                props.getProperty("node.name"));
    }

    @Override
    public void disconnect() throws APPCException {
        if(connectionDetails == null){
            throw new APPCException("Invalid connection details - null value");
        }
        this.disconnect(connectionDetails.getHost());
    }

    //private methods
    private String getModuleConfigurePath(String deviceMountPointName, String moduleName, String nodeName){

        String deviceSpecificPath = deviceMountPointName + "/yang-ext:mount/" + moduleName + ":" + nodeName;

        return Constants.CONFIGURE_PATH + deviceSpecificPath;
    }

    private String getConnectPath(){

        return Constants.CONNECT_PATH;
    }

    private String getCheckConnectivityPath(String deviceMountPointName) {
        return Constants.CHECK_CONNECTION_PATH + deviceMountPointName;
    }

    private String getDisconnectPath(String deviceMountPointName) {
        return Constants.DISCONNECT_PATH + deviceMountPointName;
    }

    private String getPayload() {
        return "{\n" +
                "    \"config:module\":\n" +
                "        {\n" +
                "        \"type\":\"odl-sal-netconf-connector-cfg:sal-netconf-connector\",\n" +
                "        \"netconf-northbound-ssh\\odl-sal-netconf-connector-cfg:name\":"+connectionDetails.getHost()+",\n" +
                "        \"odl-sal-netconf-connector-cfg:address\":"+connectionDetails.getHost()+",\n" +
                "        \"odl-sal-netconf-connector-cfg:port\":"+connectionDetails.getPort()+",\n" +
                "        \"odl-sal-netconf-connector-cfg:username\":"+connectionDetails.getUsername()+",\n" +
                "        \"odl-sal-netconf-connector-cfg:password\":"+connectionDetails.getPassword()+",\n" +
                "        \"tcp-only\":\"false\",\n" +
                "        \"odl-sal-netconf-connector-cfg:event-executor\":\n" +
                "            {\n" +
                "            \"type\":\"netty:netty-event-executor\",\n" +
                "            \"name\":\"global-event-executor\"\n" +
                "            },\n" +
                "        \"odl-sal-netconf-connector-cfg:binding-registry\":\n" +
                "            {\n" +
                "            \"type\":\"opendaylight-md-sal-binding:binding-broker-osgi-registry\",\n" +
                "            \"name\":\"binding-osgi-broker\"\n" +
                "            },\n" +
                "        \"odl-sal-netconf-connector-cfg:dom-registry\":\n" +
                "            {\n" +
                "            \"type\":\"opendaylight-md-sal-dom:dom-broker-osgi-registry\",\n" +
                "            \"name\":\"dom-broker\"\n" +
                "            },\n" +
                "        \"odl-sal-netconf-connector-cfg:client-dispatcher\":\n" +
                "            {\n" +
                "            \"type\":\"odl-netconf-cfg:netconf-client-dispatcher\",\n" +
                "            \"name\":\"global-netconf-dispatcher\"\n" +
                "            },\n" +
                "        \"odl-sal-netconf-connector-cfg:processing-executor\":\n" +
                "            {\n" +
                "            \"type\":\"threadpool:threadpool\",\n" +
                "            \"name\":\"global-netconf-processing-executor\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
    }
}