Fix License Header of vfc-driver
[vfc/nfvo/driver/sfc.git] / zte / sfc-driver / sfc-driver / src / main / java / org / openo / sfc / resources / DriverResource.java
1 /**
2  * Copyright 2016 ZTE Corporation.
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 package org.openo.sfc.resources;
17
18 import com.codahale.metrics.annotation.Timed;
19 import org.openo.sfc.entity.DelReqInfo;
20 import org.openo.sfc.entity.FlowClassfierReq4N;
21 import org.openo.sfc.entity.FlowClassifierReq4S;
22 import org.openo.sfc.entity.PortChainReq4N;
23 import org.openo.sfc.entity.PortChainReq4S;
24 import org.openo.sfc.entity.PortPairGroupReq4N;
25 import org.openo.sfc.entity.PortPairGroupReq4S;
26 import org.openo.sfc.entity.Result;
27 import org.openo.sfc.entity.portpair.PortPairReq4N;
28 import org.openo.sfc.entity.portpair.PortPairReq4S;
29 import org.openo.sfc.service.SdnServiceConsumer;
30 import org.openo.sfc.utils.SfcDriverUtil;
31 import org.openo.sfc.wrapper.N2sReqWrapper;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import javax.ws.rs.DELETE;
36 import javax.ws.rs.NotFoundException;
37 import javax.ws.rs.POST;
38 import javax.ws.rs.Path;
39 import javax.ws.rs.Produces;
40 import javax.ws.rs.core.MediaType;
41
42 @Path("/")
43 public class DriverResource {
44     private static final Logger LOGGER = LoggerFactory.getLogger(DriverResource.class);
45 //
46 //    @POST
47 //    @Path("/checksdncontroller")
48 //    @Produces(MediaType.APPLICATION_JSON)
49 //    @Timed
50 //    public Result checkSdnController(SdnControllerInfo sdnInfo)
51 //            throws Exception {
52 //        if (sdnInfo == null ) {
53 //            throw new NotFoundException("SdnControllerInfo is null");
54 //        }
55 //        return SdnServiceConsumer.getSdnConProxy(sdnInfo.getUrl()).querySdnController();
56 //    }
57
58     @POST
59     @Path("/createportpair")
60     @Produces(MediaType.APPLICATION_JSON)
61     @Timed
62     public Result createPortPair(PortPairReq4N portPairReq4N)
63             throws Exception {
64         if (portPairReq4N == null) {
65             throw new NotFoundException("SdnControllerInfo is null");
66         }
67         LOGGER.info(SfcDriverUtil.toJson(portPairReq4N));
68         PortPairReq4S portPairReq = N2sReqWrapper.convertPortPair(portPairReq4N);
69         String portPairReqJson = SfcDriverUtil.toJson(portPairReq);
70         LOGGER.info(portPairReqJson);
71         SdnServiceConsumer.getSdnConProxy(portPairReq4N.getUrl()).createPortPair(SfcDriverUtil.generateAuthorization(), portPairReqJson);
72 //
73 //        if(rsp.getStatus() == SfcConst.HTTP_POST_OK)
74 //        {
75 //            return new Result(portPairReq.getPortPair().get(0).getUuid());
76 //        }
77 //        return rsp;
78
79         return new Result(portPairReq.getPortPair().get(0).getUuid());
80     }
81
82     @POST
83     @Path("/createportpairgroup")
84     @Produces(MediaType.APPLICATION_JSON)
85     @Timed
86     public Result createPortPairGroup(PortPairGroupReq4N portPairGroupReq4N)
87             throws Exception {
88         if (portPairGroupReq4N == null) {
89             throw new NotFoundException("SdnControllerInfo is null");
90         }
91         LOGGER.info(SfcDriverUtil.toJson(portPairGroupReq4N));
92         PortPairGroupReq4S portPairGroupReq4S = N2sReqWrapper.convertPortPairGroup(portPairGroupReq4N);
93         String portPairGroupReqJson = SfcDriverUtil.toJson(portPairGroupReq4S);
94         LOGGER.info(portPairGroupReqJson);
95         SdnServiceConsumer.getSdnConProxy(portPairGroupReq4N.getUrl()).createPortPairGroup(
96                 SfcDriverUtil.generateAuthorization(), portPairGroupReqJson);
97 //        if(rsp.getStatus() == SfcConst.HTTP_POST_OK)
98 //        {
99 //            return new Result(portPairGroupReq4S.getPortPairGroupReqs().get(0).getUuid());
100 //        }
101 //
102 //        return rsp;
103
104         return new Result(portPairGroupReq4S.getPortPairGroupReqs().get(0).getUuid());
105
106     }
107
108     @POST
109     @Path("/createflowclassfier")
110     @Produces(MediaType.APPLICATION_JSON)
111     @Timed
112     public Result createFlowClassfier(FlowClassfierReq4N flowClassfierReq4N)
113             throws Exception {
114         if (flowClassfierReq4N == null) {
115             throw new NotFoundException("FlowClassfierReq4N is null");
116         }
117         LOGGER.info(SfcDriverUtil.toJson(flowClassfierReq4N));
118         FlowClassifierReq4S flowClassfierReq4S = N2sReqWrapper.
119                 convertFlowClassfier(flowClassfierReq4N);
120         String uuid = flowClassfierReq4S.getSfcFlowClassifier().get(0).getUuid();
121         String flowClassifierJson = SfcDriverUtil.toJson(flowClassfierReq4S);
122         LOGGER.info(flowClassifierJson);
123         SdnServiceConsumer.getSdnConProxy(flowClassfierReq4N.getUrl()).createFlowCla(SfcDriverUtil.generateAuthorization(), flowClassifierJson);
124 //        if(rsp.getStatus() == SfcConst.HTTP_POST_OK)
125 //        {
126 //            return new Result(uuid);
127 //        }
128 //        else
129 //            return rsp;
130
131         return new Result(uuid);
132     }
133
134     @POST
135     @Path("/createportchain")
136     @Produces(MediaType.APPLICATION_JSON)
137     @Timed
138     public Result creatPortChain(PortChainReq4N portChainReq4N)
139             throws Exception {
140         if (portChainReq4N == null) {
141             throw new NotFoundException("PortChainReq4N is null");
142         }
143         LOGGER.info(SfcDriverUtil.toJson(portChainReq4N));
144         PortChainReq4S portChainReq = N2sReqWrapper.converPortChain(portChainReq4N);
145         String portChainReqJson = SfcDriverUtil.toJson(portChainReq);
146         LOGGER.info(portChainReqJson);
147         SdnServiceConsumer.getSdnConProxy(portChainReq4N.getUrl()).createPortChain(
148                 SfcDriverUtil.generateAuthorization(), portChainReqJson);
149 //        if(rsp.getStatus() == SfcConst.HTTP_POST_OK)
150 //        {
151 //            return new Result(portChainReq.getPortChainReqs().get(0).getUuid());
152 //        }
153 //        return rsp;
154
155         return new Result(portChainReq.getPortChainReqs().get(0).getUuid());
156
157     }
158
159     @DELETE
160     @Path("/delportpair")
161     @Produces(MediaType.APPLICATION_JSON)
162     @Timed
163     public void delPortPair(DelReqInfo delReqInfo)
164             throws Exception {
165         if (delReqInfo == null) {
166             throw new NotFoundException("DelReqInfo is null");
167         }
168         LOGGER.info(SfcDriverUtil.toJson(delReqInfo));
169         SdnServiceConsumer.getSdnConProxy(delReqInfo.getUrl()).deletePortPair(SfcDriverUtil.generateAuthorization(), delReqInfo.getId());
170
171     }
172
173     @DELETE
174     @Path("/delportpairgroup")
175     @Produces(MediaType.APPLICATION_JSON)
176     @Timed
177     public void delPortPairGroup(DelReqInfo delReqInfo)
178             throws Exception {
179         if (delReqInfo == null) {
180             throw new NotFoundException("DelReqInfo is null");
181         }
182         LOGGER.info(SfcDriverUtil.toJson(delReqInfo));
183         SdnServiceConsumer.getSdnConProxy(delReqInfo.getUrl()).deletePortPairGroup(SfcDriverUtil.generateAuthorization(), delReqInfo.getId());
184
185     }
186
187     @DELETE
188     @Path("/delclassifier")
189     @Produces(MediaType.APPLICATION_JSON)
190     @Timed
191     public void delFlowClassfier(DelReqInfo delReqInfo)
192             throws Exception {
193         if (delReqInfo == null) {
194             throw new NotFoundException("DelReqInfo is null");
195         }
196         LOGGER.info(SfcDriverUtil.toJson(delReqInfo));
197         SdnServiceConsumer.getSdnConProxy(delReqInfo.getUrl()).deleteFlowClassifiers(SfcDriverUtil.generateAuthorization(), delReqInfo.getId());
198
199     }
200
201     @DELETE
202     @Path("/delchain")
203     @Produces(MediaType.APPLICATION_JSON)
204     @Timed
205     public void delPortChain(DelReqInfo delReqInfo)
206             throws Exception {
207         if (delReqInfo == null) {
208             throw new NotFoundException("DelReqInfo is null");
209         }
210         LOGGER.info(SfcDriverUtil.toJson(delReqInfo));
211         SdnServiceConsumer.getSdnConProxy(delReqInfo.getUrl()).deletePortChain(SfcDriverUtil.generateAuthorization(), delReqInfo.getId());
212
213     }
214 }