aboutsummaryrefslogtreecommitdiffstats
path: root/ajsc-aai/src/main/java/org/openecomp/aai/util/UpdateResource.java
blob: 1548cbe969a62622ad36fe7797771df13be372ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*-
 * ============LICENSE_START=======================================================
 * org.openecomp.aai
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.aai.util;

import java.util.Properties;
import java.util.UUID;

import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.logging.AAILogger;
import org.openecomp.aai.logging.ErrorLogHelper;
import org.openecomp.aai.logging.LogLine;
import org.openecomp.aai.domain.yang.Action;
import org.openecomp.aai.domain.yang.ActionData;
import org.openecomp.aai.domain.yang.Update;
import com.att.eelf.configuration.Configuration;


public class UpdateResource {
	
	private static 	final  String    COMPONENT = "aairestctrl";
	private static 	final  String    FROMAPPID = "AAIUPDT";
	private static 	final  String    TRANSID   = UUID.randomUUID().toString();
	private static 	final  String    UPDATE_URL = "actions/update";	

	private static final String USAGE_STRING = "Usage: updateTool.sh \n" + 
			"<node type> <update node URI> <property name>:<property value>[,<property name>:<property value]* | \n" + 
			"where update node uri is the URI path for that node \n" +
			" for ex. ./updateTool.sh pserver cloud-infrastructure/pservers/pserver/XXX prov-status:NEWSTATUS";
	
	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		
		String nodeType = null;
		String nodeURI = null;
		String updValueList = null;
		
		// Set the logging file properties to be used by EELFManager
		Properties props = System.getProperties();
		props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_UPDTOOL_LOGBACK_PROPS);
		props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_ETC_APP_PROPERTIES);
		
		AAILogger aaiLogger = new AAILogger(UpdateResource.class.getName());
		LogLine   logline = new LogLine();
		logline.init(COMPONENT, TRANSID, FROMAPPID, "main");
		try {		
			if (args.length < 3) {
				System.out.println("Nothing to update or Insufficient arguments");
				System.out.println(USAGE_STRING);  
				logline.add("msg",  "Insufficient or Invalid arguments");
				aaiLogger.info(logline, true, "0");
				System.exit(1);
			} else { 
					nodeType = args[0];
					nodeURI = args[1];
					updValueList = args[2];
					
					logline.add("nodeType", nodeType);
					logline.add("nodeURI", nodeURI);
					logline.add("updValueList",  updValueList);
	
					update(aaiLogger, logline, nodeType, nodeURI, updValueList);
			}
			aaiLogger.info(logline, true, "0");
			System.exit(0);
		
		} catch (AAIException e) {
			System.out.println("Update failed: " + e.getMessage());
			aaiLogger.error(e.getErrorObject(), logline, e);
			aaiLogger.info(logline, false, e.getErrorObject().getErrorCodeString());
			System.exit(1);
		} catch (Exception e) {
			System.out.println("Update failed: " + e.toString());
			aaiLogger.error(ErrorLogHelper.getErrorObject("AAI_7402", e.getMessage()), logline, e);
			aaiLogger.info(logline, false, "AAI_7402");
			System.exit(1);
		}
	}
	
	/**
	 * Update.
	 *
	 * @param <T> the generic type
	 * @param aaiLogger the aai logger
	 * @param logline the logline
	 * @param nodeType the node type
	 * @param nodeURI the node URI
	 * @param updValueList the upd value list
	 * @throws AAIException the AAI exception
	 */
	public static <T> void update(AAILogger aaiLogger, LogLine logline, String nodeType, 
								String nodeURI, String updValueList) throws AAIException {		
		try {		
				
				Update update = new Update();
				update.setUpdateNodeType(nodeType);
				update.setUpdateNodeUri(nodeURI);
				
				Action action = new Action();
				action.setActionType("replace");
				
				for (String updValue: updValueList.split(",")) {
					ActionData data = new ActionData();
					data.setPropertyName(updValue.substring(0, updValue.indexOf(':')));
					data.setPropertyValue(updValue.substring(updValue.indexOf(':') + 1));
					action.getActionData().add(data);
				}		

				update.getAction().add(action);	
				
				System.out.println("updating the resource... ");
			
				RestController.<Update>Put(update, FROMAPPID, TRANSID, UPDATE_URL);
				System.out.println("Update Successful");
				aaiLogger.info(logline, true, "0");
		} catch (AAIException e) {
			String msg = "Update failed.";
			logline.add("msg", msg);
			System.out.println(msg);
			throw e;
		}  catch (Exception e) {
            throw new AAIException("AAI_7402", e, "Error during UPDATE");
		}
	}
}