8a3e0bdd5e3761ad0d65fc55594e2e413dd16911
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
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  */
16
17 package org.openo.nfvo.resmanagement.service.rest;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.DELETE;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.PUT;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.QueryParam;
33 import javax.ws.rs.core.Context;
34 import javax.ws.rs.core.MediaType;
35
36 import org.openo.baseservice.remoteservice.exception.ServiceException;
37 import org.openo.nfvo.resmanagement.common.constant.HttpConstant;
38 import org.openo.nfvo.resmanagement.common.constant.ParamConstant;
39 import org.openo.nfvo.resmanagement.common.constant.UrlConstant;
40 import org.openo.nfvo.resmanagement.common.util.request.RequestUtil;
41 import org.openo.nfvo.resmanagement.common.util.response.ResponseUtil;
42 import org.openo.nfvo.resmanagement.common.util.response.RoaResponseUtil;
43 import org.openo.nfvo.resmanagement.service.base.openstack.inf.Network;
44 import org.openo.nfvo.resmanagement.service.entity.NetworkEntity;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import net.sf.json.JSONObject;
49
50 /**
51  *
52  * Network ROA Class.<br>
53  * <p>
54  * </p>
55  *
56  * @author
57  * @version     NFVO 0.5  Sep 10, 2016
58  */
59 @Path(UrlConstant.NETWORKS_URL)
60 @Consumes({MediaType.APPLICATION_JSON})
61 @Produces({MediaType.APPLICATION_JSON})
62 public class NetworkRoa {
63
64     private static final Logger LOGGER = LoggerFactory.getLogger(NetworkRoa.class);
65
66     private Network network;
67
68     /**
69      *
70      * Get details of networks.<br>
71      *
72      * @param context
73      * @return
74      * @throws ServiceException
75      * @since  NFVO 0.5
76      */
77     @GET
78     public JSONObject getNetworks(@Context HttpServletRequest context) throws ServiceException {
79         Map<String, Object> map = new HashMap<>(10);
80         List<NetworkEntity> networks = network.getList(map);
81
82         JSONObject result = new JSONObject();
83         result.put("networks", networks.toString());
84         return result;
85     }
86
87     /**
88      *
89      * Get network details.<br>
90      *
91      * @param context
92      * @param id
93      * @return
94      * @throws ServiceException
95      * @since  NFVO 0.5
96      */
97     @GET
98     @Path("/{networkId}")
99     public JSONObject getNetwork(@Context HttpServletRequest context, @PathParam("networkId") String id)
100             throws ServiceException {
101         LOGGER.info("NetworkRoa::getNetwork id:{}", id);
102         Map<String, Object> map = new HashMap<>(10);
103         map.put(ParamConstant.PARAM_ID, id);
104         List<NetworkEntity> networks = network.getList(map);
105
106         JSONObject result = new JSONObject();
107         result.put("networks", networks.toString());
108         return result;
109     }
110
111     /**
112      *
113      * Add network.<br>
114      *
115      * @param context
116      * @return
117      * @throws ServiceException
118      * @since  NFVO 0.5
119      */
120     @POST
121     @Consumes({MediaType.APPLICATION_JSON})
122     @Produces({MediaType.APPLICATION_JSON})
123     public JSONObject addNetwork(@Context HttpServletRequest context) throws ServiceException {
124         JSONObject object = RequestUtil.getJsonRequestBody(context);
125
126         LOGGER.info("NetworkRoa::addNetwork:{}", object.toString());
127         try {
128             int result = network.add(NetworkEntity.toEntity(object));
129             return RoaResponseUtil.add(result);
130         } catch(ServiceException se) {
131             LOGGER.error("NetworkRoa::addNetwork error:{}" + se);
132             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
133         }
134     }
135
136     /**
137      *
138      * Delete network.<br>
139      *
140      * @param context
141      * @param id
142      * @return
143      * @throws ServiceException
144      * @since  NFVO 0.5
145      */
146     @DELETE
147     @Consumes({MediaType.APPLICATION_JSON})
148     @Produces({MediaType.APPLICATION_JSON})
149     public JSONObject deleteNetwork(@Context HttpServletRequest context, @QueryParam(ParamConstant.PARAM_ID) String id)
150             throws ServiceException {
151         LOGGER.info("NetworkRoa::deleteNetwork id:{}", id);
152         try {
153             int result = network.delete(id);
154             return RoaResponseUtil.delete(result);
155         } catch(ServiceException se) {
156             LOGGER.error("NetworkRoa::deleteNetwork error:{}" + se);
157             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
158         }
159     }
160
161     /**
162      *
163      * Update network.<br>
164      *
165      * @param context
166      * @return
167      * @throws ServiceException
168      * @since  NFVO 0.5
169      */
170     @PUT
171     @Consumes({MediaType.APPLICATION_JSON})
172     @Produces({MediaType.APPLICATION_JSON})
173     public JSONObject updateNetwork(@Context HttpServletRequest context) throws ServiceException {
174         JSONObject object = RequestUtil.getJsonRequestBody(context);
175
176         LOGGER.info("NetworkRoa::updateNetwork:{}", object.toString());
177         try {
178             int result = network.update(object);
179             return RoaResponseUtil.update(result);
180         } catch(ServiceException se) {
181             LOGGER.error("NetworkRoa::updateNetwork error:{}" + se);
182             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
183         }
184     }
185
186     public void setNetwork(Network network) {
187         this.network = network;
188     }
189 }