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