Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [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.vtnweb.resources; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.onlab.util.Tools.nullIsNotFound; |
| 20 | |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import javax.ws.rs.Consumes; |
| 24 | import javax.ws.rs.GET; |
| 25 | import javax.ws.rs.Path; |
| 26 | import javax.ws.rs.PathParam; |
| 27 | import javax.ws.rs.Produces; |
| 28 | import javax.ws.rs.core.MediaType; |
| 29 | import javax.ws.rs.core.Response; |
| 30 | |
| 31 | import org.onosproject.codec.CodecContext; |
| 32 | import org.onosproject.rest.AbstractWebResource; |
| 33 | import org.onosproject.vtnrsc.LoadBalanceId; |
| 34 | import org.onosproject.vtnrsc.PortChain; |
| 35 | import org.onosproject.vtnrsc.PortChainId; |
| 36 | import org.onosproject.vtnrsc.portchain.PortChainService; |
| 37 | |
| 38 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 39 | |
| 40 | /** |
| 41 | * Query and program port chain. |
| 42 | */ |
| 43 | |
| 44 | @Path("portChainDeviceMap") |
| 45 | public class PortChainDeviceMapWebResource extends AbstractWebResource { |
| 46 | |
| 47 | public static final String PORT_CHAIN_NOT_FOUND = "Port chain not found"; |
| 48 | public static final String PORT_CHAIN_ID_EXIST = "Port chain exists"; |
| 49 | public static final String PORT_CHAIN_ID_NOT_EXIST = "Port chain does not exist with identifier"; |
| 50 | |
| 51 | private static final String NAME = "name"; |
| 52 | private static final String ID = "id"; |
| 53 | private static final String CLASSIFIERS = "classifiers"; |
| 54 | private static final String FORWARDERS = "forwarders"; |
| 55 | private static final String LOADBALANCEID = "loadBalanceId"; |
| 56 | |
| 57 | /** |
| 58 | * Get details of a specified port chain id. |
| 59 | * |
| 60 | * @param id port chain id |
| 61 | * @return 200 OK, 404 if given identifier does not exist |
| 62 | */ |
| 63 | @GET |
| 64 | @Path("{chain_id}") |
| 65 | @Produces(MediaType.APPLICATION_JSON) |
| 66 | @Consumes(MediaType.APPLICATION_JSON) |
| 67 | public Response getPortChainDeviceMap(@PathParam("chain_id") String id) { |
| 68 | |
| 69 | PortChain portChain = nullIsNotFound(get(PortChainService.class).getPortChain(PortChainId.of(id)), |
| 70 | PORT_CHAIN_NOT_FOUND); |
| 71 | ObjectNode result = mapper().createObjectNode(); |
| 72 | result.set("portChainDeviceMap", encode(portChain, this)); |
| 73 | |
| 74 | return ok(result.toString()).build(); |
| 75 | } |
| 76 | |
| 77 | private ObjectNode encode(PortChain portChain, CodecContext context) { |
| 78 | checkNotNull(portChain, "portChain cannot be null"); |
| 79 | ObjectNode result = context.mapper().createObjectNode(); |
| 80 | result.put(ID, portChain.portChainId().toString()) |
| 81 | .put(NAME, portChain.name()); |
| 82 | |
| 83 | Set<LoadBalanceId> loadBalanceIds = portChain.getLoadBalancePathMapKeys(); |
| 84 | for (LoadBalanceId id : loadBalanceIds) { |
| 85 | result.put(LOADBALANCEID, id.toString()) |
| 86 | .put(CLASSIFIERS, portChain.getSfcClassifiers(id).toString()) |
| 87 | .put(FORWARDERS, portChain.getSfcForwarders(id).toString()); |
| 88 | } |
| 89 | return result; |
| 90 | } |
| 91 | } |