NonRT-RIC A1 Northbound API
[nonrtric.git] / sdnc-a1-controller / oam / SdncReports / SdncReportsApi / src / main / java / com / onap / sdnc / reports / service / CertificationClientService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
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  */
21 package com.onap.sdnc.reports.service;
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.net.InetAddress;
27 import java.net.Socket;
28 import java.net.UnknownHostException;
29 import java.text.SimpleDateFormat;
30 import java.util.Calendar;
31 import java.util.List;
32
33 import org.apache.log4j.Logger;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.google.gson.Gson;
38 import com.onap.sdnc.reports.model.CertificationInputs;
39 import com.onap.sdnc.reports.model.DeviceConfig;
40 import com.onap.sdnc.reports.model.ODLClientResponse;
41 import com.onap.sdnc.reports.model.Output;
42 import com.onap.sdnc.reports.model.PreTestResponse;
43 import com.onap.sdnc.reports.repository.DeviceRepository;
44 import com.onap.sdnc.reports.repository.PreTestConfigRepository;
45
46 @Service
47 public class CertificationClientService {
48
49         private static final Logger logger = Logger.getLogger(CertificationClientService.class);
50
51         
52         @Autowired
53         DeviceRepository deviceRepository;
54         
55         @Autowired
56         PreTestConfigRepository preTestRepo;
57         
58         ObjectMapper mapper = new ObjectMapper();
59
60         public void restClient(CertificationInputs vnfinfo, List<PreTestResponse> preTestNew, String testType) {
61
62                 PreTestResponse preTest = new PreTestResponse();
63
64                 Output output = new Output();
65                 if ("network".equalsIgnoreCase(testType)) {
66                         output = pingTest(vnfinfo);
67                 }
68                 if ("protocol".equalsIgnoreCase(testType)) {
69                         output = protocolTest(vnfinfo);
70                 }
71                 preTest.setStatus(output.getStatus());
72                 preTest.setIpaddress(output.getIpaddress());
73                 preTest.setStatistics(output.getStatistics());
74                 preTest.setAvgTime(output.getAvgTime());
75                 preTest.setTesttype(testType);
76                 preTest.setHostname(output.getHostname());
77                 preTestNew.add(preTest);
78                 
79                 ODLClientResponse odlClientResponse=new ODLClientResponse();
80                 odlClientResponse.setOutput(output);
81                 
82                 testSaveResults(preTest, odlClientResponse);            
83         }
84
85         public static Output pingTest(CertificationInputs vnfinfo) {
86                 
87                 Output output = new Output();
88                 String pingCmd = "ping " + vnfinfo.getInput().getIpaddress();
89                 String pingResult = "";
90                 String testResult = "fail";
91                 String status = "unreachable";
92                 String reason = null;
93                 String timeRes = null;
94                 String percentile = null;
95                 boolean flag = false;
96                 boolean flag1 = false;
97                 try {
98                         InetAddress byIpaddress = InetAddress.getByName(vnfinfo.getInput().getIpaddress());
99                         String byHostName=vnfinfo.getInput().getHostname();                     
100                         flag = byIpaddress.isReachable(5000);
101
102                 } catch (UnknownHostException e) {
103                         logger.info("Network certification Exception : " + e);
104                 } catch (IOException e) {
105                         logger.info("Network certification Exception : " + e);
106                 }
107                 
108                 if (flag ) {
109                         try {
110                                 Runtime r = Runtime.getRuntime();
111                                 Process p = r.exec(pingCmd);
112                                 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
113                                 String inputLine;
114                                 while ((inputLine = in.readLine()) != null) {
115                                         if (pingResult.equals("")) {
116                                                 pingResult = inputLine;
117                                         } else {
118                                                 pingResult += "~" + inputLine;
119                                         }
120                                 }
121                                 String[] results = pingResult.split("~");
122                                 for (String res : results) {
123                                         if (res.trim().contains("Packets:")) {
124                                                 testResult = "pass";
125                                                 status = "reachable";
126                                                 String packets = res.trim();
127                                                 String[] lossPercentile = packets.split("\\(");
128                                                 percentile = lossPercentile[1].replace(")", "").replace(",", "").trim();
129                                         }
130                                         if (res.trim().contains("Minimum")) {
131                                                 String timeMs = res.trim();
132                                                 String[] time = timeMs.split(",");
133                                                 timeRes = time[0];
134                                         }
135                                 }
136                                 in.close();
137                         } catch (Exception e) {
138                                 logger.info("Network certification Exception : " + e);
139                                 testResult = "fail";
140                                 status = "unreachable";
141                                 reason = e.toString();
142                         }
143                 }
144                 output.setAvgTime(timeRes);
145                 output.setStatistics(percentile);
146                 output.setHostname(vnfinfo.getInput().getHostname());
147                 output.setIpaddress(vnfinfo.getInput().getIpaddress());
148                 output.setReason(reason);
149                 output.setTestresult(testResult);
150                 output.setStatus(status);
151
152                 return output;
153         }
154
155         public static Output protocolTest(CertificationInputs vnfinfo) {
156                 Output output = new Output();
157                 Socket s = null;
158                 String status = "unreachable";
159                 String reason = null;
160                 try {
161                         s = new Socket(vnfinfo.getInput().getIpaddress(), Integer.parseInt("445"));
162                         status = "reachable";
163                 } catch (Exception e) {
164                         logger.info("Protocol certification Exception : " + e);
165                         reason = e.toString();
166                         status = "unreachable";
167                 } finally {
168                         if (s != null)
169                                 try {
170                                         s.close();
171                                 } catch (Exception e) {
172                                         logger.info("Protocol certification Exception : " + e);
173                                         reason = e.toString();
174                                         status = "unreachable";
175                                 }
176                 }
177                 output.setStatus(status);
178                 output.setIpaddress(vnfinfo.getInput().getIpaddress());
179                 output.setReason(reason);
180                 
181                 return output;
182         }
183         
184         public void testSaveResults(PreTestResponse preTest,ODLClientResponse output) {
185                 boolean flag=false;
186                 long devId = 1;
187                 
188                 String timeStamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
189                 try {
190                         DeviceConfig devicename = deviceRepository.findDeviceIP(preTest.getIpaddress());
191                         devId = devicename.getId();                     
192                 } catch (Exception e) {
193                         flag=true;                      
194                 }
195                 if(flag) {
196                         deviceRepository.logDeviceName(preTest.getIpaddress(), timeStamp);      
197                 }
198                 
199                 DeviceConfig devicename = deviceRepository.findDeviceIP(preTest.getIpaddress());
200                 devId = devicename.getId();
201                 
202                 Gson gson = new Gson();
203                 String testName= preTest.getTesttype();
204                 String result = preTest.getStatus();
205                 String execuationDetails = gson.toJson(output);
206
207                 preTestRepo.logPreTestReport(testName, result, execuationDetails, timeStamp, devId);
208         }
209
210 }