<commit_before>/*
 * Copyright © 2015 ZTE and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package com.highstreet.technologies.odl.app.impl;

import com.highstreet.technologies.odl.app.impl.delegates.FC;
import com.highstreet.technologies.odl.app.impl.delegates.PredefinePath;
import com.highstreet.technologies.odl.app.impl.tools.FC2Executor;
import com.highstreet.technologies.odl.app.impl.tools.JsonUtil;
import com.highstreet.technologies.odl.app.impl.tools.MountPointServiceHolder;
import com.highstreet.technologies.odl.app.impl.tools.NeExecutor;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev170324.mw.air._interface.pac.AirInterfaceConfiguration;
import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev170324.mw.air._interface.pac.AirInterfaceStatus;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.route.rev150105.*;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.route.rev150105.fc_desc.Fc;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Future;

import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
import static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.route.rev150105.StatusG.Status;
import static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.route.rev150105.StatusG.Status.Successful;

/**
 * Created by olinchy on 5/22/17.
 */
public class RouteRPC implements RouteService
{
    public RouteRPC(DataBroker dataBroker)
    {
        this.dataBroker = dataBroker;
        fc2Executor = new FC2Executor();
    }

    private static final Logger LOG = LoggerFactory.getLogger(RouteRPC.class);
    public static HashMap<String, NeExecutor> ne_map = new HashMap<>();
    private static HashMap<Integer, PathHolder> toClear = new HashMap<>();
    private static PredefinePath predefinePath;

    static
    {
        predefinePath = JsonUtil.toObject(
                new File(System.getenv().get("ODL_KARAF_HOME") + File.separator + "data" + File.separator + "topology.json"), PredefinePath.class);
    }

    private final DataBroker dataBroker;
    private FC2Executor fc2Executor;

    @Override
    public Future<RpcResult<Void>> readAirInterface(
            ReadAirInterfaceInput input)
    {
        String lpName = input.getLpId();
        String nodeName = input.getNodeName();
        NeExecutor executor = ne_map.computeIfAbsent(
                nodeName, n -> new NeExecutor(MountPointServiceHolder.getMountPoint(n)));
        try
        {
            AirInterfaceConfiguration airInterfaceConfiguration = executor.getUnderAirPac(
                    lpName, AirInterfaceConfiguration.class, CONFIGURATION);
            AirInterfaceStatus airInterfaceStatus = executor.getUnderAirPac(
                    lpName, AirInterfaceStatus.class, OPERATIONAL);
            LOG.info(airInterfaceConfiguration.toString());
            LOG.info(airInterfaceStatus != null ? airInterfaceStatus.toString() : "airInterfaceStatus is null");
        }
        catch (Exception e)
        {
            LOG.warn("", e);
        }

        return null;
    }

    @Override
    public Future<RpcResult<RestoreFollowTopoOutput>> restoreFollowTopo(
            RestoreFollowTopoInput input)
    {
        RestoreFollowTopoOutputBuilder builder = new RestoreFollowTopoOutputBuilder();
        builder.setStatus(switchTo(
                input.getVlanid(),
                Arrays.asList(predefinePath.paths.get(String.valueOf(input.getVlanid())).main_ltps)));
        return RpcResultBuilder.success(builder.build()).buildFuture();
    }

    @Override
    public Future<RpcResult<CreateFollowTopoOutput>> createFollowTopo(
            CreateFollowTopoInput input)
    {
        CreateFollowTopoOutputBuilder builder = new CreateFollowTopoOutputBuilder();
        int vlanId = input.getVlanid();
        ArrayList<Fc> listFc = new ArrayList<>();
        Arrays.stream(predefinePath.paths.get(String.valueOf(vlanId)).main_ltps).forEach(fc -> listFc.add(fc.toFc()));

        builder.setStatus(this.create(vlanId, listFc));
        return RpcResultBuilder.success(builder.build()).buildFuture();
    }

