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