2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Modifications copyright (c) 2019 AT&T Intellectual Property
21 package org.onap.vnf.vfw.write;
23 import org.onap.vnf.vfw.CrudService;
24 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.stream.count.rev190118.stream.count.params.Streams;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import java.io.IOException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Writer for {@link Element} list node from our YANG model.
37 public final class ElementCustomizer implements WriterCustomizer<Streams> {
39 private static final Logger LOG = LoggerFactory.getLogger(ElementCustomizer.class);
41 private final CrudService<Streams> crudService;
43 public ElementCustomizer(@Nonnull final CrudService<Streams> crudService) {
44 this.crudService = crudService;
48 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id, @Nonnull final Streams dataAfter,
49 @Nonnull final WriteContext writeContext) throws WriteFailedException {
50 //perform write of data,or throw exception
51 //invoked by PUT operation,if provided data doesn't exist in Config data
52 crudService.writeData(id, dataAfter);
54 runScript(dataAfter.getActiveStreams().getValue());
56 catch (IOException e) {
57 String message = "Write operation failed " + e;
63 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id,
64 @Nonnull final Streams dataBefore,
65 @Nonnull final Streams dataAfter, @Nonnull final WriteContext writeContext)
66 throws WriteFailedException {
67 //invoked by PUT operation,if provided data does exist in Config data
68 crudService.updateData(id, dataBefore, dataAfter);
70 runScript(dataAfter.getActiveStreams().getValue());
72 catch (IOException e) {
73 String message = "Write operation failed " + e;
79 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id,
80 @Nonnull final Streams dataBefore,
81 @Nonnull final WriteContext writeContext) throws WriteFailedException {
82 //perform delete of data,or throw exception
83 // Not supported, throw exception
84 throw new WriteFailedException.DeleteFailedException(id,
85 new UnsupportedOperationException("Delete operation not supported"));
88 //Update the number of running streams running a custom script that uses the old vPacketGen REST APIs
89 private void runScript(long streams) throws IOException {
91 String script = new String("bash /opt/enable_disable_streams.sh " + streams);
92 Runtime.getRuntime().exec(script);
93 String message = "Number of running streams updated to " + streams;