3daf9a0d6960f5aceebb18a4683d13f3efdf4978
[cps.git] / 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.api.model.AnchorDetails;
30 import org.onap.cps.exceptions.CpsException;
31 import org.onap.cps.exceptions.CpsValidationException;
32 import org.onap.cps.spi.DataPersistenceService;
33 import org.onap.cps.spi.FragmentPersistenceService;
34 import org.onap.cps.spi.ModelPersistenceService;
35 import org.onap.cps.utils.YangUtils;
36 import org.opendaylight.yangtools.yang.common.Revision;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 public class CpServiceImpl implements CpService {
45
46     @Autowired
47     private ModelPersistenceService modelPersistenceService;
48
49     @Autowired
50     private DataPersistenceService dataPersistenceService;
51
52     @Autowired
53     private FragmentPersistenceService fragmentPersistenceService;
54
55     @Override
56     public final SchemaContext parseAndValidateModel(final String yangModelContent) {
57
58         try {
59             final File tempFile = File.createTempFile("yang", ".yang");
60             try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) {
61                 writer.write(yangModelContent);
62             }
63             return parseAndValidateModel(tempFile);
64         } catch (final IOException e) {
65             throw new CpsException(e);
66         }
67     }
68
69     @Override
70     public final SchemaContext parseAndValidateModel(final File yangModelFile) {
71         try {
72             return YangUtils.parseYangModelFile(yangModelFile);
73         } catch (final YangParserException e) {
74             throw new CpsValidationException("Yang file validation failed", e.getMessage());
75         } catch (final IOException e) {
76             throw new CpsException(e);
77         }
78     }
79
80     @Override
81     public final Integer storeJsonStructure(final String jsonStructure) {
82         return dataPersistenceService.storeJsonStructure(jsonStructure);
83     }
84
85     @Override
86     public final String getJsonById(final int jsonObjectId) {
87         return dataPersistenceService.getJsonById(jsonObjectId);
88     }
89
90     @Override
91     public void deleteJsonById(final int jsonObjectId) {
92         dataPersistenceService.deleteJsonById(jsonObjectId);
93     }
94
95     @Override
96     public final void storeSchemaContext(final SchemaContext schemaContext, final String dataspaceName) {
97         for (final Module module : schemaContext.getModules()) {
98             final Optional<Revision> optionalRevision = module.getRevision();
99             final String revisionValue = optionalRevision.map(Object::toString).orElse(null);
100             modelPersistenceService.storeModule(module.getNamespace().toString(), module.toString(),
101                 revisionValue, dataspaceName);
102         }
103     }
104
105     @Override
106     public String createAnchor(final AnchorDetails anchorDetails) {
107         return fragmentPersistenceService.createAnchor(anchorDetails);
108     }
109 }