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