Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | */ |
Sean Condon | 081290d | 2017-11-02 13:15:08 +0000 | [diff] [blame] | 16 | package org.onosproject.cfm.rest; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 17 | |
| 18 | import java.io.InputStream; |
| 19 | import java.net.URI; |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 20 | import java.util.Optional; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 21 | |
| 22 | import javax.ws.rs.Consumes; |
| 23 | import javax.ws.rs.DELETE; |
| 24 | import javax.ws.rs.GET; |
| 25 | import javax.ws.rs.POST; |
| 26 | import javax.ws.rs.Path; |
| 27 | import javax.ws.rs.PathParam; |
| 28 | import javax.ws.rs.Produces; |
| 29 | import javax.ws.rs.core.MediaType; |
| 30 | import javax.ws.rs.core.Response; |
| 31 | |
| 32 | import org.onosproject.cfm.web.MaintenanceAssociationCodec; |
| 33 | import org.onosproject.codec.JsonCodec; |
| 34 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation; |
| 35 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain; |
| 36 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr; |
| 37 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 38 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 39 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 40 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 41 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService; |
| 42 | import org.onosproject.rest.AbstractWebResource; |
| 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
| 46 | import com.fasterxml.jackson.databind.JsonNode; |
| 47 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 48 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 49 | |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 50 | import static org.onlab.util.Tools.readTreeFromStream; |
| 51 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 52 | /** |
| 53 | * Layer 2 CFM Maintenance Association web resource. |
| 54 | */ |
| 55 | @Path("md/{md_name}/ma") |
| 56 | public class MaWebResource extends AbstractWebResource { |
| 57 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 58 | |
| 59 | /** |
| 60 | * Get Maintenance Association by MD and MA name. |
| 61 | * |
| 62 | * @param mdName The name of a Maintenance Domain |
| 63 | * @param maName The name of a Maintenance Association belonging to the MD |
| 64 | * @return 200 OK with details of MA or 500 on Error |
| 65 | */ |
| 66 | @GET |
| 67 | @Path("{ma_name}") |
| 68 | @Produces(MediaType.APPLICATION_JSON) |
| 69 | @Consumes(MediaType.APPLICATION_JSON) |
| 70 | public Response getMa(@PathParam("md_name") String mdName, |
| 71 | @PathParam("ma_name") String maName) { |
| 72 | log.debug("GET called for MA {}/{}", mdName, maName); |
| 73 | try { |
| 74 | MdId mdId = MdIdCharStr.asMdId(mdName); |
| 75 | MaIdShort maId = MaIdCharStr.asMaId(maName); |
| 76 | MaintenanceAssociation ma = get(CfmMdService.class) |
| 77 | .getMaintenanceAssociation(mdId, maId) |
| 78 | .orElseThrow(() -> new IllegalArgumentException( |
| 79 | "MA " + maName + " not Found")); |
| 80 | ObjectNode node = mapper().createObjectNode(); |
| 81 | node.set("ma", codec(MaintenanceAssociation.class).encode(ma, this)); |
| 82 | return ok(node).build(); |
| 83 | } catch (IllegalArgumentException e) { |
| 84 | log.error("Get MA {} failed", mdName + "/" + maName, e); |
| 85 | return Response.serverError() |
| 86 | .entity("{ \"failure\":\"" + e.toString() + "\" }").build(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Delete the Maintenance Association by MD and MA name. |
| 92 | * |
| 93 | * @param mdName The name of a Maintenance Domain |
| 94 | * @param maName The name of a Maintenance Association belonging to the MD |
| 95 | * @return 200 OK if removed, 304 if item was not found or 500 on Error |
| 96 | */ |
| 97 | @DELETE |
| 98 | @Path("{ma_name}") |
| 99 | @Consumes(MediaType.APPLICATION_JSON) |
| 100 | @Produces(MediaType.APPLICATION_JSON) |
| 101 | public Response deleteMa(@PathParam("md_name") String mdName, |
| 102 | @PathParam("ma_name") String maName) { |
| 103 | log.debug("DELETE called for MA {}/{}", mdName, maName); |
| 104 | try { |
| 105 | MdId mdId = MdIdCharStr.asMdId(mdName); |
| 106 | MaIdShort maId = MaIdCharStr.asMaId(maName); |
| 107 | boolean deleted = get(CfmMdService.class) |
| 108 | .deleteMaintenanceAssociation(mdId, maId); |
| 109 | if (!deleted) { |
| 110 | return Response.notModified(mdName + "/" |
| 111 | + maName + " did not exist").build(); |
| 112 | } else { |
| 113 | return ok("{ \"success\":\"deleted " + mdName |
| 114 | + "/" + maName + "\" }").build(); |
| 115 | } |
| 116 | } catch (CfmConfigException e) { |
| 117 | log.error("Delete Maintenance Association {} failed", |
| 118 | mdName + "/" + maName, e); |
| 119 | return Response.serverError().entity("{ \"failure\":\"" + |
| 120 | e.toString() + "\" }").build(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Create Maintenance Association by MD and MA name. |
| 126 | * |
Sean Condon | 081290d | 2017-11-02 13:15:08 +0000 | [diff] [blame] | 127 | * @onos.rsModel MaCreate |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 128 | * @param mdName The name of a Maintenance Domain |
| 129 | * @param input A JSON formatted input stream specifying the MA parameters |
| 130 | * @return 200 OK or 500 on error |
| 131 | */ |
| 132 | @POST |
| 133 | @Consumes(MediaType.APPLICATION_JSON) |
| 134 | @Produces(MediaType.APPLICATION_JSON) |
| 135 | public Response createMaintenanceAssociation(@PathParam("md_name") String mdName, |
| 136 | InputStream input) { |
| 137 | log.debug("POST called to Create MA"); |
| 138 | try { |
| 139 | MdId mdId = MdIdCharStr.asMdId(mdName); |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 140 | Optional<MaintenanceDomain> md = get(CfmMdService.class) |
| 141 | .getMaintenanceDomain(mdId); |
| 142 | if (!md.isPresent()) { |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 143 | return Response.serverError().entity("{ \"failure\":\"md " |
| 144 | + mdName + " does not exist\" }").build(); |
| 145 | } |
| 146 | |
| 147 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 148 | JsonNode cfg = readTreeFromStream(mapper, input); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 149 | JsonCodec<MaintenanceAssociation> maCodec = |
| 150 | codec(MaintenanceAssociation.class); |
| 151 | |
| 152 | MaintenanceAssociation ma; |
| 153 | try { |
| 154 | ma = ((MaintenanceAssociationCodec) maCodec) |
| 155 | .decode((ObjectNode) cfg, this, mdId.getNameLength()); |
| 156 | } catch (Exception e) { |
| 157 | log.error("Create MaintenanceAssociation on MD {} failed", mdName, e); |
| 158 | return Response.serverError() |
| 159 | .entity("{ \"failure\":\"" + e.toString() + "\" }") |
| 160 | .build(); |
| 161 | } |
| 162 | |
| 163 | Boolean alreadyExists = get(CfmMdService.class) |
| 164 | .createMaintenanceAssociation(mdId, ma); |
| 165 | if (alreadyExists) { |
| 166 | return Response.notModified(mdName + "/" + ma.maId() + |
| 167 | " already exists").build(); |
| 168 | } |
| 169 | return Response |
| 170 | .created(new URI("md/" + mdName + "/ma/" + ma.maId())) |
| 171 | .entity("{ \"success\":\"" + mdName + "/" + ma.maId() + " created\" }") |
| 172 | .build(); |
| 173 | |
| 174 | } catch (Exception | CfmConfigException e) { |
| 175 | log.error("Create MaintenanceAssociation on MD {} failed", mdName, e); |
| 176 | return Response.serverError() |
| 177 | .entity("{ \"failure\":\"" + e.toString() + "\" }") |
| 178 | .build(); |
| 179 | } |
| 180 | } |
| 181 | } |