blob: 284c7327ae3d8917eb79d2c729c69c3f9d5f2dbe [file] [log] [blame]
Sean Condon06613e92017-06-09 15:14:01 +01001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Sean Condon06613e92017-06-09 15:14:01 +01003 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16package org.onosproject.drivers.microsemi.yang.impl;
17
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.osgi.service.component.annotations.Activate;
19import org.osgi.service.component.annotations.Component;
20import org.osgi.service.component.annotations.Deactivate;
Sean Condon06613e92017-06-09 15:14:01 +010021import org.onosproject.drivers.microsemi.yang.MseaSaFilteringNetconfService;
22import org.onosproject.netconf.DatastoreId;
23import org.onosproject.netconf.NetconfException;
24import org.onosproject.netconf.NetconfSession;
25import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.MseaSaFiltering;
26import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.MseaSaFilteringOpParam;
27import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.SourceIpaddressFiltering;
28import org.onosproject.yang.gen.v1.mseasafiltering.rev20160412.mseasafiltering.sourceipaddressfiltering.interfaceeth0.SourceAddressRange;
29import org.onosproject.yang.model.DefaultModelObjectData;
30import org.onosproject.yang.model.ModelConverter;
31import org.onosproject.yang.model.ModelObject;
32import org.onosproject.yang.model.ModelObjectData;
33import org.onosproject.yang.model.ResourceId;
34import org.onosproject.yang.runtime.AnnotatedNodeInfo;
35import org.onosproject.yang.runtime.CompositeData;
36import org.onosproject.yang.runtime.DefaultAnnotatedNodeInfo;
37import org.onosproject.yang.runtime.DefaultAnnotation;
38import org.onosproject.yang.runtime.DefaultCompositeStream;
39
40import java.io.ByteArrayInputStream;
41import java.util.ArrayList;
42import java.util.List;
43
44/**
45 * Implementation of the MseaSaFiltering YANG model service.
46 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070047@Component(immediate = true, service = AbstractYangServiceImpl.class)
Sean Condon06613e92017-06-09 15:14:01 +010048public class MseaSaFilteringManager extends AbstractYangServiceImpl
49 implements MseaSaFilteringNetconfService {
50 public static final String MSEA_SA_FILTERING =
51 "org.onosproject.drivers.microsemi.yang.mseasafiltering";
52 public static final String MSEA_SA_FILTERING_NS =
53 "http://www.microsemi.com/microsemi-edge-assure/msea-sa-filtering";
54
55 @Activate
56 public void activate() {
57 super.activate();
58 appId = coreService.registerApplication(MSEA_SA_FILTERING);
59 log.info("MseaSaFilteringManager Started");
60 }
61
62 @Deactivate
63 public void deactivate() {
64 super.deactivate();
65 log.info("MseaSaFilteringManager Stopped");
66 }
67
68 /**
69 * Get a filtered subset of the model.
70 * This is meant to filter the current live model
71 * against the attribute(s) given in the argument
72 * and return the filtered model.
73 */
74 @Override
75 public MseaSaFiltering getMseaSaFiltering(
76 MseaSaFilteringOpParam mseaSaFilteringFilter, NetconfSession session)
77 throws NetconfException {
78 ModelObjectData moQuery = DefaultModelObjectData.builder()
79 .addModelObject((ModelObject) mseaSaFilteringFilter
80 .sourceIpaddressFiltering())
81 .build();
82
83
84 ModelObjectData moReply = getNetconfObject(moQuery, session);
85
86 MseaSaFiltering reply = new MseaSaFilteringOpParam();
87 for (ModelObject mo:moReply.modelObjects()) {
88 if (mo instanceof SourceIpaddressFiltering) {
89 reply.sourceIpaddressFiltering((SourceIpaddressFiltering) mo);
90 }
91 }
92 return reply;
93 }
94
95 /**
96 * Get a filtered subset of the config model (from running)
97 * This is meant to filter the current live model
98 * against the attribute(s) given in the argument
99 * and return the filtered model.
100 */
101 @Override
102 public List<SourceAddressRange> getConfigMseaSaFilterIds(NetconfSession session)
103 throws NetconfException {
104
105 String xmlResult = session.getConfig(DatastoreId.RUNNING, saFilterQuery());
106 xmlResult = removeRpcReplyData(xmlResult);
107
108 DefaultCompositeStream resultDcs = new DefaultCompositeStream(
109 null, new ByteArrayInputStream(xmlResult.getBytes()));
110 CompositeData compositeData = xSer.decode(resultDcs, yCtx);
111
112 ModelObjectData moReply = ((ModelConverter) yangModelRegistry).createModel(compositeData.resourceData());
113
114 MseaSaFiltering reply = new MseaSaFilteringOpParam();
115 for (ModelObject mo:moReply.modelObjects()) {
116 if (mo instanceof SourceIpaddressFiltering) {
117 reply.sourceIpaddressFiltering((SourceIpaddressFiltering) mo);
118 }
119 }
Ray Milkeyfe0e0852018-01-18 11:14:05 -0800120 if (reply.sourceIpaddressFiltering() != null &&
Sean Condon06613e92017-06-09 15:14:01 +0100121 reply.sourceIpaddressFiltering().interfaceEth0() != null) {
122 return reply.sourceIpaddressFiltering().interfaceEth0().sourceAddressRange();
123 } else {
124 return new ArrayList<SourceAddressRange>();
125 }
126 }
127
128 /**
129 * Call NETCONF edit-config with a configuration.
130 */
131 @Override
132 public boolean setMseaSaFiltering(MseaSaFilteringOpParam mseaSaFiltering,
133 NetconfSession session, DatastoreId ncDs) throws NetconfException {
134
135 ModelObjectData moQuery = DefaultModelObjectData.builder()
136 .addModelObject((ModelObject) mseaSaFiltering
137 .sourceIpaddressFiltering()).build();
138
139 return setNetconfObject(moQuery, session, ncDs, null);
140 }
141
142 @Override
143 public boolean deleteMseaSaFilteringRange(MseaSaFilteringOpParam mseaSaFiltering,
144 NetconfSession session, DatastoreId ncDs) throws NetconfException {
145
146 ModelObjectData moQuery = DefaultModelObjectData.builder()
147 .addModelObject((ModelObject) mseaSaFiltering
148 .sourceIpaddressFiltering()).build();
149
150 ArrayList anis = new ArrayList<AnnotatedNodeInfo>();
151 if (mseaSaFiltering.sourceIpaddressFiltering().interfaceEth0() != null &&
152 mseaSaFiltering.sourceIpaddressFiltering().interfaceEth0().sourceAddressRange() != null) {
153
154 for (SourceAddressRange sar:mseaSaFiltering.sourceIpaddressFiltering()
155 .interfaceEth0().sourceAddressRange()) {
156 String sarRangeIdStr = String.valueOf(sar.rangeId());
157
158 ResourceId.Builder ridBuilder = ResourceId.builder()
159 .addBranchPointSchema("/", null)
160 .addBranchPointSchema("source-ipaddress-filtering", MSEA_SA_FILTERING_NS)
161 .addBranchPointSchema("interface-eth0", MSEA_SA_FILTERING_NS)
162 .addBranchPointSchema("source-address-range", MSEA_SA_FILTERING_NS)
163 .addKeyLeaf("range-id", MSEA_SA_FILTERING_NS, sarRangeIdStr);
164
165 AnnotatedNodeInfo ani = DefaultAnnotatedNodeInfo.builder()
166 .resourceId(ridBuilder.build())
167 .addAnnotation(new DefaultAnnotation(NC_OPERATION, OP_DELETE))
168 .build();
169
170 anis.add(ani);
171 }
172 } else {
173 //Delete all
174 ResourceId.Builder ridBuilder = ResourceId.builder()
175 .addBranchPointSchema("/", null)
176 .addBranchPointSchema("source-ipaddress-filtering", MSEA_SA_FILTERING_NS);
177 AnnotatedNodeInfo ani = DefaultAnnotatedNodeInfo.builder()
178 .resourceId(ridBuilder.build())
179 .addAnnotation(new DefaultAnnotation(NC_OPERATION, OP_DELETE))
180 .build();
181 anis.add(ani);
182 }
183
184 return setNetconfObject(moQuery, session, ncDs, anis);
185 }
186
187
188 private static String saFilterQuery() {
189 StringBuilder sb = new StringBuilder("<source-ipaddress-filtering " +
190 "xmlns=\"http://www.microsemi.com/microsemi-edge-assure/msea-sa-filtering\">");
191 sb.append("<interface-eth0>");
192 sb.append("<filter-admin-state>blacklist</filter-admin-state>");
193 sb.append("<source-address-range>");
194 sb.append("<range-id/>");
195 sb.append("</source-address-range>");
196 sb.append("</interface-eth0>");
197 sb.append("</source-ipaddress-filtering>");
198 return sb.toString();
199 }
200}