Exception handling on REST interface
[cps.git] / cps / cps-service / src / main / java / org / onap / cps / api / impl / CpServiceImpl.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.api.impl;
22
23 import java.io.BufferedWriter;
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.util.Optional;
28 import org.onap.cps.api.CpService;
29 import org.onap.cps.exceptions.CpsException;
30 import org.onap.cps.exceptions.CpsValidationException;
31 import org.onap.cps.spi.DataPersistencyService;
32 import org.onap.cps.spi.ModelPersistencyService;
33 import org.onap.cps.utils.YangUtils;
34 import org.opendaylight.yangtools.yang.common.Revision;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40
41
42 @Component
43 public class CpServiceImpl implements CpService {
44
45     @Autowired
46     private ModelPersistencyService modelPersistencyService;
47
48     @Autowired
49     private DataPersistencyService dataPersistencyService;
50
51     @Override
52     public final SchemaContext parseAndValidateModel(final String yangModelContent) {
53
54         try {
55             final File tempFile = File.createTempFile("yang", ".yang");
56             try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) {
57                 writer.write(yangModelContent);
58             }
59             return parseAndValidateModel(tempFile);
60         } catch (IOException e) {
61             throw new CpsException(e);
62         }
63     }
64
65     @Override
66     public final SchemaContext parseAndValidateModel(final File yangModelFile) {
67         try {
68             return YangUtils.parseYangModelFile(yangModelFile);
69         } catch (YangParserException e) {
70             throw new CpsValidationException("Yang file validation failed", e.getMessage());
71         } catch (IOException e) {
72             throw new CpsException(e);
73         }
74     }
75
76     @Override
77     public final Integer storeJsonStructure(final String jsonStructure) {
78         return dataPersistencyService.storeJsonStructure(jsonStructure);
79     }
80
81     @Override
82     public final String getJsonById(final int jsonObjectId) {
83         return dataPersistencyService.getJsonById(jsonObjectId);
84     }
85
86     @Override
87     public void deleteJsonById(int jsonObjectId) {
88         dataPersistencyService.deleteJsonById(jsonObjectId);
89     }
90
91     @Override
92     public final void storeSchemaContext(final SchemaContext schemaContext, final String dataspaceName) {
93         for (final Module module : schemaContext.getModules()) {
94             Optional<Revision> optionalRevision = module.getRevision();
95             String revisionValue = optionalRevision.isPresent() ? optionalRevision.get().toString() : null;
96             modelPersistencyService.storeModule(module.getNamespace().toString(), module.toString(),
97                 revisionValue, dataspaceName);
98         }
99     }
100 }