/*
 * Copyright 2016-present Open Networking Laboratory
 *
 * 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.
 */
package br.ufpa.gercom.mtuldp.store;

import com.sun.javafx.binding.StringFormatter;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.*;
import org.neo4j.driver.internal.value.StringValue;
import org.neo4j.driver.v1.*;
import org.neo4j.driver.v1.summary.ResultSummary;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.net.Device;
import org.onosproject.net.Link;
import org.slf4j.Logger;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * Skeletal ONOS application component.
 */
@Component(immediate = true)
public class MtuldpStoreManager {

    private final Logger log = getLogger(getClass());


    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    private CoreService coreService;


    private ApplicationId appID;

    @Activate
    protected void activate() {

        appID = coreService.getAppId("br.ufpa.gercom.mtuldp.store");

        log.info("Started");
    }

    @Deactivate
    protected void deactivate() {
        log.info("Stopped");
    }


    private class MtuldpStore {

        private Driver driver;
        private Session session;

        MtuldpStore(String neo4j_url) {
            this.driver = GraphDatabase.driver(neo4j_url);
            this.session = driver.session();
        }

        MtuldpStore(String neo4j_url, String user, String pass) {
            this.driver = GraphDatabase.driver(neo4j_url, AuthTokens.basic(user, pass));
            this.session = driver.session();
        }

        private boolean setDevice(Device device) {
            String query = String.format("CREATE (a:%s {type: '%s', device_id: '%s'})",
                    device.type().name(), device.type().name(), device.id().toString());

            StatementResult sresult = session.run(query);

            if (!sresult.hasNext()) {
                return false;
            }
            return true;
        }
        private boolean setLink(Link link){

        }
    }


}


@@ -660,133 +660,116 @@
 ort 
-com.sun.javafx.binding.StringFormatter;%0Aimport org.apache.commons.lang.StringUtils;%0Aimport org.apache.felix.scr.annotations.*
+org.apache.felix.scr.annotations.*;%0Aimport org.neo4j.driver.v1.AuthTokens;%0Aimport org.neo4j.driver.v1.Driver
 ;%0Aim
@@ -794,33 +794,23 @@
 ver.
-internal.value.StringValu
+v1.GraphDatabas
 e;%0Ai
@@ -835,17 +835,23 @@
 iver.v1.
-*
+Session
 ;%0Aimport
@@ -974,24 +974,65 @@
 oreService;%0A
+import org.onosproject.net.ConnectPoint;%0A
 import org.o
@@ -2199,19 +2199,22 @@
 boolean 
-set
+create
 Device(D
@@ -2225,24 +2225,25 @@
 e device) %7B%0A
+%0A
             
@@ -2424,25 +2424,22 @@
     
-StatementResult s
+ResultSummary 
 resu
@@ -2461,16 +2461,26 @@
 n(query)
+.consume()
 ;%0A%0A     
@@ -2494,26 +2494,45 @@
 if (
-!s
 result.
-hasNext()
+counters().nodesCreated() == 0
 ) %7B%0A
@@ -2638,11 +2638,14 @@
 ean 
-set
+create
 Link
@@ -2658,16 +2658,685 @@
 link)%7B%0A%0A
+            ConnectPoint src = link.src();%0A            ConnectPoint dst = link.dst();%0A%0A            String query = String.format(%22MATCH (a %7Bdevice_id:'%25s'%7D), (b %7Bdevice_id:'%25s'%7D) %22 +%0A                            %22MERGE (a)-%5Br:%25s%7Bport_src: %25d, port_dst: %25d, mtu:'null'%7D%5D-%3E(b)%22,%0A                    src.deviceId().toString(), dst.deviceId().toString(), link.type().name(),%0A                    Integer.getInteger(src.port().name()), Integer.getInteger(dst.port().name()));%0A%0A            ResultSummary result = session.run(query).consume();%0A%0A            if (result.counters().relationshipsCreated() == 0)%7B%0A                return false;%0A            %7D%0A%0A            return true;%0A
         

create queries for neo4j