blob: 799dc8f0d4c016912ad33e85c74354630f1d197a [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
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 Condon081290d2017-11-02 13:15:08 +000016package org.onosproject.cfm.rest;
Sean Condon0e89bda2017-03-21 14:23:19 +000017
18import java.io.InputStream;
19import java.net.URI;
Sean Condon3a1efef2018-02-24 13:16:03 +000020import java.util.Optional;
Sean Condon0e89bda2017-03-21 14:23:19 +000021
22import javax.ws.rs.Consumes;
23import javax.ws.rs.DELETE;
24import javax.ws.rs.GET;
25import javax.ws.rs.POST;
26import javax.ws.rs.Path;
27import javax.ws.rs.PathParam;
28import javax.ws.rs.Produces;
29import javax.ws.rs.core.MediaType;
30import javax.ws.rs.core.Response;
31
32import org.onosproject.cfm.web.MaintenanceAssociationCodec;
33import org.onosproject.codec.JsonCodec;
34import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation;
35import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain;
36import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
37import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
38import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
39import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
40import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
41import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
42import org.onosproject.rest.AbstractWebResource;
43import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
46import com.fasterxml.jackson.databind.JsonNode;
47import com.fasterxml.jackson.databind.ObjectMapper;
48import com.fasterxml.jackson.databind.node.ObjectNode;
49
Ray Milkey86ee5e82018-04-02 15:33:07 -070050import static org.onlab.util.Tools.readTreeFromStream;
51
Sean Condon0e89bda2017-03-21 14:23:19 +000052/**
53 * Layer 2 CFM Maintenance Association web resource.
54 */
55@Path("md/{md_name}/ma")
56public 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 Condon081290d2017-11-02 13:15:08 +0000127 * @onos.rsModel MaCreate
Sean Condon0e89bda2017-03-21 14:23:19 +0000128 * @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 Condon3a1efef2018-02-24 13:16:03 +0000140 Optional<MaintenanceDomain> md = get(CfmMdService.class)
141 .getMaintenanceDomain(mdId);
142 if (!md.isPresent()) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000143 return Response.serverError().entity("{ \"failure\":\"md "
144 + mdName + " does not exist\" }").build();
145 }
146
147 ObjectMapper mapper = new ObjectMapper();
Ray Milkey86ee5e82018-04-02 15:33:07 -0700148 JsonNode cfg = readTreeFromStream(mapper, input);
Sean Condon0e89bda2017-03-21 14:23:19 +0000149 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}