All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opendaylight.sfc.sbrest.provider.task.SbRestAbstractTask Maven / Gradle / Ivy

There is a newer version: 0.10.4
Show newest version
/*
 * Copyright (c) 2014, 2017 Cisco Systems, Inc. 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 org.opendaylight.sfc.sbrest.provider.task;

import com.google.common.annotations.VisibleForTesting;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.sfc.sbrest.json.ExporterFactory;
import org.opendaylight.yangtools.yang.binding.DataObject;

public abstract class SbRestAbstractTask implements Runnable {

    private final ExecutorService odlExecutor;
    private final RestOperation restOperation;
    private final String jsonObject;
    private final List restUriList = new ArrayList<>();

    public SbRestAbstractTask(@NonNull RestOperation restOperation, @NonNull ExporterFactory exporterFactory,
                              @Nullable DataObject dataObject, @NonNull ExecutorService odlExecutor) {
        this.restOperation = restOperation;
        this.odlExecutor = odlExecutor;

        if (dataObject == null) {
            this.jsonObject = null;
        } else if (restOperation.equals(RestOperation.DELETE)) {
            this.jsonObject = exporterFactory.getExporter().exportJsonNameOnly(dataObject);
        } else {
            this.jsonObject = exporterFactory.getExporter().exportJson(dataObject);
        }
    }

    public static Class getLoggerClass() {
        return SbRestAbstractTask.class;
    }

    @Override
    public void run() {
        for (String restUri : this.restUriList) {
            odlExecutor.execute(new WsTask(restUri, restOperation, jsonObject));
        }
    }

    protected void addRestUri(String uri) {
        restUriList.add(uri);
    }

    @VisibleForTesting
    String getJsonObject() {
        return jsonObject;
    }

    @VisibleForTesting
    List getRestUriListCopy() {
        return Collections.unmodifiableList(restUriList);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy