blob: ecf1fe9b5b7cdc8d9ff4ac57815d11cc397e1c03 [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;
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)
85 .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 *
128 * @param input A JSON formatted input stream specifying the MA parameters
129 * @return 200 OK, 304 if MD already exists or 500 on error
130 */
131 @POST
132 @Consumes(MediaType.APPLICATION_JSON)
133 @Produces(MediaType.APPLICATION_JSON)
134 public Response createMaintenanceDomain(InputStream input) {
135 log.debug("POST called to Create MD");
136 try {
137 ObjectMapper mapper = new ObjectMapper();
138 JsonNode cfg = mapper.readTree(input);
139 MaintenanceDomain md = codec(MaintenanceDomain.class).decode((ObjectNode) cfg, this);
140
141 if (get(CfmMdService.class).createMaintenanceDomain(md)) {
142 return Response.notModified(md.mdId().toString() + " already exists").build();
143 }
144 return Response
145 .created(new URI("md/" + md.mdId()))
146 .entity("{ \"success\":\"" + md.mdId() + " created\" }")
147 .build();
148
149 } catch (Exception | CfmConfigException e) {
150 log.error("Create MaintenanceDomain", e);
151 return Response.serverError()
152 .entity("{ \"failure\":\"" + e.toString() + "\" }")
153 .build();
154 }
155 }
156}