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; |
daniel park | b18424c | 2018-02-05 15:43:43 +0900 | [diff] [blame^] | 30 | import static org.onosproject.openstacknode.api.Constants.GATEWAY; |
| 31 | import static org.onosproject.openstacknode.api.Constants.HOST_NAME; |
| 32 | import static org.onosproject.openstacknode.api.Constants.MANAGEMENT_IP; |
| 33 | import static org.onosproject.openstacknode.api.Constants.DATA_IP; |
| 34 | import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME; |
| 35 | import static org.onosproject.openstacknode.api.Constants.UPLINK_PORT; |
| 36 | |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import javax.ws.rs.Consumes; |
| 41 | import javax.ws.rs.DELETE; |
| 42 | import javax.ws.rs.POST; |
| 43 | import javax.ws.rs.PUT; |
| 44 | import javax.ws.rs.Path; |
| 45 | import javax.ws.rs.Produces; |
| 46 | import javax.ws.rs.core.Context; |
| 47 | import javax.ws.rs.core.MediaType; |
| 48 | import javax.ws.rs.core.Response; |
| 49 | import javax.ws.rs.core.UriBuilder; |
| 50 | import javax.ws.rs.core.UriInfo; |
| 51 | import java.io.InputStream; |
| 52 | import java.util.Set; |
| 53 | |
| 54 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 55 | import static javax.ws.rs.core.Response.created; |
| 56 | |
| 57 | @Path("configure") |
| 58 | public class OpenstackNodeWebResource extends AbstractWebResource { |
| 59 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 60 | |
| 61 | private static final String MESSAGE_NODE = "Received node %s request"; |
| 62 | private static final String NODES = "nodes"; |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 63 | private static final String CREATE = "CREATE"; |
| 64 | private static final String UPDATE = "UPDATE"; |
| 65 | private static final String NODE_ID = "NODE_ID"; |
| 66 | private static final String DELETE = "DELETE"; |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 67 | private static final String TYPE = "type"; |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 68 | private static final String INTEGRATION_BRIDGE = "integrationBridge"; |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 69 | |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 70 | |
| 71 | private final OpenstackNodeAdminService osNodeAdminService = |
| 72 | DefaultServiceDirectory.getService(OpenstackNodeAdminService.class); |
| 73 | private final OpenstackNodeService osNodeService = |
| 74 | DefaultServiceDirectory.getService(OpenstackNodeService.class); |
| 75 | |
| 76 | @Context |
| 77 | private UriInfo uriInfo; |
| 78 | |
| 79 | @POST |
| 80 | @Consumes(MediaType.APPLICATION_JSON) |
| 81 | @Produces(MediaType.APPLICATION_JSON) |
| 82 | public Response createNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 83 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 84 | |
| 85 | readNodeConfiguration(input).forEach(osNode -> { |
| 86 | OpenstackNode existing = osNodeService.node(osNode.hostname()); |
| 87 | if (existing == null) { |
| 88 | osNodeAdminService.createNode(osNode); |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 93 | .path(NODES) |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 94 | .path(NODE_ID); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 95 | |
| 96 | return created(locationBuilder.build()).build(); |
| 97 | } |
| 98 | |
| 99 | @PUT |
| 100 | @Consumes(MediaType.APPLICATION_JSON) |
| 101 | @Produces(MediaType.APPLICATION_JSON) |
| 102 | public Response updateNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 103 | log.trace(String.format(MESSAGE_NODE, UPDATE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 104 | |
| 105 | Set<OpenstackNode> nodes = readNodeConfiguration(input); |
| 106 | for (OpenstackNode osNode: nodes) { |
| 107 | OpenstackNode existing = osNodeService.node(osNode.hostname()); |
| 108 | if (existing == null) { |
| 109 | log.warn("There is no node configuration to update : {}", osNode.hostname()); |
| 110 | return Response.notModified().build(); |
| 111 | } else if (!existing.equals(osNode)) { |
| 112 | osNodeAdminService.updateNode(osNode); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return Response.ok().build(); |
| 117 | } |
| 118 | |
| 119 | @DELETE |
| 120 | @Consumes(MediaType.APPLICATION_JSON) |
| 121 | @Produces(MediaType.APPLICATION_JSON) |
| 122 | public Response deleteNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 123 | log.trace(String.format(MESSAGE_NODE, DELETE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 124 | |
| 125 | Set<OpenstackNode> nodes = readNodeConfiguration(input); |
| 126 | for (OpenstackNode osNode: nodes) { |
| 127 | OpenstackNode existing = osNodeService.node(osNode.hostname()); |
| 128 | if (existing == null) { |
| 129 | log.warn("There is no node configuration to delete : {}", osNode.hostname()); |
| 130 | return Response.notModified().build(); |
| 131 | } else { |
| 132 | osNodeAdminService.removeNode(osNode.hostname()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return Response.ok().build(); |
| 137 | } |
| 138 | |
| 139 | private Set<OpenstackNode> readNodeConfiguration(InputStream input) { |
| 140 | Set<OpenstackNode> nodeSet = Sets.newHashSet(); |
| 141 | try { |
| 142 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 143 | ArrayNode nodes = (ArrayNode) jsonTree.path(NODES); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 144 | nodes.forEach(node -> { |
| 145 | try { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 146 | String hostname = node.get(HOST_NAME).asText(); |
| 147 | String type = node.get(TYPE).asText(); |
| 148 | String mIp = node.get(MANAGEMENT_IP).asText(); |
| 149 | String iBridge = node.get(INTEGRATION_BRIDGE).asText(); |
daniel park | b18424c | 2018-02-05 15:43:43 +0900 | [diff] [blame^] | 150 | |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 151 | DefaultOpenstackNode.Builder nodeBuilder = DefaultOpenstackNode.builder() |
| 152 | .hostname(hostname) |
| 153 | .type(OpenstackNode.NodeType.valueOf(type)) |
| 154 | .managementIp(IpAddress.valueOf(mIp)) |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 155 | .intgBridge(DeviceId.deviceId(iBridge)) |
| 156 | .state(NodeState.INIT); |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 157 | |
daniel park | b18424c | 2018-02-05 15:43:43 +0900 | [diff] [blame^] | 158 | if (type.equals(GATEWAY)) { |
| 159 | nodeBuilder.uplinkPort(node.get(UPLINK_PORT).asText()); |
| 160 | } |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 161 | if (node.get(VLAN_INTF_NAME) != null) { |
| 162 | nodeBuilder.vlanIntf(node.get(VLAN_INTF_NAME).asText()); |
| 163 | } |
| 164 | if (node.get(DATA_IP) != null) { |
| 165 | nodeBuilder.dataIp(IpAddress.valueOf(node.get(DATA_IP).asText())); |
| 166 | } |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 167 | |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 168 | log.trace("node is {}", nodeBuilder.build().toString()); |
| 169 | nodeSet.add(nodeBuilder.build()); |
| 170 | } catch (Exception e) { |
| 171 | log.error(e.toString()); |
| 172 | throw new IllegalArgumentException(); |
| 173 | } |
| 174 | }); |
| 175 | } catch (Exception e) { |
| 176 | throw new IllegalArgumentException(e); |
| 177 | } |
| 178 | |
| 179 | return nodeSet; |
| 180 | } |
| 181 | } |