blob: e0390a90953fb1a06c186ea0de268a2e3c092ab2 [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
50/**
51 * Layer 2 CFM Maintenance Association web resource.
52 */
53@Path("md/{md_name}/ma")
54public 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 Condon081290d2017-11-02 13:15:08 +0000125 * @onos.rsModel MaCreate
Sean Condon0e89bda2017-03-21 14:23:19 +0000126 * @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 Condon3a1efef2018-02-24 13:16:03 +0000138 Optional<MaintenanceDomain> md = get(CfmMdService.class)
139 .getMaintenanceDomain(mdId);
140 if (!md.isPresent()) {
Sean Condon0e89bda2017-03-21 14:23:19 +0000141 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}