2 * Copyright (c) 2015 Yale University and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.alto.manager;
11 import java.io.IOException;
13 import org.apache.http.HttpResponse;
14 import org.apache.karaf.shell.commands.Argument;
15 import org.apache.karaf.shell.commands.Command;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 @Command(scope = "alto", name = "set", description = "Set property")
20 public class AltoSet extends AltoManager {
21 private static final Logger log = LoggerFactory.getLogger(AltoSet.class);
23 @Argument(index = 0, name = "property-name", description = "Property Name", required = true, multiValued = false)
24 String property = null;
26 @Argument(index = 1, name = "property-value", description = "Property Value", required = true, multiValued = false)
34 protected Object doExecute() throws Exception {
35 if (AltoManagerConstants.DEFAULT_NETWORK_MAP_PROPERTY.equals(property)) {
36 if (!ifNetworkMapExist(value)) {
37 throw new RuntimeException("Network map \"" + value + "\" does not exist.");
39 setDefaultNetworkMap();
41 throw new UnsupportedOperationException("Unsupported property \"" + property + "\".");
46 private boolean ifNetworkMapExist(String resourceId) throws IOException {
47 HttpResponse response = httpGet(AltoManagerConstants.NETWORK_MAP_URL + resourceId);
48 logResponse(response);
49 int statusCode = response.getStatusLine().getStatusCode();
50 return (statusCode == 200);
53 private void setDefaultNetworkMap() throws IOException {
54 log.info("Setting default network map");
55 httpPut(AltoManagerConstants.IRD_DEFAULT_NETWORK_MAP_URL, queryData(value));
58 private String queryData(String resourceId) {
59 return "{\"alto-service:default-alto-network-map\":{\"alto-service:resource-id\":\"" + resourceId + "\"}}";