org.onosproject.rest.resources.NetworkConfigWebResource Maven / Gradle / Ivy
/*
* Copyright 2015-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.rest.resources;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.net.config.Config;
import org.onosproject.net.config.InvalidConfigException;
import org.onosproject.net.config.NetworkConfigService;
import org.onosproject.net.config.SubjectFactory;
import org.onosproject.rest.AbstractWebResource;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
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.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static org.onlab.util.Tools.emptyIsNotFound;
import static org.onlab.util.Tools.nullIsNotFound;
import static org.onlab.util.Tools.readTreeFromStream;
/**
* Manage network configurations.
*/
@Path("network/configuration")
public class NetworkConfigWebResource extends AbstractWebResource {
//FIX ME not found Multi status error code 207 in jaxrs Response Status.
private static final int MULTI_STATUS_RESPONE = 207;
private String subjectClassNotFoundErrorString(String subjectClassKey) {
return "Config for '" + subjectClassKey + "' not found";
}
private String subjectNotFoundErrorString(String subjectClassKey,
String subjectKey) {
return "Config for '"
+ subjectClassKey + "/" + subjectKey
+ "' not found";
}
private String configKeyNotFoundErrorString(String subjectClassKey,
String subjectKey,
String configKey) {
return "Config for '"
+ subjectClassKey + "/" + subjectKey + "/" + configKey
+ "' not found";
}
private String subjectClassInvalidErrorString(String subjectClassKey) {
return "Config for '" + subjectClassKey + "' is invalid";
}
private String subjectClassNotValidErrorString(String subjectClassKey) {
return "subjectClassKey '" + subjectClassKey + "' not found";
}
/**
* Gets entire network configuration base.
*
* @return 200 OK with network configuration JSON
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("unchecked")
public Response download() {
NetworkConfigService service = get(NetworkConfigService.class);
ObjectNode root = mapper().createObjectNode();
service.getSubjectClasses().forEach(sc -> {
SubjectFactory subjectFactory = service.getSubjectFactory(sc);
produceJson(service, newObject(root, subjectFactory.subjectClassKey()),
subjectFactory, sc);
});
return ok(root).build();
}
/**
* Gets all network configuration for a subject class.
*
* @param subjectClassKey subject class key
* @return 200 OK with network configuration JSON
*/
@GET
@Path("{subjectClassKey}")
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("unchecked")
public Response download(@PathParam("subjectClassKey") String subjectClassKey) {
NetworkConfigService service = get(NetworkConfigService.class);
ObjectNode root = mapper().createObjectNode();
SubjectFactory subjectFactory =
nullIsNotFound(service.getSubjectFactory(subjectClassKey),
subjectClassNotFoundErrorString(subjectClassKey));
produceJson(service, root, subjectFactory, subjectFactory.subjectClass());
return ok(root).build();
}
/**
* Gets all network configuration for a subjectKey.
*
* @param subjectClassKey subjectKey class key
* @param subjectKey subjectKey key
* @return 200 OK with network configuration JSON
*/
@GET
@Path("{subjectClassKey}/{subjectKey}")
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("unchecked")
public Response download(@PathParam("subjectClassKey") String subjectClassKey,
@PathParam("subjectKey") String subjectKey) {
NetworkConfigService service = get(NetworkConfigService.class);
ObjectNode root = mapper().createObjectNode();
SubjectFactory subjectFactory =
nullIsNotFound(service.getSubjectFactory(subjectClassKey),
subjectClassNotFoundErrorString(subjectClassKey));
produceSubjectJson(service, root, subjectFactory.createSubject(subjectKey),
true,
subjectNotFoundErrorString(subjectClassKey, subjectKey));
return ok(root).build();
}
/**
* Gets specific network configuration for a subjectKey.
*
* @param subjectClassKey subjectKey class key
* @param subjectKey subjectKey key
* @param configKey configuration class key
* @return 200 OK with network configuration JSON
*/
@GET
@Path("{subjectClassKey}/{subjectKey}/{configKey}")
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("unchecked")
public Response download(@PathParam("subjectClassKey") String subjectClassKey,
@PathParam("subjectKey") String subjectKey,
@PathParam("configKey") String configKey) {
NetworkConfigService service = get(NetworkConfigService.class);
SubjectFactory subjectFactory =
nullIsNotFound(service.getSubjectFactory(subjectClassKey),
subjectClassNotFoundErrorString(subjectClassKey));
Object subject =
nullIsNotFound(subjectFactory.createSubject(subjectKey),
subjectNotFoundErrorString(subjectClassKey, subjectKey));
Class configClass =
nullIsNotFound(service.getConfigClass(subjectClassKey, configKey),
configKeyNotFoundErrorString(subjectClassKey, subjectKey, configKey));
Config config = nullIsNotFound((Config) service.getConfig(subject, configClass),
configKeyNotFoundErrorString(subjectClassKey,
subjectKey,
configKey));
return ok(config.node()).build();
}
@SuppressWarnings("unchecked")
private void produceJson(NetworkConfigService service, ObjectNode node,
SubjectFactory subjectFactory, Class subjectClass) {
service.getSubjects(subjectClass).forEach(s ->
produceSubjectJson(service, newObject(node, subjectFactory.subjectKey(s)), s, false, ""));
}
private void produceSubjectJson(NetworkConfigService service, ObjectNode node,
Object subject,
boolean emptyIsError,
String emptyErrorMessage) {
Set extends Config
© 2015 - 2025 Weber Informatics LLC | Privacy Policy