<commit_before>// Start of user code Copyright
/*******************************************************************************
 * Copyright (c) 2011, 2012 IBM Corporation and others.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 *  
 *  The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *  and the Eclipse Distribution License is available at
 *  http://www.eclipse.org/org/documents/edl-v10.php.
 *  
 *  Contributors:
 *  
 *	   <NAME>	       - initial API and implementation
 *     <NAME>     - adapted for OSLC4J
 *     <NAME>        - initial implementation of code generator (https://bugs.eclipse.org/bugs/show_bug.cgi?id=422448)
 *     <NAME>   - Support for multiple Service Providers.
 *     <NAME>       - Support for multiple Service Providers.
 *
 * This file is generated by org.eclipse.lyo.oslc4j.codegenerator
 *******************************************************************************/
// End of user code

package se.ericsson.cf.scott.sandbox.twin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletContextEvent;

import eu.scott.warehouse.domains.mission.AgentRequest;
import eu.scott.warehouse.domains.mission.Mission;
import eu.scott.warehouse.domains.pddl.PlanExecutionResult;

// Start of user code imports
import org.eclipse.paho.client.mqttv3.MqttException;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import se.ericsson.cf.scott.sandbox.twin.clients.TwinRegistrationClient;
import se.ericsson.cf.scott.sandbox.twin.trs.TrsMqttClientManager;
import se.ericsson.cf.scott.sandbox.twin.trs.TwinAckRegistrationAgent;

//import se.ericsson.cf.scott.sandbox.twin.ros.RobotClientNode;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import eu.scott.warehouse.lib.MqttClientBuilder;
import eu.scott.warehouse.lib.MqttTopics;
import eu.scott.warehouse.lib.TrsMqttGateway;
import java.util.UUID;
import javax.servlet.ServletContext;

import org.eclipse.lyo.client.oslc.OslcClient;
import org.eclipse.lyo.store.Store;

// End of user code

// Start of user code pre_class_code
// End of user code

public class RobotTwinManager {

    // Start of user code class_attributes
    public final static String PACKAGE_ROOT = RobotTwinManager.class.getPackage().getName();
    private final static Logger log = LoggerFactory.getLogger(RobotTwinManager.class);
    private final static UUID uuid = UUID.randomUUID();
    private static String trsTopic;
    private static TrsMqttClientManager trsClientManager;
    private static Store store;
    private static ServletContext servletContext;
    private static TrsMqttGateway mqttGateway;
    // End of user code
    
    
    // Start of user code class_methods
    @NotNull
    public static String getTwinUUID() {
        return "twn-" + uuid.toString();
    }

    public static String getTrsTopic() {
        if(Strings.isNullOrEmpty(trsTopic)) {
            log.warn("The TRS topic was requested before it was set");
        }
        return trsTopic;
    }

    public static void setTrsTopic(String trsTopic) {
        RobotTwinManager.trsTopic = trsTopic;
    }

    public static TrsMqttClientManager getTrsClientManager() {
        return trsClientManager;
    }

    public static void setTrsClientManager(TrsMqttClientManager trsClientManager) {
        RobotTwinManager.trsClientManager = trsClientManager;
    }

    public static ServletContext getServletContext() {
        return servletContext;
    }

    private static void registerTwins() {
        final OslcClient client = new OslcClient();
        final TwinRegistrationClient registrationClient = new TwinRegistrationClient(
            client, "http://sandbox-whc:8080/services/service2/registrationRequests/register");

        for(String id: ImmutableList.of("r1", "r2", "r3")) {
            registrationClient.registerTwin("robot", id);
        }

    }

    private static void setStore(final Store store) {
        RobotTwinManager.store = store;
    }
    // End of user code

    public static void contextInitializeServletListener(final ServletContextEvent servletContextEvent)
    {
        
        // Start of user code contextInitializeServletListener
        log.info("Twin {} is starting", getTwinUUID());
        servletContext = servletContextEvent.getServletContext();
//        final Store store = LyoStoreManager.initLyoStore();
//        setStore(store);

//        RosManager.runRosNode();
//        new Thread(RobotTwinManager::runRosNode).run();
//        RosManager.execMainNode();

        final String mqttBroker = AdaptorHelper.p("trs.mqtt.broker");
//        final TrsMqttClientManager trsClientManager = new TrsMqttClientManager(mqttBroker);
//        setTrsClientManager(trsClientManager);
//        new Thread(trsClientManager::connectAndSubscribeToPlans).run();
        // FIXME Andrew@2018-07-31: remove non-gateway based code
        try {
            mqttGateway = new MqttClientBuilder().withBroker(mqttBroker)
                                                 .withId(getTwinUUID())
                                                 .withRegistration(new TwinAckRegistrationAgent(
                                                         MqttTopics.WHC_PLANS))
                                                 .build();
        } catch (MqttException e) {
            log.error("Failed to initialise the MQTT gateway", e);
        }

        registerTwins();
        // End of user code
    }

