Rename packages because of updated dependency
[alto.git] / alto-manager / src / main / java / org / opendaylight / alto / manager / AltoSet.java
1 /*
2  * Copyright (c) 2015 Yale University and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.alto.manager;
10
11 import java.io.IOException;
12
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;
18
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);
22
23   @Argument(index = 0, name = "property-name", description = "Property Name", required = true, multiValued = false)
24   String property = null;
25
26   @Argument(index = 1, name = "property-value", description = "Property Value", required = true, multiValued = false)
27   String value = null;
28
29   public AltoSet() {
30     super();
31   }
32
33   @Override
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.");
38       }
39       setDefaultNetworkMap();
40     } else {
41       throw new UnsupportedOperationException("Unsupported property \"" + property + "\".");
42     }
43     return null;
44   }
45
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);
51   }
52
53   private void setDefaultNetworkMap() throws IOException {
54     log.info("Setting default network map");
55     httpPut(AltoManagerConstants.IRD_DEFAULT_NETWORK_MAP_URL, queryData(value));
56   }
57
58   private String queryData(String resourceId) {
59     return "{\"alto-service:default-alto-network-map\":{\"alto-service:resource-id\":\"" + resourceId + "\"}}";
60   }
61 }