Add Kubevirt API config with unit tests

Change-Id: Id62ae526fd043c3147c3eb0fc82fa49a3f634112
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubevirtApiConfig.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubevirtApiConfig.java
new file mode 100644
index 0000000..1fa1fd9
--- /dev/null
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubevirtApiConfig.java
@@ -0,0 +1,242 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.kubevirtnode.api;
+
+import com.google.common.base.MoreObjects;
+import org.onlab.packet.IpAddress;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.Scheme.HTTPS;
+
+/**
+ * Default implementation of KubeVirt API configuration.
+ */
+public final class DefaultKubevirtApiConfig implements KubevirtApiConfig {
+
+    private static final String NOT_NULL_MSG = "API Config % cannot be null";
+
+    private final Scheme scheme;
+    private final IpAddress ipAddress;
+    private final int port;
+    private final State state;
+    private final String token;
+    private final String caCertData;
+    private final String clientCertData;
+    private final String clientKeyData;
+
+    /**
+     * Default constructor for Kubevirt API config.
+     *
+     * @param scheme            authentication scheme
+     * @param ipAddress         IP address of API server
+     * @param port              port number of API server
+     * @param state             API server connectivity state
+     * @param token             token used for authenticating against API server
+     * @param caCertData        CA certificate data
+     * @param clientCertData    client certificate data
+     * @param clientKeyData     client key data
+     */
+    private DefaultKubevirtApiConfig(Scheme scheme, IpAddress ipAddress,
+                                    int port, State state,
+                                    String token, String caCertData,
+                                    String clientCertData, String clientKeyData) {
+        this.scheme = scheme;
+        this.ipAddress = ipAddress;
+        this.port = port;
+        this.state = state;
+        this.token = token;
+        this.caCertData = caCertData;
+        this.clientCertData = clientCertData;
+        this.clientKeyData = clientKeyData;
+    }
+
+    @Override
+    public Scheme scheme() {
+        return scheme;
+    }
+
+    @Override
+    public IpAddress ipAddress() {
+        return ipAddress;
+    }
+
+    @Override
+    public int port() {
+        return port;
+    }
+
+    @Override
+    public State state() {
+        return state;
+    }
+
+    @Override
+    public KubevirtApiConfig updateState(State newState) {
+        return new Builder()
+                .scheme(scheme)
+                .ipAddress(ipAddress)
+                .port(port)
+                .state(newState)
+                .token(token)
+                .caCertData(caCertData)
+                .clientCertData(clientCertData)
+                .clientKeyData(clientKeyData)
+                .build();
+    }
+
+    @Override
+    public String token() {
+        return token;
+    }
+
+    @Override
+    public String caCertData() {
+        return caCertData;
+    }
+
+    @Override
+    public String clientCertData() {
+        return clientCertData;
+    }
+
+    @Override
+    public String clientKeyData() {
+        return clientKeyData;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+        DefaultKubevirtApiConfig that = (DefaultKubevirtApiConfig) o;
+        return port == that.port && scheme == that.scheme &&
+                ipAddress.equals(that.ipAddress) && state == that.state &&
+                token.equals(that.token) && caCertData.equals(that.caCertData) &&
+                clientCertData.equals(that.clientCertData) &&
+                clientKeyData.equals(that.clientKeyData);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(scheme, ipAddress, port, state, token,
+                caCertData, clientCertData, clientKeyData);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("scheme", scheme)
+                .add("ipAddress", ipAddress)
+                .add("port", port)
+                .add("state", state)
+                .add("token", token)
+                .add("caCertData", caCertData)
+                .add("clientCertData", clientCertData)
+                .add("clientKeyData", clientKeyData)
+                .toString();
+    }
+
+    /**
+     * Returns new builder instance.
+     *
+     * @return KubeVirt API server config builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static final class Builder implements KubevirtApiConfig.Builder {
+
+        private Scheme scheme;
+        private IpAddress ipAddress;
+        private int port;
+        private State state;
+        private String token;
+        private String caCertData;
+        private String clientCertData;
+        private String clientKeyData;
+
+        @Override
+        public KubevirtApiConfig build() {
+            checkArgument(scheme != null, NOT_NULL_MSG, "scheme");
+            checkArgument(ipAddress != null, NOT_NULL_MSG, "ipAddress");
+            checkArgument(state != null, NOT_NULL_MSG, "state");
+
+            if (scheme == HTTPS) {
+                checkArgument(caCertData != null, NOT_NULL_MSG, "caCertData");
+                checkArgument(clientCertData != null, NOT_NULL_MSG, "clientCertData");
+                checkArgument(clientKeyData != null, NOT_NULL_MSG, "clientKeyData");
+            }
+
+            return new DefaultKubevirtApiConfig(scheme, ipAddress, port, state,
+                    token, caCertData, clientCertData, clientKeyData);
+        }
+
+        @Override
+        public KubevirtApiConfig.Builder scheme(Scheme scheme) {
+            this.scheme = scheme;
+            return this;
+        }
+
+        @Override
+        public KubevirtApiConfig.Builder ipAddress(IpAddress ipAddress) {
+            this.ipAddress = ipAddress;
+            return this;
+        }
+
+        @Override
+        public KubevirtApiConfig.Builder port(int port) {
+            this.port = port;
+            return this;
+        }
+
+        @Override
+        public KubevirtApiConfig.Builder state(State state) {
+            this.state = state;
+            return this;
+        }
+
+        @Override
+        public KubevirtApiConfig.Builder token(String token) {
+            this.token = token;
+            return this;
+        }
+
+        @Override
+        public KubevirtApiConfig.Builder caCertData(String caCertData) {
+            this.caCertData = caCertData;
+            return this;
+        }
+
+        @Override
+        public KubevirtApiConfig.Builder clientCertData(String clientCertData) {
+            this.clientCertData = clientCertData;
+            return this;
+        }
+
+        @Override
+        public KubevirtApiConfig.Builder clientKeyData(String clientKeyData) {
+            this.clientKeyData = clientKeyData;
+            return this;
+        }
+    }
+}
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfig.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfig.java
new file mode 100644
index 0000000..e33e4b5
--- /dev/null
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfig.java
@@ -0,0 +1,195 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.kubevirtnode.api;
+
+import org.onlab.packet.IpAddress;
+
+/**
+ * Representation of configuration used in KubeVirt API server.
+ */
+public interface KubevirtApiConfig {
+
+    /**
+     * Lists of authentication schemes.
+     */
+    enum Scheme {
+        /**
+         * Signifies that this is a HTTP authentication scheme.
+         */
+        HTTP,
+
+        /**
+         * Signifies that this is a HTTPS authentication scheme.
+         */
+        HTTPS,
+    }
+
+    /**
+     * Lists of API server connectivity states.
+     */
+    enum State {
+        /**
+         * Signifies that client is connected to KubeVirt API server.
+         */
+        CONNECTED,
+
+        /**
+         * Signifies that client is dis-connected from KubeVirt API server.
+         */
+        DISCONNECTED,
+    }
+
+    /**
+     * Returns the authentication scheme.
+     *
+     * @return authentication scheme
+     */
+    Scheme scheme();
+
+    /**
+     * Returns the IP address of KubeVirt API server.
+     *
+     * @return IP address of KubeVirt API server
+     */
+    IpAddress ipAddress();
+
+    /**
+     * Returns the port number of KubeVirt API server.
+     *
+     * @return port number of KubeVirt API server
+     */
+    int port();
+
+    /**
+     * Returns the connectivity state to KubeVirt API server.
+     *
+     * @return connectivity state to KubeVirt API server
+     */
+    State state();
+
+    /**
+     * Returns new KubeVirt API config instance with given state.
+     *
+     * @param newState updated state
+     * @return updated kubernetes API config
+     */
+    KubevirtApiConfig updateState(State newState);
+
+    /**
+     * Returns the token used for authenticating to API server.
+     *
+     * @return token value
+     */
+    String token();
+
+    /**
+     * Returns the CA certificate data.
+     *
+     * @return CA certificate data
+     */
+    String caCertData();
+
+    /**
+     * Returns the client certificate data.
+     *
+     * @return client certificate data
+     */
+    String clientCertData();
+
+    /**
+     * Returns the client key data.
+     *
+     * @return client key data
+     */
+    String clientKeyData();
+
+    /**
+     * Builder of new API config entity.
+     */
+    interface Builder {
+
+        /**
+         * Builds an immutable KubeVirt API config instance.
+         *
+         * @return KubeVirt API config instance
+         */
+        KubevirtApiConfig build();
+
+        /**
+         * Returns KubeVirt API server config builder with supplied scheme.
+         *
+         * @param scheme scheme of authentication
+         * @return KubeVirt API config builder
+         */
+        Builder scheme(Scheme scheme);
+
+        /**
+         * Returns KubeVirt API server config builder with supplied IP address.
+         *
+         * @param ipAddress IP address of KubeVirt API server
+         * @return KubeVirt API config builder
+         */
+        Builder ipAddress(IpAddress ipAddress);
+
+        /**
+         * Returns KubeVirt API server config builder with supplied port number.
+         *
+         * @param port port number of KubeVirt API server
+         * @return KubeVirt API config builder
+         */
+        Builder port(int port);
+
+        /**
+         * Returns KubeVirt API server config builder with supplied state.
+         *
+         * @param state connectivity state
+         * @return KubeVirt API config builder
+         */
+        Builder state(State state);
+
+        /**
+         * Returns KubeVirt API server config builder with supplied token.
+         *
+         * @param token token for authentication
+         * @return KubeVirt API config builder
+         */
+        Builder token(String token);
+
+        /**
+         * Returns KubeVirt API server config builder with supplied CA certificate data.
+         *
+         * @param caCertData CA certificate data
+         * @return KubeVirt API config builder
+         */
+        Builder caCertData(String caCertData);
+
+        /**
+         * Returns KubeVirt API server config builder with supplied client certificate data.
+         *
+         * @param clientCertData client certificate data
+         * @return KubeVirt API config builder
+         */
+        Builder clientCertData(String clientCertData);
+
+        /**
+         * Returns KubeVirt API server config builder with supplied client key data.
+         *
+         * @param clientKeyData client key data
+         * @return KubeVirt API config builder
+         */
+        Builder clientKeyData(String clientKeyData);
+    }
+}
diff --git a/apps/kubevirt-node/api/src/test/java/org/onosproject/kubevirtnode/api/DefaultKubevirtApiConfigTest.java b/apps/kubevirt-node/api/src/test/java/org/onosproject/kubevirtnode/api/DefaultKubevirtApiConfigTest.java
new file mode 100644
index 0000000..4aa3d87
--- /dev/null
+++ b/apps/kubevirt-node/api/src/test/java/org/onosproject/kubevirtnode/api/DefaultKubevirtApiConfigTest.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.kubevirtnode.api;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.Scheme.HTTP;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.Scheme.HTTPS;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.State.CONNECTED;
+
+
+/**
+ * Unit tests for DefaultKubevirtApiConfig.
+ */
+public final class DefaultKubevirtApiConfigTest {
+
+    private static final KubevirtApiConfig.Scheme SCHEME_1 = HTTP;
+    private static final KubevirtApiConfig.Scheme SCHEME_2 = HTTPS;
+
+    private static final IpAddress IP_ADDRESS_1 = IpAddress.valueOf("192.168.0.200");
+    private static final IpAddress IP_ADDRESS_2 = IpAddress.valueOf("192.168.0.201");
+
+    private static final int PORT_1 = 6443;
+    private static final int PORT_2 = 443;
+
+    private static final String TOKEN_1 = "token1";
+    private static final String TOKEN_2 = "token2";
+
+    private static final String CA_CERT_DATA_1 = "caCertData1";
+    private static final String CA_CERT_DATA_2 = "caCertData2";
+
+    private static final String CLIENT_CERT_DATA_1 = "clientCertData1";
+    private static final String CLIENT_CERT_DATA_2 = "clientCertData2";
+
+    private static final String CLIENT_KEY_DATA_1 = "clientKeyData1";
+    private static final String CLIENT_KEY_DATA_2 = "clientKeyData2";
+
+    private KubevirtApiConfig config1;
+    private KubevirtApiConfig sameAsConfig1;
+    private KubevirtApiConfig config2;
+
+    /**
+     * Tests class immutability.
+     */
+    @Test
+    public void testImmutability() {
+        assertThatClassIsImmutable(DefaultKubevirtApiConfig.class);
+    }
+
+    /**
+     * Initial setup for this unit test.
+     */
+    @Before
+    public void setUp() {
+        config1 = DefaultKubevirtApiConfig.builder()
+                .scheme(SCHEME_1)
+                .ipAddress(IP_ADDRESS_1)
+                .port(PORT_1)
+                .state(CONNECTED)
+                .token(TOKEN_1)
+                .caCertData(CA_CERT_DATA_1)
+                .clientCertData(CLIENT_CERT_DATA_1)
+                .clientKeyData(CLIENT_KEY_DATA_1)
+                .build();
+
+        sameAsConfig1 = DefaultKubevirtApiConfig.builder()
+                .scheme(SCHEME_1)
+                .ipAddress(IP_ADDRESS_1)
+                .port(PORT_1)
+                .state(CONNECTED)
+                .token(TOKEN_1)
+                .caCertData(CA_CERT_DATA_1)
+                .clientCertData(CLIENT_CERT_DATA_1)
+                .clientKeyData(CLIENT_KEY_DATA_1)
+                .build();
+
+        config2 = DefaultKubevirtApiConfig.builder()
+                .scheme(SCHEME_2)
+                .ipAddress(IP_ADDRESS_2)
+                .port(PORT_2)
+                .state(CONNECTED)
+                .token(TOKEN_2)
+                .caCertData(CA_CERT_DATA_2)
+                .clientCertData(CLIENT_CERT_DATA_2)
+                .clientKeyData(CLIENT_KEY_DATA_2)
+                .build();
+    }
+
+    /**
+     * Tests object equality.
+     */
+    @Test
+    public void testEquality() {
+        new EqualsTester().addEqualityGroup(config1, sameAsConfig1)
+                .addEqualityGroup(config2)
+                .testEquals();
+    }
+
+    /**
+     * Test object construction.
+     */
+    @Test
+    public void testConstruction() {
+        KubevirtApiConfig config = config1;
+
+        assertEquals(SCHEME_1, config.scheme());
+        assertEquals(IP_ADDRESS_1, config.ipAddress());
+        assertEquals(PORT_1, config.port());
+        assertEquals(CONNECTED, config.state());
+        assertEquals(TOKEN_1, config.token());
+        assertEquals(CA_CERT_DATA_1, config.caCertData());
+        assertEquals(CLIENT_CERT_DATA_1, config.clientCertData());
+        assertEquals(CLIENT_KEY_DATA_1, config.clientKeyData());
+    }
+}