blob: 409216bc20e89f524481a489f2331b5f09bef841 [file] [log] [blame]
Himal Kumarb43724d2016-04-29 14:15:57 +10001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Himal Kumarb43724d2016-04-29 14:15:57 +10003 *
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.castor;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onlab.packet.IpAddress;
21import org.onosproject.rest.AbstractWebResource;
22
23import javax.ws.rs.Consumes;
24import javax.ws.rs.GET;
25import javax.ws.rs.POST;
26import javax.ws.rs.Path;
27import javax.ws.rs.core.MediaType;
28import javax.ws.rs.core.Response;
29
30/**
31 * The Web Resource for REST API calls to the Castor application.
32 */
33@Path("castor")
34public class CastorWebResource extends AbstractWebResource {
35
36
37 /**
38 * Get the present ARP Mapping.
39 * Use this to get the present ARP map stored by Castor
40 *
41 * @return 200 OK
42 */
43 @GET
44 @Path("mac-map")
45 public Response getMac() {
46 String result = get(CastorStore.class).getAddressMap().toString();
47 ObjectNode node = mapper().createObjectNode().put("response", result);
48 return ok(node).build();
49 }
50
51 /**
52 * Get list of added peers.
53 * List of peers added.
54 *
55 * @return 200 OK
56 */
57 @GET
58 @Path("get-peers")
59 public Response getPeers() {
60 String result = get(CastorStore.class).getCustomersMap().toString();
61 ObjectNode node = mapper().createObjectNode().put("response", result);
62 return ok(node).build();
63 }
64
65 /**
66 * Add a Peer.
67 * Use this to add a Customer or a BGP Peer
68 *
69 * @onos.rsModel PeerModel
70 * @param incomingData json Data
71 * @return 200 OK
72 */
73 @POST
74 @Path("add-peer")
75 @Consumes(MediaType.APPLICATION_JSON)
76 public Response addPeer(String incomingData) {
77
78 String arpResult = ", Mac was known";
79
80 try {
81 ObjectMapper mapper = new ObjectMapper();
82 Peer peer = mapper.readValue(incomingData, Peer.class);
83 get(ConnectivityManagerService.class).setUpConnectivity(peer);
84 if ((get(CastorStore.class)).getAddressMap()
85 .get(IpAddress.valueOf(peer.getIpAddress())) != null) {
86 get(ConnectivityManagerService.class).setUpL2(peer);
87 } else {
88 get(ArpService.class).createArp(peer);
89 arpResult = ", ARP packet sent, MAC was not known";
90 }
91 } catch (Exception e) {
92 e.printStackTrace();
93 String result = "Unable to process due to some reason, Try again";
94 ObjectNode node = mapper().createObjectNode().put("response", result);
95 return ok(node).build();
96 }
97
98 String result = "Success: Peer Entered" + arpResult;
99 ObjectNode node = mapper().createObjectNode().put("response", result);
100 return ok(node).build();
101 }
102
103 /**
104 * Delete a Peer.
105 * Use this to delete a Peer. IpAddress should match as entered while adding.
106 *
107 * @onos.rsModel PeerModel
108 * @param incomingData json Data
109 * @return 200 OK
110 */
111 @POST
112 @Path("delete-peer")
113 @Consumes(MediaType.APPLICATION_JSON)
114 public Response deletePeer(String incomingData) {
115
116 try {
117 ObjectMapper mapper = new ObjectMapper();
118 Peer peer = mapper.readValue(incomingData, Peer.class);
119 get(ConnectivityManagerService.class).deletePeer(peer);
120 } catch (Exception e) {
121 e.printStackTrace();
122 return Response.status(500).entity("Unable to delete the peer").build();
123 }
124 String result = "Peer Deleted";
125 ObjectNode node = mapper().createObjectNode().put("response", result);
126 return ok(node).build();
127 }
128
129 /**
130 * Add router server.
131 * Use this to add to add Route Servers for initializing
132 *
133 * @onos.rsModel PeerModel
134 * @param incomingData json Data
135 * @return 200 OK
136 */
137 @POST
138 @Path("route-server")
139 @Consumes(MediaType.APPLICATION_JSON)
140 public Response addRouteServer(String incomingData) {
141
142 try {
143 ObjectMapper mapper = new ObjectMapper();
144 Peer peer = mapper.readValue(incomingData, Peer.class);
145 get(ConnectivityManagerService.class).start(peer);
146 } catch (Exception e) {
147 e.printStackTrace();
148 return Response.status(500).entity("Unable to add the route server").build();
149 }
150 String result = "Server Entered";
151 ObjectNode node = mapper().createObjectNode().put("response", result);
152 return ok(node).build();
153 }
154}