2 * Copyright 2016 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.nfvo.jujuvnfmadapter.service.rest;
20 import java.io.IOException;
21 import java.util.List;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.Path;
28 import javax.ws.rs.PathParam;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.core.Context;
31 import javax.ws.rs.core.MediaType;
33 import org.openo.baseservice.remoteservice.exception.ServiceException;
34 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
35 import org.openo.nfvo.jujuvnfmadapter.common.EntityUtils;
36 import org.openo.nfvo.jujuvnfmadapter.common.FileUtils;
37 import org.openo.nfvo.jujuvnfmadapter.common.JujuConfigUtil;
38 import org.openo.nfvo.jujuvnfmadapter.common.EntityUtils.ExeRes;
39 import org.openo.nfvo.jujuvnfmadapter.common.servicetoken.JujuVnfmRestfulUtil;
40 import org.openo.nfvo.jujuvnfmadapter.common.servicetoken.VnfmRestfulUtil;
41 import org.openo.nfvo.jujuvnfmadapter.service.adapter.impl.AdapterResourceManager;
42 import org.openo.nfvo.jujuvnfmadapter.service.constant.Constant;
43 import org.openo.nfvo.jujuvnfmadapter.service.constant.UrlConstant;
44 import org.openo.nfvo.jujuvnfmadapter.common.SwitchController;
45 import org.openo.nfvo.jujuvnfmadapter.common.VnfmUtil;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
51 import net.sf.json.JSONObject;
59 * @version NFVO 0.5 Sep 13, 2016
62 @Consumes(MediaType.APPLICATION_JSON)
63 @Produces(MediaType.APPLICATION_JSON)
64 public class ConfigRoa {
66 private static final Logger LOG = LoggerFactory.getLogger(ConfigRoa.class);
78 public String initUI(@Context HttpServletRequest context, @Context HttpServletResponse resp) {
79 return EntityUtils.toString(new SwitchController(), SwitchController.class);
89 * @throws ServiceException
93 @Path("/debug/{type}")
94 public boolean setDebugModel(@PathParam("type") int type, @Context HttpServletRequest context,
95 @Context HttpServletResponse resp) throws ServiceException {
97 SwitchController.setDebugModel(true);
99 SwitchController.setDebugModel(false);
101 LOG.debug("change to debug model:" + SwitchController.isDebugModel());
102 return SwitchController.isDebugModel();
112 * @throws ServiceException
116 @Path("/mock/{methodName}")
117 public boolean mock(@PathParam("methodName") String methodName, @Context HttpServletRequest context,
118 @Context HttpServletResponse resp) throws ServiceException {
119 if("getVnfmById".equals(methodName)) {
120 new MockUp<VnfmUtil>() {
123 public JSONObject getVnfmById(String vnfmId) {
124 JSONObject json = new JSONObject();
125 json.put("vnfmId", vnfmId);
126 json.put("vnfdId", "testVnfdId");
127 json.put("vnfPackageId", "testPackageId");
128 json.put("version", "1");
129 json.put("url", JujuConfigUtil.getValue("jujuvnfm_server_url"));
133 } else if("execute".equals(methodName)) {
134 new MockUp<EntityUtils>() {
137 public ExeRes execute(String dir, List<String> command) {
138 ExeRes er = new ExeRes();
139 String resContent = null;
141 resContent = new String(
142 FileUtils.readFile(new File(JujuConfigUtil.getValue("juju_cmd_res_file")), "UTF-8"));
143 } catch(Exception e) {
144 LOG.error("mock fail",e);
145 resContent = "mock fail";
147 er.setBody(resContent);
151 }else if("fetchDownloadUrlFromCatalog".equals(methodName)) {
152 new MockUp<AdapterResourceManager>() {
154 public String fetchDownloadUrlFromCatalog(String csarId){
155 return JujuConfigUtil.getValue("catalog_download_url");
169 * @throws ServiceException
173 @Path("/unmock/{methodName}")
174 public boolean unmock(@PathParam("methodName") String methodName, @Context HttpServletRequest context,
175 @Context HttpServletResponse resp) throws ServiceException {
176 if("getVnfmById".equals(methodName)) {
177 new MockUp<VnfmUtil>() {
180 public JSONObject getVnfmById(String vnfmId) {
181 RestfulResponse rsp = VnfmRestfulUtil.getRemoteResponse(
182 String.format(UrlConstant.REST_ESRINFO_GET, vnfmId), JujuVnfmRestfulUtil.GET_TYPE, null);
183 if(rsp == null || rsp.getStatus() != Constant.HTTP_OK) {
186 LOG.error("funtion=getVnfmById, status={}", rsp.getStatus());
187 return JSONObject.fromObject(rsp.getResponseContent());