Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 org.onlab.util.Tools.nullIsNotFound; |
| 19 | |
| 20 | import javax.ws.rs.Consumes; |
| 21 | import javax.ws.rs.GET; |
| 22 | import javax.ws.rs.Path; |
| 23 | import javax.ws.rs.PathParam; |
| 24 | import javax.ws.rs.Produces; |
| 25 | import javax.ws.rs.core.MediaType; |
| 26 | import javax.ws.rs.core.Response; |
| 27 | |
| 28 | import org.onosproject.rest.AbstractWebResource; |
| 29 | import org.onosproject.vtnrsc.PortChainId; |
| 30 | import org.onosproject.vtnrsc.ServiceFunctionGroup; |
| 31 | import org.onosproject.vtnrsc.portchainsfmap.PortChainSfMapService; |
| 32 | |
| 33 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 34 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 35 | |
| 36 | /** |
| 37 | * Query service function and load details by port chain. |
| 38 | */ |
| 39 | |
| 40 | @Path("portChainSfMap") |
| 41 | public class PortChainSfMapWebResource extends AbstractWebResource { |
| 42 | |
| 43 | public static final String PORT_CHAIN_NOT_FOUND = "Port chain not found"; |
| 44 | public static final String PORT_CHAIN_ID_EXIST = "Port chain exists"; |
| 45 | public static final String PORT_CHAIN_ID_NOT_EXIST = "Port chain does not exist with identifier"; |
| 46 | |
| 47 | /** |
| 48 | * Get service function details of a specified port chain id. |
| 49 | * |
| 50 | * @param id port chain id |
| 51 | * @return 200 OK, 404 if given identifier does not exist |
| 52 | */ |
| 53 | @GET |
| 54 | @Path("{chainId}") |
| 55 | @Produces(MediaType.APPLICATION_JSON) |
| 56 | @Consumes(MediaType.APPLICATION_JSON) |
| 57 | public Response getPortChainSfMap(@PathParam("chainId") String id) { |
| 58 | |
| 59 | Iterable<ServiceFunctionGroup> serviceFunctionGroups = nullIsNotFound(get(PortChainSfMapService.class) |
| 60 | .getServiceFunctions(PortChainId.of(id)), |
| 61 | PORT_CHAIN_NOT_FOUND); |
| 62 | ObjectNode result = mapper().createObjectNode(); |
| 63 | ArrayNode portChainSfMap = result.putArray("portChainSfMap"); |
| 64 | if (serviceFunctionGroups != null) { |
| 65 | for (final ServiceFunctionGroup serviceFunctionGroup : serviceFunctionGroups) { |
| 66 | portChainSfMap.add(codec(ServiceFunctionGroup.class).encode(serviceFunctionGroup, this)); |
| 67 | } |
| 68 | } |
| 69 | return ok(result.toString()).build(); |
| 70 | } |
| 71 | } |