Update gvnfm-driver .gitreview file
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / gvnfm / jujuvnfmadapter / service / rest / ConfigRoa.java
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.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.rest;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.List;
22
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;
32
33 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.EntityUtils;
34 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.FileUtils;
35 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.JujuConfigUtil;
36 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.SwitchController;
37 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.VnfmUtil;
38 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.EntityUtils.ExeRes;
39 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.servicetoken.JujuVnfmRestfulUtil;
40 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.servicetoken.VnfmRestfulUtil;
41 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.adapter.impl.AdapterResourceManager;
42 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
43 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.UrlConstant;
44 import org.openo.baseservice.remoteservice.exception.ServiceException;
45 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import mockit.Mock;
50 import mockit.MockUp;
51 import net.sf.json.JSONObject;
52
53 /**
54  * <br/>
55  * <p>
56  * </p>
57  * 
58  * @author
59  * @version NFVO 0.5 Sep 13, 2016
60  */
61 @Path("/v1/config")
62 @Consumes(MediaType.APPLICATION_JSON)
63 @Produces(MediaType.APPLICATION_JSON)
64 public class ConfigRoa {
65
66     private static final Logger LOG = LoggerFactory.getLogger(ConfigRoa.class);
67
68     /**
69      * <br/>
70      * 
71      * @param context
72      * @param resp
73      * @return
74      * @since NFVO 0.5
75      */
76     @GET
77     @Path("/")
78     public String initUI(@Context HttpServletRequest context, @Context HttpServletResponse resp) {
79         return EntityUtils.toString(new SwitchController(), SwitchController.class);
80     }
81
82     /**
83      * <br/>
84      * 
85      * @param type
86      * @param context
87      * @param resp
88      * @return
89      * @throws ServiceException
90      * @since NFVO 0.5
91      */
92     @GET
93     @Path("/debug/{type}")
94     public boolean setDebugModel(@PathParam("type") int type, @Context HttpServletRequest context,
95             @Context HttpServletResponse resp) throws ServiceException {
96         if(type == 1) {
97             SwitchController.setDebugModel(true);
98         } else {
99             SwitchController.setDebugModel(false);
100         }
101         LOG.debug("change to debug model:" + SwitchController.isDebugModel());
102         return SwitchController.isDebugModel();
103     }
104
105     /**
106      * <br/>
107      * 
108      * @param methodName
109      * @param context
110      * @param resp
111      * @return
112      * @throws ServiceException
113      * @since NFVO 0.5
114      */
115     @GET
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>() {
121
122                 @Mock
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"));
130                     return json;
131                 }
132             };
133         } else if("execute".equals(methodName)) {
134             new MockUp<EntityUtils>() {
135
136                 @Mock
137                 public ExeRes execute(String dir, List<String> command) {
138                     ExeRes er = new ExeRes();
139                     String resContent = null;
140                     try {
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";
146                     }
147                     er.setBody(resContent);
148                     return er;
149                 }
150             };
151         }else if("fetchDownloadUrlFromCatalog".equals(methodName)) {
152             new MockUp<AdapterResourceManager>() {
153                 @Mock
154                 public String fetchDownloadUrlFromCatalog(String csarId){
155                     return JujuConfigUtil.getValue("catalog_download_url");
156                 }
157             };
158         }
159         return true;
160     }
161
162     /**
163      * <br/>
164      * 
165      * @param methodName
166      * @param context
167      * @param resp
168      * @return
169      * @throws ServiceException
170      * @since NFVO 0.5
171      */
172     @GET
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>() {
178
179                 @Mock
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) {
184                         return null;
185                     }
186                     LOG.error("funtion=getVnfmById, status={}", rsp.getStatus());
187                     return JSONObject.fromObject(rsp.getResponseContent());
188                 }
189             };
190         }
191         return true;
192     }
193 }