blob: 0e523a70929eb05c5e7d1b474a303cd83c4cd1ed [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());
52 public static final String JSON_NOT_NULL = "JsonNode can not be null";
53
54 /**
55 * Get all Maintenance Domains.
56 *
57 * @return 200 OK with a list of MDs and their children
58 */
59 @GET
60 @Produces(MediaType.APPLICATION_JSON)
61 @Consumes(MediaType.APPLICATION_JSON)
62 public Response getMds() {
63 log.debug("GET called for all MDs");
64 Collection<MaintenanceDomain> mdMap =
65 get(CfmMdService.class).getAllMaintenanceDomain();
66 ArrayNode arrayNode = mapper().createArrayNode();
67 arrayNode.add(codec(MaintenanceDomain.class).encode(mdMap, this));
68 return ok(mapper().createObjectNode().set("mds", arrayNode)).build();
69 }
70
71 /**
72 * Get Maintenance Domain by name.
73 *
74 * @param mdName The name of a Maintenance Domain
75 * @return 200 OK with the details of the MD and its children or 500 on error
76 */
77 @GET
78 @Path("{md_name}")
79 @Produces(MediaType.APPLICATION_JSON)
80 @Consumes(MediaType.APPLICATION_JSON)
81 public Response getMd(@PathParam("md_name") String mdName) {
82 log.debug("GET called for MD {}", mdName);
83 try {
84 MaintenanceDomain md = get(CfmMdService.class)
Sean Condon96b896d2017-12-11 12:44:29 -080085 //FIXME Handle other types of name constructs e.g. DomainName
Sean Condon0e89bda2017-03-21 14:23:19 +000086 .getMaintenanceDomain(MdIdCharStr.asMdId(mdName))
87 .orElseThrow(() -> new IllegalArgumentException(
88 "MD " + mdName + " not Found"));
89 ObjectNode result = mapper().createObjectNode();
90 result.set("md", codec(MaintenanceDomain.class).encode(md, this));
91 return ok(result.toString()).build();
92 } catch (IllegalArgumentException e) {
93 log.error("Get MD {} failed", mdName, e);
94 return Response.serverError()
95 .entity("{ \"failure\":\"" + e.toString() + "\" }").build();
96 }
97 }
98
99 /**
100 * Delete Maintenance Domain by name.
101 *
102 * @param mdName The name of a Maintenance Domain
103 * @return 200 OK, or 304 if not found or 500 on error
104 */
105 @DELETE
106 @Path("{md_name}")
107 @Consumes(MediaType.APPLICATION_JSON)
108 @Produces(MediaType.APPLICATION_JSON)
109 public Response deleteMd(@PathParam("md_name") String mdName) {
110 log.debug("DELETE called for MD {}", mdName);
111 try {
112 MdId mdId = MdIdCharStr.asMdId(mdName);
113 boolean deleted = get(CfmMdService.class).deleteMaintenanceDomain(mdId);
114 if (!deleted) {
115 return Response.notModified(mdName + " did not exist").build();
116 } else {
117 return ok("{ \"success\":\"deleted " + mdName + "\" }").build();
118 }
119 } catch (CfmConfigException e) {
120 log.error("Delete Maintenance Domain {} failed", mdName, e);
121 return Response.serverError()
122 .entity("{ \"failure\":\"" + e.toString() + "\" }").build();
123 }
124 }
125
126 /**
127 * Create Maintenance Domain.
128 *
Sean Condon081290d2017-11-02 13:15:08 +0000129 * @onos.rsModel MdCreate
Sean Condon0e89bda2017-03-21 14:23:19 +0000130 * @param input A JSON formatted input stream specifying the MA parameters
131 * @return 200 OK, 304 if MD already exists or 500 on error
132 */
133 @POST
134 @Consumes(MediaType.APPLICATION_JSON)
135 @Produces(MediaType.APPLICATION_JSON)
136 public Response createMaintenanceDomain(InputStream input) {
137 log.debug("POST called to Create MD");
138 try {
139 ObjectMapper mapper = new ObjectMapper();
140 JsonNode cfg = mapper.readTree(input);
141 MaintenanceDomain md = codec(MaintenanceDomain.class).decode((ObjectNode) cfg, this);
142
143 if (get(CfmMdService.class).createMaintenanceDomain(md)) {
144 return Response.notModified(md.mdId().toString() + " already exists").build();
145 }
146 return Response
147 .created(new URI("md/" + md.mdId()))
148 .entity("{ \"success\":\"" + md.mdId() + " created\" }")
149 .build();
150
151 } catch (Exception | CfmConfigException e) {
152 log.error("Create MaintenanceDomain", e);
153 return Response.serverError()
154 .entity("{ \"failure\":\"" + e.toString() + "\" }")
155 .build();
156 }
157 }
158}