    @Override
    public Future<RpcResult<SwitchFollowTopoOutput>> switchFollowTopo(
            SwitchFollowTopoInput input)
    {
        SwitchFollowTopoOutputBuilder builder = new SwitchFollowTopoOutputBuilder();
        builder.setStatus(
                switchTo(
                        input.getVlanid(),
                        Arrays.asList(predefinePath.paths.get(String.valueOf(input.getVlanid())).backup_ltps)));<commit_msg>refactoring : using some method to skip the step of restarting odl when make some changes in topology,json<commit_after>/*
 * Copyright © 2015 ZTE and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package com.highstreet.technologies.odl.app.impl;

import com.highstreet.technologies.odl.app.impl.delegates.FC;
import com.highstreet.technologies.odl.app.impl.delegates.PredefinePath;
import com.highstreet.technologies.odl.app.impl.tools.FC2Executor;
import com.highstreet.technologies.odl.app.impl.tools.JsonUtil;
import com.highstreet.technologies.odl.app.impl.tools.MountPointServiceHolder;
import com.highstreet.technologies.odl.app.impl.tools.NeExecutor;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev170324.mw.air._interface.pac.AirInterfaceConfiguration;
import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev170324.mw.air._interface.pac.AirInterfaceStatus;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.route.rev150105.*;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.route.rev150105.fc_desc.Fc;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Future;

import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
import static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.route.rev150105.StatusG.Status;
import static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.route.rev150105.StatusG.Status.Successful;

/**
 * Created by olinchy on 5/22/17.
 */
public class RouteRPC implements RouteService
{
    public RouteRPC(DataBroker dataBroker)
    {
        this.dataBroker = dataBroker;
        fc2Executor = new FC2Executor();
    }

    private static final Logger LOG = LoggerFactory.getLogger(RouteRPC.class);
    public static HashMap<String, NeExecutor> ne_map = new HashMap<>();
    private static HashMap<Integer, PathHolder> toClear = new HashMap<>();
    private static PredefinePath predefinePath;

    static
    {
        predefinePath = JsonUtil.toObject(
                new File(System.getenv().get("ODL_KARAF_HOME") + File.separator + "data" + File.separator + "topology.json"), PredefinePath.class);
    }

    private final DataBroker dataBroker;
    private FC2Executor fc2Executor;

    @Override
    public Future<RpcResult<Void>> readAirInterface(
            ReadAirInterfaceInput input)
    {
        String lpName = input.getLpId();
        String nodeName = input.getNodeName();
        NeExecutor executor = ne_map.computeIfAbsent(
                nodeName, n -> new NeExecutor(MountPointServiceHolder.getMountPoint(n)));
        try
        {
            AirInterfaceConfiguration airInterfaceConfiguration = executor.getUnderAirPac(
                    lpName, AirInterfaceConfiguration.class, CONFIGURATION);
            AirInterfaceStatus airInterfaceStatus = executor.getUnderAirPac(
                    lpName, AirInterfaceStatus.class, OPERATIONAL);
            LOG.info(airInterfaceConfiguration.toString());
            LOG.info(airInterfaceStatus != null ? airInterfaceStatus.toString() : "airInterfaceStatus is null");
        }
        catch (Exception e)
        {
            LOG.warn("", e);
        }

        return null;
    }

    @Override
    public Future<RpcResult<RestoreFollowTopoOutput>> restoreFollowTopo(
            RestoreFollowTopoInput input)
    {
        RestoreFollowTopoOutputBuilder builder = new RestoreFollowTopoOutputBuilder();
        builder.setStatus(switchTo(
                input.getVlanid(),
                Arrays.asList(predefinePath.paths.get(String.valueOf(input.getVlanid())).main_ltps)));
        return RpcResultBuilder.success(builder.build()).buildFuture();
    }

    @Override
    public Future<RpcResult<CreateFollowTopoOutput>> createFollowTopo(
            CreateFollowTopoInput input)
    {
        // refresh topology.json before create
        predefinePath = JsonUtil.toObject(
                new File(System.getenv().get("ODL_KARAF_HOME") + File.separator + "data" + File.separator + "topology.json"), PredefinePath.class);

        CreateFollowTopoOutputBuilder builder = new CreateFollowTopoOutputBuilder();
        int vlanId = input.getVlanid();
        ArrayList<Fc> listFc = new ArrayList<>();
        Arrays.stream(predefinePath.paths.get(String.valueOf(vlanId)).main_ltps).forEach(fc -> listFc.add(fc.toFc()));

        builder.setStatus(this.create(vlanId, listFc));
        return RpcResultBuilder.success(builder.build()).buildFuture();
    }

    @Override
    public Future<RpcResult<SwitchFollowTopoOutput>> switchFollowTopo(
            SwitchFollowTopoInput input)
    {
        SwitchFollowTopoOutputBuilder builder = new SwitchFollowTopoOutputBuilder();
        builder.setStatus(
                switchTo(
                        input.getVlanid(),
                        Arrays.asList(predefinePath.paths.get(String.valueOf(input.getVlanid())).backup_ltps)));
{'fraction_non_alphanumeric': 0.10443323121283114, 'fraction_numerical': 0.0111731843575419, 'mean_word_length': 3.5712520593080725, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 12}, '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': '1093905', 'n_tokens_mistral': 3467, 'n_tokens_neox': 3308, 'n_words': 532}