Applying license changes to all files
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / openecomp / appc / provider / lcm / util / RequestInputBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.provider.lcm.util;
26
27 import java.text.ParseException;
28 import java.text.SimpleDateFormat;
29 import java.util.TimeZone;
30
31 import org.opendaylight.yang.gen.v1.org.openecomp.appc.lcm.rev160108.Payload;
32 import org.opendaylight.yang.gen.v1.org.openecomp.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
33 import org.opendaylight.yang.gen.v1.org.openecomp.appc.lcm.rev160108.common.header.CommonHeader;
34 import org.opendaylight.yang.gen.v1.org.openecomp.appc.lcm.rev160108.common.header.common.header.Flags;
35 import org.openecomp.appc.domainmodel.lcm.Flags.Mode;
36 import org.openecomp.appc.domainmodel.lcm.RequestContext;
37 import org.openecomp.appc.domainmodel.lcm.VNFOperation;
38 import org.openecomp.appc.requesthandler.objects.RequestHandlerInput;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41
42
43
44 public class RequestInputBuilder {
45     private static EELFLogger logger = EELFManager.getInstance().getApplicationLogger();
46
47     private static final String FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
48
49     private RequestContext requestContext;
50     private String rpcName;
51
52     public RequestInputBuilder() {
53     }
54
55
56     public RequestInputBuilder requestContext() {
57         this.requestContext = new RequestContext();
58         return this;
59     }
60
61     public RequestInputBuilder action(String action) {
62         this.requestContext.setAction(VNFOperation.findByString(action));
63         return this;
64     }
65
66
67     public RequestInputBuilder additionalContext(String key, String value) {
68         this.requestContext.addKeyValueToAdditionalContext(key, value);
69         return this;
70     }
71
72     public RequestInputBuilder payload(Payload payload) {
73         if (payload != null) {
74             this.requestContext.setPayload(payload.getValue());
75         }
76         return this;
77     }
78
79     public RequestHandlerInput  build (){
80         RequestHandlerInput request = new RequestHandlerInput();
81         request.setRequestContext(this.requestContext);
82         request.setRpcName(rpcName);
83         return  request;
84     }
85
86     public RequestInputBuilder rpcName(String rpcName) {
87         this.rpcName = rpcName;
88         return this;
89     }
90
91     public RequestInputBuilder commonHeader(CommonHeader commonHeader) throws ParseException {
92         org.openecomp.appc.domainmodel.lcm.CommonHeader header = new org.openecomp.appc.domainmodel.lcm.CommonHeader();
93         this.requestContext.setCommonHeader(header);
94
95         try {
96             if(null != commonHeader.getTimestamp()) {
97                 SimpleDateFormat format = new SimpleDateFormat(FORMAT);
98                 format.setLenient(false);
99                 format.setTimeZone(TimeZone.getTimeZone("UTC"));
100                 header.setTimestamp(format.parse(commonHeader.getTimestamp().getValue()).toInstant());
101             }else{
102                 throw new ParseException("Missing mandatory parameter : timestamp " , 0);
103             }
104         } catch (ParseException e) {
105             logger.error(String.format("DATE format is incorrect: %s", e.getMessage()));
106             throw e;
107         }
108         header.setApiVer(commonHeader.getApiVer());
109         header.setRequestId(commonHeader.getRequestId());
110         header.setOriginatorId(commonHeader.getOriginatorId());
111         header.setSubRequestId(commonHeader.getSubRequestId());
112
113         Flags inFlags = commonHeader.getFlags();
114         boolean force = false;
115         Mode mode = null;
116         int ttl = 0;
117         if (inFlags != null) {
118
119             if (null != inFlags.getForce()) {
120                 force = Boolean.parseBoolean(inFlags.getForce().toString().toLowerCase());
121             }
122             if (null != inFlags.getMode()) {
123                 mode = Mode.valueOf(inFlags.getMode().name());
124             }
125             if (null != inFlags.getTtl()) {
126                 ttl = inFlags.getTtl();
127             }
128
129         }
130         this.requestContext.getCommonHeader().setFlags(new org.openecomp.appc.domainmodel.lcm.Flags(mode, force, ttl));
131         return this;
132     }
133
134     public RequestInputBuilder actionIdentifiers(ActionIdentifiers actionIdentifiers)  throws ParseException  {
135         if(null!= actionIdentifiers) {
136             org.openecomp.appc.domainmodel.lcm.ActionIdentifiers actionIds = new org.openecomp.appc.domainmodel.lcm.ActionIdentifiers();
137             actionIds.setServiceInstanceId(actionIdentifiers.getServiceInstanceId());
138             actionIds.setVnfcName(actionIdentifiers.getVnfcName());
139             actionIds.setvServerId(actionIdentifiers.getVserverId());
140             actionIds.setVnfId(actionIdentifiers.getVnfId());
141             this.requestContext.setActionIdentifiers(actionIds);
142             return this;
143         }else{
144             throw new ParseException("Missing action identifier" , 0);
145         }
146     }
147
148
149 }