<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.stratos.gce.extension;

import com.google.api.services.compute.model.TargetPool;
import org.apache.stratos.load.balancer.common.domain.*;
import org.apache.stratos.load.balancer.extension.api.LoadBalancer;
import org.apache.stratos.load.balancer.extension.api.exception.LoadBalancerExtensionException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class GCELoadBalancer implements LoadBalancer {

    private static final Log log = LogFactory.getLog(GCELoadBalancer.class);

    private GCEOperations gceOperations;
    /**
     * We create one load balancer per cluster(stratos side).
     * A Load balancer has one target pool and set of forwarding rules
     * This hash map is used to hold cluster IDs and corresponding Load balancer configuration
     * So one cluster will have one loadBalancerConfiguration object
     */
    private HashMap<String, LoadBalancerConfiguration> clusterToLoadBalancerConfigurationMap;

    public GCELoadBalancer() {
        try {
            gceOperations = new GCEOperations();
            clusterToLoadBalancerConfigurationMap = new HashMap<String, LoadBalancerConfiguration>();

        } catch (LoadBalancerExtensionException e) {
            log.error(e);
        } catch (GeneralSecurityException e) {
            log.error(e);
        } catch (IOException e) {
            log.error(e);
        }
    }

    @Override
    public void start() throws LoadBalancerExtensionException {
        log.info("Starting GCE Load balancer instance");

        //iterate through clusterToLoadBalancerConfigurationMap
        //for each cluster{
        //if Load balancer status == not running -->
        //      execute commands through GCE API for start the load balancer
        //}

        log.info("GCE Load balancer instance started");


    }

    @Override
    public void stop() throws LoadBalancerExtensionException {

        log.info("GCE Load Balancer is stopping");
        //iterate through hashmap and remove all
    }

    @Override
    public boolean configure(Topology topology) throws LoadBalancerExtensionException {


        log.info("Configuring Load balancer ");

        //printing topology value as a xml for testing purposes
       /* XStream xstream = new XStream(new Sun14ReflectionProvider(
                new FieldDictionary(new ImmutableFieldKeySorter())),
                new DomDriver("utf-8"));
        log.info(xstream.toXML(topology));
        */

        for (Service service : topology.getServices()) {
            for (Cluster cluster : service.getClusters()) { //for each cluster
                //we create one load balancer per cluster
                //Only create configuration object. Not execute.

                //check whether this cluster has a forwarding rule configuration or not
                if (clusterToLoadBalancerConfigurationMap.containsKey(cluster.getClusterId())) {

                    //It has a loadBalancer configured. Take it and update it as the given topology.


                } else {
                    //doesn't have a loadBalancerConfiguration object. So crate a new one and add to hash map

                    TargetPool targetPool = new TargetPool();

                    List<Integer> forwardingRulesList = new ArrayList<Integer>();

                    for (Member member : cluster.getMembers()) {

                        //TODO:set instances(members) to targetPool

                        //add forwarding rules(Ports to be forwarded)

                        //Iterator iterator = member.getPorts().iterator();


                        for(Object port : member.getPorts()){
                            log.info("=================port: "+ port.toString());
                        }

                    }

                    LoadBalancerConfiguration loadBalancerConfiguration = new LoadBalancerConfiguration(
                            cluster.getClusterId(), targetPool);
<commit_msg>adding logic for load balancer configuration<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.stratos.gce.extension;

import com.google.api.services.compute.model.TargetPool;
import org.apache.stratos.load.balancer.common.domain.*;
import org.apache.stratos.load.balancer.extension.api.LoadBalancer;
import org.apache.stratos.load.balancer.extension.api.exception.LoadBalancerExtensionException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class GCELoadBalancer implements LoadBalancer {

    private static final Log log = LogFactory.getLog(GCELoadBalancer.class);

    private GCEOperations gceOperations;
    /**
     * We create one load balancer per cluster(stratos side).
     * A Load balancer has one target pool and set of forwarding rules
     * This hash map is used to hold cluster IDs and corresponding Load balancer configuration
     * So one cluster will have one loadBalancerConfiguration object
     */
    private HashMap<String, LoadBalancerConfiguration> clusterToLoadBalancerConfigurationMap;

    public GCELoadBalancer() {
        try {
            gceOperations = new GCEOperations();
            clusterToLoadBalancerConfigurationMap = new HashMap<String, LoadBalancerConfiguration>();

        } catch (LoadBalancerExtensionException e) {
            log.error(e);
        } catch (GeneralSecurityException e) {
            log.error(e);
        } catch (IOException e) {
            log.error(e);
        }
    }

    @Override
    public void start() throws LoadBalancerExtensionException {
        log.info("Starting GCE Load balancer instance");

        //iterate through clusterToLoadBalancerConfigurationMap
        //for each cluster{
        //if Load balancer status == not running -->
        //      execute commands through GCE API for start the load balancer
        //}

        log.info("GCE Load balancer instance started");


    }

    @Override
    public void stop() throws LoadBalancerExtensionException {

        log.info("GCE Load Balancer is stopping");
        //iterate through hashmap and remove all
    }

    @Override
    public boolean configure(Topology topology) throws LoadBalancerExtensionException {


        log.info("Configuring Load balancer ");

        //printing topology value as a xml for testing purposes
       /* XStream xstream = new XStream(new Sun14ReflectionProvider(
                new FieldDictionary(new ImmutableFieldKeySorter())),
                new DomDriver("utf-8"));
        log.info(xstream.toXML(topology));
        */

        for (Service service : topology.getServices()) {
            for (Cluster cluster : service.getClusters()) { //for each cluster
                //we create one load balancer per cluster
                //Only create configuration object. Not execute.

                //check whether this cluster has a forwarding rule configuration or not
                if (clusterToLoadBalancerConfigurationMap.containsKey(cluster.getClusterId())) {

                    //It has a loadBalancer configured. Take it and update it as the given topology.


                } else {
                    //doesn't have a loadBalancerConfiguration object. So crate a new one and add to hash map

                    TargetPool targetPool = new TargetPool();

                    List<Integer> forwardingRulesList = new ArrayList<Integer>();

                    for (Member member : cluster.getMembers()) {

                        //TODO:set instances(members) to targetPool

                        //add forwarding rules(Ports to be forwarded)

                        //Iterator iterator = member.getPorts().iterator();


                        for(Object port : member.getPorts()){
                            log.info("=================port: "+ ((Port) port).getValue());
                        }

                    }

                    LoadBalancerConfiguration loadBalancerConfiguration = new LoadBalancerConfiguration(
                            cluster.getClusterId(), targetPool);

{'fraction_non_alphanumeric': 0.07912087912087912, 'fraction_numerical': 0.0013986013986013986, 'mean_word_length': 2.528727529080014, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2021776', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2311, 'n_words': 824}