package ru.kirillius.pf.sdn.core.Networking;

import lombok.Builder;
import lombok.Getter;
import ru.kirillius.pf.sdn.core.Context;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Abstraction for retrieving prefixes announced by a specific autonomous system.
 */
public interface ASInfoProvider {
    /**
     * Returns IPv4 subnets originated by the provided autonomous system number.
     */
    List<IPv4Subnet> getPrefixes(int as);

    IPQueryInfo queryAddress(String address);


    @Builder
    class IPQueryInfo {
        @Getter
        private List<Integer> ASN;
        @Getter
        private List<IPv4Subnet> prefixes;
    }

    /**
     * Instantiates a provider class using the context-aware constructor.
     */
    static ASInfoProvider instantiate(Class<? extends ASInfoProvider> providerClass, Context context) {
        try {
            var constructor = providerClass.getConstructor(Context.class);
            return constructor.newInstance(context);
        } catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
                 IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