    public static void contextDestroyServletListener(ServletContextEvent servletContextEvent) <commit_msg>Initialise SP info HC maps<commit_after>// Start of user code Copyright
/*******************************************************************************
 * Copyright (c) 2011, 2012 IBM Corporation and others.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 *  
 *  The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *  and the Eclipse Distribution License is available at
 *  http://www.eclipse.org/org/documents/edl-v10.php.
 *  
 *  Contributors:
 *  
 *	   <NAME>	       - initial API and implementation
 *     <NAME>     - adapted for OSLC4J
 *     <NAME>        - initial implementation of code generator (https://bugs.eclipse.org/bugs/show_bug.cgi?id=422448)
 *     <NAME>   - Support for multiple Service Providers.
 *     <NAME>       - Support for multiple Service Providers.
 *
 * This file is generated by org.eclipse.lyo.oslc4j.codegenerator
 *******************************************************************************/
// End of user code

package se.ericsson.cf.scott.sandbox.twin;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import eu.scott.warehouse.lib.hazelcast.HazelcastFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletContextEvent;

import eu.scott.warehouse.domains.mission.AgentRequest;
import eu.scott.warehouse.domains.mission.Mission;
import eu.scott.warehouse.domains.pddl.PlanExecutionResult;

// Start of user code imports
import org.eclipse.paho.client.mqttv3.MqttException;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import se.ericsson.cf.scott.sandbox.twin.clients.TwinRegistrationClient;
import se.ericsson.cf.scott.sandbox.twin.trs.TrsMqttClientManager;
import se.ericsson.cf.scott.sandbox.twin.trs.TwinAckRegistrationAgent;

//import se.ericsson.cf.scott.sandbox.twin.ros.RobotClientNode;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import eu.scott.warehouse.lib.MqttClientBuilder;
import eu.scott.warehouse.lib.MqttTopics;
import eu.scott.warehouse.lib.TrsMqttGateway;
import java.util.UUID;
import javax.servlet.ServletContext;

import org.eclipse.lyo.client.oslc.OslcClient;
import org.eclipse.lyo.store.Store;

// End of user code

// Start of user code pre_class_code
// End of user code

public class RobotTwinManager {

    // Start of user code class_attributes
    public final static String PACKAGE_ROOT = RobotTwinManager.class.getPackage().getName();
    private final static Logger log = LoggerFactory.getLogger(RobotTwinManager.class);
    private final static UUID uuid = UUID.randomUUID();
    private static String trsTopic;
    private static TrsMqttClientManager trsClientManager;
    private static Store store;
    private static ServletContext servletContext;
    private static TrsMqttGateway mqttGateway;
    private static HazelcastInstance hc;
    private static IMap<String, Object> robotProviders;
    private static IMap<Object, Object> shelfProviders;
    private static IMap<Object, Object> beltProviders;
    // End of user code
    
    
    // Start of user code class_methods
    @NotNull
    public static String getTwinUUID() {
        return "twn-" + uuid.toString();
    }

    public static String getTrsTopic() {
        if(Strings.isNullOrEmpty(trsTopic)) {
            log.warn("The TRS topic was requested before it was set");
        }
        return trsTopic;
    }

    public static void setTrsTopic(String trsTopic) {
        RobotTwinManager.trsTopic = trsTopic;
    }

    public static TrsMqttClientManager getTrsClientManager() {
        return trsClientManager;
    }

    public static void setTrsClientManager(TrsMqttClientManager trsClientManager) {
        RobotTwinManager.trsClientManager = trsClientManager;
    }

    public static ServletContext getServletContext() {
        return servletContext;
    }

    private static void registerTwins() {
        final OslcClient client = new OslcClient();
        final TwinRegistrationClient registrationClient = new TwinRegistrationClient(
            client, "http://sandbox-whc:8080/services/service2/registrationRequests/register");

        for(String id: ImmutableList.of("r1", "r2", "r3")) {
            registrationClient.registerTwin("robot", id);
        }

    }

    private static void setStore(final Store store) {
        RobotTwinManager.store = store;
    }
    // End of user code

    public static void contextInitializeServletListener(final ServletContextEvent servletContextEvent)
    {
        
        // Start of user code contextInitializeServletListener
        log.info("Twin {} is starting", getTwinUUID());
        servletContext = servletContextEvent.getServletContext();
//        final Store store = LyoStoreManager.initLyoStore();
//        setStore(store);

//        RosManager.runRosNode();
//        new Thread(RobotTwinManager::runRosNode).run();
//        RosManager.execMainNode();

        final String mqttBroker = AdaptorHelper.p("trs.mqtt.broker");
//        final TrsMqttClientManager trsClientManager = new TrsMqttClientManager(mqttBroker);
//        setTrsClientManager(trsClientManager);
//        new Thread(trsClientManager::connectAndSubscribeToPlans).run();
        // FIXME Andrew@2018-07-31: remove non-gateway based code
        try {
            mqttGateway = new MqttClientBuilder().withBroker(mqttBroker)
                                                 .withId(getTwinUUID())
                                                 .withRegistration(new TwinAckRegistrationAgent(
                                                         MqttTopics.WHC_PLANS))
                                                 .build();
        } catch (MqttException e) {
            log.error("Failed to initialise the MQTT gateway", e);
        }

        hc = HazelcastFactory.INSTANCE.instanceFromDefaultXmlConfig("twin");

        robotProviders = hc.getMap("twin-providers-robot");
        shelfProviders = hc.getMap("twin-providers-shelf");
        beltProviders = hc.getMap("twin-providers-belt");

        registerTwins();
        // End of user code
    }

    public static void contextDestroyServletListener(ServletContextEvent servletContextEvent) 
{'fraction_non_alphanumeric': 0.11240911690221388, 'fraction_numerical': 0.007025569806388367, 'mean_word_length': 3.0563286944996686, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5633386', 'n_tokens_mistral': 3581, 'n_tokens_neox': 3449, 'n_words': 793}