Add kubevirt API config service, expose it via REST API and CLI
Change-Id: I45a867ad54622656475758e7b4af38c19e551790
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigAdminService.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigAdminService.java
new file mode 100644
index 0000000..d6c249e
--- /dev/null
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigAdminService.java
@@ -0,0 +1,46 @@
+/*
+ * 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;
+
+/**
+ * Service for administering inventory of KubeVirt API configs.
+ */
+public interface KubevirtApiConfigAdminService extends KubevirtApiConfigService {
+
+ /**
+ * Creates a API config.
+ *
+ * @param config KubeVirt API server config
+ */
+ void createApiConfig(KubevirtApiConfig config);
+
+ /**
+ * Updates the API config.
+ *
+ * @param config KubeVirt API server config
+ */
+ void updateApiConfig(KubevirtApiConfig config);
+
+ /**
+ * Removes the KubeVirt API config with the given endpoint.
+ * Endpoint comprises of scheme (HTTP), IP address and port
+ *
+ * @param endpoint KubeVirt API endpoint
+ * @return removed KubeVirt API server config; null if no config
+ * associated with the endpoint
+ */
+ KubevirtApiConfig removeApiConfig(String endpoint);
+}
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigEvent.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigEvent.java
new file mode 100644
index 0000000..780151c
--- /dev/null
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigEvent.java
@@ -0,0 +1,54 @@
+/*
+ * 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.onosproject.event.AbstractEvent;
+
+/**
+ * Describes KubeVirt API server config event.
+ */
+public class KubevirtApiConfigEvent extends AbstractEvent<KubevirtApiConfigEvent.Type, KubevirtApiConfig> {
+
+ /**
+ * Lists of KubeVirt API server config event types.
+ */
+ public enum Type {
+ /**
+ * Signifies that API config is created.
+ */
+ KUBEVIRT_API_CONFIG_CREATED,
+
+ /**
+ * Signifies that API config is updated.
+ */
+ KUBEVIRT_API_CONFIG_UPDATED,
+
+ /**
+ * Signifies that API config is removed.
+ */
+ KUBEVIRT_API_CONFIG_REMOVED,
+ }
+
+ /**
+ * Creates an event with the given type and node.
+ *
+ * @param type event type
+ * @param subject KubeVirt API config
+ */
+ public KubevirtApiConfigEvent(Type type, KubevirtApiConfig subject) {
+ super(type, subject);
+ }
+}
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigListener.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigListener.java
new file mode 100644
index 0000000..447ce49
--- /dev/null
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigListener.java
@@ -0,0 +1,24 @@
+/*
+ * 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.onosproject.event.EventListener;
+
+/**
+ * Listener for KubeVirt API config event.
+ */
+public interface KubevirtApiConfigListener extends EventListener<KubevirtApiConfigEvent> {
+}
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigService.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigService.java
new file mode 100644
index 0000000..237735e
--- /dev/null
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigService.java
@@ -0,0 +1,33 @@
+/*
+ * 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.onosproject.event.ListenerService;
+
+/**
+ * Service for interfacing with the inventory of KubeVirt API server config.
+ */
+public interface KubevirtApiConfigService
+ extends ListenerService<KubevirtApiConfigEvent, KubevirtApiConfigListener> {
+ String APP_ID = "org.onosproject.kubevirtnode";
+
+ /**
+ * Returns the registered API config.
+ *
+ * @return API config
+ */
+ KubevirtApiConfig apiConfig();
+}
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigStore.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigStore.java
new file mode 100644
index 0000000..63e7caf
--- /dev/null
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigStore.java
@@ -0,0 +1,66 @@
+/*
+ * 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.onosproject.store.Store;
+
+import java.util.Set;
+
+/**
+ * Manages inventory of KubeVirt API config; not intended for direct use.
+ */
+public interface KubevirtApiConfigStore
+ extends Store<KubevirtApiConfigEvent, KubevirtApiConfigStoreDelegate> {
+
+ /**
+ * Creates a API config.
+ *
+ * @param config KubeVirt API server config
+ */
+ void createApiConfig(KubevirtApiConfig config);
+
+ /**
+ * Updates the API config.
+ *
+ * @param config KubeVirt API server config
+ */
+ void updateApiConfig(KubevirtApiConfig config);
+
+ /**
+ * Removes the KubeVirt API config with the given endpoint.
+ * Endpoint comprises of scheme (HTTP), IP address and port
+ *
+ * @param endpoint KubeVirt API endpoint
+ * @return removed KubeVirt API server config; null if no config
+ * associated with the endpoint
+ */
+ KubevirtApiConfig removeApiConfig(String endpoint);
+
+ /**
+ * Returns all registered KubeVirt API configs.
+ *
+ * @return set of KubeVirt API configs
+ */
+ Set<KubevirtApiConfig> apiConfigs();
+
+ /**
+ * Returns the API config.
+ *
+ * @param endpoint endpoint
+ * @return KubeVirt API config
+ */
+ KubevirtApiConfig apiConfig(String endpoint);
+}
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigStoreDelegate.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigStoreDelegate.java
new file mode 100644
index 0000000..ba58322
--- /dev/null
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtApiConfigStoreDelegate.java
@@ -0,0 +1,24 @@
+/*
+ * 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.onosproject.store.StoreDelegate;
+
+/**
+ * KubeVirt API server config store delegate.
+ */
+public interface KubevirtApiConfigStoreDelegate extends StoreDelegate<KubevirtApiConfigEvent> {
+}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtApiConfigListCommand.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtApiConfigListCommand.java
new file mode 100644
index 0000000..d88cd0d
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtApiConfigListCommand.java
@@ -0,0 +1,57 @@
+/*
+ * 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.cli;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigService;
+
+import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.prettyJson;
+
+/**
+ * Lists all KubeVirt API server configs registered to the service.
+ */
+@Service
+@Command(scope = "onos", name = "kubevirt-api-configs",
+ description = "Lists all KubeVirt API server configs registered to the service")
+public class KubevirtApiConfigListCommand extends AbstractShellCommand {
+
+ private static final String FORMAT = "%-10s%-25s%-10s%-10s";
+
+ @Override
+ protected void doExecute() throws Exception {
+ KubevirtApiConfigService service = get(KubevirtApiConfigService.class);
+ KubevirtApiConfig config = service.apiConfig();
+
+ if (outputJson()) {
+ print("%s", json(config));
+ } else {
+ print(FORMAT, "Scheme", "IpAddress", "Port", "State");
+ print(FORMAT, config.scheme().name(), config.ipAddress().toString(),
+ config.port(), config.state().name());
+ }
+ }
+
+ private String json(KubevirtApiConfig config) {
+ ObjectMapper mapper = new ObjectMapper();
+ ObjectNode node = jsonForEntity(config, KubevirtApiConfig.class);
+ return prettyJson(mapper, node.toString());
+ }
+}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtApiConfigCodec.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtApiConfigCodec.java
new file mode 100644
index 0000000..3c09605
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtApiConfigCodec.java
@@ -0,0 +1,161 @@
+/*
+ * 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.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.commons.lang.StringUtils;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.kubevirtnode.api.DefaultKubevirtApiConfig;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
+
+import static org.onlab.util.Tools.nullIsIllegal;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.Scheme.HTTPS;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.State.DISCONNECTED;
+
+/**
+ * KubeVirt API server config codec used for serializing and de-serializing JSON string.
+ */
+public final class KubevirtApiConfigCodec extends JsonCodec<KubevirtApiConfig> {
+
+ private static final String SCHEME = "scheme";
+ private static final String IP_ADDRESS = "ipAddress";
+ private static final String PORT = "port";
+ private static final String STATE = "state";
+ private static final String TOKEN = "token";
+ private static final String CA_CERT_DATA = "caCertData";
+ private static final String CLIENT_CERT_DATA = "clientCertData";
+ private static final String CLIENT_KEY_DATA = "clientKeyData";
+
+ private static final String MISSING_MESSAGE = " is required in KubevirtApiConfig";
+
+ @Override
+ public ObjectNode encode(KubevirtApiConfig entity, CodecContext context) {
+ ObjectNode node = context.mapper().createObjectNode()
+ .put(SCHEME, entity.scheme().name())
+ .put(IP_ADDRESS, entity.ipAddress().toString())
+ .put(PORT, entity.port())
+ .put(STATE, entity.state().name());
+
+ if (entity.scheme() == HTTPS) {
+ node.put(CA_CERT_DATA, entity.caCertData())
+ .put(CLIENT_CERT_DATA, entity.clientCertData())
+ .put(CLIENT_KEY_DATA, entity.clientKeyData());
+
+ if (entity.token() != null) {
+ node.put(TOKEN, entity.token());
+ }
+
+ } else {
+ if (entity.token() != null) {
+ node.put(TOKEN, entity.token());
+ }
+
+ if (entity.caCertData() != null) {
+ node.put(CA_CERT_DATA, entity.caCertData());
+ }
+
+ if (entity.clientCertData() != null) {
+ node.put(CLIENT_CERT_DATA, entity.clientCertData());
+ }
+
+ if (entity.clientKeyData() != null) {
+ node.put(CLIENT_KEY_DATA, entity.clientKeyData());
+ }
+ }
+
+ return node;
+ }
+
+ @Override
+ public KubevirtApiConfig decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ KubevirtApiConfig.Scheme scheme = KubevirtApiConfig.Scheme.valueOf(nullIsIllegal(
+ json.get(SCHEME).asText(), SCHEME + MISSING_MESSAGE));
+ IpAddress ipAddress = IpAddress.valueOf(nullIsIllegal(
+ json.get(IP_ADDRESS).asText(), IP_ADDRESS + MISSING_MESSAGE));
+ int port = json.get(PORT).asInt();
+
+ KubevirtApiConfig.Builder builder = DefaultKubevirtApiConfig.builder()
+ .scheme(scheme)
+ .ipAddress(ipAddress)
+ .port(port)
+ .state(DISCONNECTED);
+
+ JsonNode tokenJson = json.get(TOKEN);
+ JsonNode caCertDataJson = json.get(CA_CERT_DATA);
+ JsonNode clientCertDataJson = json.get(CLIENT_CERT_DATA);
+ JsonNode clientKeyDataJson = json.get(CLIENT_KEY_DATA);
+
+ String token = "";
+ String caCertData = "";
+ String clientCertData = "";
+ String clientKeyData = "";
+
+ if (scheme == HTTPS) {
+ caCertData = nullIsIllegal(caCertDataJson.asText(),
+ CA_CERT_DATA + MISSING_MESSAGE);
+ clientCertData = nullIsIllegal(clientCertDataJson.asText(),
+ CLIENT_CERT_DATA + MISSING_MESSAGE);
+ clientKeyData = nullIsIllegal(clientKeyDataJson.asText(),
+ CLIENT_KEY_DATA + MISSING_MESSAGE);
+
+ if (tokenJson != null) {
+ token = tokenJson.asText();
+ }
+
+ } else {
+ if (tokenJson != null) {
+ token = tokenJson.asText();
+ }
+
+ if (caCertDataJson != null) {
+ caCertData = caCertDataJson.asText();
+ }
+
+ if (clientCertDataJson != null) {
+ clientCertData = clientCertDataJson.asText();
+ }
+
+ if (clientKeyDataJson != null) {
+ clientKeyData = clientKeyDataJson.asText();
+ }
+ }
+
+ if (StringUtils.isNotEmpty(token)) {
+ builder.token(token);
+ }
+
+ if (StringUtils.isNotEmpty(caCertData)) {
+ builder.caCertData(caCertData);
+ }
+
+ if (StringUtils.isNotEmpty(clientCertData)) {
+ builder.clientCertData(clientCertData);
+ }
+
+ if (StringUtils.isNotEmpty(clientKeyData)) {
+ builder.clientKeyData(clientKeyData);
+ }
+
+ return builder.build();
+ }
+}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/DistributedKubevirtApiConfigStore.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/DistributedKubevirtApiConfigStore.java
new file mode 100644
index 0000000..991b52a
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/DistributedKubevirtApiConfigStore.java
@@ -0,0 +1,184 @@
+/*
+ * 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.impl;
+
+import com.google.common.collect.ImmutableSet;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnode.api.DefaultKubevirtApiConfig;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigStore;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigStoreDelegate;
+import org.onosproject.store.AbstractStore;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.MapEvent;
+import org.onosproject.store.service.MapEventListener;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.Versioned;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent.Type.KUBEVIRT_API_CONFIG_CREATED;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent.Type.KUBEVIRT_API_CONFIG_REMOVED;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent.Type.KUBEVIRT_API_CONFIG_UPDATED;
+import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.endpoint;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of KubeVirt API config store using consistent map.
+ */
+@Component(immediate = true, service = KubevirtApiConfigStore.class)
+public class DistributedKubevirtApiConfigStore
+ extends AbstractStore<KubevirtApiConfigEvent, KubevirtApiConfigStoreDelegate>
+ implements KubevirtApiConfigStore {
+
+ private final Logger log = getLogger(getClass());
+
+ private static final String ERR_NOT_FOUND = " does not exist";
+ private static final String ERR_DUPLICATE = " already exists";
+ private static final String APP_ID = "org.onosproject.kubevirtnode";
+
+ private static final KryoNamespace
+ SERIALIZER_KUBEVIRT_API_CONFIG = KryoNamespace.newBuilder()
+ .register(KryoNamespaces.API)
+ .register(KubevirtApiConfig.class)
+ .register(DefaultKubevirtApiConfig.class)
+ .register(KubevirtApiConfig.Scheme.class)
+ .register(KubevirtApiConfig.State.class)
+ .register(Collection.class)
+ .build();
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected StorageService storageService;
+
+ private final ExecutorService eventExecutor = newSingleThreadExecutor(
+ groupedThreads(this.getClass().getSimpleName(), "event-handler", log));
+
+ private final MapEventListener<String, KubevirtApiConfig> apiConfigMapListener =
+ new KubevirtApiConfigMapListener();
+ private ConsistentMap<String, KubevirtApiConfig> apiConfigStore;
+
+ @Activate
+ protected void activate() {
+ ApplicationId appId = coreService.registerApplication(APP_ID);
+ apiConfigStore = storageService.<String, KubevirtApiConfig>consistentMapBuilder()
+ .withSerializer(Serializer.using(SERIALIZER_KUBEVIRT_API_CONFIG))
+ .withName("k8s-apiconfig-store")
+ .withApplicationId(appId)
+ .build();
+ apiConfigStore.addListener(apiConfigMapListener);
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ apiConfigStore.removeListener(apiConfigMapListener);
+ eventExecutor.shutdown();
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createApiConfig(KubevirtApiConfig config) {
+ String key = endpoint(config);
+ apiConfigStore.compute(key, (endpoint, existing) -> {
+ final String error = key + ERR_DUPLICATE;
+ checkArgument(existing == null, error);
+ return config;
+ });
+ }
+
+ @Override
+ public void updateApiConfig(KubevirtApiConfig config) {
+ String key = endpoint(config);
+ apiConfigStore.compute(key, (endpoint, existing) -> {
+ final String error = key + ERR_NOT_FOUND;
+ checkArgument(existing != null, error);
+ return config;
+ });
+ }
+
+ @Override
+ public KubevirtApiConfig removeApiConfig(String endpoint) {
+ Versioned<KubevirtApiConfig> apiConfig = apiConfigStore.remove(endpoint);
+ if (apiConfig == null) {
+ final String error = endpoint + ERR_NOT_FOUND;
+ throw new IllegalArgumentException(error);
+ }
+ return apiConfig.value();
+ }
+
+ @Override
+ public Set<KubevirtApiConfig> apiConfigs() {
+ return ImmutableSet.copyOf(apiConfigStore.asJavaMap().values());
+ }
+
+ @Override
+ public KubevirtApiConfig apiConfig(String endpoint) {
+ return apiConfigStore.asJavaMap().get(endpoint);
+ }
+
+ private class KubevirtApiConfigMapListener
+ implements MapEventListener<String, KubevirtApiConfig> {
+
+ @Override
+ public void event(MapEvent<String, KubevirtApiConfig> event) {
+ switch (event.type()) {
+ case INSERT:
+ log.debug("Kubevirt API config created {}", event.newValue());
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubevirtApiConfigEvent(
+ KUBEVIRT_API_CONFIG_CREATED, event.newValue().value()
+ )));
+ break;
+ case UPDATE:
+ log.debug("Kubevirt API config updated {}", event.newValue());
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubevirtApiConfigEvent(
+ KUBEVIRT_API_CONFIG_UPDATED, event.newValue().value()
+ )));
+ break;
+ case REMOVE:
+ log.debug("Kubevirt API config removed {}", event.oldValue());
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubevirtApiConfigEvent(
+ KUBEVIRT_API_CONFIG_REMOVED, event.oldValue().value()
+ )));
+ break;
+ default:
+ // do nothing
+ break;
+ }
+ }
+ }
+}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/KubevirtApiConfigManager.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/KubevirtApiConfigManager.java
new file mode 100644
index 0000000..06e2ccf
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/KubevirtApiConfigManager.java
@@ -0,0 +1,152 @@
+/*
+ * 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.impl;
+
+/**
+ * Service administering the inventory of KubeVirt API configs.
+ */
+
+import com.google.common.base.Strings;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.LeadershipService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.event.ListenerRegistry;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigAdminService;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigListener;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigService;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigStore;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigStoreDelegate;
+import org.onosproject.store.service.StorageService;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.slf4j.Logger;
+
+import java.util.concurrent.ExecutorService;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.endpoint;
+import static org.slf4j.LoggerFactory.getLogger;
+
+@Component(
+ immediate = true,
+ service = {KubevirtApiConfigService.class, KubevirtApiConfigAdminService.class }
+)
+public class KubevirtApiConfigManager
+ extends ListenerRegistry<KubevirtApiConfigEvent, KubevirtApiConfigListener>
+ implements KubevirtApiConfigService, KubevirtApiConfigAdminService {
+
+ private final Logger log = getLogger(getClass());
+
+ private static final String MSG_CONFIG = "KubeVirt API config %s %s";
+ private static final String MSG_CREATED = "created";
+ private static final String MSG_UPDATED = "updated";
+ private static final String MSG_REMOVED = "removed";
+
+ private static final String ERR_NULL_CONFIG = "KubeVirt API config cannot be null";
+ private static final String ERR_NULL_ENDPOINT = "KubeVirt API endpoint cannot be null";
+ private static final String ERR_UNIQUE_CONFIG = "KubeVirt API config should be unique";
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected ClusterService clusterService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected LeadershipService leadershipService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected StorageService storageService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected KubevirtApiConfigStore configStore;
+
+ private final ExecutorService eventExecutor = newSingleThreadExecutor(
+ groupedThreads(this.getClass().getSimpleName(), "event-handler", log));
+
+ private final KubevirtApiConfigStoreDelegate delegate = new InternalApiConfigStoreDelegate();
+
+ private ApplicationId appId;
+
+ @Activate
+ protected void activate() {
+ appId = coreService.registerApplication(APP_ID);
+ configStore.setDelegate(delegate);
+
+ leadershipService.runForLeadership(appId.name());
+
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ configStore.unsetDelegate(delegate);
+
+ leadershipService.withdraw(appId.name());
+ eventExecutor.shutdown();
+
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createApiConfig(KubevirtApiConfig config) {
+ checkNotNull(config, ERR_NULL_CONFIG);
+ checkArgument(configStore.apiConfigs().size() == 0, ERR_UNIQUE_CONFIG);
+
+ configStore.createApiConfig(config);
+ log.info(String.format(MSG_CONFIG, endpoint(config), MSG_CREATED));
+ }
+
+ @Override
+ public void updateApiConfig(KubevirtApiConfig config) {
+ checkNotNull(config, ERR_NULL_CONFIG);
+ configStore.updateApiConfig(config);
+ log.info(String.format(MSG_CONFIG, endpoint(config), MSG_UPDATED));
+ }
+
+ @Override
+ public KubevirtApiConfig removeApiConfig(String endpoint) {
+ checkArgument(!Strings.isNullOrEmpty(endpoint), ERR_NULL_ENDPOINT);
+ KubevirtApiConfig config = configStore.removeApiConfig(endpoint);
+ log.info(String.format(MSG_CONFIG, endpoint, MSG_REMOVED));
+ return config;
+ }
+
+ @Override
+ public KubevirtApiConfig apiConfig() {
+ return configStore.apiConfigs().stream().findAny().orElse(null);
+ }
+
+ private class InternalApiConfigStoreDelegate implements KubevirtApiConfigStoreDelegate {
+
+ @Override
+ public void notify(KubevirtApiConfigEvent event) {
+ if (event != null) {
+ log.trace("send KubeVirt API config event {}", event);
+ process(event);
+ }
+ }
+ }
+}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/util/KubevirtNodeUtil.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/util/KubevirtNodeUtil.java
index 646965b..dba7d35 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/util/KubevirtNodeUtil.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/util/KubevirtNodeUtil.java
@@ -16,6 +16,9 @@
package org.onosproject.kubevirtnode.util;
import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.commons.lang.StringUtils;
+import org.onlab.packet.IpAddress;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -43,6 +46,37 @@
}
/**
+ * Generates endpoint URL by referring to scheme, ipAddress and port.
+ *
+ * @param apiConfig kubernetes API config
+ * @return generated endpoint URL
+ */
+ public static String endpoint(KubevirtApiConfig apiConfig) {
+ return endpoint(apiConfig.scheme(), apiConfig.ipAddress(), apiConfig.port());
+ }
+
+ /**
+ * Generates endpoint URL by referring to scheme, ipAddress and port.
+ *
+ * @param scheme scheme
+ * @param ipAddress IP address
+ * @param port port number
+ * @return generated endpoint URL
+ */
+ public static String endpoint(KubevirtApiConfig.Scheme scheme, IpAddress ipAddress, int port) {
+ StringBuilder endpoint = new StringBuilder();
+ String protocol = StringUtils.lowerCase(scheme.name());
+
+ endpoint.append(protocol);
+ endpoint.append(COLON_SLASH);
+ endpoint.append(ipAddress.toString());
+ endpoint.append(COLON);
+ endpoint.append(port);
+
+ return endpoint.toString();
+ }
+
+ /**
* Generates a DPID (of:0000000000000001) from an index value.
*
* @param index index value
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtApiConfigWebResource.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtApiConfigWebResource.java
new file mode 100644
index 0000000..1c6985b
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtApiConfigWebResource.java
@@ -0,0 +1,134 @@
+/*
+ * 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.web;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigAdminService;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.UriInfo;
+import java.io.InputStream;
+
+import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
+import static javax.ws.rs.core.Response.created;
+import static org.onlab.util.Tools.readTreeFromStream;
+import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.endpoint;
+
+/**
+ * Handles REST API call of kubernetes node config.
+ */
+@Path("api-config")
+public class KubevirtApiConfigWebResource extends AbstractWebResource {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ private static final String MESSAGE_CONFIG = "Received API config %s request";
+
+ private static final String CREATE = "CREATE";
+ private static final String UPDATE = "UPDATE";
+ private static final String REMOVE = "REMOVE";
+ private static final String QUERY = "QUERY";
+ private static final String API_CONFIG = "apiConfig";
+
+ private static final String ENDPOINT = "endpoint";
+ private static final String ERROR_MESSAGE = " cannot be null";
+
+ private final KubevirtApiConfigAdminService service = get(KubevirtApiConfigAdminService.class);
+
+ @Context
+ private UriInfo uriInfo;
+
+ /**
+ * Creates a set of KubeVirt API config from the JSON input stream.
+ *
+ * @param input KubeVirt API configs JSON input stream
+ * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
+ * is malformed
+ * @onos.rsModel KubevirtApiConfig
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response createApiConfigs(InputStream input) {
+ log.trace(String.format(MESSAGE_CONFIG, CREATE));
+
+ KubevirtApiConfig config = readApiConfig(input);
+ if (config != null) {
+ service.createApiConfig(config);
+ }
+
+ UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
+ .path(API_CONFIG);
+
+ return created(locationBuilder.build()).build();
+ }
+
+ /**
+ * Removes a KubeVirt API config.
+ *
+ * @param endpoint KubeVirt API endpoint
+ * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed
+ */
+ @DELETE
+ @Path("{endpoint : .+}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response deleteApiConfig(@PathParam("endpoint") String endpoint) {
+ log.trace(String.format(MESSAGE_CONFIG, REMOVE));
+
+ KubevirtApiConfig existing = service.apiConfig();
+
+ if (existing == null) {
+ log.warn("There is no API configuration to delete");
+ return Response.notModified().build();
+ } else {
+ if (endpoint.equals(endpoint(existing))) {
+ service.removeApiConfig(endpoint);
+ } else {
+ log.warn("There is no API configuration to delete for endpoint {}", endpoint);
+ }
+ }
+
+ return Response.noContent().build();
+ }
+
+ private KubevirtApiConfig readApiConfig(InputStream input) {
+ KubevirtApiConfig config;
+ try {
+ JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
+ ObjectNode objectNode = jsonTree.deepCopy();
+ config = codec(KubevirtApiConfig.class).decode(objectNode, this);
+ } catch (Exception e) {
+ throw new IllegalArgumentException(e);
+ }
+
+ return config;
+ }
+}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeCodecRegister.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeCodecRegister.java
index efdf164..55d740f 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeCodecRegister.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeCodecRegister.java
@@ -16,8 +16,10 @@
package org.onosproject.kubevirtnode.web;
import org.onosproject.codec.CodecService;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
import org.onosproject.kubevirtnode.api.KubevirtNode;
import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
+import org.onosproject.kubevirtnode.codec.KubevirtApiConfigCodec;
import org.onosproject.kubevirtnode.codec.KubevirtNodeCodec;
import org.onosproject.kubevirtnode.codec.KubevirtPhyInterfaceCodec;
import org.osgi.service.component.annotations.Activate;
@@ -44,6 +46,7 @@
protected void activate() {
codecService.registerCodec(KubevirtNode.class, new KubevirtNodeCodec());
codecService.registerCodec(KubevirtPhyInterface.class, new KubevirtPhyInterfaceCodec());
+ codecService.registerCodec(KubevirtApiConfig.class, new KubevirtApiConfigCodec());
log.info("Started");
}
@@ -52,6 +55,7 @@
protected void deactivate() {
codecService.unregisterCodec(KubevirtNode.class);
codecService.unregisterCodec(KubevirtPhyInterface.class);
+ codecService.unregisterCodec(KubevirtApiConfig.class);
log.info("Stopped");
}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebApplication.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebApplication.java
index aa30a6a..5d03a76 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebApplication.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebApplication.java
@@ -25,6 +25,6 @@
public class KubevirtNodeWebApplication extends AbstractWebApplication {
@Override
public Set<Class<?>> getClasses() {
- return getClasses(KubevirtNodeWebResource.class);
+ return getClasses(KubevirtNodeWebResource.class, KubevirtApiConfigWebResource.class);
}
}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebResource.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebResource.java
index e3b18ba..137fc11 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebResource.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebResource.java
@@ -50,7 +50,7 @@
/**
* Handles REST API call of KubeVirt node config.
*/
-@Path("configure")
+@Path("node")
public class KubevirtNodeWebResource extends AbstractWebResource {
private final Logger log = LoggerFactory.getLogger(getClass());
@@ -84,7 +84,6 @@
* @onos.rsModel KubevirtNode
*/
@POST
- @Path("node")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createNodes(InputStream input) {
@@ -113,7 +112,6 @@
* @onos.rsModel KubevirtNode
*/
@PUT
- @Path("node")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateNodes(InputStream input) {
@@ -141,7 +139,7 @@
* 304 NOT_MODIFIED without the updated config
*/
@DELETE
- @Path("node/{hostname}")
+ @Path("{hostname}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response deleteNode(@PathParam("hostname") String hostname) {
@@ -186,7 +184,7 @@
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
- @Path("init/node/{hostname}")
+ @Path("init/{hostname}")
public Response initNode(@PathParam("hostname") String hostname) {
log.trace(String.format(MESSAGE_NODE, QUERY));
diff --git a/apps/kubevirt-node/app/src/main/resources/definitions/KubevirtApiConfig.json b/apps/kubevirt-node/app/src/main/resources/definitions/KubevirtApiConfig.json
new file mode 100644
index 0000000..4e0800d
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/resources/definitions/KubevirtApiConfig.json
@@ -0,0 +1,32 @@
+{
+ "type": "object",
+ "required": [
+ "clusterName",
+ "segmentId",
+ "scheme",
+ "ipAddress",
+ "port"
+ ],
+ "properties": {
+ "clusterName": {
+ "type": "string",
+ "example": "kubernetes"
+ },
+ "segmentId": {
+ "type": "integer",
+ "example": 1
+ },
+ "scheme": {
+ "type": "string",
+ "example": "HTTP"
+ },
+ "ipAddress": {
+ "type": "string",
+ "example": "10.10.10.2"
+ },
+ "port": {
+ "type": "integer",
+ "example": 6443
+ }
+ }
+}
\ No newline at end of file
diff --git a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtApiConfigCodecTest.java b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtApiConfigCodecTest.java
new file mode 100644
index 0000000..773401e
--- /dev/null
+++ b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtApiConfigCodecTest.java
@@ -0,0 +1,158 @@
+/*
+ * 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.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnode.api.DefaultKubevirtApiConfig;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.Scheme.HTTPS;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.State.CONNECTED;
+import static org.onosproject.kubevirtnode.codec.KubevirtApiConfigJsonMatcher.matchesKubevirtApiConfig;
+import static org.onosproject.net.NetTestTools.APP_ID;
+
+/**
+ * Unit tests for KubeVirt API config codec.
+ */
+public class KubevirtApiConfigCodecTest {
+
+ MockCodecContext context;
+
+ JsonCodec<KubevirtApiConfig> kubevirtApiConfigCodec;
+
+ final CoreService mockCoreService = createMock(CoreService.class);
+ private static final String REST_APP_ID = "org.onosproject.rest";
+
+ /**
+ * Initial setup for this unit test.
+ */
+ @Before
+ public void setUp() {
+ context = new MockCodecContext();
+ kubevirtApiConfigCodec = new KubevirtApiConfigCodec();
+
+ assertThat(kubevirtApiConfigCodec, notNullValue());
+
+ expect(mockCoreService.registerApplication(REST_APP_ID))
+ .andReturn(APP_ID).anyTimes();
+ replay(mockCoreService);
+ context.registerService(CoreService.class, mockCoreService);
+ }
+
+ /**
+ * Tests the KubeVirt API config encoding.
+ */
+ @Test
+ public void testKubevirtApiConfigEncode() {
+ KubevirtApiConfig config = DefaultKubevirtApiConfig.builder()
+ .scheme(HTTPS)
+ .ipAddress(IpAddress.valueOf("10.10.10.23"))
+ .port(6443)
+ .state(CONNECTED)
+ .token("token")
+ .caCertData("caCertData")
+ .clientCertData("clientCertData")
+ .clientKeyData("clientKeyData")
+ .build();
+ ObjectNode configJson = kubevirtApiConfigCodec.encode(config, context);
+ assertThat(configJson, matchesKubevirtApiConfig(config));
+ }
+
+ /**
+ * Tests the KubeVirt API config decoding.
+ *
+ * @throws IOException IO exception
+ */
+ @Test
+ public void testKubevirtApiConfigDecode() throws IOException {
+ KubevirtApiConfig config = getKubevirtApiConfig("KubevirtApiConfig.json");
+
+ assertEquals("HTTPS", config.scheme().name());
+ assertEquals("10.134.34.223", config.ipAddress().toString());
+ assertEquals(6443, config.port());
+ assertEquals("token", config.token());
+ assertEquals("caCertData", config.caCertData());
+ assertEquals("clientCertData", config.clientCertData());
+ assertEquals("clientKeyData", config.clientKeyData());
+ }
+
+ private KubevirtApiConfig getKubevirtApiConfig(String resourceName) throws IOException {
+ InputStream jsonStream = KubevirtNodeCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ assertThat(json, notNullValue());
+ KubevirtApiConfig config = kubevirtApiConfigCodec.decode((ObjectNode) json, context);
+ assertThat(config, notNullValue());
+ return config;
+ }
+
+ private class MockCodecContext implements CodecContext {
+ private final ObjectMapper mapper = new ObjectMapper();
+ private final CodecManager manager = new CodecManager();
+ private final Map<Class<?>, Object> services = new HashMap<>();
+
+ /**
+ * Constructs a new mock codec context.
+ */
+ public MockCodecContext() {
+ manager.activate();
+ }
+
+ @Override
+ public ObjectMapper mapper() {
+ return mapper;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> JsonCodec<T> codec(Class<T> entityClass) {
+ if (entityClass == KubevirtApiConfig.class) {
+ return (JsonCodec<T>) kubevirtApiConfigCodec;
+ }
+ return manager.getCodec(entityClass);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> T getService(Class<T> serviceClass) {
+ return (T) services.get(serviceClass);
+ }
+
+ // for registering mock services
+ public <T> void registerService(Class<T> serviceClass, T impl) {
+ services.put(serviceClass, impl);
+ }
+ }
+}
diff --git a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtApiConfigJsonMatcher.java b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtApiConfigJsonMatcher.java
new file mode 100644
index 0000000..17df9bc
--- /dev/null
+++ b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtApiConfigJsonMatcher.java
@@ -0,0 +1,138 @@
+/*
+ * 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.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
+
+/**
+ * Hamcrest matcher for KubeVirt API config.
+ */
+public final class KubevirtApiConfigJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final KubevirtApiConfig kubevirtApiConfig;
+
+ private static final String SCHEME = "scheme";
+ private static final String IP_ADDRESS = "ipAddress";
+ private static final String PORT = "port";
+ private static final String STATE = "state";
+ private static final String TOKEN = "token";
+ private static final String CA_CERT_DATA = "caCertData";
+ private static final String CLIENT_CERT_DATA = "clientCertData";
+ private static final String CLIENT_KEY_DATA = "clientKeyData";
+
+ private KubevirtApiConfigJsonMatcher(KubevirtApiConfig kubevirtApiConfig) {
+ this.kubevirtApiConfig = kubevirtApiConfig;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+ // check scheme
+ String jsonScheme = jsonNode.get(SCHEME).asText();
+ String scheme = kubevirtApiConfig.scheme().name();
+ if (!jsonScheme.equals(scheme)) {
+ description.appendText("scheme was " + jsonScheme);
+ return false;
+ }
+
+ // check IP address
+ String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText();
+ String ipAddress = kubevirtApiConfig.ipAddress().toString();
+ if (!jsonIpAddress.equals(ipAddress)) {
+ description.appendText("ipAddress was " + jsonIpAddress);
+ return false;
+ }
+
+ // check port
+ int jsonPort = jsonNode.get(PORT).asInt();
+ int port = kubevirtApiConfig.port();
+ if (jsonPort != port) {
+ description.appendText("port was " + jsonPort);
+ return false;
+ }
+
+ // check state
+ JsonNode jsonState = jsonNode.get(STATE);
+ String state = kubevirtApiConfig.state().name();
+ if (jsonState != null) {
+ if (!jsonState.asText().equals(state)) {
+ description.appendText("state was " + jsonState);
+ return false;
+ }
+ }
+
+ // check token
+ JsonNode jsonToken = jsonNode.get(TOKEN);
+ String token = kubevirtApiConfig.token();
+ if (jsonToken != null) {
+ if (!jsonToken.asText().equals(token)) {
+ description.appendText("token was " + jsonToken);
+ return false;
+ }
+ }
+
+ // check caCertData
+ JsonNode jsonCaCertData = jsonNode.get(CA_CERT_DATA);
+ String caCertData = kubevirtApiConfig.caCertData();
+ if (jsonCaCertData != null) {
+ if (!jsonCaCertData.asText().equals(caCertData)) {
+ description.appendText("caCertData was " + jsonCaCertData);
+ return false;
+ }
+ }
+
+ // check clientCertData
+ JsonNode jsonClientCertData = jsonNode.get(CLIENT_CERT_DATA);
+ String clientCertData = kubevirtApiConfig.clientCertData();
+
+ if (jsonClientCertData != null) {
+ if (!jsonClientCertData.asText().equals(clientCertData)) {
+ description.appendText("clientCertData was " + jsonClientCertData);
+ return false;
+ }
+ }
+
+ // check clientKeyData
+ JsonNode jsonClientKeyData = jsonNode.get(CLIENT_KEY_DATA);
+ String clientKeyData = kubevirtApiConfig.clientKeyData();
+
+ if (jsonClientKeyData != null) {
+ if (!jsonClientKeyData.asText().equals(clientKeyData)) {
+ description.appendText("clientKeyData was " + jsonClientKeyData);
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(kubevirtApiConfig.toString());
+ }
+
+ /**
+ * Factory to allocate an kubevirtApiConfig matcher.
+ *
+ * @param config kubevirtApiConfig object we are looking for
+ * @return matcher
+ */
+ public static KubevirtApiConfigJsonMatcher matchesKubevirtApiConfig(KubevirtApiConfig config) {
+ return new KubevirtApiConfigJsonMatcher(config);
+ }
+}
diff --git a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/impl/KubevirtApiConfigManagerTest.java b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/impl/KubevirtApiConfigManagerTest.java
new file mode 100644
index 0000000..0e5db34
--- /dev/null
+++ b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/impl/KubevirtApiConfigManagerTest.java
@@ -0,0 +1,179 @@
+/*
+ * 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.impl;
+
+import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.MoreExecutors;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cluster.ClusterServiceAdapter;
+import org.onosproject.cluster.LeadershipServiceAdapter;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.event.Event;
+import org.onosproject.kubevirtnode.api.DefaultKubevirtApiConfig;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigListener;
+import org.onosproject.store.service.TestStorageService;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.State.DISCONNECTED;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent.Type.KUBEVIRT_API_CONFIG_CREATED;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfigEvent.Type.KUBEVIRT_API_CONFIG_REMOVED;
+import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.endpoint;
+
+
+/**
+ * Unit tests for KubeVirt API config manager.
+ */
+public class KubevirtApiConfigManagerTest {
+
+ private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
+
+ private static final String ERR_SIZE = "Number of configs did not match";
+ private static final String ERR_NOT_MATCH = "Config did not match";
+ private static final String ERR_NOT_FOUND = "Config did not exist";
+
+ private KubevirtApiConfig apiConfig1;
+ private KubevirtApiConfig apiConfig2;
+
+ private final TestKubevirtApiConfigListener testListener = new TestKubevirtApiConfigListener();
+
+ private KubevirtApiConfigManager target;
+ private DistributedKubevirtApiConfigStore configStore;
+
+ /**
+ * Initial setup for this unit test.
+ */
+ @Before
+ public void setUp() {
+ apiConfig1 = DefaultKubevirtApiConfig.builder()
+ .scheme(KubevirtApiConfig.Scheme.HTTP)
+ .ipAddress(IpAddress.valueOf("10.10.10.2"))
+ .port(6443)
+ .state(DISCONNECTED)
+ .build();
+ apiConfig2 = DefaultKubevirtApiConfig.builder()
+ .scheme(KubevirtApiConfig.Scheme.HTTP)
+ .ipAddress(IpAddress.valueOf("10.10.10.3"))
+ .port(6443)
+ .state(DISCONNECTED)
+ .build();
+
+ configStore = new DistributedKubevirtApiConfigStore();
+ TestUtils.setField(configStore, "coreService", new TestCoreService());
+ TestUtils.setField(configStore, "storageService", new TestStorageService());
+ TestUtils.setField(configStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
+ configStore.activate();
+
+ target = new KubevirtApiConfigManager();
+ target.storageService = new TestStorageService();
+ target.coreService = new TestCoreService();
+ target.clusterService = new TestClusterService();
+ target.leadershipService = new TestLeadershipService();
+ target.configStore = configStore;
+ target.addListener(testListener);
+ target.activate();
+ testListener.events.clear();
+ }
+
+ /**
+ * Clean up unit test.
+ */
+ @After
+ public void tearDown() {
+ target.removeListener(testListener);
+ target.deactivate();
+ configStore.deactivate();
+ configStore = null;
+ target = null;
+ }
+
+ /**
+ * Checks if creating and removing a config work well with proper events.
+ */
+ @Test
+ public void testCreateAndRemoveConfig() {
+ target.createApiConfig(apiConfig1);
+ assertNotNull(target.apiConfig());
+
+ target.removeApiConfig(endpoint(apiConfig1));
+ assertNull(target.apiConfig());
+
+ validateEvents(KUBEVIRT_API_CONFIG_CREATED, KUBEVIRT_API_CONFIG_REMOVED);
+ }
+
+ /**
+ * Checks if the stored config is unique or not.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testConfigUniqueness() {
+ target.createApiConfig(apiConfig1);
+ target.createApiConfig(apiConfig2);
+ validateEvents(KUBEVIRT_API_CONFIG_CREATED);
+ }
+
+ /**
+ * Checks if removing null config fails with proper exception.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testRemoveNullConfig() {
+ target.removeApiConfig(null);
+ }
+
+ private void validateEvents(Enum... types) {
+ int i = 0;
+ assertEquals("Number of events did not match", types.length, testListener.events.size());
+ for (Event event : testListener.events) {
+ assertEquals("Incorrect event received", types[i], event.type());
+ i++;
+ }
+ testListener.events.clear();
+ }
+
+ private static class TestKubevirtApiConfigListener implements KubevirtApiConfigListener {
+ private List<KubevirtApiConfigEvent> events = Lists.newArrayList();
+
+ @Override
+ public void event(KubevirtApiConfigEvent event) {
+ events.add(event);
+ }
+ }
+
+ private static class TestCoreService extends CoreServiceAdapter {
+ @Override
+ public ApplicationId registerApplication(String name) {
+ return TEST_APP_ID;
+ }
+ }
+
+ private class TestClusterService extends ClusterServiceAdapter {
+
+ }
+
+ private static class TestLeadershipService extends LeadershipServiceAdapter {
+
+ }
+}
diff --git a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/web/KubevirtApiConfigWebResourceTest.java b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/web/KubevirtApiConfigWebResourceTest.java
new file mode 100644
index 0000000..e6d1034
--- /dev/null
+++ b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/web/KubevirtApiConfigWebResourceTest.java
@@ -0,0 +1,175 @@
+/*
+ * 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.web;
+
+import org.glassfish.jersey.server.ResourceConfig;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.kubevirtnode.api.DefaultKubevirtApiConfig;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
+import org.onosproject.kubevirtnode.api.KubevirtApiConfigAdminService;
+import org.onosproject.kubevirtnode.codec.KubevirtApiConfigCodec;
+import org.onosproject.rest.resources.ResourceTest;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.InputStream;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.Scheme.HTTPS;
+import static org.onosproject.kubevirtnode.api.KubevirtApiConfig.State.DISCONNECTED;
+
+/**
+ * Unit test for KubeVirt node REST API.
+ */
+public class KubevirtApiConfigWebResourceTest extends ResourceTest {
+
+ final KubevirtApiConfigAdminService mockConfigAdminService =
+ createMock(KubevirtApiConfigAdminService.class);
+ private static final String PATH = "api-config";
+
+ private KubevirtApiConfig kubevirtApiConfig;
+
+ /**
+ * Constructs a KubeVirt API config resource test instance.
+ */
+ public KubevirtApiConfigWebResourceTest() {
+ super(ResourceConfig.forApplicationClass(KubevirtNodeWebApplication.class));
+ }
+
+ /**
+ * Sets up the global values for all the tests.
+ */
+ @Before
+ public void setUpTest() {
+ final CodecManager codecService = new CodecManager();
+ codecService.activate();
+ codecService.registerCodec(KubevirtApiConfig.class, new KubevirtApiConfigCodec());
+ ServiceDirectory testDirectory =
+ new TestServiceDirectory()
+ .add(KubevirtApiConfigAdminService.class, mockConfigAdminService)
+ .add(CodecService.class, codecService);
+ setServiceDirectory(testDirectory);
+
+ kubevirtApiConfig = DefaultKubevirtApiConfig.builder()
+ .scheme(HTTPS)
+ .ipAddress(IpAddress.valueOf("10.134.34.223"))
+ .port(6443)
+ .state(DISCONNECTED)
+ .token("tokenMod")
+ .caCertData("caCertData")
+ .clientCertData("clientCertData")
+ .clientKeyData("clientKeyData")
+ .build();
+ }
+
+ /**
+ * Tests the results of the REST API POST method with creating new configs operation.
+ */
+ @Test
+ public void testCreateConfigWithCreateOperation() {
+ mockConfigAdminService.createApiConfig(anyObject());
+ replay(mockConfigAdminService);
+
+ final WebTarget wt = target();
+ InputStream jsonStream = KubevirtApiConfigWebResourceTest.class
+ .getResourceAsStream("kubevirt-api-config.json");
+ Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ .post(Entity.json(jsonStream));
+ final int status = response.getStatus();
+
+ assertThat(status, is(201));
+
+ verify(mockConfigAdminService);
+ }
+
+ /**
+ * Tests the results of the REST API POST method without creating new configs operation.
+ */
+ @Test
+ public void testCreateConfigWithNullConfig() {
+ mockConfigAdminService.createApiConfig(null);
+ replay(mockConfigAdminService);
+
+ final WebTarget wt = target();
+ InputStream jsonStream = KubevirtApiConfigWebResourceTest.class
+ .getResourceAsStream("kubevirt-api-config.json");
+ Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ .post(Entity.json(jsonStream));
+ final int status = response.getStatus();
+
+ assertThat(status, is(500));
+ }
+
+ /**
+ * Tests the results of the REST API DELETE method with deleting the configs.
+ */
+ @Test
+ public void testDeleteConfigsWithDeletionOperation() {
+ expect(mockConfigAdminService.apiConfig())
+ .andReturn(kubevirtApiConfig).once();
+ expect(mockConfigAdminService.removeApiConfig(anyString()))
+ .andReturn(kubevirtApiConfig).once();
+ replay(mockConfigAdminService);
+
+ String location = PATH + "/https://10.134.34.223:6443";
+
+ final WebTarget wt = target();
+ Response response = wt.path(location).request(
+ MediaType.APPLICATION_JSON_TYPE).delete();
+
+ final int status = response.getStatus();
+
+ assertThat(status, is(204));
+
+ verify(mockConfigAdminService);
+ }
+
+ /**
+ * Tests the results of the REST API DELETE method without deleting the configs.
+ */
+ @Test
+ public void testDeleteConfigsWithoutDeletionOperation() {
+ expect(mockConfigAdminService.apiConfig()).andReturn(null).once();
+ replay(mockConfigAdminService);
+
+ String location = PATH + "/https://test:8663";
+
+ final WebTarget wt = target();
+ Response response = wt.path(location).request(
+ MediaType.APPLICATION_JSON_TYPE).delete();
+
+ final int status = response.getStatus();
+
+ assertThat(status, is(304));
+
+ verify(mockConfigAdminService);
+ }
+}
diff --git a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebResourceTest.java b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebResourceTest.java
index 7d15e90..cf52f00 100644
--- a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebResourceTest.java
+++ b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/web/KubevirtNodeWebResourceTest.java
@@ -35,7 +35,6 @@
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-
import java.io.InputStream;
import static org.easymock.EasyMock.anyObject;
@@ -53,7 +52,7 @@
public class KubevirtNodeWebResourceTest extends ResourceTest {
final KubevirtNodeAdminService mockKubevirtNodeAdminService = createMock(KubevirtNodeAdminService.class);
- private static final String NODE_PATH = "configure/node";
+ private static final String PATH = "node";
private KubevirtNode kubevirtNode;
@@ -101,7 +100,7 @@
InputStream jsonStream = KubevirtNodeWebResourceTest.class
.getResourceAsStream("kubevirt-worker-node.json");
- Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.json(jsonStream));
final int status = response.getStatus();
@@ -123,7 +122,7 @@
InputStream jsonStream = KubevirtNodeWebResourceTest.class
.getResourceAsStream("kubevirt-worker-node.json");
- Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
.put(Entity.json(jsonStream));
final int status = response.getStatus();
@@ -144,7 +143,7 @@
InputStream jsonStream = KubevirtNodeWebResourceTest.class
.getResourceAsStream("kubevirt-worker-node.json");
- Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
.put(Entity.json(jsonStream));
final int status = response.getStatus();
@@ -162,7 +161,7 @@
expect(mockKubevirtNodeAdminService.removeNode(anyString())).andReturn(kubevirtNode).once();
replay(mockKubevirtNodeAdminService);
- String location = NODE_PATH + "/worker-node";
+ String location = PATH + "/worker-node";
final WebTarget wt = target();
Response response = wt.path(location).request(
@@ -183,7 +182,7 @@
expect(mockKubevirtNodeAdminService.node(anyString())).andReturn(null).once();
replay(mockKubevirtNodeAdminService);
- String location = NODE_PATH + "/worker-node";
+ String location = PATH + "/worker-node";
final WebTarget wt = target();
Response response = wt.path(location).request(
diff --git a/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/codec/KubevirtApiConfig.json b/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/codec/KubevirtApiConfig.json
new file mode 100644
index 0000000..f706cc3
--- /dev/null
+++ b/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/codec/KubevirtApiConfig.json
@@ -0,0 +1,9 @@
+{
+ "scheme" : "HTTPS",
+ "ipAddress" : "10.134.34.223",
+ "port" : 6443,
+ "token": "token",
+ "caCertData": "caCertData",
+ "clientCertData": "clientCertData",
+ "clientKeyData": "clientKeyData"
+}
\ No newline at end of file
diff --git a/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/web/kubevirt-api-config.json b/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/web/kubevirt-api-config.json
new file mode 100644
index 0000000..f706cc3
--- /dev/null
+++ b/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/web/kubevirt-api-config.json
@@ -0,0 +1,9 @@
+{
+ "scheme" : "HTTPS",
+ "ipAddress" : "10.134.34.223",
+ "port" : 6443,
+ "token": "token",
+ "caCertData": "caCertData",
+ "clientCertData": "clientCertData",
+ "clientKeyData": "clientKeyData"
+}
\ No newline at end of file