sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.openstacknode.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onlab.osgi.DefaultServiceDirectory; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.openstacknode.api.NodeState; |
| 25 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 26 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
| 27 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
| 28 | import org.onosproject.openstacknode.impl.DefaultOpenstackNode; |
| 29 | import org.onosproject.rest.AbstractWebResource; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import javax.ws.rs.Consumes; |
| 34 | import javax.ws.rs.DELETE; |
| 35 | import javax.ws.rs.POST; |
| 36 | import javax.ws.rs.PUT; |
| 37 | import javax.ws.rs.Path; |
| 38 | import javax.ws.rs.Produces; |
| 39 | import javax.ws.rs.core.Context; |
| 40 | import javax.ws.rs.core.MediaType; |
| 41 | import javax.ws.rs.core.Response; |
| 42 | import javax.ws.rs.core.UriBuilder; |
| 43 | import javax.ws.rs.core.UriInfo; |
| 44 | import java.io.InputStream; |
| 45 | import java.util.Set; |
| 46 | |
| 47 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 48 | import static javax.ws.rs.core.Response.created; |
Ray Milkey | b784adb | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 49 | import static org.onlab.util.Tools.readTreeFromStream; |
| 50 | |
| 51 | /** |
| 52 | * Handles REST API call of openstack node config. |
| 53 | */ |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 54 | |
| 55 | @Path("configure") |
| 56 | public class OpenstackNodeWebResource extends AbstractWebResource { |
| 57 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 58 | |
| 59 | private static final String MESSAGE_NODE = "Received node %s request"; |
| 60 | private static final String NODES = "nodes"; |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 61 | private static final String CREATE = "CREATE"; |
| 62 | private static final String UPDATE = "UPDATE"; |
| 63 | private static final String NODE_ID = "NODE_ID"; |
| 64 | private static final String DELETE = "DELETE"; |
| 65 | |
| 66 | private static final String HOST_NAME = "hostname"; |
| 67 | private static final String TYPE = "type"; |
| 68 | private static final String MANAGEMENT_IP = "managementIp"; |
| 69 | private static final String DATA_IP = "dataIp"; |
| 70 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
| 71 | private static final String VLAN_INTF_NAME = "vlanPort"; |
| 72 | |
| 73 | // GATEWAY node specific fields |
| 74 | private static final String ROUTER_BRIDGE = "routerBridge"; |
| 75 | |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 76 | |
| 77 | private final OpenstackNodeAdminService osNodeAdminService = |
| 78 | DefaultServiceDirectory.getService(OpenstackNodeAdminService.class); |
| 79 | private final OpenstackNodeService osNodeService = |
| 80 | DefaultServiceDirectory.getService(OpenstackNodeService.class); |
| 81 | |
| 82 | @Context |
| 83 | private UriInfo uriInfo; |
| 84 | |
| 85 | @POST |
| 86 | @Consumes(MediaType.APPLICATION_JSON) |
| 87 | @Produces(MediaType.APPLICATION_JSON) |
| 88 | public Response createNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 89 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 90 | |
| 91 | readNodeConfiguration(input).forEach(osNode -> { |
| 92 | OpenstackNode existing = osNodeService.node(osNode.hostname()); |
| 93 | if (existing == null) { |
| 94 | osNodeAdminService.createNode(osNode); |
| 95 | } |
| 96 | }); |
| 97 | |
| 98 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 99 | .path(NODES) |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 100 | .path(NODE_ID); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 101 | |
| 102 | return created(locationBuilder.build()).build(); |
| 103 | } |
| 104 | |
| 105 | @PUT |
| 106 | @Consumes(MediaType.APPLICATION_JSON) |
| 107 | @Produces(MediaType.APPLICATION_JSON) |
| 108 | public Response updateNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 109 | log.trace(String.format(MESSAGE_NODE, UPDATE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 110 | |
| 111 | Set<OpenstackNode> nodes = readNodeConfiguration(input); |
| 112 | for (OpenstackNode osNode: nodes) { |
| 113 | OpenstackNode existing = osNodeService.node(osNode.hostname()); |
| 114 | if (existing == null) { |
| 115 | log.warn("There is no node configuration to update : {}", osNode.hostname()); |
| 116 | return Response.notModified().build(); |
| 117 | } else if (!existing.equals(osNode)) { |
| 118 | osNodeAdminService.updateNode(osNode); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return Response.ok().build(); |
| 123 | } |
| 124 | |
| 125 | @DELETE |
| 126 | @Consumes(MediaType.APPLICATION_JSON) |
| 127 | @Produces(MediaType.APPLICATION_JSON) |
| 128 | public Response deleteNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 129 | log.trace(String.format(MESSAGE_NODE, DELETE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 130 | |
| 131 | Set<OpenstackNode> nodes = readNodeConfiguration(input); |
| 132 | for (OpenstackNode osNode: nodes) { |
| 133 | OpenstackNode existing = osNodeService.node(osNode.hostname()); |
| 134 | if (existing == null) { |
| 135 | log.warn("There is no node configuration to delete : {}", osNode.hostname()); |
| 136 | return Response.notModified().build(); |
| 137 | } else { |
| 138 | osNodeAdminService.removeNode(osNode.hostname()); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return Response.ok().build(); |
| 143 | } |
| 144 | |
| 145 | private Set<OpenstackNode> readNodeConfiguration(InputStream input) { |
| 146 | Set<OpenstackNode> nodeSet = Sets.newHashSet(); |
| 147 | try { |
Ray Milkey | b784adb | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 148 | JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 149 | ArrayNode nodes = (ArrayNode) jsonTree.path(NODES); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 150 | nodes.forEach(node -> { |
| 151 | try { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 152 | String hostname = node.get(HOST_NAME).asText(); |
| 153 | String type = node.get(TYPE).asText(); |
| 154 | String mIp = node.get(MANAGEMENT_IP).asText(); |
| 155 | String iBridge = node.get(INTEGRATION_BRIDGE).asText(); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 156 | String rBridge = null; |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 157 | if (node.get(ROUTER_BRIDGE) != null) { |
| 158 | rBridge = node.get(ROUTER_BRIDGE).asText(); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 159 | } |
| 160 | DefaultOpenstackNode.Builder nodeBuilder = DefaultOpenstackNode.builder() |
| 161 | .hostname(hostname) |
| 162 | .type(OpenstackNode.NodeType.valueOf(type)) |
| 163 | .managementIp(IpAddress.valueOf(mIp)) |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 164 | .intgBridge(DeviceId.deviceId(iBridge)) |
| 165 | .state(NodeState.INIT); |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 166 | |
| 167 | if (node.get(VLAN_INTF_NAME) != null) { |
| 168 | nodeBuilder.vlanIntf(node.get(VLAN_INTF_NAME).asText()); |
| 169 | } |
| 170 | if (node.get(DATA_IP) != null) { |
| 171 | nodeBuilder.dataIp(IpAddress.valueOf(node.get(DATA_IP).asText())); |
| 172 | } |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 173 | if (rBridge != null) { |
| 174 | nodeBuilder.routerBridge(DeviceId.deviceId(rBridge)); |
| 175 | } |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 176 | |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 177 | log.trace("node is {}", nodeBuilder.build().toString()); |
| 178 | nodeSet.add(nodeBuilder.build()); |
| 179 | } catch (Exception e) { |
| 180 | log.error(e.toString()); |
| 181 | throw new IllegalArgumentException(); |
| 182 | } |
| 183 | }); |
| 184 | } catch (Exception e) { |
| 185 | throw new IllegalArgumentException(e); |
| 186 | } |
| 187 | |
| 188 | return nodeSet; |
| 189 | } |
| 190 | } |