NLPController.java 15.8 KB
Newer Older
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
package adt.mainnode.controller;

import adt.mainnode.dto.Asset;
import adt.mainnode.dto.PortDto;
import adt.mainnode.identity.VlanIdentity;

import adt.mainnode.entity.*;
import adt.mainnode.identity.PortIdentity;
import adt.mainnode.repository.*;
import adt.mainnode.service.AssetService;
import adt.mainnode.service.InitializeService;
import adt.mainnode.service.EmailService;
import java.util.Date;
import com.fasterxml.jackson.core.JsonProcessingException;
//import org.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.util.JSONPObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.concurrent.Executors;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import adt.mainnode.identity.ScanInfoIdentity;
import adt.mainnode.dto.*;


@RestController
@RequestMapping("nlp")
@Api(
        value = "/nlp",
        produces = MediaType.APPLICATION_JSON_VALUE,
        tags = {"NLP"}
)
@Slf4j
public class NLPController {

    private final AssetService assetService;
    private final VlanRepository vlanRepository;
    private final SnapshotRepository snapshotRepository;
    private final CredentialsRepository credentialsRepository;
    private final ScanInfoRepository scanInfoRepository;
    private final ScanConfigRepository scanConfigRepository;
    private final NodeRepository nodeRepository;
    private final HostRepository hostRepository;
    private final PortRepository portRepository;
    @Value("${main.ip}")
    private String mainIP;
    private final InitializeService initializeService;

    private final EmailService emailService;


    @Autowired
    public NLPController(InitializeService initializeService, EmailService emailService, AssetService assetService, VlanRepository vlanRepository, NodeRepository nodeRepository,ScanConfigRepository scanConfigRepository,SnapshotRepository snapshotRepository, CredentialsRepository credentialsRepository, ScanInfoRepository scanInfoRepository, HostRepository hostRepository, PortRepository portRepository) {

        this.assetService = assetService;
        this.vlanRepository = vlanRepository;
        this.snapshotRepository = snapshotRepository;
        this.credentialsRepository = credentialsRepository;
        this.scanInfoRepository = scanInfoRepository;
        this.scanConfigRepository = scanConfigRepository;
        this.nodeRepository = nodeRepository;
        this.hostRepository = hostRepository;
        this.portRepository = portRepository;
        this.initializeService = initializeService;
        this.emailService = emailService;
    }


