summaryrefslogtreecommitdiffstats
path: root/javatoscachecker/service/src/main/java/org/onap/tosca/checker/service/CheckerController.java
blob: cde0323ed0ddeed67e99c52703452d32ef6dc161 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Copyright (c) 2017 <AT&T>.  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.
 */
package org.onap.tosca.checker.service;

import java.util.logging.Logger;
import java.util.logging.Level;

import java.util.concurrent.Callable;

import java.net.URI;
import java.net.URISyntaxException;

import javax.servlet.http.HttpServletRequest; 

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.onap.tosca.checker.Checker;
import org.onap.tosca.checker.CheckerException;
import org.onap.tosca.checker.Report;
import org.onap.tosca.checker.Catalog;
import org.onap.tosca.checker.Target;
import org.onap.tosca.checker.TargetLocator;
import org.onap.tosca.checker.CommonLocator;



@RestController
public class CheckerController implements ApplicationContextAware {

	private static Logger log = Logger.getLogger(CheckerController.class.getName());

	private ApplicationContext			appCtx;
	@Autowired
	private Catalogs					catalogs;
	private Checker						checker;
	
	public void setApplicationContext(ApplicationContext theCtx) throws BeansException {
    this.appCtx = theCtx;
	}

	/**
	 * standalone checking, everything will be forgotten
	 */
	@RequestMapping(value={"/check_template/"}, method={RequestMethod.POST}, produces={"application/json"})
	public Report validate(@RequestBody String theTemplate,
												 HttpServletRequest theRequest) {

System.out.println("Posting unnamed template");
		CachedTarget target = new CachedTarget("", requestURI(theRequest));
		target.setContent(theTemplate);
		((InCatalogLocator)this.checker.getTargetLocator()).setCatalog(null);

		try {
			this.checker.check(target);
		}
		catch (CheckerException cx) {
			log.log(Level.WARNING, "Failed to check tosca template", cx);
			target.getReport().add(cx);
		}

		return target.getReport();
	}

	/**
	 * checking with respect to a namespace/catalog but the outcome is forgotten (not added to the catalog)
	 */
	@RequestMapping(value={"/check_template/{catalog}"}, method={RequestMethod.POST}, produces={"application/json"})
	public Report validate(@RequestBody String theTemplate,
												 @PathVariable(value="catalog") String theCatalog,
												 HttpServletRequest theRequest) 
																										throws NoSuchCatalogException {

System.out.println("Posting unnamed template to catalog " + theCatalog);
		Catalog catalog = this.catalogs.getCatalog(theCatalog);
		if (catalog == null)
			throw new NoSuchCatalogException(theCatalog);
		else
			((InCatalogLocator)this.checker.getTargetLocator()).setCatalog(catalog);

		CachedTarget target = new CachedTarget("", requestURI(theRequest));
		target.setContent(theTemplate);

		try {
			this.checker.check(target, new Catalog(catalog));
		}
		catch (CheckerException cx) {
			log.log(Level.WARNING, "Failed to check tosca template", cx);
			target.getReport().add(cx);
		}

		return target.getReport();
	}

	/**
	 * checking with respect to a namespace/catalog, the outcome is registered within the catalog 
	 */	
	@RequestMapping(value={"/check_template/{catalog}/{name}"}, method={RequestMethod.POST}, produces={"application/json"})
	public Report validate(@RequestBody String theTemplate,
												 @PathVariable(value="catalog") String theCatalog,
												 @PathVariable(value="name") String theName,
												 HttpServletRequest theRequest)
																								throws TargetConflictException {
System.out.println("Posting template named " + theName + " to catalog " + theCatalog);
		
		Catalog catalog = this.catalogs.getCatalog(theCatalog);
		((InCatalogLocator)this.checker.getTargetLocator()).setCatalog(catalog);

		URI targetURI = requestURI(theRequest);
		if (catalog != null) {
			if (catalog.getTarget(targetURI) != null)
				throw new TargetConflictException(theName, theCatalog);
		}

		CachedTarget target = new CachedTarget("", targetURI);
		target.setContent(theTemplate);

		try {
			if (catalog == null)
				this.checker.check(target);
			else
				this.checker.check(target, catalog);
		}
		catch (CheckerException cx) {
			log.log(Level.WARNING, "Failed to check tosca template", cx);
			target.getReport().add(cx);
		}

		if (target.getReport().isEmpty() && catalog == null)
			this.catalogs.setCatalog(theCatalog, checker.catalog());

		return target.getReport();
	}
	
	@RequestMapping(value={"/check_template/{catalog}"}, method={RequestMethod.GET})
	public ResponseEntity<Void> validate(@PathVariable(value="catalog") String theCatalog) {

		Catalog cat = catalogs.getCatalog(theCatalog);
		if (cat == null) {
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}
		
		return new ResponseEntity(HttpStatus.OK);
	}
	
	@RequestMapping(value={"/check_template/{catalog}"}, method={RequestMethod.DELETE})
	public ResponseEntity<Void> deleteCatalog(@PathVariable(value="catalog") String theCatalog) {

		Catalog cat = catalogs.removeCatalog(theCatalog);
		if (cat == null) {
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}
		
		return new ResponseEntity(HttpStatus.OK);
	}
	
	@RequestMapping(value={"/check_template/{catalog}/{name}"}, method={RequestMethod.GET})
	public ResponseEntity<String> retrieve(@PathVariable(value="catalog") String theCatalog,
												 								 @PathVariable(value="name") String theTemplateName,
																		 		 HttpServletRequest theRequest) {

		Catalog cat = catalogs.getCatalog(theCatalog);
		if (cat == null) {
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}

		Target t = cat.getTarget(requestURI(theRequest));
		if (t == null) {
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}

		return new ResponseEntity("{}", HttpStatus.OK);
	}
	

	@PostConstruct
	public void initController() {
		log.entering(getClass().getName(), "initCheckerController");

		try {
			this.checker = new Checker();
			this.checker.setTargetLocator(new InCatalogLocator());
		}
		catch (CheckerException cx) {
			log.log(Level.WARNING, "CheckerController setup failed", cx);
			throw new BeanInitializationException("Failed to create a checker", cx);
		}

		log.log(Level.INFO, "CheckerController started");
	}

	@PreDestroy
	public void cleanupController() {
		log.entering(getClass().getName(), "cleanupCheckerController");
	}

	private URI requestURI(HttpServletRequest theRequest) {
		try {
			return new URI(String.format("%s://%s:%d%s", theRequest.getScheme(),
																									 theRequest.getServerName(),
																									 theRequest.getServerPort(),
																									 theRequest.getRequestURI().toString()));
		}
		catch(URISyntaxException urisx) {
			throw new RuntimeException(urisx);
		}		
	}

	/**
	 */
	public static class InCatalogLocator extends CommonLocator {
		
		private ThreadLocal<Catalog> catalog = new ThreadLocal();

		public InCatalogLocator() {
		}

		protected void setCatalog(Catalog theCatalog) {
			this.catalog.set(theCatalog);
		}

		/** */
		@Override
		public Target resolve(String theName) {
			Target target = null;
			if (this.catalog.get() != null)
				target = this.catalog.get()
															.targets()
															.stream()
															.filter(t -> t.getName().equals(theName))
															.findFirst()
															.orElse(null);
			return target == null ?	super.resolve(theName) :	target;
		}
	}

}