blob: aa16d9e80a3dd590273a2f011a4b56634546ec83 [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;
20import java.util.Collection;
21
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.incubator.net.l2monitoring.cfm.MaintenanceDomain;
33import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
34import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
35import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
36import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
37import org.onosproject.rest.AbstractWebResource;
38import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
41import com.fasterxml.jackson.databind.JsonNode;
42import com.fasterxml.jackson.databind.ObjectMapper;
43import com.fasterxml.jackson.databind.node.ArrayNode;
44import com.fasterxml.jackson.databind.node.ObjectNode;
45
46/**
47 * Layer 2 CFM Maintenance Domain web resource.
48 */
49@Path("md")
50public class MdWebResource extends AbstractWebResource {
51 private final Logger log = LoggerFactory.getLogger(getClass());
Sean Condon0e89bda2017-03-21 14:23:19 +000052
53 /**
54 * Get all Maintenance Domains.
55 *
56 * @return 200 OK with a list of MDs and their children
57 */
58 @GET
59 @Produces(MediaType.APPLICATION_JSON)
60 @Consumes(MediaType.APPLICATION_JSON)
61 public Response getMds() {
62 log.debug("GET called for all MDs");
63 Collection<MaintenanceDomain> mdMap =
64 get(CfmMdService.class).getAllMaintenanceDomain();
65 ArrayNode arrayNode = mapper().createArrayNode();
66 arrayNode.add(codec(MaintenanceDomain.class).encode(mdMap, this));
67 return ok(mapper().createObjectNode().set("mds", arrayNode)).build();
68 }
69
70 /**
71 * Get Maintenance Domain by name.
72 *
73 * @param mdName The name of a Maintenance Domain
74 * @return 200 OK with the details of the MD and its children or 500 on error
75 */
76 @GET
77 @Path("{md_name}")
78 @Produces(MediaType.APPLICATION_JSON)
79 @Consumes(MediaType.APPLICATION_JSON)
80 public Response getMd(@PathParam("md_name") String mdName) {
81 log.debug("GET called for MD {}", mdName);
82 try {
83 MaintenanceDomain md = get(CfmMdService.class)
Sean Condon96b896d2017-12-11 12:44:29 -080084 //FIXME Handle other types of name constructs e.g. DomainName
Sean Condon0e89bda2017-03-21 14:23:19 +000085 .getMaintenanceDomain(MdIdCharStr.asMdId(mdName))
86 .orElseThrow(() -> new IllegalArgumentException(
87 "MD " + mdName + " not Found"));
88 ObjectNode result = mapper().createObjectNode();
89 result.set("md", codec(MaintenanceDomain.class).encode(md, this));
90 return ok(result.toString()).build();
91 } catch (IllegalArgumentException e) {
92 log.error("Get MD {} failed", mdName, e);
93 return Response.serverError()
94 .entity("{ \"failure\":\"" + e.toString() + "\" }").build();
95 }
96 }
97
98 /**
99 * Delete Maintenance Domain by name.
100 *
101 * @param mdName The name of a Maintenance Domain
102 * @return 200 OK, or 304 if not found or 500 on error
103 */
104 @DELETE
105 @Path("{md_name}")
106 @Consumes(MediaType.APPLICATION_JSON)
107 @Produces(MediaType.APPLICATION_JSON)
108 public Response deleteMd(@PathParam("md_name") String mdName) {
109 log.debug("DELETE called for MD {}", mdName);
110 try {
111 MdId mdId = MdIdCharStr.asMdId(mdName);
112 boolean deleted = get(CfmMdService.class).deleteMaintenanceDomain(mdId);
113 if (!deleted) {
114 return Response.notModified(mdName + " did not exist").build();
115 } else {
116 return ok("{ \"success\":\"deleted " + mdName + "\" }").build();
117 }
118 } catch (CfmConfigException e) {
119 log.error("Delete Maintenance Domain {} failed", mdName, e);
120 return Response.serverError()
121 .entity("{ \"failure\":\"" + e.toString() + "\" }").build();
122 }
123 }
124
125 /**
126 * Create Maintenance Domain.
127 *
Sean Condon081290d2017-11-02 13:15:08 +0000128 * @onos.rsModel MdCreate
Sean Condon0e89bda2017-03-21 14:23:19 +0000129 * @param input A JSON formatted input stream specifying the MA parameters
130 * @return 200 OK, 304 if MD already exists or 500 on error
131 */
132 @POST
133 @Consumes(MediaType.APPLICATION_JSON)
134 @Produces(MediaType.APPLICATION_JSON)
135 public Response createMaintenanceDomain(InputStream input) {
136 log.debug("POST called to Create MD");
137 try {
138 ObjectMapper mapper = new ObjectMapper();
139 JsonNode cfg = mapper.readTree(input);
140 MaintenanceDomain md = codec(MaintenanceDomain.class).decode((ObjectNode) cfg, this);
141
142 if (get(CfmMdService.class).createMaintenanceDomain(md)) {
143 return Response.notModified(md.mdId().toString() + " already exists").build();
144 }
145 return Response
146 .created(new URI("md/" + md.mdId()))
147 .entity("{ \"success\":\"" + md.mdId() + " created\" }")
148 .build();
149
150 } catch (Exception | CfmConfigException e) {
151 log.error("Create MaintenanceDomain", e);
152 return Response.serverError()
153 .entity("{ \"failure\":\"" + e.toString() + "\" }")
154 .build();
155 }
156 }
157}