    @CrossOrigin
    @ApiOperation(value = "Save a new set of assets", response = JSONObject.class)
    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    JSONObject processInput(HttpServletRequest httpServletRequest,
                            HttpServletResponse response,
                            @RequestBody String plainText,
                            @RequestParam String token) {

        emailService.sendEmail("test");

        Credentials credential = credentialsRepository.findByToken(token);

        log.info("Received NLP request. Token matching response: {}", credential);

        if(credential != null && credential.getStatus().equals("OPERATIVE")) {

            if(plainText.length() == 0){

                try {
                    response.sendError(400, "Query was unspecified. Cannot process this input.");
                }catch(IOException ioe){
                    //
                }

                return null;
            }

            if(credential.getAvailableEndpoint().contains("(POST) nlp/")) {

                boolean fail= false;
                try {

                    PrintWriter writer = new PrintWriter("./curex_NLP_oct2019_v2/text_files_runtagger/net1.txt", "UTF-8");
                    writer.println(plainText);
                    writer.close();

                    String target = new String("python3 curex_NLP_oct2019_v2/main_tag.py");

                    Runtime rt = Runtime.getRuntime();
                    Process proc = rt.exec(target);
                    int exitCode = proc.waitFor();

                    StringBuffer output = new StringBuffer();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

                    String line = "";
                    String nlp_outCode = "";
                    while ((line = reader.readLine()) != null) {
                        output.append(line + "\n");
                        nlp_outCode = line;
                    }

                    if (nlp_outCode.equals("-1")) {

                        try {
                            response.sendError(400, "The format from the input text is invalid and cannot be processed.");
                        } catch (IOException ioe) {
                            //
                        }

                        return null;
                    }


                } catch (Throwable t) {

                    t.printStackTrace();
                    fail = true;
                }
                if(fail == false){
                JSONParser parser = new JSONParser();
                JSONObject jsonObject = null;
                try {
                    Object obj = parser.parse(new FileReader("./curexRDF/result_formatted.json"));

                    jsonObject = (JSONObject) obj;

                    JSONObject mainObj = (JSONObject) jsonObject;

                    String vlan_id = (String) mainObj.get("vlan_id");

                    log.info("NLP processing iteration. Recognized IP: {}", vlan_id);

                    List<ScanConfig> registeredConfs = scanConfigRepository.findAllByIpToScan(vlan_id);

                    log.info("NLP processing iteration. Scanning configuration matching response: {}", registeredConfs);

                    JSONArray devices = (JSONArray) mainObj.get("devices");

                    log.info("NLP processing iteration. Associated devices: {}", devices);

                    if (registeredConfs != null) {

                        List<ScanInfoDto> scanInfoDtos = new ArrayList<>();
                        for (int x = 0; x < registeredConfs.size(); x++) {

                            ScanConfig registeredConf = registeredConfs.get(x);

                            JSONObject firstDevice = (JSONObject) devices.get(0);

                            log.info("NLP processing iteration. Identified processed node as: {}", devices);

                            String node_ip = (String) firstDevice.get("ip_address");

                            // UPDATE JSON'S 'vlan_id' value
                            mainObj.put("vlan_id", registeredConf.getVlanName());

                            Date mainNode_timestamp = new Date();
                            String snapshot_id = (String) mainObj.get("snapshot_id");//DigestUtils.md5Hex(mainNode_timestamp.toString());



                            log.info("NLP processing iteration. The following IP identifies its associated node: {}", node_ip);

                            ///////////////////
                            ScanInfoIdentity scanId = ScanInfoIdentity.createScanInfoIdentity(registeredConf.getVlanName(), (String) mainObj.get("snapshot_id"));

                            ScanInfo scanInfo = ScanInfo.createScanInfo(scanId, node_ip, (String) mainObj.get("timestamp_dist_node_scan_start"), (String) mainObj.get("timestamp_dist_node_scan_end"), mainNode_timestamp);
                            //scanInfoRepository.save(scanInfo);
                            ///////////////////
                            Snapshot snapshot = Snapshot.createSnapshot_nlp(registeredConf.getVlanName(), registeredConf.getNodeId(), snapshot_id, mainNode_timestamp, (String) mainObj.get("timestamp_dist_node_scan_start"), (String) mainObj.get("timestamp_dist_node_scan_end"), "nlp");

                            //snapshotRepository.save(snapshot);


                            Node node = Node.createNode(registeredConf.getNodeId(), null, node_ip, null);

                            //nodeRepository.save(node);

                            VlanIdentity vlanId = VlanIdentity.createVlanIdentity(registeredConf.getNodeId(), registeredConf.getVlanName());

                            Vlan vlan = Vlan.createVlan(vlanId, registeredConf.getDelay(), vlan_id, vlan_id, registeredConf.getMask());

                            //vlanRepository.save(vlan);

                            // HOST CREATION
                            List<HostDto> hostDtos = new ArrayList<>();
                            for (int i = 0; i < devices.size(); i++) {

                                JSONObject device = (JSONObject) devices.get(i);

                                // A HOST'S PORTS CREATION
                                JSONArray ports = (JSONArray) device.get("ports");
                                List<PortDto> portDtos = new ArrayList<>();

                                for (int j = 0; j < ports.size(); j++) {

                                    JSONObject port = (JSONObject) ports.get(j);

                                    PortDto port_toAdd = PortDto.createPortDto_nlp(
                                            (String) port.get("port"),
                                            (String) port.get("service"),
                                            (String) port.get("service_version"),
                                            (String) port.get("protocol"),
                                            (String) port.get("discovery_method"),
                                            (String) port.get("state"));

                                    portDtos.add(port_toAdd);

                                    PortIdentity portId = PortIdentity.createPortIdentity(
                                            (String) port.get("port"),
                                            (String) device.get("ip_address"),
                                            registeredConf.getVlanName(),
                                            snapshot_id);
                                    Port final_portItem = Port.createPort(portId, port_toAdd);

                                    //portRepository.save(final_portItem);
                                }
                                Host host = Host.createHost_nlp(
                                        registeredConf.getVlanName(),
                                        (String) device.get("ip_address"),
                                        (String) device.get("hostname"),
                                        (String) device.get("mac_address"),
                                        (String) device.get("mac_manufacturer"),
                                        (String) device.get("operating_system"),
                                        snapshot_id, portDtos);

                                //hostRepository.save(host);

                                HostDto hostDto = HostDto.createHostDto(host);
                                hostDtos.add(hostDto);

                            }

                            ScanInfoDto scanInfoDto = ScanInfoDto.createScanInfoDto_nlp(registeredConf.getVlanName(), mainNode_timestamp, node_ip,
                                    (String) mainObj.get("timestamp_dist_node_scan_start"), (String) mainObj.get("timestamp_dist_node_scan_end"), hostDtos,
                                    null);

                            scanInfoDtos.add(scanInfoDto);
                        }

                        if (scanInfoDtos.size() > 0) {
                            Asset asset = Asset.createAsset_nlp(scanInfoDtos, "nlp");
                            log.info("check{}", asset);
                            log.info("check2{}", credential);
                            assetService.saveAssets(asset, credentialsRepository.findByName("VLAN2"));
                        }
                        //snapshotRepository.save(snapshot);

                        try {

                            String target = new String("python3 curexRDF/main_json2rdf.py http://138.4.130.6:8890/sparql-auth");

                            Runtime rt = Runtime.getRuntime();
                            Process proc = rt.exec(target);
                            int exitCode = proc.waitFor();

                            StringBuffer output = new StringBuffer();
                            BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));


                            String line = "";
                            String rdf_outCode = "";
                            while ((line = reader.readLine()) != null) {
                                output.append(line + "\n");
                                rdf_outCode = line;
                            }

                            if (rdf_outCode.equals("-1")) {

                                try {
                                    response.sendError(400, "Information has not been stored, it may be duplicated.");
                                } catch (IOException ioe) {
                                    //
                                }

                                return null;
                            }
                            if (rdf_outCode.equals("-2")) {

                                try {
                                    response.sendError(500, "Unable to establish connection with SPARQL endpoint.");
                                } catch (IOException ioe) {
                                    //
                                }

                                return null;
                            }

                        } catch (Throwable t) {

                            t.printStackTrace();
                        }


                    } else {

                        try {
                            response.sendError(400, "You have provided information from an unregistered IP. Please, configure its specifications in the administrator's website.");
                        } catch (IOException ioe) {
                            //
                        }

                        log.error("[NLP request from {}] You have provided information from an unregistered IP. Please, configure its specifications in the administrator's website.", httpServletRequest.getRemoteAddr());

                        return null;
                    }

                    return mainObj;

                } catch (Exception e) {

                    e.printStackTrace();
                } catch (Throwable t) {

                    t.printStackTrace();
                }

            }

            }
        }

        try {
            response.sendError(400, "Unrecognized token. Access denied.");
        }catch(IOException ioe){
            //
        }

        return null;
    }

}