blob: c8613149d8e502fe4873b38a6860a6f3c2f4b30d [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 */
16package org.onosproject.cfm.impl;
17
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 *
124 * @param mdName The name of a Maintenance Domain
125 * @param input A JSON formatted input stream specifying the MA parameters
126 * @return 200 OK or 500 on error
127 */
128 @POST
129 @Consumes(MediaType.APPLICATION_JSON)
130 @Produces(MediaType.APPLICATION_JSON)
131 public Response createMaintenanceAssociation(@PathParam("md_name") String mdName,
132 InputStream input) {
133 log.debug("POST called to Create MA");
134 try {
135 MdId mdId = MdIdCharStr.asMdId(mdName);
136 MaintenanceDomain md = get(CfmMdService.class)
137 .getMaintenanceDomain(mdId).get();
138 if (md == null) {
139 return Response.serverError().entity("{ \"failure\":\"md "
140 + mdName + " does not exist\" }").build();
141 }
142
143 ObjectMapper mapper = new ObjectMapper();
144 JsonNode cfg = mapper.readTree(input);
145 JsonCodec<MaintenanceAssociation> maCodec =
146 codec(MaintenanceAssociation.class);
147
148 MaintenanceAssociation ma;
149 try {
150 ma = ((MaintenanceAssociationCodec) maCodec)
151 .decode((ObjectNode) cfg, this, mdId.getNameLength());
152 } catch (Exception e) {
153 log.error("Create MaintenanceAssociation on MD {} failed", mdName, e);
154 return Response.serverError()
155 .entity("{ \"failure\":\"" + e.toString() + "\" }")
156 .build();
157 }
158
159 Boolean alreadyExists = get(CfmMdService.class)
160 .createMaintenanceAssociation(mdId, ma);
161 if (alreadyExists) {
162 return Response.notModified(mdName + "/" + ma.maId() +
163 " already exists").build();
164 }
165 return Response
166 .created(new URI("md/" + mdName + "/ma/" + ma.maId()))
167 .entity("{ \"success\":\"" + mdName + "/" + ma.maId() + " created\" }")
168 .build();
169
170 } catch (Exception | CfmConfigException e) {
171 log.error("Create MaintenanceAssociation on MD {} failed", mdName, e);
172 return Response.serverError()
173 .entity("{ \"failure\":\"" + e.toString() + "\" }")
174 .build();
175 }
176 }
177}